@wyw-in-js/webpack-loader 1.0.8 → 2.0.0-alpha.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/esm/WYWinJSDebugPlugin.js +13 -15
- package/esm/WYWinJSDebugPlugin.js.map +1 -1
- package/esm/cache.js +50 -51
- package/esm/cache.js.map +1 -1
- package/esm/index.js +222 -174
- package/esm/index.js.map +1 -1
- package/esm/outputCssLoader.js +15 -18
- package/esm/outputCssLoader.js.map +1 -1
- package/package.json +14 -10
- package/types/WYWinJSDebugPlugin.js +5 -9
- package/types/cache.js +7 -10
- package/types/index.js +126 -63
- package/types/outputCssLoader.js +3 -6
- package/lib/WYWinJSDebugPlugin.js +0 -25
- package/lib/WYWinJSDebugPlugin.js.map +0 -1
- package/lib/cache.js +0 -71
- package/lib/cache.js.map +0 -1
- package/lib/index.js +0 -198
- package/lib/index.js.map +0 -1
- package/lib/outputCssLoader.js +0 -27
- package/lib/outputCssLoader.js.map +0 -1
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import { createFileReporter } from
|
|
1
|
+
import { createFileReporter } from "@wyw-in-js/transform";
|
|
2
2
|
export const sharedState = {};
|
|
3
3
|
export class WYWinJSDebugPlugin {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
16
|
-
}
|
|
4
|
+
onDone;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const { emitter, onDone } = createFileReporter(options ?? false);
|
|
7
|
+
sharedState.emitter = emitter;
|
|
8
|
+
this.onDone = onDone;
|
|
9
|
+
}
|
|
10
|
+
apply(compiler) {
|
|
11
|
+
compiler.hooks.shutdown.tap("WYWinJSDebug", () => {
|
|
12
|
+
this.onDone(process.cwd());
|
|
13
|
+
});
|
|
14
|
+
}
|
|
17
15
|
}
|
|
18
|
-
//# sourceMappingURL=WYWinJSDebugPlugin.js.map
|
|
16
|
+
//# sourceMappingURL=WYWinJSDebugPlugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAGA,SAAS,0BAA0B;AAEnC,OAAO,MAAM,cAET,EAAE;AAEN,OAAO,MAAM,mBAAmB;CAC9B,AAAiB;CAEjB,YAAY,SAAgC;EAC1C,MAAM,EAAE,SAAS,WAAW,mBAAmB,WAAW,MAAM;AAChE,cAAY,UAAU;AACtB,OAAK,SAAS;;CAGhB,MAAM,UAAoB;AACxB,WAAS,MAAM,SAAS,IAAI,sBAAsB;AAChD,QAAK,OAAO,QAAQ,KAAK,CAAC;IAC1B","names":[],"sources":["../src/WYWinJSDebugPlugin.ts"],"version":3,"sourcesContent":["import type { Compiler } from 'webpack';\n\nimport type { EventEmitter, IFileReporterOptions } from '@wyw-in-js/transform';\nimport { createFileReporter } from '@wyw-in-js/transform';\n\nexport const sharedState: {\n emitter?: EventEmitter;\n} = {};\n\nexport class WYWinJSDebugPlugin {\n private readonly onDone: (root: string) => void;\n\n constructor(options?: IFileReporterOptions) {\n const { emitter, onDone } = createFileReporter(options ?? false);\n sharedState.emitter = emitter;\n this.onDone = onDone;\n }\n\n apply(compiler: Compiler) {\n compiler.hooks.shutdown.tap('WYWinJSDebug', () => {\n this.onDone(process.cwd());\n });\n }\n}\n"],"file":"WYWinJSDebugPlugin.js"}
|
package/esm/cache.js
CHANGED
|
@@ -1,62 +1,61 @@
|
|
|
1
|
+
import { createRequire } from "module";
|
|
2
|
+
const nodeRequire = createRequire(import.meta.url);
|
|
1
3
|
let cacheProviderSeq = 0;
|
|
2
4
|
const cacheProviderIds = new WeakMap();
|
|
3
5
|
const cacheProvidersById = new Map();
|
|
4
|
-
export const registerCacheProvider = cacheProvider => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
export const registerCacheProvider = (cacheProvider) => {
|
|
7
|
+
const knownId = cacheProviderIds.get(cacheProvider);
|
|
8
|
+
if (knownId) {
|
|
9
|
+
return knownId;
|
|
10
|
+
}
|
|
11
|
+
cacheProviderSeq += 1;
|
|
12
|
+
const id = `${cacheProviderSeq}`;
|
|
13
|
+
cacheProviderIds.set(cacheProvider, id);
|
|
14
|
+
cacheProvidersById.set(id, cacheProvider);
|
|
15
|
+
return id;
|
|
14
16
|
};
|
|
15
|
-
|
|
16
17
|
// memory cache, which is the default cache implementation in WYW-in-JS
|
|
17
|
-
|
|
18
18
|
class MemoryCache {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
cache = new Map();
|
|
20
|
+
dependenciesCache = new Map();
|
|
21
|
+
get(key) {
|
|
22
|
+
return Promise.resolve(this.cache.get(key) ?? "");
|
|
23
|
+
}
|
|
24
|
+
getDependencies(key) {
|
|
25
|
+
return Promise.resolve(this.dependenciesCache.get(key) ?? []);
|
|
26
|
+
}
|
|
27
|
+
set(key, value) {
|
|
28
|
+
this.cache.set(key, value);
|
|
29
|
+
return Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
setDependencies(key, value) {
|
|
32
|
+
this.dependenciesCache.set(key, value);
|
|
33
|
+
return Promise.resolve();
|
|
34
|
+
}
|
|
35
35
|
}
|
|
36
36
|
export const memoryCache = new MemoryCache();
|
|
37
|
-
|
|
38
37
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
* return cache instance from `options.cacheProvider`
|
|
39
|
+
* @param cacheProvider string | ICache | undefined
|
|
40
|
+
* @returns ICache instance
|
|
41
|
+
*/
|
|
43
42
|
export const getCacheInstance = async (cacheProvider, cacheProviderId) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
43
|
+
if (cacheProviderId) {
|
|
44
|
+
const cacheProviderInstance = cacheProvidersById.get(cacheProviderId);
|
|
45
|
+
if (!cacheProviderInstance) {
|
|
46
|
+
throw new Error(`Invalid cache provider id: ${cacheProviderId}`);
|
|
47
|
+
}
|
|
48
|
+
return cacheProviderInstance;
|
|
49
|
+
}
|
|
50
|
+
if (!cacheProvider) {
|
|
51
|
+
return memoryCache;
|
|
52
|
+
}
|
|
53
|
+
if (typeof cacheProvider === "string") {
|
|
54
|
+
return nodeRequire(cacheProvider);
|
|
55
|
+
}
|
|
56
|
+
if (typeof cacheProvider === "object" && "get" in cacheProvider && "set" in cacheProvider) {
|
|
57
|
+
return cacheProvider;
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`Invalid cache provider: ${cacheProvider}`);
|
|
61
60
|
};
|
|
62
|
-
//# sourceMappingURL=cache.js.map
|
|
61
|
+
//# sourceMappingURL=cache.js.map
|
package/esm/cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAAS,qBAAqB;AAE9B,MAAM,cAAc,cAAc,OAAO,KAAK,IAAI;AASlD,IAAI,mBAAmB;AACvB,MAAM,mBAAmB,IAAI,SAAyB;AACtD,MAAM,qBAAqB,IAAI,KAAqB;AAEpD,OAAO,MAAM,yBAAyB,kBAAkC;CACtE,MAAM,UAAU,iBAAiB,IAAI,cAAc;AACnD,KAAI,SAAS;AACX,SAAO;;AAGT,qBAAoB;CACpB,MAAM,KAAK,GAAG;AACd,kBAAiB,IAAI,eAAe,GAAG;AACvC,oBAAmB,IAAI,IAAI,cAAc;AACzC,QAAO;;;AAKT,MAAM,YAA8B;CAClC,AAAQ,QAA6B,IAAI,KAAK;CAE9C,AAAQ,oBAA2C,IAAI,KAAK;CAE5D,AAAO,IAAI,KAA8B;AACvC,SAAO,QAAQ,QAAQ,KAAK,MAAM,IAAI,IAAI,IAAI,GAAG;;CAGnD,AAAO,gBAAgB,KAAgC;AACrD,SAAO,QAAQ,QAAQ,KAAK,kBAAkB,IAAI,IAAI,IAAI,EAAE,CAAC;;CAG/D,AAAO,IAAI,KAAa,OAA8B;AACpD,OAAK,MAAM,IAAI,KAAK,MAAM;AAC1B,SAAO,QAAQ,SAAS;;CAG1B,AAAO,gBAAgB,KAAa,OAAgC;AAClE,OAAK,kBAAkB,IAAI,KAAK,MAAM;AACtC,SAAO,QAAQ,SAAS;;;AAI5B,OAAO,MAAM,cAAc,IAAI,aAAa;;;;;;AAO5C,OAAO,MAAM,mBAAmB,OAC9B,eACA,oBACoB;AACpB,KAAI,iBAAiB;EACnB,MAAM,wBAAwB,mBAAmB,IAAI,gBAAgB;AACrE,MAAI,CAAC,uBAAuB;AAC1B,SAAM,IAAI,MAAM,8BAA8B,kBAAkB;;AAGlE,SAAO;;AAGT,KAAI,CAAC,eAAe;AAClB,SAAO;;AAET,KAAI,OAAO,kBAAkB,UAAU;AACrC,SAAO,YAAY,cAAc;;AAEnC,KACE,OAAO,kBAAkB,YACzB,SAAS,iBACT,SAAS,eACT;AACA,SAAO;;AAET,OAAM,IAAI,MAAM,2BAA2B,gBAAgB","names":[],"sources":["../src/cache.ts"],"version":3,"sourcesContent":["import { createRequire } from 'module';\n\nconst nodeRequire = createRequire(import.meta.url);\n\nexport interface ICache {\n get: (key: string) => Promise<string>;\n getDependencies?: (key: string) => Promise<string[]>;\n set: (key: string, value: string) => Promise<void>;\n setDependencies?: (key: string, value: string[]) => Promise<void>;\n}\n\nlet cacheProviderSeq = 0;\nconst cacheProviderIds = new WeakMap<ICache, string>();\nconst cacheProvidersById = new Map<string, ICache>();\n\nexport const registerCacheProvider = (cacheProvider: ICache): string => {\n const knownId = cacheProviderIds.get(cacheProvider);\n if (knownId) {\n return knownId;\n }\n\n cacheProviderSeq += 1;\n const id = `${cacheProviderSeq}`;\n cacheProviderIds.set(cacheProvider, id);\n cacheProvidersById.set(id, cacheProvider);\n return id;\n};\n\n// memory cache, which is the default cache implementation in WYW-in-JS\n\nclass MemoryCache implements ICache {\n private cache: Map<string, string> = new Map();\n\n private dependenciesCache: Map<string, string[]> = new Map();\n\n public get(key: string): Promise<string> {\n return Promise.resolve(this.cache.get(key) ?? '');\n }\n\n public getDependencies(key: string): Promise<string[]> {\n return Promise.resolve(this.dependenciesCache.get(key) ?? []);\n }\n\n public set(key: string, value: string): Promise<void> {\n this.cache.set(key, value);\n return Promise.resolve();\n }\n\n public setDependencies(key: string, value: string[]): Promise<void> {\n this.dependenciesCache.set(key, value);\n return Promise.resolve();\n }\n}\n\nexport const memoryCache = new MemoryCache();\n\n/**\n * return cache instance from `options.cacheProvider`\n * @param cacheProvider string | ICache | undefined\n * @returns ICache instance\n */\nexport const getCacheInstance = async (\n cacheProvider: string | ICache | undefined,\n cacheProviderId?: string | undefined\n): Promise<ICache> => {\n if (cacheProviderId) {\n const cacheProviderInstance = cacheProvidersById.get(cacheProviderId);\n if (!cacheProviderInstance) {\n throw new Error(`Invalid cache provider id: ${cacheProviderId}`);\n }\n\n return cacheProviderInstance;\n }\n\n if (!cacheProvider) {\n return memoryCache;\n }\n if (typeof cacheProvider === 'string') {\n return nodeRequire(cacheProvider);\n }\n if (\n typeof cacheProvider === 'object' &&\n 'get' in cacheProvider &&\n 'set' in cacheProvider\n ) {\n return cacheProvider;\n }\n throw new Error(`Invalid cache provider: ${cacheProvider}`);\n};\n"],"file":"cache.js"}
|
package/esm/index.js
CHANGED
|
@@ -1,183 +1,231 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import { logger } from
|
|
10
|
-
import { transform, TransformCacheCollection } from
|
|
11
|
-
import { sharedState } from
|
|
12
|
-
import { getCacheInstance, registerCacheProvider } from
|
|
13
|
-
export { WYWinJSDebugPlugin } from
|
|
14
|
-
const outputCssLoader =
|
|
15
|
-
const stripQueryAndHash = request => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
* This file contains a Webpack 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 path from "path";
|
|
7
|
+
import crypto from "crypto";
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
import { logger } from "@wyw-in-js/shared";
|
|
10
|
+
import { disposeEvalBroker, transform, TransformCacheCollection } from "@wyw-in-js/transform";
|
|
11
|
+
import { sharedState } from "./WYWinJSDebugPlugin.js";
|
|
12
|
+
import { getCacheInstance, registerCacheProvider } from "./cache.js";
|
|
13
|
+
export { WYWinJSDebugPlugin } from "./WYWinJSDebugPlugin.js";
|
|
14
|
+
const outputCssLoader = fileURLToPath(new URL("./outputCssLoader.js", import.meta.url));
|
|
15
|
+
const stripQueryAndHash = (request) => {
|
|
16
|
+
const queryIdx = request.indexOf("?");
|
|
17
|
+
const hashIdx = request.indexOf("#");
|
|
18
|
+
if (queryIdx === -1) {
|
|
19
|
+
return hashIdx === -1 ? request : request.slice(0, hashIdx);
|
|
20
|
+
}
|
|
21
|
+
if (hashIdx === -1) return request.slice(0, queryIdx);
|
|
22
|
+
return request.slice(0, Math.min(queryIdx, hashIdx));
|
|
23
23
|
};
|
|
24
|
-
const hashText = text => crypto.createHash(
|
|
25
|
-
const
|
|
26
|
-
|
|
24
|
+
const hashText = (text) => crypto.createHash("sha256").update(text).digest("hex").slice(0, 12);
|
|
25
|
+
const COMPILER_SCOPE_NAME = "WYWinJSResolverScope";
|
|
26
|
+
let compilerScopeId = 0;
|
|
27
|
+
const compilerStates = new WeakMap();
|
|
27
28
|
const getResolverKey = (importer, stack) => {
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
const root = stack.length ? stack[stack.length - 1] : importer;
|
|
30
|
+
return stripQueryAndHash(root);
|
|
30
31
|
};
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const createResolverScope = () => {
|
|
33
|
+
const resolvers = new Map();
|
|
34
|
+
compilerScopeId += 1;
|
|
35
|
+
const key = `webpack:${compilerScopeId}`;
|
|
36
|
+
return {
|
|
37
|
+
asyncResolve: (what, importer, stack = [importer]) => {
|
|
38
|
+
const resolverKeys = [getResolverKey(importer, stack), stripQueryAndHash(importer)].filter((candidate, idx, all) => all.indexOf(candidate) === idx);
|
|
39
|
+
const selectedResolvers = resolverKeys.map((resolverKey) => resolvers.get(resolverKey)).filter((resolver) => Boolean(resolver));
|
|
40
|
+
if (selectedResolvers.length === 0) {
|
|
41
|
+
throw new Error("No resolver found");
|
|
42
|
+
}
|
|
43
|
+
// Root and importer resolver side effects both matter for dependency
|
|
44
|
+
// tracking, so keep them aligned and verify they agree on the answer.
|
|
45
|
+
return Promise.all(selectedResolvers.map((resolver) => resolver(what, importer, stack))).then((results) => {
|
|
46
|
+
const firstResult = results[0];
|
|
47
|
+
if (results.some((result) => result !== firstResult)) {
|
|
48
|
+
throw new Error("Resolvers returned different results");
|
|
49
|
+
}
|
|
50
|
+
return firstResult;
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
cache: new TransformCacheCollection(),
|
|
54
|
+
dispose: () => {
|
|
55
|
+
resolvers.clear();
|
|
56
|
+
},
|
|
57
|
+
key,
|
|
58
|
+
replaceResolver: (resourcePath, resolver) => {
|
|
59
|
+
resolvers.set(stripQueryAndHash(resourcePath), resolver);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
34
62
|
};
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
63
|
+
const disposeCompilerState = (state) => {
|
|
64
|
+
state.clearResolvers();
|
|
65
|
+
disposeEvalBroker(state.cache);
|
|
66
|
+
};
|
|
67
|
+
const getCompilerState = (compiler) => {
|
|
68
|
+
const cached = compilerStates.get(compiler);
|
|
69
|
+
if (cached) {
|
|
70
|
+
return cached;
|
|
71
|
+
}
|
|
72
|
+
// Resolver identity must stay stable across files within one compiler or we
|
|
73
|
+
// churn both the shared transform cache salt and the eval broker/runner.
|
|
74
|
+
const scope = createResolverScope();
|
|
75
|
+
const state = {
|
|
76
|
+
...scope,
|
|
77
|
+
clearResolvers: scope.dispose,
|
|
78
|
+
dispose: () => disposeCompilerState(state),
|
|
79
|
+
hooksInstalled: false
|
|
80
|
+
};
|
|
81
|
+
const installHooks = () => {
|
|
82
|
+
if (state.hooksInstalled) return;
|
|
83
|
+
state.hooksInstalled = true;
|
|
84
|
+
compiler.hooks.done?.tap(COMPILER_SCOPE_NAME, () => {
|
|
85
|
+
state.clearResolvers();
|
|
86
|
+
});
|
|
87
|
+
compiler.hooks.failed?.tap(COMPILER_SCOPE_NAME, () => {
|
|
88
|
+
state.clearResolvers();
|
|
89
|
+
});
|
|
90
|
+
compiler.hooks.watchClose?.tap(COMPILER_SCOPE_NAME, () => {
|
|
91
|
+
state.dispose();
|
|
92
|
+
compilerStates.delete(compiler);
|
|
93
|
+
});
|
|
94
|
+
compiler.hooks.shutdown?.tap(COMPILER_SCOPE_NAME, () => {
|
|
95
|
+
state.dispose();
|
|
96
|
+
compilerStates.delete(compiler);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
installHooks();
|
|
100
|
+
compilerStates.set(compiler, state);
|
|
101
|
+
return state;
|
|
102
|
+
};
|
|
103
|
+
const createInvocationScope = () => {
|
|
104
|
+
const scope = createResolverScope();
|
|
105
|
+
return {
|
|
106
|
+
...scope,
|
|
107
|
+
dispose: () => {
|
|
108
|
+
scope.dispose();
|
|
109
|
+
disposeEvalBroker(scope.cache);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
55
112
|
};
|
|
56
|
-
function addResolver(resourcePath, resolver) {
|
|
57
|
-
if (!resolvers[resourcePath]) {
|
|
58
|
-
resolvers[resourcePath] = [];
|
|
59
|
-
}
|
|
60
|
-
resolvers[resourcePath].push(resolver);
|
|
61
|
-
return () => {
|
|
62
|
-
resolvers[resourcePath] = resolvers[resourcePath].filter(r => r !== resolver);
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
113
|
const webpack5Loader = function webpack5LoaderPlugin(content, inputSourceMap) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
114
|
+
function convertSourceMap(value, filename) {
|
|
115
|
+
if (typeof value === "string" || !value) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
...value,
|
|
120
|
+
file: value.file ?? filename,
|
|
121
|
+
mappings: value.mappings ?? "",
|
|
122
|
+
names: value.names ?? [],
|
|
123
|
+
sources: value.sources ?? [],
|
|
124
|
+
version: value.version ?? 3
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
// tell Webpack this loader is async
|
|
128
|
+
this.async();
|
|
129
|
+
const resolveOptions = { dependencyType: "esm" };
|
|
130
|
+
const resolveModule = this.getResolve(resolveOptions);
|
|
131
|
+
const isPromiseLike = (value) => typeof value?.then === "function";
|
|
132
|
+
const resolveModuleAsync = (context, request) => new Promise((resolve, reject) => {
|
|
133
|
+
let settled = false;
|
|
134
|
+
const finish = (err, result) => {
|
|
135
|
+
if (settled) return;
|
|
136
|
+
settled = true;
|
|
137
|
+
if (err) {
|
|
138
|
+
reject(err);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (typeof result === "string") {
|
|
142
|
+
resolve(result);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
reject(new Error(`Cannot resolve ${request}`));
|
|
146
|
+
};
|
|
147
|
+
try {
|
|
148
|
+
const maybePromise = resolveModule(context, request, finish);
|
|
149
|
+
if (isPromiseLike(maybePromise)) {
|
|
150
|
+
maybePromise.then((result) => finish(null, result), (err) => finish(err, null));
|
|
151
|
+
}
|
|
152
|
+
} catch (err) {
|
|
153
|
+
finish(err, null);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
const { _compiler: compiler } = this;
|
|
157
|
+
const compilerState = compiler ? getCompilerState(compiler) : createInvocationScope();
|
|
158
|
+
compilerState.replaceResolver(this.resourcePath, (what, importer) => {
|
|
159
|
+
const importerPath = stripQueryAndHash(importer);
|
|
160
|
+
const context = path.isAbsolute(importerPath) ? path.dirname(importerPath) : path.join(process.cwd(), path.dirname(importerPath));
|
|
161
|
+
return resolveModuleAsync(context, what).then((result) => {
|
|
162
|
+
const filePath = stripQueryAndHash(result);
|
|
163
|
+
if (path.isAbsolute(filePath)) {
|
|
164
|
+
this.addDependency(filePath);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
const { asyncResolve, cache: transformCache, key: asyncResolveKey } = compilerState;
|
|
170
|
+
logger("loader %s", this.resourcePath);
|
|
171
|
+
const { sourceMap = undefined, preprocessor = undefined, keepComments = undefined, prefixer = undefined, extension = ".wyw-in-js.css", cssImport = "require", cacheProvider, ...rest } = this.getOptions() || {};
|
|
172
|
+
const outputFileName = this.resourcePath.replace(/\.[^.]+$/, extension);
|
|
173
|
+
const transformServices = {
|
|
174
|
+
options: {
|
|
175
|
+
filename: this.resourcePath,
|
|
176
|
+
inputSourceMap: convertSourceMap(inputSourceMap, this.resourcePath),
|
|
177
|
+
pluginOptions: rest,
|
|
178
|
+
prefixer,
|
|
179
|
+
keepComments,
|
|
180
|
+
preprocessor,
|
|
181
|
+
root: process.cwd()
|
|
182
|
+
},
|
|
183
|
+
asyncResolveKey,
|
|
184
|
+
cache: transformCache,
|
|
185
|
+
emitWarning: (message) => {
|
|
186
|
+
const warning = new Error(message);
|
|
187
|
+
// Remove the stack so webpack's ModuleWarning doesn't copy it into
|
|
188
|
+
// `details`, which causes the message to render twice (once from
|
|
189
|
+
// .message, once from .details containing "Error: <same message>").
|
|
190
|
+
delete warning.stack;
|
|
191
|
+
this.emitWarning(warning);
|
|
192
|
+
},
|
|
193
|
+
eventEmitter: sharedState.emitter
|
|
194
|
+
};
|
|
195
|
+
transform(transformServices, content.toString(), asyncResolve).then(async (result) => {
|
|
196
|
+
try {
|
|
197
|
+
if (result.cssText) {
|
|
198
|
+
let { cssText } = result;
|
|
199
|
+
if (sourceMap) {
|
|
200
|
+
cssText += `/*# sourceMappingURL=data:application/json;base64,${Buffer.from(result.cssSourceMapText || "").toString("base64")}*/`;
|
|
201
|
+
}
|
|
202
|
+
await Promise.all(result.dependencies?.map((dep) => asyncResolve(dep, this.resourcePath)) ?? []);
|
|
203
|
+
const cacheInstance = await getCacheInstance(cacheProvider);
|
|
204
|
+
const cacheProviderId = cacheProvider && typeof cacheProvider === "object" ? registerCacheProvider(cacheInstance) : "";
|
|
205
|
+
await cacheInstance.set(this.resourcePath, cssText);
|
|
206
|
+
await cacheInstance.setDependencies?.(this.resourcePath, this.getDependencies());
|
|
207
|
+
const wywQuery = [`wyw=${encodeURIComponent(extension.replace(/^\./, ""))}`];
|
|
208
|
+
if (this.hot) {
|
|
209
|
+
wywQuery.push(`v=${encodeURIComponent(hashText(cssText))}`);
|
|
210
|
+
}
|
|
211
|
+
const resourcePathWithQuery = `${this.resourcePath}?${wywQuery.join("&")}`;
|
|
212
|
+
const request = `${outputFileName}!=!${outputCssLoader}?cacheProvider=${encodeURIComponent(typeof cacheProvider === "string" ? cacheProvider : "")}&cacheProviderId=${encodeURIComponent(cacheProviderId)}!${resourcePathWithQuery}`;
|
|
213
|
+
const stringifiedRequest = JSON.stringify(this.utils.contextify(this.context || this.rootContext, request));
|
|
214
|
+
const importCss = cssImport === "import" ? `import ${stringifiedRequest};` : `require(${stringifiedRequest});`;
|
|
215
|
+
this.callback(null, `${result.code}\n\n${importCss}`, result.sourceMap ?? undefined);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
this.callback(null, result.code, result.sourceMap ?? undefined);
|
|
219
|
+
} catch (err) {
|
|
220
|
+
this.callback(err);
|
|
221
|
+
}
|
|
222
|
+
}, (err) => {
|
|
223
|
+
this.callback(err);
|
|
224
|
+
}).catch((err) => this.callback(err)).finally(() => {
|
|
225
|
+
if (!compiler) {
|
|
226
|
+
compilerState.dispose();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
181
229
|
};
|
|
182
230
|
export default webpack5Loader;
|
|
183
|
-
//# sourceMappingURL=index.js.map
|
|
231
|
+
//# sourceMappingURL=index.js.map
|