2014-06-03 14:45:26 +08:00
|
|
|
/*
|
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-21 18:35:09 +08:00
|
|
|
"use strict";
|
2014-06-03 14:45:26 +08:00
|
|
|
|
2018-03-22 19:05:58 +08:00
|
|
|
const { ConcatSource } = require("webpack-sources");
|
2018-11-17 01:11:51 +08:00
|
|
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
2017-08-02 21:39:43 +08:00
|
|
|
const Template = require("./Template");
|
2014-06-03 14:45:26 +08:00
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
|
|
|
|
|
|
2019-01-05 21:23:59 +08:00
|
|
|
const joinIterableWithComma = iterable => {
|
|
|
|
|
// This is more performant than Array.from().join(", ")
|
|
|
|
|
// as it doesn't create an array
|
|
|
|
|
let str = "";
|
|
|
|
|
let first = true;
|
|
|
|
|
for (const item of iterable) {
|
|
|
|
|
if (first) {
|
|
|
|
|
first = false;
|
|
|
|
|
} else {
|
|
|
|
|
str += ", ";
|
|
|
|
|
}
|
2019-05-21 15:44:06 +08:00
|
|
|
str += item;
|
2019-01-05 21:23:59 +08:00
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-14 19:06:59 +08:00
|
|
|
const printExportsInfoToSource = (source, indent, exportsInfo) => {
|
|
|
|
|
let hasExports = false;
|
|
|
|
|
for (const exportInfo of exportsInfo.orderedExports) {
|
|
|
|
|
source.add(
|
|
|
|
|
Template.toComment(
|
|
|
|
|
`${indent}export ${
|
|
|
|
|
exportInfo.name
|
|
|
|
|
} [${exportInfo.getProvidedInfo()}] [${exportInfo.getUsedInfo()}] [${exportInfo.getRenameInfo()}]`
|
|
|
|
|
) + "\n"
|
|
|
|
|
);
|
|
|
|
|
if (exportInfo.exportsInfo) {
|
|
|
|
|
printExportsInfoToSource(source, indent + " ", exportInfo.exportsInfo);
|
|
|
|
|
}
|
|
|
|
|
hasExports = true;
|
|
|
|
|
}
|
|
|
|
|
const otherExportsInfo = exportsInfo.otherExportsInfo;
|
|
|
|
|
if (otherExportsInfo.provided !== false || otherExportsInfo.used !== false) {
|
|
|
|
|
const title = hasExports ? "other exports" : "exports";
|
|
|
|
|
source.add(
|
|
|
|
|
Template.toComment(
|
|
|
|
|
`${indent}${title} [${otherExportsInfo.getProvidedInfo()}] [${otherExportsInfo.getUsedInfo()}]`
|
|
|
|
|
) + "\n"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-21 18:35:09 +08:00
|
|
|
class FunctionModuleTemplatePlugin {
|
2018-11-17 01:11:51 +08:00
|
|
|
constructor({ compilation }) {
|
|
|
|
|
this.compilation = compilation;
|
2018-08-06 16:42:41 +08:00
|
|
|
}
|
|
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
/**
|
|
|
|
|
* @param {ModuleTemplate} moduleTemplate a module template
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2017-02-21 18:35:09 +08:00
|
|
|
apply(moduleTemplate) {
|
2018-02-25 09:00:20 +08:00
|
|
|
moduleTemplate.hooks.render.tap(
|
|
|
|
|
"FunctionModuleTemplatePlugin",
|
|
|
|
|
(moduleSource, module) => {
|
2018-11-17 01:11:51 +08:00
|
|
|
const { chunkGraph } = this.compilation;
|
2018-02-25 09:00:20 +08:00
|
|
|
const source = new ConcatSource();
|
2018-11-17 01:18:44 +08:00
|
|
|
const args = [];
|
2018-11-17 01:11:51 +08:00
|
|
|
const runtimeRequirements = chunkGraph.getModuleRuntimeRequirements(
|
|
|
|
|
module
|
|
|
|
|
);
|
|
|
|
|
const needModule = runtimeRequirements.has(RuntimeGlobals.module);
|
|
|
|
|
const needExports = runtimeRequirements.has(RuntimeGlobals.exports);
|
|
|
|
|
const needRequire = runtimeRequirements.has(RuntimeGlobals.require);
|
|
|
|
|
if (needExports || needRequire || needModule)
|
2018-11-17 01:18:44 +08:00
|
|
|
args.push(
|
|
|
|
|
needModule
|
|
|
|
|
? module.moduleArgument
|
|
|
|
|
: "__unused" + module.moduleArgument
|
|
|
|
|
);
|
|
|
|
|
if (needExports || needRequire)
|
|
|
|
|
args.push(
|
|
|
|
|
needExports
|
|
|
|
|
? module.exportsArgument
|
|
|
|
|
: "__unused" + module.exportsArgument
|
|
|
|
|
);
|
2018-11-17 01:11:51 +08:00
|
|
|
if (needRequire) args.push("__webpack_require__");
|
2018-02-25 09:00:20 +08:00
|
|
|
source.add("/***/ (function(" + args.join(", ") + ") {\n\n");
|
|
|
|
|
if (module.buildInfo.strict) source.add('"use strict";\n');
|
|
|
|
|
source.add(moduleSource);
|
|
|
|
|
source.add("\n\n/***/ })");
|
|
|
|
|
return source;
|
2017-02-21 18:35:09 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-21 18:35:09 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
moduleTemplate.hooks.package.tap(
|
|
|
|
|
"FunctionModuleTemplatePlugin",
|
2018-11-17 01:18:44 +08:00
|
|
|
(moduleSource, module, { moduleGraph, chunkGraph }) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (moduleTemplate.runtimeTemplate.outputOptions.pathinfo) {
|
|
|
|
|
const source = new ConcatSource();
|
|
|
|
|
const req = module.readableIdentifier(
|
|
|
|
|
moduleTemplate.runtimeTemplate.requestShortener
|
|
|
|
|
);
|
2019-01-05 21:23:59 +08:00
|
|
|
const reqStr = req.replace(/\*\//g, "*_/");
|
|
|
|
|
const reqStrStar = "*".repeat(reqStr.length);
|
|
|
|
|
source.add("/*!****" + reqStrStar + "****!*\\\n");
|
|
|
|
|
source.add(" !*** " + reqStr + " ***!\n");
|
|
|
|
|
source.add(" \\****" + reqStrStar + "****/\n");
|
2019-01-23 17:05:32 +08:00
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(module);
|
2019-03-14 19:06:59 +08:00
|
|
|
printExportsInfoToSource(source, "", exportsInfo);
|
2018-11-17 01:18:44 +08:00
|
|
|
source.add(
|
|
|
|
|
Template.toComment(
|
2019-01-05 21:23:59 +08:00
|
|
|
`runtime requirements: ${joinIterableWithComma(
|
2018-11-17 01:18:44 +08:00
|
|
|
chunkGraph.getModuleRuntimeRequirements(module)
|
2019-01-05 21:23:59 +08:00
|
|
|
)}`
|
2018-11-17 01:18:44 +08:00
|
|
|
) + "\n"
|
|
|
|
|
);
|
2018-11-17 01:11:51 +08:00
|
|
|
const optimizationBailout = moduleGraph.getOptimizationBailout(
|
2018-08-16 19:55:41 +08:00
|
|
|
module
|
2018-08-06 16:42:41 +08:00
|
|
|
);
|
|
|
|
|
if (optimizationBailout) {
|
|
|
|
|
for (const text of optimizationBailout) {
|
2018-02-25 09:00:20 +08:00
|
|
|
let code;
|
|
|
|
|
if (typeof text === "function") {
|
|
|
|
|
code = text(moduleTemplate.runtimeTemplate.requestShortener);
|
|
|
|
|
} else {
|
|
|
|
|
code = text;
|
|
|
|
|
}
|
|
|
|
|
source.add(Template.toComment(`${code}`) + "\n");
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
source.add(moduleSource);
|
|
|
|
|
return source;
|
2017-05-28 21:25:07 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
return moduleSource;
|
2017-02-21 18:35:09 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-21 18:35:09 +08:00
|
|
|
|
2017-12-14 04:35:39 +08:00
|
|
|
moduleTemplate.hooks.hash.tap("FunctionModuleTemplatePlugin", hash => {
|
2017-02-21 18:35:09 +08:00
|
|
|
hash.update("FunctionModuleTemplatePlugin");
|
|
|
|
|
hash.update("2");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
module.exports = FunctionModuleTemplatePlugin;
|