@linaria/vite
Version:
Blazing fast zero-runtime CSS in JS library
176 lines (175 loc) • 6.69 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => linaria
});
module.exports = __toCommonJS(src_exports);
var import_fs = require("fs");
var import_path = __toESM(require("path"));
var import_pluginutils = require("@rollup/pluginutils");
var import_vite = require("vite");
var import_babel_preset = require("@linaria/babel-preset");
var import_logger = require("@linaria/logger");
var import_utils = require("@linaria/utils");
function linaria({
debug,
include,
exclude,
sourceMap,
preprocessor,
...rest
} = {}) {
const filter = (0, import_pluginutils.createFilter)(include, exclude);
const cssLookup = {};
const cssFileLookup = {};
let config;
let devServer;
const { emitter, onDone } = (0, import_utils.createFileReporter)(debug ?? false);
const targets = [];
const cache = new import_babel_preset.TransformCacheCollection();
return {
name: "linaria",
enforce: "post",
buildEnd() {
onDone(process.cwd());
},
configResolved(resolvedConfig) {
config = resolvedConfig;
},
configureServer(_server) {
devServer = _server;
},
load(url) {
const [id] = url.split("?", 1);
return cssLookup[id];
},
/* eslint-disable-next-line consistent-return */
resolveId(importeeUrl) {
const [id] = importeeUrl.split("?", 1);
if (cssLookup[id])
return id;
return cssFileLookup[id];
},
handleHotUpdate(ctx) {
if (ctx.modules.length)
return ctx.modules;
const affected = targets.filter(
(x) => (
// file is dependency of any target
x.dependencies.some((dep) => dep === ctx.file) || // or changed module is a dependency of any target
x.dependencies.some((dep) => ctx.modules.some((m) => m.file === dep))
)
);
const deps = affected.flatMap((target) => target.dependencies);
for (const depId of deps) {
cache.invalidateForFile(depId);
}
return affected.map((target) => devServer.moduleGraph.getModuleById(target.id)).concat(ctx.modules).filter((m) => !!m);
},
async transform(code, url) {
const [id] = url.split("?", 1);
if (url.includes("node_modules") || !filter(url) || id in cssLookup)
return;
const log = import_logger.linariaLogger.extend("vite");
log("Vite transform", (0, import_utils.getFileIdx)(id));
const asyncResolve = async (what, importer, stack) => {
const resolved = await this.resolve(what, importer);
if (resolved) {
if (resolved.external) {
const resolvedId2 = (0, import_utils.syncResolve)(what, importer, stack);
log("resolve \u2705 '%s'@'%s -> %O\n%s", what, importer, resolved);
return resolvedId2;
}
log("resolve \u2705 '%s'@'%s -> %O\n%s", what, importer, resolved);
const resolvedId = resolved.id.split("?", 1)[0];
if (resolvedId.startsWith("\0")) {
return null;
}
if (!(0, import_fs.existsSync)(resolvedId)) {
await (0, import_vite.optimizeDeps)(config);
}
return resolvedId;
}
log("resolve \u274C '%s'@'%s", what, importer);
throw new Error(`Could not resolve ${what}`);
};
const transformServices = {
options: {
filename: id,
root: process.cwd(),
preprocessor,
pluginOptions: rest
},
cache,
eventEmitter: emitter
};
const result = await (0, import_babel_preset.transform)(transformServices, code, asyncResolve);
let { cssText, dependencies } = result;
if (!cssText)
return;
dependencies ??= [];
const slug = (0, import_babel_preset.slugify)(cssText);
const cssFilename = import_path.default.normalize(`${id.replace(/\.[jt]sx?$/, "")}_${slug}.css`).replace(/\\/g, import_path.default.posix.sep);
const cssRelativePath = import_path.default.relative(config.root, cssFilename).replace(/\\/g, import_path.default.posix.sep);
const cssId = `/${cssRelativePath}`;
if (sourceMap && result.cssSourceMapText) {
const map = Buffer.from(result.cssSourceMapText).toString("base64");
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
}
cssLookup[cssFilename] = cssText;
cssFileLookup[cssId] = cssFilename;
result.code += `
import ${JSON.stringify(cssFilename)};
`;
if (devServer?.moduleGraph) {
const module2 = devServer.moduleGraph.getModuleById(cssId);
if (module2) {
devServer.moduleGraph.invalidateModule(module2);
module2.lastHMRTimestamp = module2.lastInvalidationTimestamp || Date.now();
}
}
for (let i = 0, end = dependencies.length; i < end; i++) {
const depModule = await this.resolve(dependencies[i], url, {
isEntry: false
});
if (depModule)
dependencies[i] = depModule.id;
}
const target = targets.find((t) => t.id === id);
if (!target)
targets.push({ id, dependencies });
else
target.dependencies = dependencies;
return { code: result.code, map: result.sourceMap };
}
};
}
//# sourceMappingURL=index.js.map