@wyw-in-js/esbuild 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/{index.js → index.mjs} +15 -4
- package/esm/index.mjs.map +1 -0
- package/lib/index.js +13 -2
- package/lib/index.js.map +1 -1
- package/package.json +10 -10
- package/types/index.d.ts +4 -2
- package/types/index.js +8 -2
- package/esm/index.js.map +0 -1
|
@@ -7,12 +7,14 @@
|
|
|
7
7
|
import { readFileSync } from 'fs';
|
|
8
8
|
import { basename, dirname, isAbsolute, join, parse, posix } from 'path';
|
|
9
9
|
import { transformSync } from 'esbuild';
|
|
10
|
-
import { slugify, transform, TransformCacheCollection } from '@wyw-in-js/transform';
|
|
10
|
+
import { slugify, transform, TransformCacheCollection, createFileReporter } from '@wyw-in-js/transform';
|
|
11
11
|
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
|
|
12
12
|
export default function wywInJS({
|
|
13
|
+
debug,
|
|
13
14
|
sourceMap,
|
|
14
15
|
preprocessor,
|
|
15
16
|
esbuildOptions,
|
|
17
|
+
filter = /\.(js|jsx|ts|tsx)$/,
|
|
16
18
|
...rest
|
|
17
19
|
} = {}) {
|
|
18
20
|
let options = esbuildOptions;
|
|
@@ -21,6 +23,10 @@ export default function wywInJS({
|
|
|
21
23
|
name: 'wyw-in-js',
|
|
22
24
|
setup(build) {
|
|
23
25
|
const cssLookup = new Map();
|
|
26
|
+
const {
|
|
27
|
+
emitter,
|
|
28
|
+
onDone
|
|
29
|
+
} = createFileReporter(debug ?? false);
|
|
24
30
|
const asyncResolve = async (token, importer) => {
|
|
25
31
|
const context = isAbsolute(importer) ? dirname(importer) : join(process.cwd(), dirname(importer));
|
|
26
32
|
const result = await build.resolve(token, {
|
|
@@ -32,6 +38,9 @@ export default function wywInJS({
|
|
|
32
38
|
}
|
|
33
39
|
return result.path.replace(/\\/g, posix.sep);
|
|
34
40
|
};
|
|
41
|
+
build.onEnd(() => {
|
|
42
|
+
onDone(process.cwd());
|
|
43
|
+
});
|
|
35
44
|
build.onResolve({
|
|
36
45
|
filter: /\.wyw\.css$/
|
|
37
46
|
}, args => {
|
|
@@ -50,8 +59,9 @@ export default function wywInJS({
|
|
|
50
59
|
resolveDir: basename(args.path)
|
|
51
60
|
};
|
|
52
61
|
});
|
|
62
|
+
const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
|
|
53
63
|
build.onLoad({
|
|
54
|
-
filter:
|
|
64
|
+
filter: filterRegexp
|
|
55
65
|
}, async args => {
|
|
56
66
|
const rawCode = readFileSync(args.path, 'utf8');
|
|
57
67
|
const {
|
|
@@ -94,7 +104,8 @@ export default function wywInJS({
|
|
|
94
104
|
preprocessor,
|
|
95
105
|
pluginOptions: rest
|
|
96
106
|
},
|
|
97
|
-
cache
|
|
107
|
+
cache,
|
|
108
|
+
eventEmitter: emitter
|
|
98
109
|
};
|
|
99
110
|
const result = await transform(transformServices, code, asyncResolve);
|
|
100
111
|
if (!result.cssText) {
|
|
@@ -126,4 +137,4 @@ export default function wywInJS({
|
|
|
126
137
|
}
|
|
127
138
|
};
|
|
128
139
|
}
|
|
129
|
-
//# sourceMappingURL=index.
|
|
140
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["readFileSync","basename","dirname","isAbsolute","join","parse","posix","transformSync","slugify","transform","TransformCacheCollection","createFileReporter","nodeModulesRegex","wywInJS","debug","sourceMap","preprocessor","esbuildOptions","filter","rest","options","cache","name","setup","build","cssLookup","Map","emitter","onDone","asyncResolve","token","importer","context","process","cwd","result","resolve","resolveDir","kind","errors","length","Error","path","replace","sep","onEnd","onResolve","args","namespace","onLoad","contents","get","loader","filterRegexp","RegExp","rawCode","ext","filename","test","initialOptions","jsxFactory","jsxFragment","transformed","sourcefile","sourcemap","code","esbuildMap","Buffer","from","map","toString","transformServices","root","pluginOptions","eventEmitter","cssText","slug","cssFilename","JSON","stringify","cssSourceMapText","wywMap","set"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This file contains an esbuild loader for wyw-in-js.\n * It uses the transform.ts function to generate class names from source code,\n * returns transformed code without template literals and attaches generated source maps\n */\n\nimport { readFileSync } from 'fs';\nimport { basename, dirname, isAbsolute, join, parse, posix } from 'path';\n\nimport type { Plugin, TransformOptions, Loader } from 'esbuild';\nimport { transformSync } from 'esbuild';\n\nimport type {\n PluginOptions,\n Preprocessor,\n IFileReporterOptions,\n} from '@wyw-in-js/transform';\nimport {\n slugify,\n transform,\n TransformCacheCollection,\n createFileReporter,\n} from '@wyw-in-js/transform';\n\ntype EsbuildPluginOptions = {\n debug?: IFileReporterOptions | false | null | undefined;\n esbuildOptions?: TransformOptions;\n filter?: RegExp | string;\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nconst nodeModulesRegex = /^(?:.*[\\\\/])?node_modules(?:[\\\\/].*)?$/;\n\nexport default function wywInJS({\n debug,\n sourceMap,\n preprocessor,\n esbuildOptions,\n filter = /\\.(js|jsx|ts|tsx)$/,\n ...rest\n}: EsbuildPluginOptions = {}): Plugin {\n let options = esbuildOptions;\n const cache = new TransformCacheCollection();\n return {\n name: 'wyw-in-js',\n setup(build) {\n const cssLookup = new Map<string, string>();\n\n const { emitter, onDone } = createFileReporter(debug ?? false);\n\n const asyncResolve = async (\n token: string,\n importer: string\n ): Promise<string> => {\n const context = isAbsolute(importer)\n ? dirname(importer)\n : join(process.cwd(), dirname(importer));\n\n const result = await build.resolve(token, {\n resolveDir: context,\n kind: 'import-statement',\n });\n\n if (result.errors.length > 0) {\n throw new Error(`Cannot resolve ${token}`);\n }\n\n return result.path.replace(/\\\\/g, posix.sep);\n };\n\n build.onEnd(() => {\n onDone(process.cwd());\n });\n\n build.onResolve({ filter: /\\.wyw\\.css$/ }, (args) => {\n return {\n namespace: 'wyw-in-js',\n path: args.path,\n };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, (args) => {\n return {\n contents: cssLookup.get(args.path),\n loader: 'css',\n resolveDir: basename(args.path),\n };\n });\n\n const filterRegexp =\n typeof filter === 'string' ? new RegExp(filter) : filter;\n\n build.onLoad({ filter: filterRegexp }, async (args) => {\n const rawCode = readFileSync(args.path, 'utf8');\n const { ext, name: filename } = parse(args.path);\n const loader = ext.replace(/^\\./, '') as Loader;\n\n if (nodeModulesRegex.test(args.path)) {\n return {\n loader,\n contents: rawCode,\n };\n }\n\n if (!options) {\n options = {};\n if ('jsxFactory' in build.initialOptions) {\n options.jsxFactory = build.initialOptions.jsxFactory;\n }\n if ('jsxFragment' in build.initialOptions) {\n options.jsxFragment = build.initialOptions.jsxFragment;\n }\n }\n\n const transformed = transformSync(rawCode, {\n ...options,\n sourcefile: args.path,\n sourcemap: sourceMap,\n loader,\n });\n let { code } = transformed;\n\n if (sourceMap) {\n const esbuildMap = Buffer.from(transformed.map).toString('base64');\n code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;\n }\n\n const transformServices = {\n options: {\n filename: args.path,\n root: process.cwd(),\n preprocessor,\n pluginOptions: rest,\n },\n cache,\n eventEmitter: emitter,\n };\n\n const result = await transform(transformServices, code, asyncResolve);\n\n if (!result.cssText) {\n return {\n contents: code,\n loader,\n resolveDir: dirname(args.path),\n };\n }\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const cssFilename = `${filename}_${slug}.wyw.css`;\n\n let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n const wywMap = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `/*# sourceMappingURL=data:application/json;base64,${wywMap}*/`;\n }\n\n cssLookup.set(cssFilename, cssText);\n\n return {\n contents,\n loader,\n resolveDir: dirname(args.path),\n };\n });\n },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Ee,IAAI;YACdb,MAAM;YACNf,UAAU,EAAEnC,OAAO,CAAC6C,IAAI,CAACL,IAAI;UAC/B,CAAC;QACH;QAEA,IAAI;UAAEiC;QAAQ,CAAC,GAAGxC,MAAM;QAExB,MAAMyC,IAAI,GAAGpE,OAAO,CAACmE,OAAO,CAAC;QAC7B,MAAME,WAAW,GAAI,GAAEpB,QAAS,IAAGmB,IAAK,UAAS;QAEjD,IAAI1B,QAAQ,GAAI,UAAS4B,IAAI,CAACC,SAAS,CAACF,WAAW,CAAE,KAAI1C,MAAM,CAAC8B,IAAK,EAAC;QAEtE,IAAIlD,SAAS,IAAIoB,MAAM,CAAC6C,gBAAgB,EAAE;UACxC,MAAMX,GAAG,GAAGF,MAAM,CAACC,IAAI,CAACjC,MAAM,CAAC6C,gBAAgB,CAAC,CAACV,QAAQ,CAAC,QAAQ,CAAC;UACnEK,OAAO,IAAK,qDAAoDN,GAAI,IAAG;UACvE,MAAMY,MAAM,GAAGd,MAAM,CAACC,IAAI,CAACU,IAAI,CAACC,SAAS,CAAC5C,MAAM,CAACpB,SAAS,CAAC,CAAC,CAACuD,QAAQ,CACnE,QACF,CAAC;UACDpB,QAAQ,IAAK,qDAAoD+B,MAAO,IAAG;QAC7E;QAEAxD,SAAS,CAACyD,GAAG,CAACL,WAAW,EAAEF,OAAO,CAAC;QAEnC,OAAO;UACLzB,QAAQ;UACRE,MAAM;UACNf,UAAU,EAAEnC,OAAO,CAAC6C,IAAI,CAACL,IAAI;QAC/B,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH"}
|
package/lib/index.js
CHANGED
|
@@ -16,9 +16,11 @@ var _transform = require("@wyw-in-js/transform");
|
|
|
16
16
|
|
|
17
17
|
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
|
|
18
18
|
function wywInJS({
|
|
19
|
+
debug,
|
|
19
20
|
sourceMap,
|
|
20
21
|
preprocessor,
|
|
21
22
|
esbuildOptions,
|
|
23
|
+
filter = /\.(js|jsx|ts|tsx)$/,
|
|
22
24
|
...rest
|
|
23
25
|
} = {}) {
|
|
24
26
|
let options = esbuildOptions;
|
|
@@ -27,6 +29,10 @@ function wywInJS({
|
|
|
27
29
|
name: 'wyw-in-js',
|
|
28
30
|
setup(build) {
|
|
29
31
|
const cssLookup = new Map();
|
|
32
|
+
const {
|
|
33
|
+
emitter,
|
|
34
|
+
onDone
|
|
35
|
+
} = (0, _transform.createFileReporter)(debug !== null && debug !== void 0 ? debug : false);
|
|
30
36
|
const asyncResolve = async (token, importer) => {
|
|
31
37
|
const context = (0, _path.isAbsolute)(importer) ? (0, _path.dirname)(importer) : (0, _path.join)(process.cwd(), (0, _path.dirname)(importer));
|
|
32
38
|
const result = await build.resolve(token, {
|
|
@@ -38,6 +44,9 @@ function wywInJS({
|
|
|
38
44
|
}
|
|
39
45
|
return result.path.replace(/\\/g, _path.posix.sep);
|
|
40
46
|
};
|
|
47
|
+
build.onEnd(() => {
|
|
48
|
+
onDone(process.cwd());
|
|
49
|
+
});
|
|
41
50
|
build.onResolve({
|
|
42
51
|
filter: /\.wyw\.css$/
|
|
43
52
|
}, args => {
|
|
@@ -56,8 +65,9 @@ function wywInJS({
|
|
|
56
65
|
resolveDir: (0, _path.basename)(args.path)
|
|
57
66
|
};
|
|
58
67
|
});
|
|
68
|
+
const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
|
|
59
69
|
build.onLoad({
|
|
60
|
-
filter:
|
|
70
|
+
filter: filterRegexp
|
|
61
71
|
}, async args => {
|
|
62
72
|
const rawCode = (0, _fs.readFileSync)(args.path, 'utf8');
|
|
63
73
|
const {
|
|
@@ -100,7 +110,8 @@ function wywInJS({
|
|
|
100
110
|
preprocessor,
|
|
101
111
|
pluginOptions: rest
|
|
102
112
|
},
|
|
103
|
-
cache
|
|
113
|
+
cache,
|
|
114
|
+
eventEmitter: emitter
|
|
104
115
|
};
|
|
105
116
|
const result = await (0, _transform.transform)(transformServices, code, asyncResolve);
|
|
106
117
|
if (!result.cssText) {
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["_fs","require","_path","_esbuild","_transform","nodeModulesRegex","wywInJS","sourceMap","preprocessor","esbuildOptions","rest","options","cache","TransformCacheCollection","name","setup","build","cssLookup","Map","asyncResolve","token","importer","context","isAbsolute","dirname","join","process","cwd","result","resolve","resolveDir","kind","errors","length","Error","path","replace","posix","sep","onResolve","filter","args","namespace","onLoad","contents","get","loader","basename","rawCode","readFileSync","ext","filename","parse","test","initialOptions","jsxFactory","jsxFragment","transformed","transformSync","sourcefile","sourcemap","code","esbuildMap","Buffer","from","map","toString","transformServices","root","pluginOptions","transform","cssText","slug","slugify","cssFilename","JSON","stringify","cssSourceMapText","wywMap","set"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This file contains an esbuild loader for wyw-in-js.\n * It uses the transform.ts function to generate class names from source code,\n * returns transformed code without template literals and attaches generated source maps\n */\n\nimport { readFileSync } from 'fs';\nimport { basename, dirname, isAbsolute, join, parse, posix } from 'path';\n\nimport type { Plugin, TransformOptions, Loader } from 'esbuild';\nimport { transformSync } from 'esbuild';\n\nimport type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';\nimport {\n slugify,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\ntype EsbuildPluginOptions = {\n esbuildOptions?: TransformOptions;\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nconst nodeModulesRegex = /^(?:.*[\\\\/])?node_modules(?:[\\\\/].*)?$/;\n\nexport default function wywInJS({\n sourceMap,\n preprocessor,\n esbuildOptions,\n ...rest\n}: EsbuildPluginOptions = {}): Plugin {\n let options = esbuildOptions;\n const cache = new TransformCacheCollection();\n return {\n name: 'wyw-in-js',\n setup(build) {\n const cssLookup = new Map<string, string>();\n\n const asyncResolve = async (\n token: string,\n importer: string\n ): Promise<string> => {\n const context = isAbsolute(importer)\n ? dirname(importer)\n : join(process.cwd(), dirname(importer));\n\n const result = await build.resolve(token, {\n resolveDir: context,\n kind: 'import-statement',\n });\n\n if (result.errors.length > 0) {\n throw new Error(`Cannot resolve ${token}`);\n }\n\n return result.path.replace(/\\\\/g, posix.sep);\n };\n\n build.onResolve({ filter: /\\.wyw\\.css$/ }, (args) => {\n return {\n namespace: 'wyw-in-js',\n path: args.path,\n };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, (args) => {\n return {\n contents: cssLookup.get(args.path),\n loader: 'css',\n resolveDir: basename(args.path),\n };\n });\n\n build.onLoad({ filter: /\\.(js|jsx|ts|tsx)$/ }, async (args) => {\n const rawCode = readFileSync(args.path, 'utf8');\n const { ext, name: filename } = parse(args.path);\n const loader = ext.replace(/^\\./, '') as Loader;\n\n if (nodeModulesRegex.test(args.path)) {\n return {\n loader,\n contents: rawCode,\n };\n }\n\n if (!options) {\n options = {};\n if ('jsxFactory' in build.initialOptions) {\n options.jsxFactory = build.initialOptions.jsxFactory;\n }\n if ('jsxFragment' in build.initialOptions) {\n options.jsxFragment = build.initialOptions.jsxFragment;\n }\n }\n\n const transformed = transformSync(rawCode, {\n ...options,\n sourcefile: args.path,\n sourcemap: sourceMap,\n loader,\n });\n let { code } = transformed;\n\n if (sourceMap) {\n const esbuildMap = Buffer.from(transformed.map).toString('base64');\n code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;\n }\n\n const transformServices = {\n options: {\n filename: args.path,\n root: process.cwd(),\n preprocessor,\n pluginOptions: rest,\n },\n cache,\n };\n\n const result = await transform(transformServices, code, asyncResolve);\n\n if (!result.cssText) {\n return {\n contents: code,\n loader,\n resolveDir: dirname(args.path),\n };\n }\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const cssFilename = `${filename}_${slug}.wyw.css`;\n\n let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n const wywMap = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `/*# sourceMappingURL=data:application/json;base64,${wywMap}*/`;\n }\n\n cssLookup.set(cssFilename, cssText);\n\n return {\n contents,\n loader,\n resolveDir: dirname(args.path),\n };\n });\n },\n };\n}\n"],"mappings":";;;;;;AAMA,IAAAA,GAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAGA,IAAAE,QAAA,GAAAF,OAAA;AAGA,IAAAG,UAAA,GAAAH,OAAA;AAbA;AACA;AACA;AACA;AACA;;AAqBA,MAAMI,gBAAgB,GAAG,wCAAwC;AAElD,SAASC,OAAOA,CAAC;EAC9BC,SAAS;EACTC,YAAY;EACZC,cAAc;EACd,GAAGC;AACiB,CAAC,GAAG,CAAC,CAAC,EAAU;EACpC,IAAIC,OAAO,GAAGF,cAAc;EAC5B,MAAMG,KAAK,GAAG,IAAIC,mCAAwB,CAAC,CAAC;EAC5C,OAAO;IACLC,IAAI,EAAE,WAAW;IACjBC,KAAKA,CAACC,KAAK,EAAE;MACX,MAAMC,SAAS,GAAG,IAAIC,GAAG,CAAiB,CAAC;MAE3C,MAAMC,YAAY,GAAG,MAAAA,CACnBC,KAAa,EACbC,QAAgB,KACI;QACpB,MAAMC,OAAO,GAAG,IAAAC,gBAAU,EAACF,QAAQ,CAAC,GAChC,IAAAG,aAAO,EAACH,QAAQ,CAAC,GACjB,IAAAI,UAAI,EAACC,OAAO,CAACC,GAAG,CAAC,CAAC,EAAE,IAAAH,aAAO,EAACH,QAAQ,CAAC,CAAC;QAE1C,MAAMO,MAAM,GAAG,MAAMZ,KAAK,CAACa,OAAO,CAACT,KAAK,EAAE;UACxCU,UAAU,EAAER,OAAO;UACnBS,IAAI,EAAE;QACR,CAAC,CAAC;QAEF,IAAIH,MAAM,CAACI,MAAM,CAACC,MAAM,GAAG,CAAC,EAAE;UAC5B,MAAM,IAAIC,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B,QAAS,IAAGqB,IAAK,UAAS;QAEjD,IAAI5B,QAAQ,GAAI,UAAS+B,IAAI,CAACC,SAAS,CAACF,WAAW,CAAE,KAAI9C,MAAM,CAACiC,IAAK,EAAC;QAEtE,IAAItD,SAAS,IAAIqB,MAAM,CAACiD,gBAAgB,EAAE;UACxC,MAAMZ,GAAG,GAAGF,MAAM,CAACC,IAAI,CAACpC,MAAM,CAACiD,gBAAgB,CAAC,CAACX,QAAQ,CAAC,QAAQ,CAAC;UACnEK,OAAO,IAAK,qDAAoDN,GAAI,IAAG;UACvE,MAAMa,MAAM,GAAGf,MAAM,CAACC,IAAI,CAACW,IAAI,CAACC,SAAS,CAAChD,MAAM,CAACrB,SAAS,CAAC,CAAC,CAAC2D,QAAQ,CACnE,QACF,CAAC;UACDtB,QAAQ,IAAK,qDAAoDkC,MAAO,IAAG;QAC7E;QAEA7D,SAAS,CAAC8D,GAAG,CAACL,WAAW,EAAEH,OAAO,CAAC;QAEnC,OAAO;UACL3B,QAAQ;UACRE,MAAM;UACNhB,UAAU,EAAE,IAAAN,aAAO,EAACiB,IAAI,CAACN,IAAI;QAC/B,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["_fs","require","_path","_esbuild","_transform","nodeModulesRegex","wywInJS","debug","sourceMap","preprocessor","esbuildOptions","filter","rest","options","cache","TransformCacheCollection","name","setup","build","cssLookup","Map","emitter","onDone","createFileReporter","asyncResolve","token","importer","context","isAbsolute","dirname","join","process","cwd","result","resolve","resolveDir","kind","errors","length","Error","path","replace","posix","sep","onEnd","onResolve","args","namespace","onLoad","contents","get","loader","basename","filterRegexp","RegExp","rawCode","readFileSync","ext","filename","parse","test","initialOptions","jsxFactory","jsxFragment","transformed","transformSync","sourcefile","sourcemap","code","esbuildMap","Buffer","from","map","toString","transformServices","root","pluginOptions","eventEmitter","transform","cssText","slug","slugify","cssFilename","JSON","stringify","cssSourceMapText","wywMap","set"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This file contains an esbuild loader for wyw-in-js.\n * It uses the transform.ts function to generate class names from source code,\n * returns transformed code without template literals and attaches generated source maps\n */\n\nimport { readFileSync } from 'fs';\nimport { basename, dirname, isAbsolute, join, parse, posix } from 'path';\n\nimport type { Plugin, TransformOptions, Loader } from 'esbuild';\nimport { transformSync } from 'esbuild';\n\nimport type {\n PluginOptions,\n Preprocessor,\n IFileReporterOptions,\n} from '@wyw-in-js/transform';\nimport {\n slugify,\n transform,\n TransformCacheCollection,\n createFileReporter,\n} from '@wyw-in-js/transform';\n\ntype EsbuildPluginOptions = {\n debug?: IFileReporterOptions | false | null | undefined;\n esbuildOptions?: TransformOptions;\n filter?: RegExp | string;\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nconst nodeModulesRegex = /^(?:.*[\\\\/])?node_modules(?:[\\\\/].*)?$/;\n\nexport default function wywInJS({\n debug,\n sourceMap,\n preprocessor,\n esbuildOptions,\n filter = /\\.(js|jsx|ts|tsx)$/,\n ...rest\n}: EsbuildPluginOptions = {}): Plugin {\n let options = esbuildOptions;\n const cache = new TransformCacheCollection();\n return {\n name: 'wyw-in-js',\n setup(build) {\n const cssLookup = new Map<string, string>();\n\n const { emitter, onDone } = createFileReporter(debug ?? false);\n\n const asyncResolve = async (\n token: string,\n importer: string\n ): Promise<string> => {\n const context = isAbsolute(importer)\n ? dirname(importer)\n : join(process.cwd(), dirname(importer));\n\n const result = await build.resolve(token, {\n resolveDir: context,\n kind: 'import-statement',\n });\n\n if (result.errors.length > 0) {\n throw new Error(`Cannot resolve ${token}`);\n }\n\n return result.path.replace(/\\\\/g, posix.sep);\n };\n\n build.onEnd(() => {\n onDone(process.cwd());\n });\n\n build.onResolve({ filter: /\\.wyw\\.css$/ }, (args) => {\n return {\n namespace: 'wyw-in-js',\n path: args.path,\n };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, (args) => {\n return {\n contents: cssLookup.get(args.path),\n loader: 'css',\n resolveDir: basename(args.path),\n };\n });\n\n const filterRegexp =\n typeof filter === 'string' ? new RegExp(filter) : filter;\n\n build.onLoad({ filter: filterRegexp }, async (args) => {\n const rawCode = readFileSync(args.path, 'utf8');\n const { ext, name: filename } = parse(args.path);\n const loader = ext.replace(/^\\./, '') as Loader;\n\n if (nodeModulesRegex.test(args.path)) {\n return {\n loader,\n contents: rawCode,\n };\n }\n\n if (!options) {\n options = {};\n if ('jsxFactory' in build.initialOptions) {\n options.jsxFactory = build.initialOptions.jsxFactory;\n }\n if ('jsxFragment' in build.initialOptions) {\n options.jsxFragment = build.initialOptions.jsxFragment;\n }\n }\n\n const transformed = transformSync(rawCode, {\n ...options,\n sourcefile: args.path,\n sourcemap: sourceMap,\n loader,\n });\n let { code } = transformed;\n\n if (sourceMap) {\n const esbuildMap = Buffer.from(transformed.map).toString('base64');\n code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;\n }\n\n const transformServices = {\n options: {\n filename: args.path,\n root: process.cwd(),\n preprocessor,\n pluginOptions: rest,\n },\n cache,\n eventEmitter: emitter,\n };\n\n const result = await transform(transformServices, code, asyncResolve);\n\n if (!result.cssText) {\n return {\n contents: code,\n loader,\n resolveDir: dirname(args.path),\n };\n }\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const cssFilename = `${filename}_${slug}.wyw.css`;\n\n let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n const wywMap = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `/*# sourceMappingURL=data:application/json;base64,${wywMap}*/`;\n }\n\n cssLookup.set(cssFilename, cssText);\n\n return {\n contents,\n loader,\n resolveDir: dirname(args.path),\n };\n });\n },\n };\n}\n"],"mappings":";;;;;;AAMA,IAAAA,GAAA,GAA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I;UAC/B,CAAC;QACH;QAEA,IAAI;UAAEuC;QAAQ,CAAC,GAAG9C,MAAM;QAExB,MAAM+C,IAAI,GAAG,IAAAC,kBAAO,EAACF,OAAO,CAAC;QAC7B,MAAMG,WAAW,GAAI,GAAExB,QAAS,IAAGsB,IAAK,UAAS;QAEjD,IAAI/B,QAAQ,GAAI,UAASkC,IAAI,CAACC,SAAS,CAACF,WAAW,CAAE,KAAIjD,MAAM,CAACmC,IAAK,EAAC;QAEtE,IAAI5D,SAAS,IAAIyB,MAAM,CAACoD,gBAAgB,EAAE;UACxC,MAAMb,GAAG,GAAGF,MAAM,CAACC,IAAI,CAACtC,MAAM,CAACoD,gBAAgB,CAAC,CAACZ,QAAQ,CAAC,QAAQ,CAAC;UACnEM,OAAO,IAAK,qDAAoDP,GAAI,IAAG;UACvE,MAAMc,MAAM,GAAGhB,MAAM,CAACC,IAAI,CAACY,IAAI,CAACC,SAAS,CAACnD,MAAM,CAACzB,SAAS,CAAC,CAAC,CAACiE,QAAQ,CACnE,QACF,CAAC;UACDxB,QAAQ,IAAK,qDAAoDqC,MAAO,IAAG;QAC7E;QAEAnE,SAAS,CAACoE,GAAG,CAACL,WAAW,EAAEH,OAAO,CAAC;QAEnC,OAAO;UACL9B,QAAQ;UACRE,MAAM;UACNhB,UAAU,EAAE,IAAAN,aAAO,EAACiB,IAAI,CAACN,IAAI;QAC/B,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wyw-in-js/esbuild",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@wyw-in-js/shared": "0.
|
|
6
|
-
"@wyw-in-js/transform": "0.
|
|
5
|
+
"@wyw-in-js/shared": "0.4.1",
|
|
6
|
+
"@wyw-in-js/transform": "0.4.1"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"@types/node": "^16.18.55",
|
|
10
10
|
"esbuild": "^0.15.16",
|
|
11
|
-
"@wyw-in-js/babel-config": "0.
|
|
12
|
-
"@wyw-in-js/eslint-config": "0.
|
|
13
|
-
"@wyw-in-js/jest-preset": "0.
|
|
14
|
-
"@wyw-in-js/ts-config": "0.
|
|
11
|
+
"@wyw-in-js/babel-config": "0.4.1",
|
|
12
|
+
"@wyw-in-js/eslint-config": "0.4.1",
|
|
13
|
+
"@wyw-in-js/jest-preset": "0.4.1",
|
|
14
|
+
"@wyw-in-js/ts-config": "0.4.1"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">=16.0.0"
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
|
20
|
-
"import": "./esm/index.
|
|
20
|
+
"import": "./esm/index.mjs",
|
|
21
21
|
"require": "./lib/index.js",
|
|
22
22
|
"types": "./types/index.d.ts"
|
|
23
23
|
},
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
],
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"main": "lib/index.js",
|
|
31
|
-
"module": "esm/index.
|
|
31
|
+
"module": "esm/index.mjs",
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"esbuild": ">=0.12.0"
|
|
34
34
|
},
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"types": "types/index.d.ts",
|
|
39
39
|
"scripts": {
|
|
40
|
-
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
40
|
+
"build:esm": "babel src --out-dir esm --out-file-extension .mjs --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
41
41
|
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
42
42
|
"build:types": "tsc --project ./tsconfig.lib.json --baseUrl . --rootDir ./src",
|
|
43
43
|
"lint": "eslint --ext .js,.ts ."
|
package/types/index.d.ts
CHANGED
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
* returns transformed code without template literals and attaches generated source maps
|
|
5
5
|
*/
|
|
6
6
|
import type { Plugin, TransformOptions } from 'esbuild';
|
|
7
|
-
import type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';
|
|
7
|
+
import type { PluginOptions, Preprocessor, IFileReporterOptions } from '@wyw-in-js/transform';
|
|
8
8
|
type EsbuildPluginOptions = {
|
|
9
|
+
debug?: IFileReporterOptions | false | null | undefined;
|
|
9
10
|
esbuildOptions?: TransformOptions;
|
|
11
|
+
filter?: RegExp | string;
|
|
10
12
|
preprocessor?: Preprocessor;
|
|
11
13
|
sourceMap?: boolean;
|
|
12
14
|
} & Partial<PluginOptions>;
|
|
13
|
-
export default function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest }?: EsbuildPluginOptions): Plugin;
|
|
15
|
+
export default function wywInJS({ debug, sourceMap, preprocessor, esbuildOptions, filter, ...rest }?: EsbuildPluginOptions): Plugin;
|
|
14
16
|
export {};
|
package/types/index.js
CHANGED
|
@@ -10,13 +10,14 @@ const path_1 = require("path");
|
|
|
10
10
|
const esbuild_1 = require("esbuild");
|
|
11
11
|
const transform_1 = require("@wyw-in-js/transform");
|
|
12
12
|
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
|
|
13
|
-
function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) {
|
|
13
|
+
function wywInJS({ debug, sourceMap, preprocessor, esbuildOptions, filter = /\.(js|jsx|ts|tsx)$/, ...rest } = {}) {
|
|
14
14
|
let options = esbuildOptions;
|
|
15
15
|
const cache = new transform_1.TransformCacheCollection();
|
|
16
16
|
return {
|
|
17
17
|
name: 'wyw-in-js',
|
|
18
18
|
setup(build) {
|
|
19
19
|
const cssLookup = new Map();
|
|
20
|
+
const { emitter, onDone } = (0, transform_1.createFileReporter)(debug ?? false);
|
|
20
21
|
const asyncResolve = async (token, importer) => {
|
|
21
22
|
const context = (0, path_1.isAbsolute)(importer)
|
|
22
23
|
? (0, path_1.dirname)(importer)
|
|
@@ -30,6 +31,9 @@ function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) {
|
|
|
30
31
|
}
|
|
31
32
|
return result.path.replace(/\\/g, path_1.posix.sep);
|
|
32
33
|
};
|
|
34
|
+
build.onEnd(() => {
|
|
35
|
+
onDone(process.cwd());
|
|
36
|
+
});
|
|
33
37
|
build.onResolve({ filter: /\.wyw\.css$/ }, (args) => {
|
|
34
38
|
return {
|
|
35
39
|
namespace: 'wyw-in-js',
|
|
@@ -43,7 +47,8 @@ function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) {
|
|
|
43
47
|
resolveDir: (0, path_1.basename)(args.path),
|
|
44
48
|
};
|
|
45
49
|
});
|
|
46
|
-
|
|
50
|
+
const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
|
|
51
|
+
build.onLoad({ filter: filterRegexp }, async (args) => {
|
|
47
52
|
const rawCode = (0, fs_1.readFileSync)(args.path, 'utf8');
|
|
48
53
|
const { ext, name: filename } = (0, path_1.parse)(args.path);
|
|
49
54
|
const loader = ext.replace(/^\./, '');
|
|
@@ -81,6 +86,7 @@ function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) {
|
|
|
81
86
|
pluginOptions: rest,
|
|
82
87
|
},
|
|
83
88
|
cache,
|
|
89
|
+
eventEmitter: emitter,
|
|
84
90
|
};
|
|
85
91
|
const result = await (0, transform_1.transform)(transformServices, code, asyncResolve);
|
|
86
92
|
if (!result.cssText) {
|
package/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["readFileSync","basename","dirname","isAbsolute","join","parse","posix","transformSync","slugify","transform","TransformCacheCollection","nodeModulesRegex","wywInJS","sourceMap","preprocessor","esbuildOptions","rest","options","cache","name","setup","build","cssLookup","Map","asyncResolve","token","importer","context","process","cwd","result","resolve","resolveDir","kind","errors","length","Error","path","replace","sep","onResolve","filter","args","namespace","onLoad","contents","get","loader","rawCode","ext","filename","test","initialOptions","jsxFactory","jsxFragment","transformed","sourcefile","sourcemap","code","esbuildMap","Buffer","from","map","toString","transformServices","root","pluginOptions","cssText","slug","cssFilename","JSON","stringify","cssSourceMapText","wywMap","set"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This file contains an esbuild loader for wyw-in-js.\n * It uses the transform.ts function to generate class names from source code,\n * returns transformed code without template literals and attaches generated source maps\n */\n\nimport { readFileSync } from 'fs';\nimport { basename, dirname, isAbsolute, join, parse, posix } from 'path';\n\nimport type { Plugin, TransformOptions, Loader } from 'esbuild';\nimport { transformSync } from 'esbuild';\n\nimport type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';\nimport {\n slugify,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\ntype EsbuildPluginOptions = {\n esbuildOptions?: TransformOptions;\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nconst nodeModulesRegex = /^(?:.*[\\\\/])?node_modules(?:[\\\\/].*)?$/;\n\nexport default function wywInJS({\n sourceMap,\n preprocessor,\n esbuildOptions,\n ...rest\n}: EsbuildPluginOptions = {}): Plugin {\n let options = esbuildOptions;\n const cache = new TransformCacheCollection();\n return {\n name: 'wyw-in-js',\n setup(build) {\n const cssLookup = new Map<string, string>();\n\n const asyncResolve = async (\n token: string,\n importer: string\n ): Promise<string> => {\n const context = isAbsolute(importer)\n ? dirname(importer)\n : join(process.cwd(), dirname(importer));\n\n const result = await build.resolve(token, {\n resolveDir: context,\n kind: 'import-statement',\n });\n\n if (result.errors.length > 0) {\n throw new Error(`Cannot resolve ${token}`);\n }\n\n return result.path.replace(/\\\\/g, posix.sep);\n };\n\n build.onResolve({ filter: /\\.wyw\\.css$/ }, (args) => {\n return {\n namespace: 'wyw-in-js',\n path: args.path,\n };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, (args) => {\n return {\n contents: cssLookup.get(args.path),\n loader: 'css',\n resolveDir: basename(args.path),\n };\n });\n\n build.onLoad({ filter: /\\.(js|jsx|ts|tsx)$/ }, async (args) => {\n const rawCode = readFileSync(args.path, 'utf8');\n const { ext, name: filename } = parse(args.path);\n const loader = ext.replace(/^\\./, '') as Loader;\n\n if (nodeModulesRegex.test(args.path)) {\n return {\n loader,\n contents: rawCode,\n };\n }\n\n if (!options) {\n options = {};\n if ('jsxFactory' in build.initialOptions) {\n options.jsxFactory = build.initialOptions.jsxFactory;\n }\n if ('jsxFragment' in build.initialOptions) {\n options.jsxFragment = build.initialOptions.jsxFragment;\n }\n }\n\n const transformed = transformSync(rawCode, {\n ...options,\n sourcefile: args.path,\n sourcemap: sourceMap,\n loader,\n });\n let { code } = transformed;\n\n if (sourceMap) {\n const esbuildMap = Buffer.from(transformed.map).toString('base64');\n code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;\n }\n\n const transformServices = {\n options: {\n filename: args.path,\n root: process.cwd(),\n preprocessor,\n pluginOptions: rest,\n },\n cache,\n };\n\n const result = await transform(transformServices, code, asyncResolve);\n\n if (!result.cssText) {\n return {\n contents: code,\n loader,\n resolveDir: dirname(args.path),\n };\n }\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const cssFilename = `${filename}_${slug}.wyw.css`;\n\n let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n const wywMap = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `/*# sourceMappingURL=data:application/json;base64,${wywMap}*/`;\n }\n\n cssLookup.set(cssFilename, cssText);\n\n return {\n contents,\n loader,\n resolveDir: dirname(args.path),\n };\n });\n },\n };\n}\n"],"mappings":"AAAA;A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