@embroider/webpack 3.2.2-unstable.996b2ff → 3.2.2-unstable.b33cb4c
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.2-unstable.
|
3
|
+
"version": "3.2.2-unstable.b33cb4c",
|
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.2-unstable.
|
22
|
+
"@embroider/babel-loader-9": "3.1.2-unstable.b33cb4c",
|
23
|
+
"@embroider/hbs-loader": "3.0.4-unstable.b33cb4c",
|
24
|
+
"@embroider/shared-internals": "2.5.2-unstable.b33cb4c",
|
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.3-unstable.
|
45
|
+
"@embroider/core": "3.4.3-unstable.b33cb4c",
|
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.3-unstable.
|
57
|
+
"@embroider/core": "3.4.3-unstable.b33cb4c",
|
58
58
|
"webpack": "^5.0.0"
|
59
59
|
},
|
60
60
|
"engines": {
|
@@ -89,7 +89,14 @@ function getDefaultResolveHook(taps) {
|
|
89
89
|
function getAdaptedResolve(defaultResolve) {
|
90
90
|
return function (request) {
|
91
91
|
return new Promise(resolve => {
|
92
|
-
|
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) => {
|
93
100
|
if (err) {
|
94
101
|
// unfortunately webpack doesn't let us distinguish between Not Found
|
95
102
|
// and other unexpected exceptions here.
|
@@ -104,69 +111,100 @@ function getAdaptedResolve(defaultResolve) {
|
|
104
111
|
}
|
105
112
|
class WebpackModuleRequest {
|
106
113
|
static from(state, babelLoaderPrefix, appRoot) {
|
107
|
-
var _a
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
114
|
+
var _a;
|
115
|
+
let specifier = state.request;
|
116
|
+
if (specifier.includes(virtualLoaderName) || // prevents recursion on requests we have already sent to our virtual loader
|
117
|
+
specifier.startsWith('!') // ignores internal webpack resolvers
|
118
|
+
) {
|
119
|
+
return;
|
120
|
+
}
|
121
|
+
let fromFile;
|
122
|
+
if (state.contextInfo.issuer) {
|
123
|
+
fromFile = state.contextInfo.issuer;
|
124
|
+
}
|
125
|
+
else {
|
126
|
+
// when the files emitted from our virtual-loader try to import things,
|
127
|
+
// those requests show in webpack as having no issuer. But we can see here
|
128
|
+
// which requests they are and adjust the issuer so they resolve things from
|
129
|
+
// the correct logical place.
|
113
130
|
for (let dep of state.dependencies) {
|
114
|
-
let match = virtualRequestPattern.exec((
|
131
|
+
let match = virtualRequestPattern.exec((_a = dep._parentModule) === null || _a === void 0 ? void 0 : _a.userRequest);
|
115
132
|
if (match) {
|
116
|
-
|
117
|
-
|
133
|
+
fromFile = new URLSearchParams(match.groups.query).get('f');
|
134
|
+
break;
|
118
135
|
}
|
119
136
|
}
|
120
137
|
}
|
121
|
-
if (
|
122
|
-
|
123
|
-
typeof ((_c = state.contextInfo) === null || _c === void 0 ? void 0 : _c.issuer) === 'string' &&
|
124
|
-
state.contextInfo.issuer !== '' &&
|
125
|
-
!state.request.includes(virtualLoaderName) && // prevents recursion on requests we have already sent to our virtual loader
|
126
|
-
!state.request.startsWith('!') // ignores internal webpack resolvers
|
127
|
-
) {
|
128
|
-
return new WebpackModuleRequest(babelLoaderPrefix, appRoot, state);
|
138
|
+
if (!fromFile) {
|
139
|
+
return;
|
129
140
|
}
|
141
|
+
return new WebpackModuleRequest(babelLoaderPrefix, appRoot, specifier, fromFile, state.contextInfo._embroiderMeta, false, false, state);
|
130
142
|
}
|
131
|
-
constructor(babelLoaderPrefix, appRoot,
|
143
|
+
constructor(babelLoaderPrefix, appRoot, specifier, fromFile, meta, isVirtual, isNotFound, originalState) {
|
132
144
|
this.babelLoaderPrefix = babelLoaderPrefix;
|
133
145
|
this.appRoot = appRoot;
|
134
|
-
this.
|
146
|
+
this.specifier = specifier;
|
147
|
+
this.fromFile = fromFile;
|
148
|
+
this.meta = meta;
|
135
149
|
this.isVirtual = isVirtual;
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
150
|
+
this.isNotFound = isNotFound;
|
151
|
+
this.originalState = originalState;
|
152
|
+
}
|
153
|
+
get debugType() {
|
154
|
+
return 'webpack';
|
155
|
+
}
|
156
|
+
// Webpack mostly relies on mutation to adjust requests. We could create a
|
157
|
+
// whole new ResolveData instead, and that would allow defaultResolving to
|
158
|
+
// happen, but for the output of that process to actually affect the
|
159
|
+
// downstream code in Webpack we would still need to mutate the original
|
160
|
+
// ResolveData with the results (primarily the `createData`). So since we
|
161
|
+
// cannot avoid the mutation anyway, it seems best to do it earlier rather
|
162
|
+
// than later, so that everything from here forward is "normal".
|
163
|
+
//
|
164
|
+
// Technically a NormalModuleLoader `resolve` hook *can* directly return a
|
165
|
+
// Module, but that is not how the stock one works, and it would force us to
|
166
|
+
// copy more of Webpack's default behaviors into the inside of our hook. Like,
|
167
|
+
// we would need to invoke afterResolve, createModule, createModuleClass, etc,
|
168
|
+
// just like webpack does if we wanted to produce a Module directly.
|
169
|
+
//
|
170
|
+
// So the mutation strategy is much less intrusive, even though it means there
|
171
|
+
// is the risk of state leakage all over the place.
|
172
|
+
//
|
173
|
+
// We mitigate that risk by waiting until the last possible moment to apply
|
174
|
+
// our desired ModuleRequest fields to the ResolveData. This means that as
|
175
|
+
// requests evolve through the module-resolver they aren't actually all
|
176
|
+
// mutating the shared state. Only when a request is allowed to bubble back
|
177
|
+
// out to webpack does that happen.
|
178
|
+
toWebpackResolveData() {
|
179
|
+
this.originalState.request = this.specifier;
|
180
|
+
this.originalState.context = (0, path_1.dirname)(this.fromFile);
|
181
|
+
this.originalState.contextInfo.issuer = this.fromFile;
|
182
|
+
this.originalState.contextInfo._embroiderMeta = this.meta;
|
183
|
+
return this.originalState;
|
144
184
|
}
|
145
185
|
alias(newSpecifier) {
|
146
|
-
this.
|
147
|
-
|
186
|
+
if (newSpecifier === this.specifier) {
|
187
|
+
return this;
|
188
|
+
}
|
189
|
+
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, newSpecifier, this.fromFile, this.meta, this.isVirtual, false, this.originalState);
|
148
190
|
}
|
149
191
|
rehome(newFromFile) {
|
150
192
|
if (this.fromFile === newFromFile) {
|
151
193
|
return this;
|
152
194
|
}
|
153
|
-
|
154
|
-
this.state.contextInfo.issuer = newFromFile;
|
155
|
-
this.state.context = (0, path_1.dirname)(newFromFile);
|
156
|
-
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.state);
|
157
|
-
}
|
195
|
+
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.specifier, newFromFile, this.meta, this.isVirtual, false, this.originalState);
|
158
196
|
}
|
159
197
|
virtualize(filename) {
|
160
198
|
let params = new URLSearchParams();
|
161
199
|
params.set('f', filename);
|
162
200
|
params.set('a', this.appRoot);
|
163
|
-
|
164
|
-
next.isVirtual = true;
|
165
|
-
return next;
|
201
|
+
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, `${this.babelLoaderPrefix}${virtualLoaderName}?${params.toString()}!`, this.fromFile, this.meta, true, false, this.originalState);
|
166
202
|
}
|
167
203
|
withMeta(meta) {
|
168
|
-
this.
|
169
|
-
|
204
|
+
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, meta, this.isVirtual, this.isNotFound, this.originalState);
|
205
|
+
}
|
206
|
+
notFound() {
|
207
|
+
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.specifier, this.fromFile, this.meta, this.isVirtual, true, this.originalState);
|
170
208
|
}
|
171
209
|
}
|
172
210
|
//# 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,CA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D,UAAU,CAAC,QAAgB;QACzB,IAAI,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;QACnC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;QAC1B,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,IAAI,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QAC7F,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,QAAQ,CAAC,IAAqC;QAC5C,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,cAAc,GAAG,IAAI,CAAC;QAC7C,OAAO,IAAI,oBAAoB,CAAC,IAAI,CAAC,iBAAiB,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAS,CAAC;IAC5F,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 } 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({ name: '@embroider/webpack', stage: 50 }, (state: unknown, 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\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 defaultResolve(request.state, (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\nclass WebpackModuleRequest implements ModuleRequest {\n readonly specifier: string;\n readonly fromFile: string;\n readonly meta: Record<string, any> | undefined;\n\n static from(state: any, babelLoaderPrefix: string, appRoot: string): WebpackModuleRequest | undefined {\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 if (!state.contextInfo?.issuer && Array.isArray(state.dependencies)) {\n for (let dep of state.dependencies) {\n let match = virtualRequestPattern.exec(dep._parentModule?.userRequest);\n if (match) {\n state.contextInfo.issuer = new URLSearchParams(match.groups!.query).get('f');\n state.context = dirname(state.contextInfo.issuer);\n }\n }\n }\n\n if (\n typeof state.request === 'string' &&\n typeof state.context === 'string' &&\n typeof state.contextInfo?.issuer === 'string' &&\n state.contextInfo.issuer !== '' &&\n !state.request.includes(virtualLoaderName) && // prevents recursion on requests we have already sent to our virtual loader\n !state.request.startsWith('!') // ignores internal webpack resolvers\n ) {\n return new WebpackModuleRequest(babelLoaderPrefix, appRoot, state);\n }\n }\n\n constructor(\n private babelLoaderPrefix: string,\n private appRoot: string,\n public state: {\n request: string;\n context: string;\n contextInfo: {\n issuer: string;\n _embroiderMeta?: Record<string, any> | undefined;\n };\n },\n public isVirtual = false\n ) {\n // these get copied here because we mutate the underlying state as we\n // convert one request into the next, and it seems better for debuggability\n // if the fields on the previous request don't change when you make a new\n // one (although it is true that only the newest one has a a valid `state`\n // that can actually be handed back to webpack)\n this.specifier = state.request;\n this.fromFile = state.contextInfo.issuer;\n this.meta = state.contextInfo._embroiderMeta ? { ...state.contextInfo._embroiderMeta } : undefined;\n }\n\n alias(newSpecifier: string) {\n this.state.request = newSpecifier;\n return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.state) as this;\n }\n rehome(newFromFile: string) {\n if (this.fromFile === newFromFile) {\n return this;\n } else {\n this.state.contextInfo.issuer = newFromFile;\n this.state.context = dirname(newFromFile);\n return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.state) as this;\n }\n }\n virtualize(filename: string) {\n let params = new URLSearchParams();\n params.set('f', filename);\n params.set('a', this.appRoot);\n let next = this.alias(`${this.babelLoaderPrefix}${virtualLoaderName}?${params.toString()}!`);\n next.isVirtual = true;\n return next;\n }\n withMeta(meta: Record<string, any> | undefined): this {\n this.state.contextInfo._embroiderMeta = meta;\n return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.state) 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;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;Q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C,SAAS,EACd,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,aAAa,CACX,CAAC;IACZ,CAAC;IACD,QAAQ;QACN,OAAO,IAAI,oBAAoB,CAC7B,IAAI,CAAC,iBAAiB,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"]}
|