@embroider/webpack 3.2.3-unstable.8c7b24d → 3.2.3-unstable.a90d03d
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/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@embroider/webpack",
|
3
|
-
"version": "3.2.3-unstable.
|
3
|
+
"version": "3.2.3-unstable.a90d03d",
|
4
4
|
"private": false,
|
5
5
|
"description": "Builds EmberJS apps with Webpack",
|
6
6
|
"repository": {
|
@@ -19,9 +19,9 @@
|
|
19
19
|
"scripts": {},
|
20
20
|
"dependencies": {
|
21
21
|
"@babel/core": "^7.14.5",
|
22
|
-
"@embroider/babel-loader-9": "3.1.2-unstable.
|
23
|
-
"@embroider/hbs-loader": "3.0.4-unstable.
|
24
|
-
"@embroider/shared-internals": "2.5.3-unstable.
|
22
|
+
"@embroider/babel-loader-9": "3.1.2-unstable.a90d03d",
|
23
|
+
"@embroider/hbs-loader": "3.0.4-unstable.a90d03d",
|
24
|
+
"@embroider/shared-internals": "2.5.3-unstable.a90d03d",
|
25
25
|
"@types/supports-color": "^8.1.0",
|
26
26
|
"assert-never": "^1.2.1",
|
27
27
|
"babel-loader": "^8.2.2",
|
@@ -42,7 +42,7 @@
|
|
42
42
|
"thread-loader": "^3.0.4"
|
43
43
|
},
|
44
44
|
"devDependencies": {
|
45
|
-
"@embroider/core": "3.4.6-unstable.
|
45
|
+
"@embroider/core": "3.4.6-unstable.a90d03d",
|
46
46
|
"@types/csso": "^3.5.1",
|
47
47
|
"@types/debug": "^4.1.5",
|
48
48
|
"@types/fs-extra": "^9.0.12",
|
@@ -54,7 +54,7 @@
|
|
54
54
|
"webpack": "^5.38.1"
|
55
55
|
},
|
56
56
|
"peerDependencies": {
|
57
|
-
"@embroider/core": "3.4.6-unstable.
|
57
|
+
"@embroider/core": "3.4.6-unstable.a90d03d",
|
58
58
|
"webpack": "^5.0.0"
|
59
59
|
},
|
60
60
|
"engines": {
|
@@ -37,20 +37,20 @@ class EmbroiderPlugin {
|
|
37
37
|
__classPrivateFieldGet(this, _EmbroiderPlugin_instances, "m", _EmbroiderPlugin_addLoaderAlias).call(this, compiler, virtualLoaderName, virtualLoaderPath);
|
38
38
|
compiler.hooks.normalModuleFactory.tap('@embroider/webpack', nmf => {
|
39
39
|
let defaultResolve = getDefaultResolveHook(nmf.hooks.resolve.taps);
|
40
|
-
let adaptedResolve = getAdaptedResolve(defaultResolve);
|
41
40
|
nmf.hooks.resolve.tapAsync({ name: '@embroider/webpack', stage: 50 }, (state, callback) => {
|
42
|
-
let request = WebpackModuleRequest.from(state, __classPrivateFieldGet(this, _EmbroiderPlugin_babelLoaderPrefix, "f"), __classPrivateFieldGet(this, _EmbroiderPlugin_appRoot, "f"));
|
41
|
+
let request = WebpackModuleRequest.from(defaultResolve, state, __classPrivateFieldGet(this, _EmbroiderPlugin_babelLoaderPrefix, "f"), __classPrivateFieldGet(this, _EmbroiderPlugin_appRoot, "f"));
|
43
42
|
if (!request) {
|
44
43
|
defaultResolve(state, callback);
|
45
44
|
return;
|
46
45
|
}
|
47
|
-
__classPrivateFieldGet(this, _EmbroiderPlugin_resolver, "f").resolve(request
|
46
|
+
__classPrivateFieldGet(this, _EmbroiderPlugin_resolver, "f").resolve(request).then(resolution => {
|
48
47
|
switch (resolution.type) {
|
49
48
|
case 'not_found':
|
50
49
|
callback(resolution.err);
|
51
50
|
break;
|
52
51
|
case 'found':
|
53
|
-
|
52
|
+
case 'ignored':
|
53
|
+
callback(null, undefined);
|
54
54
|
break;
|
55
55
|
default:
|
56
56
|
throw (0, assert_never_1.default)(resolution);
|
@@ -84,33 +84,8 @@ function getDefaultResolveHook(taps) {
|
|
84
84
|
let { fn } = taps.find(t => t.name === 'NormalModuleFactory');
|
85
85
|
return fn;
|
86
86
|
}
|
87
|
-
// This converts the raw function we got out of webpack into the right interface
|
88
|
-
// for use by @embroider/core's resolver.
|
89
|
-
function getAdaptedResolve(defaultResolve) {
|
90
|
-
return function (request) {
|
91
|
-
return new Promise(resolve => {
|
92
|
-
if (request.isNotFound) {
|
93
|
-
// TODO: we can make sure this looks correct in webpack output when a
|
94
|
-
// user encounters it
|
95
|
-
let err = new Error(`module not found ${request.specifier}`);
|
96
|
-
err.code = 'MODULE_NOT_FOUND';
|
97
|
-
resolve({ type: 'not_found', err });
|
98
|
-
}
|
99
|
-
defaultResolve(request.toWebpackResolveData(), (err, value) => {
|
100
|
-
if (err) {
|
101
|
-
// unfortunately webpack doesn't let us distinguish between Not Found
|
102
|
-
// and other unexpected exceptions here.
|
103
|
-
resolve({ type: 'not_found', err });
|
104
|
-
}
|
105
|
-
else {
|
106
|
-
resolve({ type: 'found', result: value });
|
107
|
-
}
|
108
|
-
});
|
109
|
-
});
|
110
|
-
};
|
111
|
-
}
|
112
87
|
class WebpackModuleRequest {
|
113
|
-
static from(state, babelLoaderPrefix, appRoot) {
|
88
|
+
static from(resolveFunction, state, babelLoaderPrefix, appRoot) {
|
114
89
|
var _a;
|
115
90
|
let specifier = state.request;
|
116
91
|
if (specifier.includes(virtualLoaderName) || // prevents recursion on requests we have already sent to our virtual loader
|
@@ -138,9 +113,10 @@ class WebpackModuleRequest {
|
|
138
113
|
if (!fromFile) {
|
139
114
|
return;
|
140
115
|
}
|
141
|
-
return new WebpackModuleRequest(babelLoaderPrefix, appRoot, specifier, fromFile, state.contextInfo._embroiderMeta, false, false, state);
|
116
|
+
return new WebpackModuleRequest(resolveFunction, babelLoaderPrefix, appRoot, specifier, fromFile, state.contextInfo._embroiderMeta, false, false, undefined, state);
|
142
117
|
}
|
143
|
-
constructor(babelLoaderPrefix, appRoot, specifier, fromFile, meta, isVirtual, isNotFound, originalState) {
|
118
|
+
constructor(resolveFunction, babelLoaderPrefix, appRoot, specifier, fromFile, meta, isVirtual, isNotFound, resolvedTo, originalState) {
|
119
|
+
this.resolveFunction = resolveFunction;
|
144
120
|
this.babelLoaderPrefix = babelLoaderPrefix;
|
145
121
|
this.appRoot = appRoot;
|
146
122
|
this.specifier = specifier;
|
@@ -148,6 +124,7 @@ class WebpackModuleRequest {
|
|
148
124
|
this.meta = meta;
|
149
125
|
this.isVirtual = isVirtual;
|
150
126
|
this.isNotFound = isNotFound;
|
127
|
+
this.resolvedTo = resolvedTo;
|
151
128
|
this.originalState = originalState;
|
152
129
|
}
|
153
130
|
get debugType() {
|
@@ -180,31 +157,63 @@ class WebpackModuleRequest {
|
|
180
157
|
this.originalState.context = (0, path_1.dirname)(this.fromFile);
|
181
158
|
this.originalState.contextInfo.issuer = this.fromFile;
|
182
159
|
this.originalState.contextInfo._embroiderMeta = this.meta;
|
160
|
+
if (this.resolvedTo) {
|
161
|
+
if (this.resolvedTo.type === 'found') {
|
162
|
+
this.originalState.createData = this.resolvedTo.result;
|
163
|
+
}
|
164
|
+
}
|
183
165
|
return this.originalState;
|
184
166
|
}
|
185
167
|
alias(newSpecifier) {
|
186
168
|
if (newSpecifier === this.specifier) {
|
187
169
|
return this;
|
188
170
|
}
|
189
|
-
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, newSpecifier, this.fromFile, this.meta, this.isVirtual, false, this.originalState);
|
171
|
+
return new WebpackModuleRequest(this.resolveFunction, this.babelLoaderPrefix, this.appRoot, newSpecifier, this.fromFile, this.meta, this.isVirtual, false, undefined, this.originalState);
|
190
172
|
}
|
191
173
|
rehome(newFromFile) {
|
192
174
|
if (this.fromFile === newFromFile) {
|
193
175
|
return this;
|
194
176
|
}
|
195
|
-
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.specifier, newFromFile, this.meta, this.isVirtual, false, this.originalState);
|
177
|
+
return new WebpackModuleRequest(this.resolveFunction, this.babelLoaderPrefix, this.appRoot, this.specifier, newFromFile, this.meta, this.isVirtual, false, undefined, this.originalState);
|
196
178
|
}
|
197
179
|
virtualize(filename) {
|
198
180
|
let params = new URLSearchParams();
|
199
181
|
params.set('f', filename);
|
200
182
|
params.set('a', this.appRoot);
|
201
|
-
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, `${this.babelLoaderPrefix}${virtualLoaderName}?${params.toString()}!`, this.fromFile, this.meta, true, false, this.originalState);
|
183
|
+
return new WebpackModuleRequest(this.resolveFunction, this.babelLoaderPrefix, this.appRoot, `${this.babelLoaderPrefix}${virtualLoaderName}?${params.toString()}!`, this.fromFile, this.meta, true, false, undefined, this.originalState);
|
202
184
|
}
|
203
185
|
withMeta(meta) {
|
204
|
-
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, meta, this.isVirtual, this.isNotFound, this.originalState);
|
186
|
+
return new WebpackModuleRequest(this.resolveFunction, this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, meta, this.isVirtual, this.isNotFound, this.resolvedTo, this.originalState);
|
205
187
|
}
|
206
188
|
notFound() {
|
207
|
-
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, this.meta, this.isVirtual, true, this.originalState);
|
189
|
+
return new WebpackModuleRequest(this.resolveFunction, this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, this.meta, this.isVirtual, true, undefined, this.originalState);
|
190
|
+
}
|
191
|
+
resolveTo(resolution) {
|
192
|
+
return new WebpackModuleRequest(this.resolveFunction, this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, this.meta, this.isVirtual, this.isNotFound, resolution, this.originalState);
|
193
|
+
}
|
194
|
+
async defaultResolve() {
|
195
|
+
if (this.isNotFound) {
|
196
|
+
// TODO: we can make sure this looks correct in webpack output when a
|
197
|
+
// user encounters it
|
198
|
+
let err = new Error(`module not found ${this.specifier}`);
|
199
|
+
err.code = 'MODULE_NOT_FOUND';
|
200
|
+
return { type: 'not_found', err };
|
201
|
+
}
|
202
|
+
return await new Promise(resolve => this.resolveFunction(this.toWebpackResolveData(), err => {
|
203
|
+
if (err) {
|
204
|
+
// unfortunately webpack doesn't let us distinguish between Not Found
|
205
|
+
// and other unexpected exceptions here.
|
206
|
+
resolve({ type: 'not_found', err });
|
207
|
+
}
|
208
|
+
else {
|
209
|
+
resolve({
|
210
|
+
type: 'found',
|
211
|
+
result: this.originalState.createData,
|
212
|
+
isVirtual: this.isVirtual,
|
213
|
+
filename: this.originalState.createData.resource,
|
214
|
+
});
|
215
|
+
}
|
216
|
+
}));
|
208
217
|
}
|
209
218
|
}
|
210
219
|
//# sourceMappingURL=webpack-resolver-plugin.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"webpack-resolver-plugin.js","sourceRoot":"","sources":["webpack-resolver-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,+BAAwC;AAExC,0CAA6G;AAE7G,gEAAuC;AACvC,gFAAgD;AAIhD,MAAM,iBAAiB,GAAG,uCAAuC,CAAC;AAClE,MAAM,iBAAiB,GAAG,IAAA,cAAO,EAAC,SAAS,EAAE,qBAAqB,CAAC,CAAC;AACpE,MAAM,qBAAqB,GAAG,IAAI,MAAM,CAAC,GAAG,IAAA,8BAAY,EAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;AAE/F,MAAa,eAAe;IAK1B,YAAY,IAA8B,EAAE,iBAAyB;;QAJrE,4CAA6B;QAC7B,qDAA2B;QAC3B,2CAAiB;QAGf,uBAAA,IAAI,6BAAa,IAAI,eAAiB,CAAC,IAAI,CAAC,MAAA,CAAC;QAC7C,uBAAA,IAAI,sCAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,4BAAY,IAAI,CAAC,OAAO,MAAA,CAAC;IAC/B,CAAC;IAeD,KAAK,CAAC,QAAkB;QACtB,uBAAA,IAAI,mEAAgB,MAApB,IAAI,EAAiB,QAAQ,EAAE,iBAAiB,EAAE,iBAAiB,CAAC,CAAC;QAErE,QAAQ,CAAC,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,oBAAoB,EAAE,GAAG,CAAC,EAAE;YACjE,IAAI,cAAc,GAAG,qBAAqB,CAAC,GAAG,CAAC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iB,EACtB,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,IAAI,EACT,IAAI,CAAC,SAAS,EACd,IAAI,EACJ,IAAI,CAAC,aAAa,CACX,CAAC;IACZ,CAAC;CACF","sourcesContent":["import { dirname, resolve } from 'path';\nimport type { ModuleRequest, ResolverFunction, Resolution } from '@embroider/core';\nimport { Resolver as EmbroiderResolver, ResolverOptions as EmbroiderResolverOptions } from '@embroider/core';\nimport type { Compiler, Module, ResolveData } from 'webpack';\nimport assertNever from 'assert-never';\nimport escapeRegExp from 'escape-string-regexp';\n\nexport { EmbroiderResolverOptions as Options };\n\nconst virtualLoaderName = '@embroider/webpack/src/virtual-loader';\nconst virtualLoaderPath = resolve(__dirname, './virtual-loader.js');\nconst virtualRequestPattern = new RegExp(`${escapeRegExp(virtualLoaderPath)}\\\\?(?<query>.+)!`);\n\nexport class EmbroiderPlugin {\n #resolver: EmbroiderResolver;\n #babelLoaderPrefix: string;\n #appRoot: string;\n\n constructor(opts: EmbroiderResolverOptions, babelLoaderPrefix: string) {\n this.#resolver = new EmbroiderResolver(opts);\n this.#babelLoaderPrefix = babelLoaderPrefix;\n this.#appRoot = opts.appRoot;\n }\n\n #addLoaderAlias(compiler: Compiler, name: string, alias: string) {\n let { resolveLoader } = compiler.options;\n if (Array.isArray(resolveLoader.alias)) {\n resolveLoader.alias.push({ name, alias });\n } else if (resolveLoader.alias) {\n resolveLoader.alias[name] = alias;\n } else {\n resolveLoader.alias = {\n [name]: alias,\n };\n }\n }\n\n apply(compiler: Compiler) {\n this.#addLoaderAlias(compiler, virtualLoaderName, virtualLoaderPath);\n\n compiler.hooks.normalModuleFactory.tap('@embroider/webpack', nmf => {\n let defaultResolve = getDefaultResolveHook(nmf.hooks.resolve.taps);\n let adaptedResolve = getAdaptedResolve(defaultResolve);\n\n nmf.hooks.resolve.tapAsync(\n { name: '@embroider/webpack', stage: 50 },\n (state: ExtendedResolveData, callback: CB) => {\n let request = WebpackModuleRequest.from(state, this.#babelLoaderPrefix, this.#appRoot);\n if (!request) {\n defaultResolve(state, callback);\n return;\n }\n\n this.#resolver.resolve(request, adaptedResolve).then(\n resolution => {\n switch (resolution.type) {\n case 'not_found':\n callback(resolution.err);\n break;\n case 'found':\n callback(null, resolution.result);\n break;\n default:\n throw assertNever(resolution);\n }\n },\n err => callback(err)\n );\n }\n );\n });\n }\n}\n\ntype CB = (err: null | Error, result?: Module | undefined) => void;\ntype DefaultResolve = (state: unknown, callback: CB) => void;\n\n// Despite being absolutely riddled with way-too-powerful tap points,\n// webpack still doesn't succeed in making it possible to provide a\n// fallback to the default resolve hook in the NormalModuleFactory. So\n// instead we will find the default behavior and call it from our own tap,\n// giving us a chance to handle its failures.\nfunction getDefaultResolveHook(taps: { name: string; fn: Function }[]): DefaultResolve {\n let { fn } = taps.find(t => t.name === 'NormalModuleFactory')!;\n return fn as DefaultResolve;\n}\n\n// This converts the raw function we got out of webpack into the right interface\n// for use by @embroider/core's resolver.\nfunction getAdaptedResolve(\n defaultResolve: DefaultResolve\n): ResolverFunction<WebpackModuleRequest, Resolution<Module, null | Error>> {\n return function (request: WebpackModuleRequest): Promise<Resolution<Module, null | Error>> {\n return new Promise(resolve => {\n if (request.isNotFound) {\n // TODO: we can make sure this looks correct in webpack output when a\n // user encounters it\n let err = new Error(`module not found ${request.specifier}`);\n (err as any).code = 'MODULE_NOT_FOUND';\n resolve({ type: 'not_found', err });\n }\n defaultResolve(request.toWebpackResolveData(), (err, value) => {\n if (err) {\n // unfortunately webpack doesn't let us distinguish between Not Found\n // and other unexpected exceptions here.\n resolve({ type: 'not_found', err });\n } else {\n resolve({ type: 'found', result: value! });\n }\n });\n });\n };\n}\n\ntype ExtendedResolveData = ResolveData & {\n contextInfo: ResolveData['contextInfo'] & { _embroiderMeta?: Record<string, any> };\n};\n\nclass WebpackModuleRequest implements ModuleRequest {\n static from(\n state: ExtendedResolveData,\n babelLoaderPrefix: string,\n appRoot: string\n ): WebpackModuleRequest | undefined {\n let specifier = state.request;\n if (\n specifier.includes(virtualLoaderName) || // prevents recursion on requests we have already sent to our virtual loader\n specifier.startsWith('!') // ignores internal webpack resolvers\n ) {\n return;\n }\n\n let fromFile: string | undefined;\n if (state.contextInfo.issuer) {\n fromFile = state.contextInfo.issuer;\n } else {\n // when the files emitted from our virtual-loader try to import things,\n // those requests show in webpack as having no issuer. But we can see here\n // which requests they are and adjust the issuer so they resolve things from\n // the correct logical place.\n for (let dep of state.dependencies) {\n let match = virtualRequestPattern.exec((dep as any)._parentModule?.userRequest);\n if (match) {\n fromFile = new URLSearchParams(match.groups!.query).get('f')!;\n break;\n }\n }\n }\n if (!fromFile) {\n return;\n }\n\n return new WebpackModuleRequest(\n babelLoaderPrefix,\n appRoot,\n specifier,\n fromFile,\n state.contextInfo._embroiderMeta,\n false,\n false,\n state\n );\n }\n\n private constructor(\n private babelLoaderPrefix: string,\n private appRoot: string,\n readonly specifier: string,\n readonly fromFile: string,\n readonly meta: Record<string, any> | undefined,\n readonly isVirtual: boolean,\n readonly isNotFound: boolean,\n private originalState: ExtendedResolveData\n ) {}\n\n get debugType() {\n return 'webpack';\n }\n\n // Webpack mostly relies on mutation to adjust requests. We could create a\n // whole new ResolveData instead, and that would allow defaultResolving to\n // happen, but for the output of that process to actually affect the\n // downstream code in Webpack we would still need to mutate the original\n // ResolveData with the results (primarily the `createData`). So since we\n // cannot avoid the mutation anyway, it seems best to do it earlier rather\n // than later, so that everything from here forward is \"normal\".\n //\n // Technically a NormalModuleLoader `resolve` hook *can* directly return a\n // Module, but that is not how the stock one works, and it would force us to\n // copy more of Webpack's default behaviors into the inside of our hook. Like,\n // we would need to invoke afterResolve, createModule, createModuleClass, etc,\n // just like webpack does if we wanted to produce a Module directly.\n //\n // So the mutation strategy is much less intrusive, even though it means there\n // is the risk of state leakage all over the place.\n //\n // We mitigate that risk by waiting until the last possible moment to apply\n // our desired ModuleRequest fields to the ResolveData. This means that as\n // requests evolve through the module-resolver they aren't actually all\n // mutating the shared state. Only when a request is allowed to bubble back\n // out to webpack does that happen.\n toWebpackResolveData(): ExtendedResolveData {\n this.originalState.request = this.specifier;\n this.originalState.context = dirname(this.fromFile);\n this.originalState.contextInfo.issuer = this.fromFile;\n this.originalState.contextInfo._embroiderMeta = this.meta;\n return this.originalState;\n }\n\n alias(newSpecifier: string) {\n if (newSpecifier === this.specifier) {\n return this;\n }\n return new WebpackModuleRequest(\n this.babelLoaderPrefix,\n this.appRoot,\n newSpecifier,\n this.fromFile,\n this.meta,\n this.isVirtual,\n false,\n this.originalState\n ) as this;\n }\n rehome(newFromFile: string) {\n if (this.fromFile === newFromFile) {\n return this;\n }\n return new WebpackModuleRequest(\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n newFromFile,\n this.meta,\n this.isVirtual,\n false,\n this.originalState\n ) as this;\n }\n virtualize(filename: string) {\n let params = new URLSearchParams();\n params.set('f', filename);\n params.set('a', this.appRoot);\n return new WebpackModuleRequest(\n this.babelLoaderPrefix,\n this.appRoot,\n `${this.babelLoaderPrefix}${virtualLoaderName}?${params.toString()}!`,\n this.fromFile,\n this.meta,\n true,\n false,\n this.originalState\n ) as this;\n }\n withMeta(meta: Record<string, any> | undefined): this {\n return new WebpackModuleRequest(\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n this.fromFile,\n meta,\n this.isVirtual,\n this.isNotFound,\n this.originalState\n ) as this;\n }\n notFound(): this {\n return new WebpackModuleRequest(\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n this.fromFile,\n this.meta,\n this.isVirtual,\n true,\n this.originalState\n ) as this;\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"webpack-resolver-plugin.js","sourceRoot":"","sources":["webpack-resolver-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,+BAAwC;AAExC,0CAA6G;AAE7G,gEAAuC;AACvC,gFAAgD;AAIhD,MAAM,iBAAiB,GAAG,uCAAuC,CAAC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AAC,oBAAoB,EAAE,EAAE,GAAG,CAAC,EAAE;YACtD,IAAI,GAAG,EAAE;gBACP,qEAAqE;gBACrE,wCAAwC;gBACxC,OAAO,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;aACrC;iBAAM;gBACL,OAAO,CAAC;oBACN,IAAI,EAAE,OAAO;oBACb,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,UAAU;oBACrC,SAAS,EAAE,IAAI,CAAC,SAAS;oBACzB,QAAQ,EAAE,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,QAAS;iBAClD,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { dirname, resolve } from 'path';\nimport type { ModuleRequest, Resolution } from '@embroider/core';\nimport { Resolver as EmbroiderResolver, ResolverOptions as EmbroiderResolverOptions } from '@embroider/core';\nimport type { Compiler, Module, ResolveData } from 'webpack';\nimport assertNever from 'assert-never';\nimport escapeRegExp from 'escape-string-regexp';\n\nexport { EmbroiderResolverOptions as Options };\n\nconst virtualLoaderName = '@embroider/webpack/src/virtual-loader';\nconst virtualLoaderPath = resolve(__dirname, './virtual-loader.js');\nconst virtualRequestPattern = new RegExp(`${escapeRegExp(virtualLoaderPath)}\\\\?(?<query>.+)!`);\n\nexport class EmbroiderPlugin {\n #resolver: EmbroiderResolver;\n #babelLoaderPrefix: string;\n #appRoot: string;\n\n constructor(opts: EmbroiderResolverOptions, babelLoaderPrefix: string) {\n this.#resolver = new EmbroiderResolver(opts);\n this.#babelLoaderPrefix = babelLoaderPrefix;\n this.#appRoot = opts.appRoot;\n }\n\n #addLoaderAlias(compiler: Compiler, name: string, alias: string) {\n let { resolveLoader } = compiler.options;\n if (Array.isArray(resolveLoader.alias)) {\n resolveLoader.alias.push({ name, alias });\n } else if (resolveLoader.alias) {\n resolveLoader.alias[name] = alias;\n } else {\n resolveLoader.alias = {\n [name]: alias,\n };\n }\n }\n\n apply(compiler: Compiler) {\n this.#addLoaderAlias(compiler, virtualLoaderName, virtualLoaderPath);\n\n compiler.hooks.normalModuleFactory.tap('@embroider/webpack', nmf => {\n let defaultResolve = getDefaultResolveHook(nmf.hooks.resolve.taps);\n\n nmf.hooks.resolve.tapAsync(\n { name: '@embroider/webpack', stage: 50 },\n (state: ExtendedResolveData, callback: CB) => {\n let request = WebpackModuleRequest.from(defaultResolve, state, this.#babelLoaderPrefix, this.#appRoot);\n if (!request) {\n defaultResolve(state, callback);\n return;\n }\n\n this.#resolver.resolve(request).then(\n resolution => {\n switch (resolution.type) {\n case 'not_found':\n callback(resolution.err);\n break;\n case 'found':\n case 'ignored':\n callback(null, undefined);\n break;\n default:\n throw assertNever(resolution);\n }\n },\n err => callback(err)\n );\n }\n );\n });\n }\n}\n\ninterface CB {\n (err: null, result: Module | undefined): void;\n (err: Error | null): void;\n}\ntype DefaultResolve = (state: ResolveData, callback: CB) => void;\n\n// Despite being absolutely riddled with way-too-powerful tap points,\n// webpack still doesn't succeed in making it possible to provide a\n// fallback to the default resolve hook in the NormalModuleFactory. So\n// instead we will find the default behavior and call it from our own tap,\n// giving us a chance to handle its failures.\nfunction getDefaultResolveHook(taps: { name: string; fn: Function }[]): DefaultResolve {\n let { fn } = taps.find(t => t.name === 'NormalModuleFactory')!;\n return fn as DefaultResolve;\n}\n\ntype ExtendedResolveData = ResolveData & {\n contextInfo: ResolveData['contextInfo'] & { _embroiderMeta?: Record<string, any> };\n};\n\ntype WebpackResolution = Resolution<ResolveData['createData'], null | Error>;\n\nclass WebpackModuleRequest implements ModuleRequest {\n static from(\n resolveFunction: DefaultResolve,\n state: ExtendedResolveData,\n babelLoaderPrefix: string,\n appRoot: string\n ): WebpackModuleRequest | undefined {\n let specifier = state.request;\n if (\n specifier.includes(virtualLoaderName) || // prevents recursion on requests we have already sent to our virtual loader\n specifier.startsWith('!') // ignores internal webpack resolvers\n ) {\n return;\n }\n\n let fromFile: string | undefined;\n if (state.contextInfo.issuer) {\n fromFile = state.contextInfo.issuer;\n } else {\n // when the files emitted from our virtual-loader try to import things,\n // those requests show in webpack as having no issuer. But we can see here\n // which requests they are and adjust the issuer so they resolve things from\n // the correct logical place.\n for (let dep of state.dependencies) {\n let match = virtualRequestPattern.exec((dep as any)._parentModule?.userRequest);\n if (match) {\n fromFile = new URLSearchParams(match.groups!.query).get('f')!;\n break;\n }\n }\n }\n if (!fromFile) {\n return;\n }\n\n return new WebpackModuleRequest(\n resolveFunction,\n babelLoaderPrefix,\n appRoot,\n specifier,\n fromFile,\n state.contextInfo._embroiderMeta,\n false,\n false,\n undefined,\n state\n );\n }\n\n private constructor(\n private resolveFunction: DefaultResolve,\n private babelLoaderPrefix: string,\n private appRoot: string,\n readonly specifier: string,\n readonly fromFile: string,\n readonly meta: Record<string, any> | undefined,\n readonly isVirtual: boolean,\n readonly isNotFound: boolean,\n readonly resolvedTo: WebpackResolution | undefined,\n private originalState: ExtendedResolveData\n ) {}\n\n get debugType() {\n return 'webpack';\n }\n\n // Webpack mostly relies on mutation to adjust requests. We could create a\n // whole new ResolveData instead, and that would allow defaultResolving to\n // happen, but for the output of that process to actually affect the\n // downstream code in Webpack we would still need to mutate the original\n // ResolveData with the results (primarily the `createData`). So since we\n // cannot avoid the mutation anyway, it seems best to do it earlier rather\n // than later, so that everything from here forward is \"normal\".\n //\n // Technically a NormalModuleLoader `resolve` hook *can* directly return a\n // Module, but that is not how the stock one works, and it would force us to\n // copy more of Webpack's default behaviors into the inside of our hook. Like,\n // we would need to invoke afterResolve, createModule, createModuleClass, etc,\n // just like webpack does if we wanted to produce a Module directly.\n //\n // So the mutation strategy is much less intrusive, even though it means there\n // is the risk of state leakage all over the place.\n //\n // We mitigate that risk by waiting until the last possible moment to apply\n // our desired ModuleRequest fields to the ResolveData. This means that as\n // requests evolve through the module-resolver they aren't actually all\n // mutating the shared state. Only when a request is allowed to bubble back\n // out to webpack does that happen.\n toWebpackResolveData(): ExtendedResolveData {\n this.originalState.request = this.specifier;\n this.originalState.context = dirname(this.fromFile);\n this.originalState.contextInfo.issuer = this.fromFile;\n this.originalState.contextInfo._embroiderMeta = this.meta;\n if (this.resolvedTo) {\n if (this.resolvedTo.type === 'found') {\n this.originalState.createData = this.resolvedTo.result;\n }\n }\n return this.originalState;\n }\n\n alias(newSpecifier: string) {\n if (newSpecifier === this.specifier) {\n return this;\n }\n return new WebpackModuleRequest(\n this.resolveFunction,\n this.babelLoaderPrefix,\n this.appRoot,\n newSpecifier,\n this.fromFile,\n this.meta,\n this.isVirtual,\n false,\n undefined,\n this.originalState\n ) as this;\n }\n rehome(newFromFile: string) {\n if (this.fromFile === newFromFile) {\n return this;\n }\n return new WebpackModuleRequest(\n this.resolveFunction,\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n newFromFile,\n this.meta,\n this.isVirtual,\n false,\n undefined,\n this.originalState\n ) as this;\n }\n virtualize(filename: string) {\n let params = new URLSearchParams();\n params.set('f', filename);\n params.set('a', this.appRoot);\n return new WebpackModuleRequest(\n this.resolveFunction,\n this.babelLoaderPrefix,\n this.appRoot,\n `${this.babelLoaderPrefix}${virtualLoaderName}?${params.toString()}!`,\n this.fromFile,\n this.meta,\n true,\n false,\n undefined,\n this.originalState\n ) as this;\n }\n withMeta(meta: Record<string, any> | undefined): this {\n return new WebpackModuleRequest(\n this.resolveFunction,\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n this.fromFile,\n meta,\n this.isVirtual,\n this.isNotFound,\n this.resolvedTo,\n this.originalState\n ) as this;\n }\n notFound(): this {\n return new WebpackModuleRequest(\n this.resolveFunction,\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n this.fromFile,\n this.meta,\n this.isVirtual,\n true,\n undefined,\n this.originalState\n ) as this;\n }\n\n resolveTo(resolution: WebpackResolution): this {\n return new WebpackModuleRequest(\n this.resolveFunction,\n this.babelLoaderPrefix,\n this.appRoot,\n this.specifier,\n this.fromFile,\n this.meta,\n this.isVirtual,\n this.isNotFound,\n resolution,\n this.originalState\n ) as this;\n }\n\n async defaultResolve(): Promise<WebpackResolution> {\n if (this.isNotFound) {\n // TODO: we can make sure this looks correct in webpack output when a\n // user encounters it\n let err = new Error(`module not found ${this.specifier}`);\n (err as any).code = 'MODULE_NOT_FOUND';\n return { type: 'not_found', err };\n }\n return await new Promise(resolve =>\n this.resolveFunction(this.toWebpackResolveData(), err => {\n if (err) {\n // unfortunately webpack doesn't let us distinguish between Not Found\n // and other unexpected exceptions here.\n resolve({ type: 'not_found', err });\n } else {\n resolve({\n type: 'found',\n result: this.originalState.createData,\n isVirtual: this.isVirtual,\n filename: this.originalState.createData.resource!,\n });\n }\n })\n );\n }\n}\n"]}
|