@wyw-in-js/esbuild 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Anton Evzhakov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/esm/index.js ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * This file contains an esbuild loader for wyw-in-js.
3
+ * It uses the transform.ts function to generate class names from source code,
4
+ * returns transformed code without template literals and attaches generated source maps
5
+ */
6
+
7
+ import { readFileSync } from 'fs';
8
+ import { basename, dirname, isAbsolute, join, parse, posix } from 'path';
9
+ import { transformSync } from 'esbuild';
10
+ import { slugify, transform, TransformCacheCollection } from '@wyw-in-js/transform';
11
+ const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
12
+ export default function wywInJS({
13
+ sourceMap,
14
+ preprocessor,
15
+ esbuildOptions,
16
+ ...rest
17
+ } = {}) {
18
+ let options = esbuildOptions;
19
+ const cache = new TransformCacheCollection();
20
+ return {
21
+ name: 'wyw-in-js',
22
+ setup(build) {
23
+ const cssLookup = new Map();
24
+ const asyncResolve = async (token, importer) => {
25
+ const context = isAbsolute(importer) ? dirname(importer) : join(process.cwd(), dirname(importer));
26
+ const result = await build.resolve(token, {
27
+ resolveDir: context,
28
+ kind: 'import-statement'
29
+ });
30
+ if (result.errors.length > 0) {
31
+ throw new Error(`Cannot resolve ${token}`);
32
+ }
33
+ return result.path.replace(/\\/g, posix.sep);
34
+ };
35
+ build.onResolve({
36
+ filter: /\.linaria\.css$/
37
+ }, args => {
38
+ return {
39
+ namespace: 'linaria',
40
+ path: args.path
41
+ };
42
+ });
43
+ build.onLoad({
44
+ filter: /.*/,
45
+ namespace: 'linaria'
46
+ }, args => {
47
+ return {
48
+ contents: cssLookup.get(args.path),
49
+ loader: 'css',
50
+ resolveDir: basename(args.path)
51
+ };
52
+ });
53
+ build.onLoad({
54
+ filter: /\.(js|jsx|ts|tsx)$/
55
+ }, async args => {
56
+ const rawCode = readFileSync(args.path, 'utf8');
57
+ const {
58
+ ext,
59
+ name: filename
60
+ } = parse(args.path);
61
+ const loader = ext.replace(/^\./, '');
62
+ if (nodeModulesRegex.test(args.path)) {
63
+ return {
64
+ loader,
65
+ contents: rawCode
66
+ };
67
+ }
68
+ if (!options) {
69
+ options = {};
70
+ if ('jsxFactory' in build.initialOptions) {
71
+ options.jsxFactory = build.initialOptions.jsxFactory;
72
+ }
73
+ if ('jsxFragment' in build.initialOptions) {
74
+ options.jsxFragment = build.initialOptions.jsxFragment;
75
+ }
76
+ }
77
+ const transformed = transformSync(rawCode, {
78
+ ...options,
79
+ sourcefile: args.path,
80
+ sourcemap: sourceMap,
81
+ loader
82
+ });
83
+ let {
84
+ code
85
+ } = transformed;
86
+ if (sourceMap) {
87
+ const esbuildMap = Buffer.from(transformed.map).toString('base64');
88
+ code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;
89
+ }
90
+ const transformServices = {
91
+ options: {
92
+ filename: args.path,
93
+ root: process.cwd(),
94
+ preprocessor,
95
+ pluginOptions: rest
96
+ },
97
+ cache
98
+ };
99
+ const result = await transform(transformServices, code, asyncResolve);
100
+ if (!result.cssText) {
101
+ return {
102
+ contents: code,
103
+ loader,
104
+ resolveDir: dirname(args.path)
105
+ };
106
+ }
107
+ let {
108
+ cssText
109
+ } = result;
110
+ const slug = slugify(cssText);
111
+ const cssFilename = `${filename}_${slug}.linaria.css`;
112
+ let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;
113
+ if (sourceMap && result.cssSourceMapText) {
114
+ const map = Buffer.from(result.cssSourceMapText).toString('base64');
115
+ cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
116
+ const linariaMap = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
117
+ contents += `/*# sourceMappingURL=data:application/json;base64,${linariaMap}*/`;
118
+ }
119
+ cssLookup.set(cssFilename, cssText);
120
+ return {
121
+ contents,
122
+ loader,
123
+ resolveDir: dirname(args.path)
124
+ };
125
+ });
126
+ }
127
+ };
128
+ }
129
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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","linariaMap","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: /\\.linaria\\.css$/ }, (args) => {\n return {\n namespace: 'linaria',\n path: args.path,\n };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'linaria' }, (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}.linaria.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 linariaMap = Buffer.from(\n JSON.stringify(result.sourceMap)\n ).toString('base64');\n contents += `/*# sourceMappingURL=data:application/json;base64,${linariaMap}*/`;\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;AACA;AACA;AACA;AACA;;AAEA,SAASA,YAAY,QAAQ,IAAI;AACjC,SAASC,QAAQ,EAAEC,OAAO,EAAEC,UAAU,EAAEC,IAAI,EAAEC,KAAK,EAAEC,KAAK,QAAQ,MAAM;AAGxE,SAASC,aAAa,QAAQ,SAAS;AAGvC,SACEC,OAAO,EACPC,SAAS,EACTC,wBAAwB,QACnB,sBAAsB;AAQ7B,MAAMC,gBAAgB,GAAG,wCAAwC;AAEjE,eAAe,SAASC,OAAOA,C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oD4B,UAAW,IAAG;QACjF;QAEAnD,SAAS,CAACoD,GAAG,CAACL,WAAW,EAAEF,OAAO,CAAC;QAEnC,OAAO;UACLtB,QAAQ;UACRE,MAAM;UACNf,UAAU,EAAE9B,OAAO,CAACwC,IAAI,CAACL,IAAI;QAC/B,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH"}
package/lib/index.js ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = wywInJS;
7
+ var _fs = require("fs");
8
+ var _path = require("path");
9
+ var _esbuild = require("esbuild");
10
+ var _transform = require("@wyw-in-js/transform");
11
+ /**
12
+ * This file contains an esbuild loader for wyw-in-js.
13
+ * It uses the transform.ts function to generate class names from source code,
14
+ * returns transformed code without template literals and attaches generated source maps
15
+ */
16
+
17
+ const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
18
+ function wywInJS({
19
+ sourceMap,
20
+ preprocessor,
21
+ esbuildOptions,
22
+ ...rest
23
+ } = {}) {
24
+ let options = esbuildOptions;
25
+ const cache = new _transform.TransformCacheCollection();
26
+ return {
27
+ name: 'wyw-in-js',
28
+ setup(build) {
29
+ const cssLookup = new Map();
30
+ const asyncResolve = async (token, importer) => {
31
+ const context = (0, _path.isAbsolute)(importer) ? (0, _path.dirname)(importer) : (0, _path.join)(process.cwd(), (0, _path.dirname)(importer));
32
+ const result = await build.resolve(token, {
33
+ resolveDir: context,
34
+ kind: 'import-statement'
35
+ });
36
+ if (result.errors.length > 0) {
37
+ throw new Error(`Cannot resolve ${token}`);
38
+ }
39
+ return result.path.replace(/\\/g, _path.posix.sep);
40
+ };
41
+ build.onResolve({
42
+ filter: /\.linaria\.css$/
43
+ }, args => {
44
+ return {
45
+ namespace: 'linaria',
46
+ path: args.path
47
+ };
48
+ });
49
+ build.onLoad({
50
+ filter: /.*/,
51
+ namespace: 'linaria'
52
+ }, args => {
53
+ return {
54
+ contents: cssLookup.get(args.path),
55
+ loader: 'css',
56
+ resolveDir: (0, _path.basename)(args.path)
57
+ };
58
+ });
59
+ build.onLoad({
60
+ filter: /\.(js|jsx|ts|tsx)$/
61
+ }, async args => {
62
+ const rawCode = (0, _fs.readFileSync)(args.path, 'utf8');
63
+ const {
64
+ ext,
65
+ name: filename
66
+ } = (0, _path.parse)(args.path);
67
+ const loader = ext.replace(/^\./, '');
68
+ if (nodeModulesRegex.test(args.path)) {
69
+ return {
70
+ loader,
71
+ contents: rawCode
72
+ };
73
+ }
74
+ if (!options) {
75
+ options = {};
76
+ if ('jsxFactory' in build.initialOptions) {
77
+ options.jsxFactory = build.initialOptions.jsxFactory;
78
+ }
79
+ if ('jsxFragment' in build.initialOptions) {
80
+ options.jsxFragment = build.initialOptions.jsxFragment;
81
+ }
82
+ }
83
+ const transformed = (0, _esbuild.transformSync)(rawCode, {
84
+ ...options,
85
+ sourcefile: args.path,
86
+ sourcemap: sourceMap,
87
+ loader
88
+ });
89
+ let {
90
+ code
91
+ } = transformed;
92
+ if (sourceMap) {
93
+ const esbuildMap = Buffer.from(transformed.map).toString('base64');
94
+ code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;
95
+ }
96
+ const transformServices = {
97
+ options: {
98
+ filename: args.path,
99
+ root: process.cwd(),
100
+ preprocessor,
101
+ pluginOptions: rest
102
+ },
103
+ cache
104
+ };
105
+ const result = await (0, _transform.transform)(transformServices, code, asyncResolve);
106
+ if (!result.cssText) {
107
+ return {
108
+ contents: code,
109
+ loader,
110
+ resolveDir: (0, _path.dirname)(args.path)
111
+ };
112
+ }
113
+ let {
114
+ cssText
115
+ } = result;
116
+ const slug = (0, _transform.slugify)(cssText);
117
+ const cssFilename = `${filename}_${slug}.linaria.css`;
118
+ let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;
119
+ if (sourceMap && result.cssSourceMapText) {
120
+ const map = Buffer.from(result.cssSourceMapText).toString('base64');
121
+ cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
122
+ const linariaMap = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
123
+ contents += `/*# sourceMappingURL=data:application/json;base64,${linariaMap}*/`;
124
+ }
125
+ cssLookup.set(cssFilename, cssText);
126
+ return {
127
+ contents,
128
+ loader,
129
+ resolveDir: (0, _path.dirname)(args.path)
130
+ };
131
+ });
132
+ }
133
+ };
134
+ }
135
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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","linariaMap","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: /\\.linaria\\.css$/ }, (args) => {\n return {\n namespace: 'linaria',\n path: args.path,\n };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'linaria' }, (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}.linaria.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 linariaMap = Buffer.from(\n JSON.stringify(result.sourceMap)\n ).toString('base64');\n contents += `/*# sourceMappingURL=data:application/json;base64,${linariaMap}*/`;\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,CAA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f,MAAM;YACNhB,UAAU,EAAE,IAAAN,aAAO,EAACiB,IAAI,CAACN,IAAI;UAC/B,CAAC;QACH;QAEA,IAAI;UAAEoC;QAAQ,CAAC,GAAG3C,MAAM;QAExB,MAAM4C,IAAI,GAAG,IAAAC,kBAAO,EAACF,OAAO,CAAC;QAC7B,MAAMG,WAAW,GAAI,GAAEvB,QAAS,IAAGqB,IAAK,cAAa;QAErD,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,UAAU,GAAGf,MAAM,CAACC,IAAI,CAC5BW,IAAI,CAACC,SAAS,CAAChD,MAAM,CAACrB,SAAS,CACjC,CAAC,CAAC2D,QAAQ,CAAC,QAAQ,CAAC;UACpBtB,QAAQ,IAAK,qDAAoDkC,UAAW,IAAG;QACjF;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"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@wyw-in-js/esbuild",
3
+ "version": "0.1.1",
4
+ "dependencies": {
5
+ "@wyw-in-js/shared": "0.1.1",
6
+ "@wyw-in-js/transform": "0.1.1"
7
+ },
8
+ "devDependencies": {
9
+ "@types/node": "^16.18.55",
10
+ "esbuild": "^0.15.16",
11
+ "@wyw-in-js/babel-config": "0.1.1",
12
+ "@wyw-in-js/eslint-config": "0.1.1",
13
+ "@wyw-in-js/jest-preset": "0.1.1",
14
+ "@wyw-in-js/ts-config": "0.1.1"
15
+ },
16
+ "engines": {
17
+ "node": ">=16.0.0"
18
+ },
19
+ "exports": {
20
+ "import": "./esm/index.js",
21
+ "require": "./lib/index.js",
22
+ "types": "./types/index.d.ts"
23
+ },
24
+ "files": [
25
+ "esm/",
26
+ "lib/",
27
+ "types/"
28
+ ],
29
+ "license": "MIT",
30
+ "main": "lib/index.js",
31
+ "module": "esm/index.js",
32
+ "peerDependencies": {
33
+ "esbuild": ">=0.12.0"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "types": "types/index.d.ts",
39
+ "scripts": {
40
+ "build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
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
+ "build:types": "tsc --project ./tsconfig.lib.json --baseUrl . --rootDir ./src",
43
+ "lint": "eslint --ext .js,.ts ."
44
+ }
45
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * This file contains an esbuild loader for wyw-in-js.
3
+ * It uses the transform.ts function to generate class names from source code,
4
+ * returns transformed code without template literals and attaches generated source maps
5
+ */
6
+ import type { Plugin, TransformOptions } from 'esbuild';
7
+ import type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';
8
+ type EsbuildPluginOptions = {
9
+ esbuildOptions?: TransformOptions;
10
+ preprocessor?: Preprocessor;
11
+ sourceMap?: boolean;
12
+ } & Partial<PluginOptions>;
13
+ export default function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest }?: EsbuildPluginOptions): Plugin;
14
+ export {};
package/types/index.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ /**
3
+ * This file contains an esbuild loader for wyw-in-js.
4
+ * It uses the transform.ts function to generate class names from source code,
5
+ * returns transformed code without template literals and attaches generated source maps
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ const fs_1 = require("fs");
9
+ const path_1 = require("path");
10
+ const esbuild_1 = require("esbuild");
11
+ const transform_1 = require("@wyw-in-js/transform");
12
+ const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
13
+ function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) {
14
+ let options = esbuildOptions;
15
+ const cache = new transform_1.TransformCacheCollection();
16
+ return {
17
+ name: 'wyw-in-js',
18
+ setup(build) {
19
+ const cssLookup = new Map();
20
+ const asyncResolve = async (token, importer) => {
21
+ const context = (0, path_1.isAbsolute)(importer)
22
+ ? (0, path_1.dirname)(importer)
23
+ : (0, path_1.join)(process.cwd(), (0, path_1.dirname)(importer));
24
+ const result = await build.resolve(token, {
25
+ resolveDir: context,
26
+ kind: 'import-statement',
27
+ });
28
+ if (result.errors.length > 0) {
29
+ throw new Error(`Cannot resolve ${token}`);
30
+ }
31
+ return result.path.replace(/\\/g, path_1.posix.sep);
32
+ };
33
+ build.onResolve({ filter: /\.linaria\.css$/ }, (args) => {
34
+ return {
35
+ namespace: 'linaria',
36
+ path: args.path,
37
+ };
38
+ });
39
+ build.onLoad({ filter: /.*/, namespace: 'linaria' }, (args) => {
40
+ return {
41
+ contents: cssLookup.get(args.path),
42
+ loader: 'css',
43
+ resolveDir: (0, path_1.basename)(args.path),
44
+ };
45
+ });
46
+ build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async (args) => {
47
+ const rawCode = (0, fs_1.readFileSync)(args.path, 'utf8');
48
+ const { ext, name: filename } = (0, path_1.parse)(args.path);
49
+ const loader = ext.replace(/^\./, '');
50
+ if (nodeModulesRegex.test(args.path)) {
51
+ return {
52
+ loader,
53
+ contents: rawCode,
54
+ };
55
+ }
56
+ if (!options) {
57
+ options = {};
58
+ if ('jsxFactory' in build.initialOptions) {
59
+ options.jsxFactory = build.initialOptions.jsxFactory;
60
+ }
61
+ if ('jsxFragment' in build.initialOptions) {
62
+ options.jsxFragment = build.initialOptions.jsxFragment;
63
+ }
64
+ }
65
+ const transformed = (0, esbuild_1.transformSync)(rawCode, {
66
+ ...options,
67
+ sourcefile: args.path,
68
+ sourcemap: sourceMap,
69
+ loader,
70
+ });
71
+ let { code } = transformed;
72
+ if (sourceMap) {
73
+ const esbuildMap = Buffer.from(transformed.map).toString('base64');
74
+ code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`;
75
+ }
76
+ const transformServices = {
77
+ options: {
78
+ filename: args.path,
79
+ root: process.cwd(),
80
+ preprocessor,
81
+ pluginOptions: rest,
82
+ },
83
+ cache,
84
+ };
85
+ const result = await (0, transform_1.transform)(transformServices, code, asyncResolve);
86
+ if (!result.cssText) {
87
+ return {
88
+ contents: code,
89
+ loader,
90
+ resolveDir: (0, path_1.dirname)(args.path),
91
+ };
92
+ }
93
+ let { cssText } = result;
94
+ const slug = (0, transform_1.slugify)(cssText);
95
+ const cssFilename = `${filename}_${slug}.linaria.css`;
96
+ let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`;
97
+ if (sourceMap && result.cssSourceMapText) {
98
+ const map = Buffer.from(result.cssSourceMapText).toString('base64');
99
+ cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
100
+ const linariaMap = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
101
+ contents += `/*# sourceMappingURL=data:application/json;base64,${linariaMap}*/`;
102
+ }
103
+ cssLookup.set(cssFilename, cssText);
104
+ return {
105
+ contents,
106
+ loader,
107
+ resolveDir: (0, path_1.dirname)(args.path),
108
+ };
109
+ });
110
+ },
111
+ };
112
+ }
113
+ exports.default = wywInJS;