Skip to content

Commit 7a36951

Browse files
committed
Merge branch 'master' of https://github.com/webpack/webpack into concatenated-module-in-3.5-compat
2 parents 438fd14 + 839915c commit 7a36951

28 files changed

Lines changed: 428 additions & 227 deletions

File tree

lib/Compilation.js

Lines changed: 178 additions & 144 deletions
Large diffs are not rendered by default.

lib/Compiler.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class Compiler extends Tapable {
201201
});
202202
});
203203
},
204-
apply: function() {
204+
apply: () => {
205205
const args = arguments;
206206
if(!deprecationReported) {
207207
console.warn("webpack: Using compiler.parser is deprecated.\n" +
@@ -214,7 +214,7 @@ class Compiler extends Tapable {
214214
parser.apply.apply(parser, args);
215215
});
216216
});
217-
}.bind(this)
217+
}
218218
};
219219

220220
this.options = {};
@@ -313,13 +313,7 @@ class Compiler extends Tapable {
313313
emitAssets(compilation, callback) {
314314
let outputPath;
315315

316-
this.applyPluginsAsync("emit", compilation, err => {
317-
if(err) return callback(err);
318-
outputPath = compilation.getPath(this.outputPath);
319-
this.outputFileSystem.mkdirp(outputPath, emitFiles.bind(this));
320-
});
321-
322-
function emitFiles(err) {
316+
const emitFiles = (err) => {
323317
if(err) return callback(err);
324318

325319
require("async").forEach(Object.keys(compilation.assets), (file, callback) => {
@@ -330,12 +324,7 @@ class Compiler extends Tapable {
330324
targetFile = targetFile.substr(0, queryStringIdx);
331325
}
332326

333-
if(targetFile.match(/\/|\\/)) {
334-
const dir = path.dirname(targetFile);
335-
this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut.bind(this));
336-
} else writeOut.call(this);
337-
338-
function writeOut(err) {
327+
const writeOut = (err) => {
339328
if(err) return callback(err);
340329
const targetPath = this.outputFileSystem.join(outputPath, targetFile);
341330
const source = compilation.assets[file];
@@ -352,14 +341,25 @@ class Compiler extends Tapable {
352341
source.existsAt = targetPath;
353342
source.emitted = true;
354343
this.outputFileSystem.writeFile(targetPath, content, callback);
355-
}
344+
};
345+
346+
if(targetFile.match(/\/|\\/)) {
347+
const dir = path.dirname(targetFile);
348+
this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut);
349+
} else writeOut();
356350

357351
}, err => {
358352
if(err) return callback(err);
359353

360354
afterEmit.call(this);
361355
});
362-
}
356+
};
357+
358+
this.applyPluginsAsync("emit", compilation, err => {
359+
if(err) return callback(err);
360+
outputPath = compilation.getPath(this.outputPath);
361+
this.outputFileSystem.mkdirp(outputPath, emitFiles);
362+
});
363363

364364
function afterEmit() {
365365
this.applyPluginsAsyncSeries1("after-emit", compilation, err => {

lib/ContextModuleFactory.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ module.exports = class ContextModuleFactory extends Tapable {
103103
if(!regExp || !resource)
104104
return callback(null, []);
105105
(function addDirectory(directory, callback) {
106-
fs.readdir(directory, function(err, files) {
106+
fs.readdir(directory, (err, files) => {
107107
if(err) return callback(err);
108108
if(!files || files.length === 0) return callback(null, []);
109109
asyncLib.map(files.filter(function(p) {
110110
return p.indexOf(".") !== 0;
111-
}), function(seqment, callback) {
111+
}), (seqment, callback) => {
112112

113113
const subResource = path.join(directory, seqment);
114114

115-
fs.stat(subResource, function(err, stat) {
115+
fs.stat(subResource, (err, stat) => {
116116
if(err) return callback(err);
117117

118118
if(stat.isDirectory()) {
@@ -141,9 +141,9 @@ module.exports = class ContextModuleFactory extends Tapable {
141141

142142
} else callback();
143143

144-
}.bind(this));
144+
});
145145

146-
}.bind(this), (err, result) => {
146+
}, (err, result) => {
147147
if(err) return callback(err);
148148

149149
if(!result) return callback(null, []);
@@ -154,7 +154,7 @@ module.exports = class ContextModuleFactory extends Tapable {
154154
return a.concat(i);
155155
}, []));
156156
});
157-
}.bind(this));
157+
});
158158
}.call(this, resource, callback));
159159
}
160160
};

lib/HotModuleReplacement.runtime.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ module.exports = function() {
462462
// remove module from cache
463463
delete installedModules[moduleId];
464464

465+
// when disposing there is no need to call dispose handler
466+
delete outdatedDependencies[moduleId];
467+
465468
// remove "parents" references from all children
466469
for(j = 0; j < module.children.length; j++) {
467470
var child = installedModules[module.children[j]];
@@ -507,30 +510,34 @@ module.exports = function() {
507510
for(moduleId in outdatedDependencies) {
508511
if(Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)) {
509512
module = installedModules[moduleId];
510-
moduleOutdatedDependencies = outdatedDependencies[moduleId];
511-
var callbacks = [];
512-
for(i = 0; i < moduleOutdatedDependencies.length; i++) {
513-
dependency = moduleOutdatedDependencies[i];
514-
cb = module.hot._acceptedDependencies[dependency];
515-
if(callbacks.indexOf(cb) >= 0) continue;
516-
callbacks.push(cb);
517-
}
518-
for(i = 0; i < callbacks.length; i++) {
519-
cb = callbacks[i];
520-
try {
521-
cb(moduleOutdatedDependencies);
522-
} catch(err) {
523-
if(options.onErrored) {
524-
options.onErrored({
525-
type: "accept-errored",
526-
moduleId: moduleId,
527-
dependencyId: moduleOutdatedDependencies[i],
528-
error: err
529-
});
513+
if(module) {
514+
moduleOutdatedDependencies = outdatedDependencies[moduleId];
515+
var callbacks = [];
516+
for(i = 0; i < moduleOutdatedDependencies.length; i++) {
517+
dependency = moduleOutdatedDependencies[i];
518+
cb = module.hot._acceptedDependencies[dependency];
519+
if(cb) {
520+
if(callbacks.indexOf(cb) >= 0) continue;
521+
callbacks.push(cb);
530522
}
531-
if(!options.ignoreErrored) {
532-
if(!error)
533-
error = err;
523+
}
524+
for(i = 0; i < callbacks.length; i++) {
525+
cb = callbacks[i];
526+
try {
527+
cb(moduleOutdatedDependencies);
528+
} catch(err) {
529+
if(options.onErrored) {
530+
options.onErrored({
531+
type: "accept-errored",
532+
moduleId: moduleId,
533+
dependencyId: moduleOutdatedDependencies[i],
534+
error: err
535+
});
536+
}
537+
if(!options.ignoreErrored) {
538+
if(!error)
539+
error = err;
540+
}
534541
}
535542
}
536543
}

lib/Parser.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -634,14 +634,14 @@ class Parser extends Tapable {
634634
statement.params.forEach(param => {
635635
this.walkPattern(param);
636636
});
637-
this.inScope(statement.params, function() {
637+
this.inScope(statement.params, () => {
638638
if(statement.body.type === "BlockStatement") {
639639
this.prewalkStatement(statement.body);
640640
this.walkStatement(statement.body);
641641
} else {
642642
this.walkExpression(statement.body);
643643
}
644-
}.bind(this));
644+
});
645645
}
646646

647647
prewalkImportDeclaration(statement) {
@@ -784,10 +784,10 @@ class Parser extends Tapable {
784784
}
785785

786786
walkCatchClause(catchClause) {
787-
this.inScope([catchClause.param], function() {
787+
this.inScope([catchClause.param], () => {
788788
this.prewalkStatement(catchClause.body);
789789
this.walkStatement(catchClause.body);
790-
}.bind(this));
790+
});
791791
}
792792

793793
prewalkVariableDeclarators(declarators) {
@@ -916,28 +916,28 @@ class Parser extends Tapable {
916916
expression.params.forEach(param => {
917917
this.walkPattern(param);
918918
});
919-
this.inScope(expression.params, function() {
919+
this.inScope(expression.params, () => {
920920
if(expression.body.type === "BlockStatement") {
921921
this.prewalkStatement(expression.body);
922922
this.walkStatement(expression.body);
923923
} else {
924924
this.walkExpression(expression.body);
925925
}
926-
}.bind(this));
926+
});
927927
}
928928

929929
walkArrowFunctionExpression(expression) {
930930
expression.params.forEach(param => {
931931
this.walkPattern(param);
932932
});
933-
this.inScope(expression.params, function() {
933+
this.inScope(expression.params, () => {
934934
if(expression.body.type === "BlockStatement") {
935935
this.prewalkStatement(expression.body);
936936
this.walkStatement(expression.body);
937937
} else {
938938
this.walkExpression(expression.body);
939939
}
940-
}.bind(this));
940+
});
941941
}
942942

943943
walkSequenceExpression(expression) {
@@ -1059,7 +1059,7 @@ class Parser extends Tapable {
10591059
const args = options.map(renameArgOrThis, this);
10601060
this.inScope(params.filter(function(identifier, idx) {
10611061
return !args[idx];
1062-
}), function() {
1062+
}), () => {
10631063
if(renameThis) {
10641064
this.scope.renames.$this = renameThis;
10651065
}
@@ -1074,7 +1074,7 @@ class Parser extends Tapable {
10741074
this.walkStatement(functionExpression.body);
10751075
} else
10761076
this.walkExpression(functionExpression.body);
1077-
}.bind(this));
1077+
});
10781078
}
10791079
if(expression.callee.type === "MemberExpression" &&
10801080
expression.callee.object.type === "FunctionExpression" &&

lib/UmdMainTemplatePlugin.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class UmdMainTemplatePlugin {
4141

4242
apply(compilation) {
4343
const mainTemplate = compilation.mainTemplate;
44-
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
44+
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
4545
let externals = chunk.getModules().filter(m => m.external);
4646
const optionalExternals = [];
4747
let requiredExternals = [];
@@ -172,19 +172,19 @@ class UmdMainTemplatePlugin {
172172
" }\n"
173173
) +
174174
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
175-
}.bind(this));
176-
mainTemplate.plugin("global-hash-paths", function(paths) {
175+
});
176+
mainTemplate.plugin("global-hash-paths", (paths) => {
177177
if(this.names.root) paths = paths.concat(this.names.root);
178178
if(this.names.amd) paths = paths.concat(this.names.amd);
179179
if(this.names.commonjs) paths = paths.concat(this.names.commonjs);
180180
return paths;
181-
}.bind(this));
182-
mainTemplate.plugin("hash", function(hash) {
181+
});
182+
mainTemplate.plugin("hash", (hash) => {
183183
hash.update("umd");
184184
hash.update(`${this.names.root}`);
185185
hash.update(`${this.names.amd}`);
186186
hash.update(`${this.names.commonjs}`);
187-
}.bind(this));
187+
});
188188
}
189189
}
190190

lib/optimize/ConcatenatedModule.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ function ensureNsObjSource(info, moduleToInfoMap, requestShortener) {
3232
}
3333
}
3434

35-
function getExternalImport(importedModule, importedVar, exportName, asCall) {
36-
const isHarmonyModule = importedModule && (!importedModule.meta || importedModule.meta.harmonyModule);
37-
if(exportName === true) return importedVar;
35+
function getExternalImport(importedModule, info, exportName, asCall) {
36+
if(exportName === true) return info.name;
3837
const used = importedModule.isUsed(exportName);
3938
if(!used) return "/* unused reexport */undefined";
40-
if(!isHarmonyModule && exportName === "default") {
41-
return asCall ? `${importedVar}_default()` : `${importedVar}_default.a`;
39+
if(info.interop && exportName === "default") {
40+
return asCall ? `${info.interopName}()` : `${info.interopName}.a`;
4241
}
4342
// TODO use Template.toNormalComment when merging with pure-module
4443
const comment = used !== exportName ? ` /* ${exportName} */` : "";
45-
const reference = `${importedVar}[${JSON.stringify(used)}${comment}]`;
44+
const reference = `${info.name}[${JSON.stringify(used)}${comment}]`;
4645
if(asCall)
4746
return `Object(${reference})`;
4847
return reference;
@@ -67,12 +66,6 @@ function getFinalName(info, exportName, moduleToInfoMap, requestShortener, asCal
6766
if(refInfo) {
6867
// module is in the concatenation
6968
return getFinalName(refInfo, reexport.exportName, moduleToInfoMap, requestShortener, asCall);
70-
} else {
71-
const dep = reexport.dependency;
72-
const importedModule = reexport.module;
73-
const exportName = reexport.exportName;
74-
const importedVar = dep.importedVar;
75-
return getExternalImport(importedModule, importedVar, exportName, asCall);
7669
}
7770
}
7871
const problem = `Cannot get final name for export "${exportName}" in "${info.module.readableIdentifier(requestShortener)}"` +
@@ -84,7 +77,7 @@ function getFinalName(info, exportName, moduleToInfoMap, requestShortener, asCal
8477
case "external":
8578
{
8679
const importedModule = info.module;
87-
return getExternalImport(importedModule, info.name, exportName, asCall);
80+
return getExternalImport(importedModule, info, exportName, asCall);
8881
}
8982
}
9083
}
@@ -367,7 +360,9 @@ class ConcatenatedModule extends Module {
367360
type: "external",
368361
module: info.module,
369362
index: idx,
370-
name: undefined
363+
name: undefined,
364+
interopName: undefined,
365+
interop: undefined
371366
};
372367
default:
373368
throw new Error(`Unsupported concatenation entry type ${info.type}`);
@@ -462,6 +457,8 @@ class ConcatenatedModule extends Module {
462457
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof",
463458
"var", "void", "volatile", "while", "with", "yield",
464459

460+
"module", "__dirname", "__filename", "exports",
461+
465462
"Array", "Date", "eval", "function", "hasOwnProperty", "Infinity", "isFinite", "isNaN",
466463
"isPrototypeOf", "length", "Math", "NaN", "name", "Number", "Object", "prototype", "String",
467464
"toString", "undefined", "valueOf",
@@ -535,9 +532,15 @@ class ConcatenatedModule extends Module {
535532
}
536533
case "external":
537534
{
535+
info.interop = info.module.meta && !info.module.meta.harmonyModule;
538536
const externalName = this.findNewName("", allUsedNames, null, info.module.readableIdentifier(requestShortener));
539537
allUsedNames.add(externalName);
540538
info.name = externalName;
539+
if(info.interop) {
540+
const externalNameInterop = this.findNewName("default", allUsedNames, null, info.module.readableIdentifier(requestShortener));
541+
allUsedNames.add(externalNameInterop);
542+
info.interopName = externalNameInterop;
543+
}
541544
break;
542545
}
543546
}
@@ -592,8 +595,8 @@ class ConcatenatedModule extends Module {
592595
case "external":
593596
result.add(`\n// EXTERNAL MODULE: ${info.module.readableIdentifier(requestShortener)}\n`);
594597
result.add(`var ${info.name} = __webpack_require__(${JSON.stringify(info.module.id)});\n`);
595-
if(info.module.meta && !info.module.meta.harmonyModule) {
596-
result.add(`var ${info.name}_default = /*#__PURE__*/__webpack_require__.n(${info.name});\n`);
598+
if(info.interop) {
599+
result.add(`var ${info.interopName} = /*#__PURE__*/__webpack_require__.n(${info.name});\n`);
597600
}
598601
break;
599602
default:

0 commit comments

Comments
 (0)