-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (40 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
const babel_1 = require("@linaria/babel");
const promises_1 = require("node:fs/promises");
const path = require("node:path");
const pluginName = 'esbuild-plugin-linaria';
const plugin = ({ filter, preprocess, linariaOptions } = {}) => {
const cssFileContentsMap = new Map();
const transform = ({ args, contents }) => {
contents = preprocess ? preprocess(contents, args) : contents;
let { cssText, code } = (0, babel_1.transform)(contents, {
filename: args.path,
inputSourceMap: linariaOptions?.inputSourceMap,
preprocessor: linariaOptions?.preprocessor,
pluginOptions: linariaOptions?.pluginOptions ?? {
babelOptions: { presets: ['@babel/preset-react', '@babel/preset-typescript'] },
},
});
if (cssText) {
const cssFilename = `${args.path}.${pluginName}.css`;
cssFileContentsMap.set(cssFilename, cssText);
code = `import '${cssFilename.replace(/\\/g, '\\\\')}';\n${code}`;
}
return { contents: code, loader: path.extname(args.path).slice(1) };
};
return {
name: pluginName,
setup: ((build, pipe) => {
if (pipe?.transform) {
return transform(pipe.transform);
}
build.onLoad({ filter: filter ?? /\.[cm]?[jt]sx?$/ }, async (args) => transform({ args, contents: await (0, promises_1.readFile)(args.path, 'utf8') }));
build.onResolve({ filter: RegExp(String.raw `\.${pluginName}\.css`) }, ({ path }) => ({ path, namespace: pluginName }));
build.onLoad({ filter: RegExp(String.raw `\.${pluginName}\.css`), namespace: pluginName }, ({ path }) => {
const contents = cssFileContentsMap.get(path);
return contents ? { contents, loader: 'css' } : undefined;
});
}),
};
};
module.exports = plugin.default = plugin;