@wyw-in-js/esbuild 0.2.3 → 0.4.0

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.
@@ -13,6 +13,7 @@ export default function wywInJS({
13
13
  sourceMap,
14
14
  preprocessor,
15
15
  esbuildOptions,
16
+ filter = /\.(js|jsx|ts|tsx)$/,
16
17
  ...rest
17
18
  } = {}) {
18
19
  let options = esbuildOptions;
@@ -50,8 +51,9 @@ export default function wywInJS({
50
51
  resolveDir: basename(args.path)
51
52
  };
52
53
  });
54
+ const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
53
55
  build.onLoad({
54
- filter: /\.(js|jsx|ts|tsx)$/
56
+ filter: filterRegexp
55
57
  }, async args => {
56
58
  const rawCode = readFileSync(args.path, 'utf8');
57
59
  const {
@@ -126,4 +128,4 @@ export default function wywInJS({
126
128
  }
127
129
  };
128
130
  }
129
- //# sourceMappingURL=index.js.map
131
+ //# 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","nodeModulesRegex","wywInJS","sourceMap","preprocessor","esbuildOptions","filter","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","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","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 filter?: RegExp | string;\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 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 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 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 };\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;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;AAS7B,MAAMC,gBAAgB,GAAG,wCAAwC;AAEjE,eAAe,SAASC,OAAOA,CAAC;EAC9BC,SAAS;EACTC,YAAY;EACZC,cAAc;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CAACC,SAAS,CAAC1C,MAAM,CAAClB,SAAS,CAAC,CAAC,CAACoD,QAAQ,CACnE,QACF,CAAC;UACDpB,QAAQ,IAAK,qDAAoD8B,MAAO,IAAG;QAC7E;QAEApD,SAAS,CAACqD,GAAG,CAACL,WAAW,EAAEF,OAAO,CAAC;QAEnC,OAAO;UACLxB,QAAQ;UACRE,MAAM;UACNd,UAAU,EAAE/B,OAAO,CAACwC,IAAI,CAACJ,IAAI;QAC/B,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH"}
package/lib/index.js CHANGED
@@ -19,6 +19,7 @@ function wywInJS({
19
19
  sourceMap,
20
20
  preprocessor,
21
21
  esbuildOptions,
22
+ filter = /\.(js|jsx|ts|tsx)$/,
22
23
  ...rest
23
24
  } = {}) {
24
25
  let options = esbuildOptions;
@@ -56,8 +57,9 @@ function wywInJS({
56
57
  resolveDir: (0, _path.basename)(args.path)
57
58
  };
58
59
  });
60
+ const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
59
61
  build.onLoad({
60
- filter: /\.(js|jsx|ts|tsx)$/
62
+ filter: filterRegexp
61
63
  }, async args => {
62
64
  const rawCode = (0, _fs.readFileSync)(args.path, 'utf8');
63
65
  const {
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,G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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","sourceMap","preprocessor","esbuildOptions","filter","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","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","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 filter?: RegExp | string;\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 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 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 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 };\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;;AAsBA,MAAMI,gBAAgB,GAAG,wCAAwC;AAElD,SAASC,OAAOA,CAAC;EAC9BC,SAAS;EACTC,YAAY;EACZC,cAAc;EACdC,MAAM,GAAG,oBAAoB;EAC7B,GAAGC;AACiB,CAAC,GAAG,CAAC,CAAC,EAAU;EACpC,IAAIC,OAAO,GAAGH,cAAc;EAC5B,MAAMI,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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2.3",
3
+ "version": "0.4.0",
4
4
  "dependencies": {
5
- "@wyw-in-js/shared": "0.2.3",
6
- "@wyw-in-js/transform": "0.2.3"
5
+ "@wyw-in-js/shared": "0.4.0",
6
+ "@wyw-in-js/transform": "0.4.0"
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.2.3",
12
- "@wyw-in-js/eslint-config": "0.2.3",
13
- "@wyw-in-js/jest-preset": "0.2.3",
14
- "@wyw-in-js/ts-config": "0.2.3"
11
+ "@wyw-in-js/babel-config": "0.4.0",
12
+ "@wyw-in-js/eslint-config": "0.4.0",
13
+ "@wyw-in-js/jest-preset": "0.4.0",
14
+ "@wyw-in-js/ts-config": "0.4.0"
15
15
  },
16
16
  "engines": {
17
17
  "node": ">=16.0.0"
18
18
  },
19
19
  "exports": {
20
- "import": "./esm/index.js",
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.js",
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
@@ -7,8 +7,9 @@ import type { Plugin, TransformOptions } from 'esbuild';
7
7
  import type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';
8
8
  type EsbuildPluginOptions = {
9
9
  esbuildOptions?: TransformOptions;
10
+ filter?: RegExp | string;
10
11
  preprocessor?: Preprocessor;
11
12
  sourceMap?: boolean;
12
13
  } & Partial<PluginOptions>;
13
- export default function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest }?: EsbuildPluginOptions): Plugin;
14
+ export default function wywInJS({ sourceMap, preprocessor, esbuildOptions, filter, ...rest }?: EsbuildPluginOptions): Plugin;
14
15
  export {};
package/types/index.js CHANGED
@@ -10,7 +10,7 @@ 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({ 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 {
@@ -43,7 +43,8 @@ function wywInJS({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) {
43
43
  resolveDir: (0, path_1.basename)(args.path),
44
44
  };
45
45
  });
46
- build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async (args) => {
46
+ const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
47
+ build.onLoad({ filter: filterRegexp }, async (args) => {
47
48
  const rawCode = (0, fs_1.readFileSync)(args.path, 'utf8');
48
49
  const { ext, name: filename } = (0, path_1.parse)(args.path);
49
50
  const loader = ext.replace(/^\./, '');
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 resolveD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