@wyw-in-js/rollup 2.0.1 → 2.1.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.
package/README.md CHANGED
@@ -53,6 +53,18 @@ wyw({
53
53
  });
54
54
  ```
55
55
 
56
+ ### Stable CSS filenames in watch mode
57
+
58
+ By default, the Rollup plugin includes a slug based on the generated CSS content in the virtual CSS filename. Some CSS
59
+ bundler plugins cache transformed CSS by filename during watch mode and work better with a stable id. You can override
60
+ the generated CSS filename with `cssFilename`:
61
+
62
+ ```js
63
+ wyw({
64
+ cssFilename: ({ id }) => `${id.replace(/\.[jt]sx?$/, '')}.css`,
65
+ });
66
+ ```
67
+
56
68
  ## Disabling vendor prefixing
57
69
 
58
70
  Stylis adds vendor-prefixed CSS by default. To disable it (and reduce CSS size), pass `prefixer: false`:
package/esm/index.mjs CHANGED
@@ -6,11 +6,12 @@
6
6
  import { createFilter } from "@rollup/pluginutils";
7
7
  import { asyncResolverFactory, logger, slugify, syncResolve } from "@wyw-in-js/shared";
8
8
  import { disposeEvalBroker, getFileIdx, transform, TransformCacheCollection } from "@wyw-in-js/transform";
9
- export default function wywInJS({ exclude, include, keepComments, prefixer, preprocessor, serializeTransform = true, sourceMap, ...rest } = {}) {
9
+ export default function wywInJS({ cssFilename, exclude, include, keepComments, prefixer, preprocessor, serializeTransform = true, sourceMap, ...rest } = {}) {
10
10
  const filter = createFilter(include, exclude);
11
11
  const cssLookup = {};
12
12
  const cache = new TransformCacheCollection();
13
13
  const emptyConfig = {};
14
+ const dependencyLoadDepth = new Map();
14
15
  let transformQueue = Promise.resolve();
15
16
  const boundResolveCache = new WeakMap();
16
17
  const getBoundResolve = (ctx) => {
@@ -25,6 +26,21 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
25
26
  });
26
27
  return boundResolve;
27
28
  };
29
+ const normalizeId = (id) => id.split("?")[0].split("#")[0];
30
+ const beginDependencyLoad = (id) => {
31
+ const normalized = normalizeId(id);
32
+ dependencyLoadDepth.set(normalized, (dependencyLoadDepth.get(normalized) ?? 0) + 1);
33
+ };
34
+ const endDependencyLoad = (id) => {
35
+ const normalized = normalizeId(id);
36
+ const depth = dependencyLoadDepth.get(normalized) ?? 0;
37
+ if (depth <= 1) {
38
+ dependencyLoadDepth.delete(normalized);
39
+ return;
40
+ }
41
+ dependencyLoadDepth.set(normalized, depth - 1);
42
+ };
43
+ const isDependencyLoad = (id) => dependencyLoadDepth.has(normalizeId(id));
28
44
  const runSerialized = async (fn) => {
29
45
  if (!serializeTransform) {
30
46
  return fn();
@@ -72,7 +88,7 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
72
88
  if (importee in cssLookup) return importee;
73
89
  },
74
90
  async transform(code, id) {
75
- return runSerialized(async () => {
91
+ const run = async () => {
76
92
  // Do not transform ignored and generated files
77
93
  if (!filter(id) || id in cssLookup) return;
78
94
  const log = logger.extend("rollup").extend(getFileIdx(id));
@@ -87,13 +103,32 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
87
103
  root: process.cwd()
88
104
  },
89
105
  cache,
90
- emitWarning: (message) => this.warn(message)
106
+ emitWarning: (message) => this.warn(message),
107
+ loadDependencyCode: async (resolved) => {
108
+ beginDependencyLoad(resolved);
109
+ try {
110
+ const loaded = await this.load({ id: resolved });
111
+ const cached = cache.get("entrypoints", resolved);
112
+ if (cached && "initialCode" in cached && typeof cached.initialCode === "string") {
113
+ return undefined;
114
+ }
115
+ return typeof loaded?.code === "string" ? loaded.code : undefined;
116
+ } finally {
117
+ endDependencyLoad(resolved);
118
+ }
119
+ }
91
120
  };
92
121
  const result = await transform(transformServices, code, createAsyncResolver(getBoundResolve(this)), emptyConfig);
93
122
  if (!result.cssText) return;
94
123
  let { cssText } = result;
95
124
  const slug = slugify(cssText);
96
- const filename = `${id.replace(/\.[jt]sx?$/, "")}_${slug}.css`;
125
+ const defaultFilename = `${id.replace(/\.[jt]sx?$/, "")}_${slug}.css`;
126
+ const filename = cssFilename?.({
127
+ cssText,
128
+ defaultFilename,
129
+ id,
130
+ slug
131
+ }) ?? defaultFilename;
97
132
  if (sourceMap && result.cssSourceMapText) {
98
133
  const map = Buffer.from(result.cssSourceMapText).toString("base64");
99
134
  cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
@@ -105,7 +140,11 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
105
140
  code: result.code,
106
141
  map: result.sourceMap
107
142
  };
108
- });
143
+ };
144
+ if (isDependencyLoad(id)) {
145
+ return run();
146
+ }
147
+ return runSerialized(run);
109
148
  }
110
149
  };
111
150
  return new Proxy(plugin, {
package/esm/index.mjs.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;AAMA,SAAS,oBAAoB;AAG7B,SACE,sBACA,QACA,SACA,mBACK;AAEP,SACE,mBACA,YACA,WACA,gCACK;AAYP,eAAe,SAAS,QAAQ,EAC9B,SACA,SACA,cACA,UACA,cACA,qBAAqB,MACrB,WACA,GAAG,SACoB,EAAE,EAAU;CACnC,MAAM,SAAS,aAAa,SAAS,QAAQ;CAC7C,MAAM,YAAuC,EAAE;CAC/C,MAAM,QAAQ,IAAI,0BAA0B;CAC5C,MAAM,cAAc,EAAE;CACtB,IAAI,iBAAiB,QAAQ,SAAS;CAItC,MAAM,oBAAoB,IAAI,SAG3B;CAEH,MAAM,mBAAmB,QAAkC;EACzD,MAAM,SAAS,kBAAkB,IAAI,IAAI;AACzC,MAAI,UAAU,OAAO,kBAAkB,IAAI,SAAS;AAClD,UAAO,OAAO;;EAGhB,MAAM,eAA0B,IAAI,QAAQ,KAAK,IAAI;AACrD,oBAAkB,IAAI,KAAK;GAAE,eAAe,IAAI;GAAS;GAAc,CAAC;AACxE,SAAO;;CAGT,MAAM,gBAAgB,OAAU,OAAqC;AACnE,MAAI,CAAC,oBAAoB;AACvB,UAAO,IAAI;;EAGb,IAAI;EACJ,MAAM,WAAW;AACjB,mBAAiB,IAAI,SAAe,YAAY;AAC9C,aAAU;IACV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SAAS;AACnE,gBAAW,qDAAqD,IAAI;;AAGtE,cAAU,YAAY;AAEtB,WAAO,QAAQ,YAAY,KAAK,UAAU,SAAS,CAAC;;AAGpD,WAAO;KAAE,MAAM,OAAO;KAAM,KAAK,OAAO;KAAW;KACnD;;EAEL;AAED,QAAO,IAAI,MAAc,QAAQ;EAC/B,IAAI,QAAQ,MAAM;AAChB,UAAO,OAAO;;EAGhB,yBAAyB,QAAQ,MAAM;AACrC,UAAO,OAAO,yBAAyB,QAAQ,KAAqB;;EAEvE,CAAC","names":[],"sources":["../src/index.ts"],"version":3,"sourcesContent":["/**\n * This file contains a Rollup 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 { createFilter } from '@rollup/pluginutils';\nimport type { Plugin, PluginContext, ResolvedId } from 'rollup';\n\nimport {\n asyncResolverFactory,\n logger,\n slugify,\n syncResolve,\n} from '@wyw-in-js/shared';\nimport type { PluginOptions, Preprocessor, Result } from '@wyw-in-js/transform';\nimport {\n disposeEvalBroker,\n getFileIdx,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\ntype RollupPluginOptions = {\n exclude?: string | string[];\n include?: string | string[];\n keepComments?: boolean | RegExp;\n prefixer?: boolean;\n preprocessor?: Preprocessor;\n serializeTransform?: boolean;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nexport default function wywInJS({\n exclude,\n include,\n keepComments,\n prefixer,\n preprocessor,\n serializeTransform = true,\n sourceMap,\n ...rest\n}: RollupPluginOptions = {}): Plugin {\n const filter = createFilter(include, exclude);\n const cssLookup: { [key: string]: string } = {};\n const cache = new TransformCacheCollection();\n const emptyConfig = {};\n let transformQueue = Promise.resolve();\n\n type ResolveFn = PluginContext['resolve'];\n\n const boundResolveCache = new WeakMap<\n PluginContext,\n { boundResolve: ResolveFn; sourceResolve: ResolveFn }\n >();\n\n const getBoundResolve = (ctx: PluginContext): ResolveFn => {\n const cached = boundResolveCache.get(ctx);\n if (cached && cached.sourceResolve === ctx.resolve) {\n return cached.boundResolve;\n }\n\n const boundResolve: ResolveFn = ctx.resolve.bind(ctx);\n boundResolveCache.set(ctx, { sourceResolve: ctx.resolve, boundResolve });\n return boundResolve;\n };\n\n const runSerialized = async <T>(fn: () => Promise<T>): Promise<T> => {\n if (!serializeTransform) {\n return fn();\n }\n\n let release: () => void;\n const previous = transformQueue;\n transformQueue = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n\n try {\n return await fn();\n } finally {\n release!();\n }\n };\n\n const createAsyncResolver = asyncResolverFactory(\n async (resolved: ResolvedId | null, what, importer, stack) => {\n if (resolved) {\n if (resolved.external) {\n // If module is marked as external, Rollup will not resolve it,\n // so we need to resolve it ourselves with default resolver\n return syncResolve(what, importer, stack);\n }\n\n // Vite adds param like `?v=667939b3` to cached modules\n const resolvedId = resolved.id.split('?')[0];\n\n if (resolvedId.startsWith('\\0')) {\n // \\0 is a special character in Rollup that tells Rollup to not include this in the bundle\n // https://rollupjs.org/guide/en/#outputexports\n return null;\n }\n\n return resolvedId;\n }\n\n throw new Error(`Could not resolve ${what}`);\n },\n (what, importer) => [what, importer]\n );\n\n const plugin: Plugin = {\n name: 'wyw-in-js',\n closeBundle() {\n disposeEvalBroker(cache);\n },\n load(id: string) {\n return cssLookup[id];\n },\n /* eslint-disable-next-line consistent-return */\n resolveId(importee: string) {\n if (importee in cssLookup) return importee;\n },\n async transform(\n code: string,\n id: string\n ): Promise<{ code: string; map: Result['sourceMap'] } | undefined> {\n return runSerialized(async () => {\n // Do not transform ignored and generated files\n if (!filter(id) || id in cssLookup) return;\n\n const log = logger.extend('rollup').extend(getFileIdx(id));\n\n log('init %s', id);\n\n const transformServices = {\n options: {\n filename: id,\n pluginOptions: rest,\n prefixer,\n keepComments,\n preprocessor,\n root: process.cwd(),\n },\n cache,\n emitWarning: (message: string) => this.warn(message),\n };\n\n const result = await transform(\n transformServices,\n code,\n createAsyncResolver(getBoundResolve(this)),\n emptyConfig\n );\n\n if (!result.cssText) return;\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const filename = `${id.replace(/\\.[jt]sx?$/, '')}_${slug}.css`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n }\n\n cssLookup[filename] = cssText;\n\n result.code += `\\nimport ${JSON.stringify(filename)};\\n`;\n\n /* eslint-disable-next-line consistent-return */\n return { code: result.code, map: result.sourceMap };\n });\n },\n };\n\n return new Proxy<Plugin>(plugin, {\n get(target, prop) {\n return target[prop as keyof Plugin];\n },\n\n getOwnPropertyDescriptor(target, prop) {\n return Object.getOwnPropertyDescriptor(target, prop as keyof Plugin);\n },\n });\n}\n"],"file":"index.mjs"}
1
+ {"mappings":";;;;;AAMA,SAAS,oBAAoB;AAG7B,SACE,sBACA,QACA,SACA,mBACK;AAEP,SACE,mBACA,YACA,WACA,gCACK;AAoBP,eAAe,SAAS,QAAQ,EAC9B,aACA,SACA,SACA,cACA,UACA,cACA,qBAAqB,MACrB,WACA,GAAG,SACoB,EAAE,EAAU;CACnC,MAAM,SAAS,aAAa,SAAS,QAAQ;CAC7C,MAAM,YAAuC,EAAE;CAC/C,MAAM,QAAQ,IAAI,0BAA0B;CAC5C,MAAM,cAAc,EAAE;CACtB,MAAM,sBAAsB,IAAI,KAAqB;CACrD,IAAI,iBAAiB,QAAQ,SAAS;CAItC,MAAM,oBAAoB,IAAI,SAG3B;CAEH,MAAM,mBAAmB,QAAkC;EACzD,MAAM,SAAS,kBAAkB,IAAI,IAAI;AACzC,MAAI,UAAU,OAAO,kBAAkB,IAAI,SAAS;AAClD,UAAO,OAAO;;EAGhB,MAAM,eAA0B,IAAI,QAAQ,KAAK,IAAI;AACrD,oBAAkB,IAAI,KAAK;GAAE,eAAe,IAAI;GAAS;GAAc,CAAC;AACxE,SAAO;;CAGT,MAAM,eAAe,OAAe,GAAG,MAAM,IAA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QAAQ;EAC/B,IAAI,QAAQ,MAAM;AAChB,UAAO,OAAO;;EAGhB,yBAAyB,QAAQ,MAAM;AACrC,UAAO,OAAO,yBAAyB,QAAQ,KAAqB;;EAEvE,CAAC","names":[],"sources":["../src/index.ts"],"version":3,"sourcesContent":["/**\n * This file contains a Rollup 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 { createFilter } from '@rollup/pluginutils';\nimport type { Plugin, PluginContext, ResolvedId } from 'rollup';\n\nimport {\n asyncResolverFactory,\n logger,\n slugify,\n syncResolve,\n} from '@wyw-in-js/shared';\nimport type { PluginOptions, Preprocessor, Result } from '@wyw-in-js/transform';\nimport {\n disposeEvalBroker,\n getFileIdx,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\ntype RollupCssFilenameContext = {\n cssText: string;\n defaultFilename: string;\n id: string;\n slug: string;\n};\n\ntype RollupPluginOptions = {\n cssFilename?: (context: RollupCssFilenameContext) => string;\n exclude?: string | string[];\n include?: string | string[];\n keepComments?: boolean | RegExp;\n prefixer?: boolean;\n preprocessor?: Preprocessor;\n serializeTransform?: boolean;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nexport default function wywInJS({\n cssFilename,\n exclude,\n include,\n keepComments,\n prefixer,\n preprocessor,\n serializeTransform = true,\n sourceMap,\n ...rest\n}: RollupPluginOptions = {}): Plugin {\n const filter = createFilter(include, exclude);\n const cssLookup: { [key: string]: string } = {};\n const cache = new TransformCacheCollection();\n const emptyConfig = {};\n const dependencyLoadDepth = new Map<string, number>();\n let transformQueue = Promise.resolve();\n\n type ResolveFn = PluginContext['resolve'];\n\n const boundResolveCache = new WeakMap<\n PluginContext,\n { boundResolve: ResolveFn; sourceResolve: ResolveFn }\n >();\n\n const getBoundResolve = (ctx: PluginContext): ResolveFn => {\n const cached = boundResolveCache.get(ctx);\n if (cached && cached.sourceResolve === ctx.resolve) {\n return cached.boundResolve;\n }\n\n const boundResolve: ResolveFn = ctx.resolve.bind(ctx);\n boundResolveCache.set(ctx, { sourceResolve: ctx.resolve, boundResolve });\n return boundResolve;\n };\n\n const normalizeId = (id: string) => id.split('?')[0].split('#')[0];\n\n const beginDependencyLoad = (id: string): void => {\n const normalized = normalizeId(id);\n dependencyLoadDepth.set(\n normalized,\n (dependencyLoadDepth.get(normalized) ?? 0) + 1\n );\n };\n\n const endDependencyLoad = (id: string): void => {\n const normalized = normalizeId(id);\n const depth = dependencyLoadDepth.get(normalized) ?? 0;\n if (depth <= 1) {\n dependencyLoadDepth.delete(normalized);\n return;\n }\n\n dependencyLoadDepth.set(normalized, depth - 1);\n };\n\n const isDependencyLoad = (id: string): boolean =>\n dependencyLoadDepth.has(normalizeId(id));\n\n const runSerialized = async <T>(fn: () => Promise<T>): Promise<T> => {\n if (!serializeTransform) {\n return fn();\n }\n\n let release: () => void;\n const previous = transformQueue;\n transformQueue = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n\n try {\n return await fn();\n } finally {\n release!();\n }\n };\n\n const createAsyncResolver = asyncResolverFactory(\n async (resolved: ResolvedId | null, what, importer, stack) => {\n if (resolved) {\n if (resolved.external) {\n // If module is marked as external, Rollup will not resolve it,\n // so we need to resolve it ourselves with default resolver\n return syncResolve(what, importer, stack);\n }\n\n // Vite adds param like `?v=667939b3` to cached modules\n const resolvedId = resolved.id.split('?')[0];\n\n if (resolvedId.startsWith('\\0')) {\n // \\0 is a special character in Rollup that tells Rollup to not include this in the bundle\n // https://rollupjs.org/guide/en/#outputexports\n return null;\n }\n\n return resolvedId;\n }\n\n throw new Error(`Could not resolve ${what}`);\n },\n (what, importer) => [what, importer]\n );\n\n const plugin: Plugin = {\n name: 'wyw-in-js',\n closeBundle() {\n disposeEvalBroker(cache);\n },\n load(id: string) {\n return cssLookup[id];\n },\n /* eslint-disable-next-line consistent-return */\n resolveId(importee: string) {\n if (importee in cssLookup) return importee;\n },\n async transform(\n code: string,\n id: string\n ): Promise<{ code: string; map: Result['sourceMap'] } | undefined> {\n const run = async () => {\n // Do not transform ignored and generated files\n if (!filter(id) || id in cssLookup) return;\n\n const log = logger.extend('rollup').extend(getFileIdx(id));\n\n log('init %s', id);\n\n const transformServices = {\n options: {\n filename: id,\n pluginOptions: rest,\n prefixer,\n keepComments,\n preprocessor,\n root: process.cwd(),\n },\n cache,\n emitWarning: (message: string) => this.warn(message),\n loadDependencyCode: async (resolved: string) => {\n beginDependencyLoad(resolved);\n try {\n const loaded = await this.load({ id: resolved });\n const cached = cache.get('entrypoints', resolved);\n if (\n cached &&\n 'initialCode' in cached &&\n typeof cached.initialCode === 'string'\n ) {\n return undefined;\n }\n\n return typeof loaded?.code === 'string' ? loaded.code : undefined;\n } finally {\n endDependencyLoad(resolved);\n }\n },\n };\n\n const result = await transform(\n transformServices,\n code,\n createAsyncResolver(getBoundResolve(this)),\n emptyConfig\n );\n\n if (!result.cssText) return;\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const defaultFilename = `${id.replace(/\\.[jt]sx?$/, '')}_${slug}.css`;\n const filename =\n cssFilename?.({ cssText, defaultFilename, id, slug }) ??\n defaultFilename;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n }\n\n cssLookup[filename] = cssText;\n\n result.code += `\\nimport ${JSON.stringify(filename)};\\n`;\n\n /* eslint-disable-next-line consistent-return */\n return { code: result.code, map: result.sourceMap };\n };\n\n if (isDependencyLoad(id)) {\n return run();\n }\n\n return runSerialized(run);\n },\n };\n\n return new Proxy<Plugin>(plugin, {\n get(target, prop) {\n return target[prop as keyof Plugin];\n },\n\n getOwnPropertyDescriptor(target, prop) {\n return Object.getOwnPropertyDescriptor(target, prop as keyof Plugin);\n },\n });\n}\n"],"file":"index.mjs"}
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@wyw-in-js/rollup",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@rollup/pluginutils": "^5.0.5",
7
- "@wyw-in-js/shared": "2.0.0",
8
- "@wyw-in-js/transform": "2.0.1"
7
+ "@wyw-in-js/shared": "2.1.0",
8
+ "@wyw-in-js/transform": "2.1.0"
9
9
  },
10
10
  "devDependencies": {
11
11
  "@types/node": "^22.0.0",
package/types/index.d.ts CHANGED
@@ -5,7 +5,14 @@
5
5
  */
6
6
  import type { Plugin } from 'rollup';
7
7
  import type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';
8
+ type RollupCssFilenameContext = {
9
+ cssText: string;
10
+ defaultFilename: string;
11
+ id: string;
12
+ slug: string;
13
+ };
8
14
  type RollupPluginOptions = {
15
+ cssFilename?: (context: RollupCssFilenameContext) => string;
9
16
  exclude?: string | string[];
10
17
  include?: string | string[];
11
18
  keepComments?: boolean | RegExp;
@@ -14,5 +21,5 @@ type RollupPluginOptions = {
14
21
  serializeTransform?: boolean;
15
22
  sourceMap?: boolean;
16
23
  } & Partial<PluginOptions>;
17
- export default function wywInJS({ exclude, include, keepComments, prefixer, preprocessor, serializeTransform, sourceMap, ...rest }?: RollupPluginOptions): Plugin;
24
+ export default function wywInJS({ cssFilename, exclude, include, keepComments, prefixer, preprocessor, serializeTransform, sourceMap, ...rest }?: RollupPluginOptions): Plugin;
18
25
  export {};
package/types/index.js CHANGED
@@ -6,11 +6,12 @@
6
6
  import { createFilter } from '@rollup/pluginutils';
7
7
  import { asyncResolverFactory, logger, slugify, syncResolve, } from '@wyw-in-js/shared';
8
8
  import { disposeEvalBroker, getFileIdx, transform, TransformCacheCollection, } from '@wyw-in-js/transform';
9
- export default function wywInJS({ exclude, include, keepComments, prefixer, preprocessor, serializeTransform = true, sourceMap, ...rest } = {}) {
9
+ export default function wywInJS({ cssFilename, exclude, include, keepComments, prefixer, preprocessor, serializeTransform = true, sourceMap, ...rest } = {}) {
10
10
  const filter = createFilter(include, exclude);
11
11
  const cssLookup = {};
12
12
  const cache = new TransformCacheCollection();
13
13
  const emptyConfig = {};
14
+ const dependencyLoadDepth = new Map();
14
15
  let transformQueue = Promise.resolve();
15
16
  const boundResolveCache = new WeakMap();
16
17
  const getBoundResolve = (ctx) => {
@@ -22,6 +23,21 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
22
23
  boundResolveCache.set(ctx, { sourceResolve: ctx.resolve, boundResolve });
23
24
  return boundResolve;
24
25
  };
26
+ const normalizeId = (id) => id.split('?')[0].split('#')[0];
27
+ const beginDependencyLoad = (id) => {
28
+ const normalized = normalizeId(id);
29
+ dependencyLoadDepth.set(normalized, (dependencyLoadDepth.get(normalized) ?? 0) + 1);
30
+ };
31
+ const endDependencyLoad = (id) => {
32
+ const normalized = normalizeId(id);
33
+ const depth = dependencyLoadDepth.get(normalized) ?? 0;
34
+ if (depth <= 1) {
35
+ dependencyLoadDepth.delete(normalized);
36
+ return;
37
+ }
38
+ dependencyLoadDepth.set(normalized, depth - 1);
39
+ };
40
+ const isDependencyLoad = (id) => dependencyLoadDepth.has(normalizeId(id));
25
41
  const runSerialized = async (fn) => {
26
42
  if (!serializeTransform) {
27
43
  return fn();
@@ -71,7 +87,7 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
71
87
  return importee;
72
88
  },
73
89
  async transform(code, id) {
74
- return runSerialized(async () => {
90
+ const run = async () => {
75
91
  // Do not transform ignored and generated files
76
92
  if (!filter(id) || id in cssLookup)
77
93
  return;
@@ -88,13 +104,31 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
88
104
  },
89
105
  cache,
90
106
  emitWarning: (message) => this.warn(message),
107
+ loadDependencyCode: async (resolved) => {
108
+ beginDependencyLoad(resolved);
109
+ try {
110
+ const loaded = await this.load({ id: resolved });
111
+ const cached = cache.get('entrypoints', resolved);
112
+ if (cached &&
113
+ 'initialCode' in cached &&
114
+ typeof cached.initialCode === 'string') {
115
+ return undefined;
116
+ }
117
+ return typeof loaded?.code === 'string' ? loaded.code : undefined;
118
+ }
119
+ finally {
120
+ endDependencyLoad(resolved);
121
+ }
122
+ },
91
123
  };
92
124
  const result = await transform(transformServices, code, createAsyncResolver(getBoundResolve(this)), emptyConfig);
93
125
  if (!result.cssText)
94
126
  return;
95
127
  let { cssText } = result;
96
128
  const slug = slugify(cssText);
97
- const filename = `${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`;
129
+ const defaultFilename = `${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`;
130
+ const filename = cssFilename?.({ cssText, defaultFilename, id, slug }) ??
131
+ defaultFilename;
98
132
  if (sourceMap && result.cssSourceMapText) {
99
133
  const map = Buffer.from(result.cssSourceMapText).toString('base64');
100
134
  cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
@@ -103,7 +137,11 @@ export default function wywInJS({ exclude, include, keepComments, prefixer, prep
103
137
  result.code += `\nimport ${JSON.stringify(filename)};\n`;
104
138
  /* eslint-disable-next-line consistent-return */
105
139
  return { code: result.code, map: result.sourceMap };
106
- });
140
+ };
141
+ if (isDependencyLoad(id)) {
142
+ return run();
143
+ }
144
+ return runSerialized(run);
107
145
  },
108
146
  };
109
147
  return new Proxy(plugin, {