Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/webpack/webpack into conc…
Browse files Browse the repository at this point in the history
…atenated-module-in-3.5-compat
  • Loading branch information
kisenka committed Aug 11, 2017
2 parents 438fd14 + 839915c commit 7a36951
Show file tree
Hide file tree
Showing 28 changed files with 428 additions and 227 deletions.
322 changes: 178 additions & 144 deletions lib/Compilation.js

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions lib/Compiler.js
Expand Up @@ -201,7 +201,7 @@ class Compiler extends Tapable {
});
});
},
apply: function() {
apply: () => {
const args = arguments;
if(!deprecationReported) {
console.warn("webpack: Using compiler.parser is deprecated.\n" +
Expand All @@ -214,7 +214,7 @@ class Compiler extends Tapable {
parser.apply.apply(parser, args);
});
});
}.bind(this)
}
};

this.options = {};
Expand Down Expand Up @@ -313,13 +313,7 @@ class Compiler extends Tapable {
emitAssets(compilation, callback) {
let outputPath;

this.applyPluginsAsync("emit", compilation, err => {
if(err) return callback(err);
outputPath = compilation.getPath(this.outputPath);
this.outputFileSystem.mkdirp(outputPath, emitFiles.bind(this));
});

function emitFiles(err) {
const emitFiles = (err) => {
if(err) return callback(err);

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

if(targetFile.match(/\/|\\/)) {
const dir = path.dirname(targetFile);
this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut.bind(this));
} else writeOut.call(this);

function writeOut(err) {
const writeOut = (err) => {
if(err) return callback(err);
const targetPath = this.outputFileSystem.join(outputPath, targetFile);
const source = compilation.assets[file];
Expand All @@ -352,14 +341,25 @@ class Compiler extends Tapable {
source.existsAt = targetPath;
source.emitted = true;
this.outputFileSystem.writeFile(targetPath, content, callback);
}
};

if(targetFile.match(/\/|\\/)) {
const dir = path.dirname(targetFile);
this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut);
} else writeOut();

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

afterEmit.call(this);
});
}
};

this.applyPluginsAsync("emit", compilation, err => {
if(err) return callback(err);
outputPath = compilation.getPath(this.outputPath);
this.outputFileSystem.mkdirp(outputPath, emitFiles);
});

function afterEmit() {
this.applyPluginsAsyncSeries1("after-emit", compilation, err => {
Expand Down
12 changes: 6 additions & 6 deletions lib/ContextModuleFactory.js
Expand Up @@ -103,16 +103,16 @@ module.exports = class ContextModuleFactory extends Tapable {
if(!regExp || !resource)
return callback(null, []);
(function addDirectory(directory, callback) {
fs.readdir(directory, function(err, files) {
fs.readdir(directory, (err, files) => {
if(err) return callback(err);
if(!files || files.length === 0) return callback(null, []);
asyncLib.map(files.filter(function(p) {
return p.indexOf(".") !== 0;
}), function(seqment, callback) {
}), (seqment, callback) => {

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

fs.stat(subResource, function(err, stat) {
fs.stat(subResource, (err, stat) => {
if(err) return callback(err);

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

} else callback();

}.bind(this));
});

}.bind(this), (err, result) => {
}, (err, result) => {
if(err) return callback(err);

if(!result) return callback(null, []);
Expand All @@ -154,7 +154,7 @@ module.exports = class ContextModuleFactory extends Tapable {
return a.concat(i);
}, []));
});
}.bind(this));
});
}.call(this, resource, callback));
}
};
53 changes: 30 additions & 23 deletions lib/HotModuleReplacement.runtime.js
Expand Up @@ -462,6 +462,9 @@ module.exports = function() {
// remove module from cache
delete installedModules[moduleId];

// when disposing there is no need to call dispose handler
delete outdatedDependencies[moduleId];

// remove "parents" references from all children
for(j = 0; j < module.children.length; j++) {
var child = installedModules[module.children[j]];
Expand Down Expand Up @@ -507,30 +510,34 @@ module.exports = function() {
for(moduleId in outdatedDependencies) {
if(Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)) {
module = installedModules[moduleId];
moduleOutdatedDependencies = outdatedDependencies[moduleId];
var callbacks = [];
for(i = 0; i < moduleOutdatedDependencies.length; i++) {
dependency = moduleOutdatedDependencies[i];
cb = module.hot._acceptedDependencies[dependency];
if(callbacks.indexOf(cb) >= 0) continue;
callbacks.push(cb);
}
for(i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
try {
cb(moduleOutdatedDependencies);
} catch(err) {
if(options.onErrored) {
options.onErrored({
type: "accept-errored",
moduleId: moduleId,
dependencyId: moduleOutdatedDependencies[i],
error: err
});
if(module) {
moduleOutdatedDependencies = outdatedDependencies[moduleId];
var callbacks = [];
for(i = 0; i < moduleOutdatedDependencies.length; i++) {
dependency = moduleOutdatedDependencies[i];
cb = module.hot._acceptedDependencies[dependency];
if(cb) {
if(callbacks.indexOf(cb) >= 0) continue;
callbacks.push(cb);
}
if(!options.ignoreErrored) {
if(!error)
error = err;
}
for(i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
try {
cb(moduleOutdatedDependencies);
} catch(err) {
if(options.onErrored) {
options.onErrored({
type: "accept-errored",
moduleId: moduleId,
dependencyId: moduleOutdatedDependencies[i],
error: err
});
}
if(!options.ignoreErrored) {
if(!error)
error = err;
}
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions lib/Parser.js
Expand Up @@ -634,14 +634,14 @@ class Parser extends Tapable {
statement.params.forEach(param => {
this.walkPattern(param);
});
this.inScope(statement.params, function() {
this.inScope(statement.params, () => {
if(statement.body.type === "BlockStatement") {
this.prewalkStatement(statement.body);
this.walkStatement(statement.body);
} else {
this.walkExpression(statement.body);
}
}.bind(this));
});
}

prewalkImportDeclaration(statement) {
Expand Down Expand Up @@ -784,10 +784,10 @@ class Parser extends Tapable {
}

walkCatchClause(catchClause) {
this.inScope([catchClause.param], function() {
this.inScope([catchClause.param], () => {
this.prewalkStatement(catchClause.body);
this.walkStatement(catchClause.body);
}.bind(this));
});
}

prewalkVariableDeclarators(declarators) {
Expand Down Expand Up @@ -916,28 +916,28 @@ class Parser extends Tapable {
expression.params.forEach(param => {
this.walkPattern(param);
});
this.inScope(expression.params, function() {
this.inScope(expression.params, () => {
if(expression.body.type === "BlockStatement") {
this.prewalkStatement(expression.body);
this.walkStatement(expression.body);
} else {
this.walkExpression(expression.body);
}
}.bind(this));
});
}

walkArrowFunctionExpression(expression) {
expression.params.forEach(param => {
this.walkPattern(param);
});
this.inScope(expression.params, function() {
this.inScope(expression.params, () => {
if(expression.body.type === "BlockStatement") {
this.prewalkStatement(expression.body);
this.walkStatement(expression.body);
} else {
this.walkExpression(expression.body);
}
}.bind(this));
});
}

walkSequenceExpression(expression) {
Expand Down Expand Up @@ -1059,7 +1059,7 @@ class Parser extends Tapable {
const args = options.map(renameArgOrThis, this);
this.inScope(params.filter(function(identifier, idx) {
return !args[idx];
}), function() {
}), () => {
if(renameThis) {
this.scope.renames.$this = renameThis;
}
Expand All @@ -1074,7 +1074,7 @@ class Parser extends Tapable {
this.walkStatement(functionExpression.body);
} else
this.walkExpression(functionExpression.body);
}.bind(this));
});
}
if(expression.callee.type === "MemberExpression" &&
expression.callee.object.type === "FunctionExpression" &&
Expand Down
12 changes: 6 additions & 6 deletions lib/UmdMainTemplatePlugin.js
Expand Up @@ -41,7 +41,7 @@ class UmdMainTemplatePlugin {

apply(compilation) {
const mainTemplate = compilation.mainTemplate;
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
let externals = chunk.getModules().filter(m => m.external);
const optionalExternals = [];
let requiredExternals = [];
Expand Down Expand Up @@ -172,19 +172,19 @@ class UmdMainTemplatePlugin {
" }\n"
) +
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
}.bind(this));
mainTemplate.plugin("global-hash-paths", function(paths) {
});
mainTemplate.plugin("global-hash-paths", (paths) => {
if(this.names.root) paths = paths.concat(this.names.root);
if(this.names.amd) paths = paths.concat(this.names.amd);
if(this.names.commonjs) paths = paths.concat(this.names.commonjs);
return paths;
}.bind(this));
mainTemplate.plugin("hash", function(hash) {
});
mainTemplate.plugin("hash", (hash) => {
hash.update("umd");
hash.update(`${this.names.root}`);
hash.update(`${this.names.amd}`);
hash.update(`${this.names.commonjs}`);
}.bind(this));
});
}
}

Expand Down
35 changes: 19 additions & 16 deletions lib/optimize/ConcatenatedModule.js
Expand Up @@ -32,17 +32,16 @@ function ensureNsObjSource(info, moduleToInfoMap, requestShortener) {
}
}

function getExternalImport(importedModule, importedVar, exportName, asCall) {
const isHarmonyModule = importedModule && (!importedModule.meta || importedModule.meta.harmonyModule);
if(exportName === true) return importedVar;
function getExternalImport(importedModule, info, exportName, asCall) {
if(exportName === true) return info.name;
const used = importedModule.isUsed(exportName);
if(!used) return "/* unused reexport */undefined";
if(!isHarmonyModule && exportName === "default") {
return asCall ? `${importedVar}_default()` : `${importedVar}_default.a`;
if(info.interop && exportName === "default") {
return asCall ? `${info.interopName}()` : `${info.interopName}.a`;
}
// TODO use Template.toNormalComment when merging with pure-module
const comment = used !== exportName ? ` /* ${exportName} */` : "";
const reference = `${importedVar}[${JSON.stringify(used)}${comment}]`;
const reference = `${info.name}[${JSON.stringify(used)}${comment}]`;
if(asCall)
return `Object(${reference})`;
return reference;
Expand All @@ -67,12 +66,6 @@ function getFinalName(info, exportName, moduleToInfoMap, requestShortener, asCal
if(refInfo) {
// module is in the concatenation
return getFinalName(refInfo, reexport.exportName, moduleToInfoMap, requestShortener, asCall);
} else {
const dep = reexport.dependency;
const importedModule = reexport.module;
const exportName = reexport.exportName;
const importedVar = dep.importedVar;
return getExternalImport(importedModule, importedVar, exportName, asCall);
}
}
const problem = `Cannot get final name for export "${exportName}" in "${info.module.readableIdentifier(requestShortener)}"` +
Expand All @@ -84,7 +77,7 @@ function getFinalName(info, exportName, moduleToInfoMap, requestShortener, asCal
case "external":
{
const importedModule = info.module;
return getExternalImport(importedModule, info.name, exportName, asCall);
return getExternalImport(importedModule, info, exportName, asCall);
}
}
}
Expand Down Expand Up @@ -367,7 +360,9 @@ class ConcatenatedModule extends Module {
type: "external",
module: info.module,
index: idx,
name: undefined
name: undefined,
interopName: undefined,
interop: undefined
};
default:
throw new Error(`Unsupported concatenation entry type ${info.type}`);
Expand Down Expand Up @@ -462,6 +457,8 @@ class ConcatenatedModule extends Module {
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof",
"var", "void", "volatile", "while", "with", "yield",

"module", "__dirname", "__filename", "exports",

"Array", "Date", "eval", "function", "hasOwnProperty", "Infinity", "isFinite", "isNaN",
"isPrototypeOf", "length", "Math", "NaN", "name", "Number", "Object", "prototype", "String",
"toString", "undefined", "valueOf",
Expand Down Expand Up @@ -535,9 +532,15 @@ class ConcatenatedModule extends Module {
}
case "external":
{
info.interop = info.module.meta && !info.module.meta.harmonyModule;
const externalName = this.findNewName("", allUsedNames, null, info.module.readableIdentifier(requestShortener));
allUsedNames.add(externalName);
info.name = externalName;
if(info.interop) {
const externalNameInterop = this.findNewName("default", allUsedNames, null, info.module.readableIdentifier(requestShortener));
allUsedNames.add(externalNameInterop);
info.interopName = externalNameInterop;
}
break;
}
}
Expand Down Expand Up @@ -592,8 +595,8 @@ class ConcatenatedModule extends Module {
case "external":
result.add(`\n// EXTERNAL MODULE: ${info.module.readableIdentifier(requestShortener)}\n`);
result.add(`var ${info.name} = __webpack_require__(${JSON.stringify(info.module.id)});\n`);
if(info.module.meta && !info.module.meta.harmonyModule) {
result.add(`var ${info.name}_default = /*#__PURE__*/__webpack_require__.n(${info.name});\n`);
if(info.interop) {
result.add(`var ${info.interopName} = /*#__PURE__*/__webpack_require__.n(${info.name});\n`);
}
break;
default:
Expand Down

0 comments on commit 7a36951

Please sign in to comment.