mirror of https://github.com/webpack/webpack.git
commit
19d60ba0cd
|
|
@ -50,7 +50,9 @@ const getSourceForCommonJsExternal = moduleAndSpecifiers => {
|
|||
.slice(1)
|
||||
.map(r => `[${JSON.stringify(r)}]`)
|
||||
.join("");
|
||||
return `module.exports = require(${moduleName})${objectLookup};`;
|
||||
return `module.exports = require(${JSON.stringify(
|
||||
moduleName
|
||||
)})${objectLookup};`;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -68,7 +70,7 @@ const checkExternalVariable = (variableName, request, runtimeTemplate) => {
|
|||
/**
|
||||
* @param {string|number} id the module id
|
||||
* @param {boolean} optional true, if the module is optional
|
||||
* @param {string} request the request path
|
||||
* @param {string|string[]} request the request path
|
||||
* @param {RuntimeTemplate} runtimeTemplate the runtime template
|
||||
* @returns {string} the generated source
|
||||
*/
|
||||
|
|
@ -82,22 +84,36 @@ const getSourceForAmdOrUmdExternal = (
|
|||
`${id}`
|
||||
)}__`;
|
||||
const missingModuleError = optional
|
||||
? checkExternalVariable(externalVariable, request, runtimeTemplate)
|
||||
? checkExternalVariable(
|
||||
externalVariable,
|
||||
Array.isArray(request) ? request.join(".") : request,
|
||||
runtimeTemplate
|
||||
)
|
||||
: "";
|
||||
return `${missingModuleError}module.exports = ${externalVariable};`;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {boolean} optional true, if the module is optional
|
||||
* @param {string} request the request path
|
||||
* @param {string|string[]} request the request path
|
||||
* @param {RuntimeTemplate} runtimeTemplate the runtime template
|
||||
* @returns {string} the generated source
|
||||
*/
|
||||
const getSourceForDefaultCase = (optional, request, runtimeTemplate) => {
|
||||
if (!Array.isArray(request)) {
|
||||
// make it an array as the look up works the same basically
|
||||
request = [request];
|
||||
}
|
||||
|
||||
const variableName = request[0];
|
||||
const missingModuleError = optional
|
||||
? checkExternalVariable(request, request, runtimeTemplate)
|
||||
? checkExternalVariable(variableName, request.join("."), runtimeTemplate)
|
||||
: "";
|
||||
return `${missingModuleError}module.exports = ${request};`;
|
||||
const objectLookup = request
|
||||
.slice(1)
|
||||
.map(r => `[${JSON.stringify(r)}]`)
|
||||
.join("");
|
||||
return `${missingModuleError}module.exports = ${variableName}${objectLookup};`;
|
||||
};
|
||||
|
||||
class ExternalModule extends Module {
|
||||
|
|
@ -172,7 +188,7 @@ class ExternalModule extends Module {
|
|||
|
||||
getSourceString(runtimeTemplate, moduleGraph, chunkGraph) {
|
||||
const request =
|
||||
typeof this.request === "object"
|
||||
typeof this.request === "object" && !Array.isArray(this.request)
|
||||
? this.request[this.externalType]
|
||||
: this.request;
|
||||
switch (this.externalType) {
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ class SourceMapDevToolPlugin {
|
|||
const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
|
||||
const requestShortener = compiler.requestShortener;
|
||||
const options = this.options;
|
||||
options.test = options.test || /\.(js|css)($|\?)/i;
|
||||
options.test = options.test || /\.(m?js|css)($|\?)/i;
|
||||
|
||||
const matchObject = ModuleFilenameHelpers.matchObject.bind(
|
||||
undefined,
|
||||
|
|
|
|||
|
|
@ -320,7 +320,11 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
|
|||
this.set("resolve.extensions", [".wasm", ".mjs", ".js", ".json"]);
|
||||
this.set("resolve.mainFiles", ["index"]);
|
||||
this.set("resolve.aliasFields", "make", options => {
|
||||
if (options.target === "web" || options.target === "webworker") {
|
||||
if (
|
||||
options.target === "web" ||
|
||||
options.target === "webworker" ||
|
||||
options.target === "electron-renderer"
|
||||
) {
|
||||
return ["browser"];
|
||||
} else {
|
||||
return [];
|
||||
|
|
|
|||
|
|
@ -54,9 +54,24 @@ module.exports = class HarmonyExportDependencyParserPlugin {
|
|||
parser.hooks.exportExpression.tap(
|
||||
"HarmonyExportDependencyParserPlugin",
|
||||
(statement, expr) => {
|
||||
const comments = parser.getComments([
|
||||
statement.range[0],
|
||||
expr.range[0]
|
||||
]);
|
||||
const dep = new HarmonyExportExpressionDependency(
|
||||
expr.range,
|
||||
statement.range
|
||||
statement.range,
|
||||
comments
|
||||
.map(c => {
|
||||
switch (c.type) {
|
||||
case "Block":
|
||||
return `/*${c.value}*/`;
|
||||
case "Line":
|
||||
return `//${c.value}\n`;
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.join("")
|
||||
);
|
||||
dep.loc = Object.create(statement.loc);
|
||||
dep.loc.index = -1;
|
||||
|
|
|
|||
|
|
@ -14,10 +14,11 @@ const NullDependency = require("./NullDependency");
|
|||
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
||||
|
||||
class HarmonyExportExpressionDependency extends NullDependency {
|
||||
constructor(range, rangeStatement) {
|
||||
constructor(range, rangeStatement, prefix) {
|
||||
super();
|
||||
this.range = range;
|
||||
this.rangeStatement = rangeStatement;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
get type() {
|
||||
|
|
@ -50,7 +51,11 @@ HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTempla
|
|||
const content = this.getContent(module, used);
|
||||
|
||||
if (dep.range) {
|
||||
source.replace(dep.rangeStatement[0], dep.range[0] - 1, content + "(");
|
||||
source.replace(
|
||||
dep.rangeStatement[0],
|
||||
dep.range[0] - 1,
|
||||
content + "(" + dep.prefix
|
||||
);
|
||||
source.replace(dep.range[1], dep.rangeStatement[1] - 1, ");");
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ module.exports = class NodeMainTemplatePlugin {
|
|||
"var promise = new Promise(function(resolve, reject) {",
|
||||
Template.indent([
|
||||
"installedChunkData = installedChunks[chunkId] = [resolve, reject];",
|
||||
"var filename = __dirname + " +
|
||||
"var filename = require('path').join(__dirname, " +
|
||||
mainTemplate.getAssetPath(
|
||||
JSON.stringify(`/${chunkFilename}`),
|
||||
{
|
||||
|
|
@ -164,7 +164,7 @@ module.exports = class NodeMainTemplatePlugin {
|
|||
contentHashType: "javascript"
|
||||
}
|
||||
) +
|
||||
";",
|
||||
");",
|
||||
"require('fs').readFile(filename, 'utf-8', function(err, content) {",
|
||||
Template.indent(
|
||||
[
|
||||
|
|
|
|||
|
|
@ -123,6 +123,9 @@
|
|||
{
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/common.arrayOfStringValues"
|
||||
},
|
||||
{
|
||||
"type": "boolean"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,12 +85,13 @@ describe("ConfigTestCases", () => {
|
|||
});
|
||||
let testConfig = {
|
||||
findBundle: function(i, options) {
|
||||
const ext = path.extname(options.output.filename);
|
||||
if (
|
||||
fs.existsSync(
|
||||
path.join(options.output.path, "bundle" + i + ".js")
|
||||
path.join(options.output.path, "bundle" + i + ext)
|
||||
)
|
||||
) {
|
||||
return "./bundle" + i + ".js";
|
||||
return "./bundle" + i + ext;
|
||||
}
|
||||
},
|
||||
timeout: 30000
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
import { value as v1 } from "./module1";
|
||||
import { value as v2 } from "./module2";
|
||||
import { value as v3 } from "./module3";
|
||||
import { value as v4 } from "./module4";
|
||||
|
||||
it("should not execute exports when annotated with pure comment", () => {
|
||||
expect(v1).toBe(42);
|
||||
expect(v2).toBe(42);
|
||||
expect(v3).toBe(42);
|
||||
expect(v4).toBe(42);
|
||||
});
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
let value = 42;
|
||||
|
||||
const inc = () => {
|
||||
value++;
|
||||
};
|
||||
|
||||
export default /*#__PURE__*/inc();
|
||||
|
||||
export { value };
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
let value = 42;
|
||||
|
||||
const inc = () => {
|
||||
value++;
|
||||
};
|
||||
|
||||
export default (/*#__PURE__*/inc());
|
||||
|
||||
export { value };
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
let value = 42;
|
||||
|
||||
const inc = () => {
|
||||
value++;
|
||||
};
|
||||
|
||||
export default /*#__PURE__*/(inc());
|
||||
|
||||
export { value };
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
let value = 42;
|
||||
|
||||
const inc = () => {
|
||||
value++;
|
||||
};
|
||||
|
||||
export
|
||||
// hello
|
||||
default
|
||||
// world
|
||||
/*#__PURE__*/
|
||||
inc()
|
||||
;
|
||||
|
||||
export { value };
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
module.exports = {
|
||||
mode: "production",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
concatenateModules: false
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
it("should not fail on optional externals", function() {
|
||||
const external = require("external");
|
||||
expect(external).toBe(EXPECTED);
|
||||
});
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
const webpack = require("../../../../");
|
||||
module.exports = [
|
||||
{
|
||||
output: {
|
||||
libraryTarget: "commonjs2"
|
||||
},
|
||||
externals: {
|
||||
external: ["webpack", "version"]
|
||||
},
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
EXPECTED: JSON.stringify(webpack.version)
|
||||
})
|
||||
]
|
||||
},
|
||||
{
|
||||
externals: {
|
||||
external: ["Array", "isArray"]
|
||||
},
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
EXPECTED: "Array.isArray"
|
||||
})
|
||||
]
|
||||
}
|
||||
];
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
it("creates source maps for .css output files by default", function() {
|
||||
var fs = require("fs");
|
||||
var source = fs.readFileSync(__filename, "utf-8");
|
||||
var match = /sourceMappingURL\s*=\s*(.*)\*\//.exec(source);
|
||||
expect(match[1]).toBe("bundle0.css.map");
|
||||
});
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var foo = {};
|
||||
|
||||
module.exports = foo;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
module.exports = {
|
||||
mode: "development",
|
||||
output: {
|
||||
filename: "bundle0.css"
|
||||
},
|
||||
node: {
|
||||
__dirname: false,
|
||||
__filename: false
|
||||
},
|
||||
devtool: "source-map"
|
||||
};
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
it("creates source maps for .js output files by default", function() {
|
||||
var fs = require("fs");
|
||||
var source = fs.readFileSync(__filename, "utf-8");
|
||||
var match = /sourceMappingURL\s*=\s*(.*)/.exec(source);
|
||||
expect(match[1]).toBe("bundle0.js.map");
|
||||
});
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var foo = {};
|
||||
|
||||
module.exports = foo;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
module.exports = {
|
||||
mode: "development",
|
||||
output: {
|
||||
filename: "bundle0.js"
|
||||
},
|
||||
node: {
|
||||
__dirname: false,
|
||||
__filename: false
|
||||
},
|
||||
devtool: "source-map"
|
||||
};
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
it("creates source maps for .mjs output files by default", function() {
|
||||
var fs = require("fs");
|
||||
var source = fs.readFileSync(__filename, "utf-8");
|
||||
var match = /sourceMappingURL\s*=\s*(.*)/.exec(source);
|
||||
expect(match[1]).toBe("bundle0.mjs.map");
|
||||
});
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var foo = {};
|
||||
|
||||
module.exports = foo;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
module.exports = {
|
||||
mode: "development",
|
||||
output: {
|
||||
filename: "bundle0.mjs"
|
||||
},
|
||||
node: {
|
||||
__dirname: false,
|
||||
__filename: false
|
||||
},
|
||||
devtool: "source-map"
|
||||
};
|
||||
Loading…
Reference in New Issue