Skip to content

Commit 2ad87ff

Browse files
committed
WIP
1 parent d0aad14 commit 2ad87ff

7 files changed

+106
-94
lines changed

lib/DelegatedModule.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ class DelegatedModule extends Module {
4949
callback();
5050
}
5151

52-
source() {
53-
const sourceModule = this.dependencies[0].module;
52+
source(depTemplates, runtime) {
53+
const dep = this.dependencies[0];
54+
const sourceModule = dep.module;
5455
let str;
5556

5657
if(!sourceModule) {
5758
str = WebpackMissingModule.moduleCode(this.sourceRequest);
5859
} else {
59-
str = `module.exports = (__webpack_require__(${JSON.stringify(sourceModule.id)}))`;
60+
str = `module.exports = (${runtime.moduleExports({
61+
module: sourceModule,
62+
request: dep.request
63+
})})`;
6064

6165
switch(this.type) {
6266
case "require":

lib/RuntimeTemplate.js

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = class RuntimeTemplate {
1212
this.requestShortener = requestShortener;
1313
}
1414

15-
comment({ request, chunkName, chunkReason, message }) {
15+
comment({ request, chunkName, chunkReason, message, exportName }) {
1616
let content;
1717
if(this.outputOptions.pathinfo) {
1818
content = [message, request, chunkName, chunkReason].filter(Boolean).map(item => this.requestShortener.shorten(item)).join(" | ");
@@ -36,6 +36,10 @@ module.exports = class RuntimeTemplate {
3636
return `!(${this.throwMissingModuleErrorFunction({ request })}())`;
3737
}
3838

39+
missingModuleStatement({ request }) {
40+
return `${this.missingModule({ request })};\n`;
41+
}
42+
3943
missingModulePromise({ request }) {
4044
return `Promise.resolve().then(${this.throwMissingModuleErrorFunction({ request })})`;
4145
}
@@ -45,20 +49,23 @@ module.exports = class RuntimeTemplate {
4549
return `${this.comment({ request })}${JSON.stringify(module.id)}`;
4650
}
4751

48-
moduleExports({ module, request }) {
52+
moduleRaw({ module, request }) {
4953
if(!module) return this.missingModule({ request });
5054
return `__webpack_require__(${this.moduleId({ module, request })})`;
5155
}
5256

57+
moduleExports({ module, request }) {
58+
return this.moduleRaw({ module, request });
59+
}
60+
5361
moduleNamespace({ module, request, strict }) {
54-
const stringifiedId = JSON.stringify(module.id);
55-
const comment = this.comment({ request });
62+
const rawModule = this.moduleRaw({ module, request });
5663
if(module.buildMeta && module.buildMeta.harmonyModule) {
57-
return `__webpack_require__(${comment}${stringifiedId})`;
64+
return rawModule;
5865
} else if(strict) {
59-
return `Object({ /* fake namespace object */ "default": __webpack_require__(${comment}${stringifiedId}) })`;
66+
return `Object({ /* fake namespace object */ "default": ${rawModule} })`;
6067
} else {
61-
return `Object(function() { var module = __webpack_require__(${comment}${stringifiedId}); return typeof module === "object" && module && module.__esModule ? module : { /* fake namespace object */ "default": module }; }())`;
68+
return `Object(function() { var module = ${rawModule}; return typeof module === "object" && module && module.__esModule ? module : { /* fake namespace object */ "default": module }; }())`;
6269
}
6370
}
6471

@@ -80,21 +87,77 @@ module.exports = class RuntimeTemplate {
8087
}
8188
header += `if(!__webpack_require__.m[${idExpr}]) { var e = new Error("Module '" + ${idExpr} + "' is not available (weak dependency)"); e.code = 'MODULE_NOT_FOUND'; throw e; } `;
8289
}
90+
const rawModule = this.moduleRaw({ module, request });
8391
if(module.buildMeta && module.buildMeta.harmonyModule) {
8492
if(header) {
85-
getModuleFunction = `function() { ${header}return __webpack_require__(${comment}${idExpr}); }`;
93+
getModuleFunction = `function() { ${header}return ${rawModule}; }`;
8694
} else {
8795
getModuleFunction = `__webpack_require__.bind(null, ${comment}${idExpr})`;
8896
}
8997
} else if(strict) {
90-
getModuleFunction = `function() { ${header}return { /* fake namespace object */ "default": __webpack_require__(${comment}${idExpr}) }; }`;
98+
getModuleFunction = `function() { ${header}return { /* fake namespace object */ "default": ${rawModule} }; }`;
9199
} else {
92-
getModuleFunction = `function() { ${header}var module = __webpack_require__(${comment}${idExpr}); return typeof module === "object" && module && module.__esModule ? module : { /* fake namespace object */ "default": module }; }`;
100+
getModuleFunction = `function() { ${header}var module = ${rawModule}; return typeof module === "object" && module && module.__esModule ? module : { /* fake namespace object */ "default": module }; }`;
93101
}
94102

95103
return `${promise || "Promise.resolve()"}.then(${getModuleFunction})`;
96104
}
97105

106+
importStatement({ update, module, request, importVar, originModule }) {
107+
if(!module) return this.missingModuleStatement({ request });
108+
const comment = this.comment({ request });
109+
const optDeclaration = update ? "" : "var ";
110+
111+
const isHarmonyModule = module.buildMeta && module.buildMeta.harmonyModule;
112+
const content = `/* harmony import */ ${optDeclaration}${importVar} = __webpack_require__(${comment}${JSON.stringify(module.id)});\n`;
113+
if(isHarmonyModule || originModule.buildMeta.strictHarmonyModule) {
114+
return content;
115+
}
116+
return `${content}/* harmony import */ ${optDeclaration}${importVar}_default = /*#__PURE__*/__webpack_require__.n(${importVar});\n`;
117+
}
118+
119+
exportFromImport({ module, exportName, originModule, asiSafe, isCall, callContext, importVar }) {
120+
const isHarmonyModule = module.buildMeta && module.buildMeta.harmonyModule;
121+
122+
if(!isHarmonyModule) {
123+
if(exportName === "default") {
124+
if(!originModule.buildMeta.strictHarmonyModule) {
125+
if(isCall)
126+
return `${importVar}_default()`;
127+
else if(asiSafe)
128+
return `(${importVar}_default())`;
129+
else
130+
return `${importVar}_default.a`;
131+
}
132+
} else if(originModule.buildMeta.strictHarmonyModule) {
133+
if(exportName) {
134+
return "/* non-default import from non-esm module */undefined";
135+
} else if(!exportName) {
136+
if(asiSafe) {
137+
return `{ /* fake namespace object */ "default": ${importVar} }`;
138+
} else {
139+
return `Object({ /* fake namespace object */ "default": ${importVar} })`;
140+
}
141+
}
142+
}
143+
}
144+
145+
if(exportName) {
146+
const used = module.isUsed(exportName);
147+
const comment = used !== exportName ? Template.toNormalComment(exportName) + " " : "";
148+
const access = `${importVar}[${comment}${JSON.stringify(used)}]`;
149+
if(isCall) {
150+
if(callContext === false && asiSafe)
151+
return `(0,${access})`;
152+
else if(callContext === false)
153+
return `Object(${access})`;
154+
}
155+
return access;
156+
} else {
157+
return importVar;
158+
}
159+
}
160+
98161
blockPromise({ block, message }) {
99162
if(!block || !block.chunks) {
100163
const comment = this.comment({ message });

lib/dependencies/AMDRequireDependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ AMDRequireDependency.Template = class AMDRequireDependencyTemplate {
5656
// has array range, function range, but no errorCallbackRange
5757
if(depBlock.arrayRange && depBlock.functionRange) {
5858
const startBlock = `${promise}.then(function() { `;
59-
const endBlock = `}${depBlock.functionBindThis ? ".bind(this)" : ""}).catch(__webpack_require__.oe)`;
59+
const endBlock = `}${depBlock.functionBindThis ? ".bind(this)" : ""}).catch(${runtime.onError()})`;
6060
source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
6161
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
6262
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");

lib/dependencies/HarmonyAcceptDependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ HarmonyAcceptDependency.Template = class HarmonyAcceptDependencyTemplate {
2424
apply(dep, source, runtime) {
2525
const content = dep.dependencies
2626
.filter(dependency => HarmonyImportDependency.Template.isImportEmitted(dependency, source))
27-
.map(dependency => dependency.getImportStatement(false, runtime))
27+
.map(dependency => dependency.getImportStatement(true, runtime))
2828
.join("");
2929

3030
if(dep.hasCallback) {

lib/dependencies/HarmonyExportImportedSpecifierDependency.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ module.exports = HarmonyExportImportedSpecifierDependency;
339339
HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
340340
harmonyInit(dep, source, runtime, dependencyTemplates) {
341341
super.harmonyInit(dep, source, runtime, dependencyTemplates);
342-
const importVar = dep.getImportVar();
343-
const content = this.getContent(dep, importVar);
342+
const content = this.getContent(dep);
344343
source.insert(-1, content);
345344
}
346345

@@ -380,7 +379,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
380379
return super.getHarmonyInitOrder(dep);
381380
}
382381

383-
getContent(dep, name) {
382+
getContent(dep) {
384383
const mode = dep.getMode();
385384
const module = dep.originModule;
386385
const importedModule = dep.module;
@@ -433,7 +432,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
433432
else
434433
content += "if(__WEBPACK_IMPORT_KEY__ !== 'default') ";
435434
const exportsName = dep.originModule.exportsArgument;
436-
return content + `(function(key) { __webpack_require__.d(${exportsName}, key, function() { return ${name}[key]; }) }(__WEBPACK_IMPORT_KEY__));\n`;
435+
return content + `(function(key) { __webpack_require__.d(${exportsName}, key, function() { return ${importVar}[key]; }) }(__WEBPACK_IMPORT_KEY__));\n`;
437436
}
438437

439438
default:

lib/dependencies/HarmonyImportDependency.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,14 @@ class HarmonyImportDependency extends ModuleDependency {
3434
return importVar;
3535
}
3636

37-
getImportStatement(declare, runtime) {
38-
const module = this.module;
39-
const comment = runtime.outputOptions.pathinfo ? Template.toComment(runtime.requestShortener.shorten(this.request)) : "";
40-
const optDeclaration = declare ? "var " : "";
41-
const optNewline = declare ? "\n" : " ";
42-
43-
if(!module) {
44-
const stringifiedError = JSON.stringify(`Cannot find module "${this.request}"`);
45-
return `throw new Error(${stringifiedError});${optNewline}`;
46-
}
47-
48-
const importVar = this.getImportVar();
49-
50-
if(importVar) {
51-
const isHarmonyModule = module.buildMeta && module.buildMeta.harmonyModule;
52-
const content = `/* harmony import */ ${optDeclaration}${importVar} = __webpack_require__(${comment}${JSON.stringify(module.id)});${optNewline}`;
53-
if(isHarmonyModule || this.originModule.buildMeta.strictHarmonyModule) {
54-
return content;
55-
}
56-
return `${content}/* harmony import */ ${optDeclaration}${importVar}_default = /*#__PURE__*/__webpack_require__.n(${importVar});${optNewline}`;
57-
}
58-
59-
return "";
37+
getImportStatement(update, runtime) {
38+
return runtime.importStatement({
39+
update,
40+
module: this.module,
41+
importVar: this.getImportVar(),
42+
request: this.request,
43+
originModule: this.originModule
44+
});
6045
}
6146

6247
updateHash(hash) {
@@ -95,7 +80,7 @@ HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate {
9580
const key = dep.module || dep.request;
9681
if(key && sourceInfo.emittedImports.get(key)) return;
9782
sourceInfo.emittedImports.set(key, true);
98-
const content = dep.getImportStatement(true, runtime);
83+
const content = dep.getImportStatement(false, runtime);
9984
source.insert(-1, content);
10085
}
10186
};

lib/dependencies/HarmonyImportSpecifierDependency.js

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -97,60 +97,21 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
9797
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
9898
apply(dep, source, runtime) {
9999
super.apply(dep, source, runtime);
100-
const importedVar = dep.getImportVar();
101-
const content = this.getContent(dep, importedVar);
100+
const content = this.getContent(dep, runtime);
102101
source.replace(dep.range[0], dep.range[1] - 1, content);
103102
}
104103

105-
getContent(dep, importedVar) {
106-
const importedModule = dep.module;
107-
const nonHarmonyImport = !(importedModule && (!importedModule.buildMeta || importedModule.buildMeta.harmonyModule));
108-
const importedVarSuffix = this.getImportVarSuffix(dep.id, importedModule);
109-
const shortHandPrefix = dep.shorthand ? `${dep.name}: ` : "";
110-
111-
// Note: dep.call and dep.shorthand are exclusive
112-
113-
if(nonHarmonyImport) {
114-
const defaultExport = dep.id === "default";
115-
if(dep.originModule.buildMeta.strictHarmonyModule) {
116-
if(defaultExport) {
117-
return `${shortHandPrefix}${importedVar}`;
118-
}
119-
120-
if(!dep.id) {
121-
if(shortHandPrefix)
122-
return `${shortHandPrefix}/* fake namespace object for non-esm import */ { default: ${importedVar} }`;
123-
else
124-
return `Object(/* fake namespace object for non-esm import */{ "default": ${importedVar} })`;
125-
}
126-
127-
return `${shortHandPrefix}/* non-default import from non-esm module */undefined`;
128-
} else {
129-
if(dep.call && defaultExport) {
130-
return `${shortHandPrefix}${importedVar}_default()`;
131-
}
132-
133-
if(defaultExport) {
134-
return `${shortHandPrefix}${importedVar}_default.a`;
135-
}
136-
}
137-
}
138-
139-
if(dep.call && dep.id && dep.directImport) {
140-
return `Object(${importedVar}${importedVarSuffix})`;
141-
}
142-
143-
return `${shortHandPrefix}${importedVar}${importedVarSuffix}`;
144-
}
145-
146-
getImportVarSuffix(id, importedModule) {
147-
if(id) {
148-
const used = importedModule ? importedModule.isUsed(id) : id;
149-
const optionalComment = id !== used ? " /* " + id + " */" : "";
150-
return `[${JSON.stringify(used)}${optionalComment}]`;
151-
}
152-
153-
return "";
104+
getContent(dep, runtime) {
105+
const exportExpr = runtime.exportFromImport({
106+
module: dep.module,
107+
exportName: dep.id,
108+
originModule: dep.originModule,
109+
asiSafe: dep.shorthand,
110+
isCall: dep.call,
111+
callContext: !dep.directImport,
112+
importVar: dep.getImportVar()
113+
});
114+
return dep.shorthand ? `${dep.name}: ${exportExpr}` : exportExpr;
154115
}
155116
};
156117

0 commit comments

Comments
 (0)