Merge pull request #10070 from vankop/lazy-require

tool for loading CommonJS modules lazily
This commit is contained in:
Tobias Koppers 2019-12-04 09:15:16 +01:00 committed by GitHub
commit da9fb528c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 63 deletions

View File

@ -7,17 +7,24 @@
const validateOptions = require("schema-utils");
const { compareModulesByIdentifier } = require("../util/comparators");
const memorize = require("../util/memorize");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
let generatorSchema;
let parserSchema;
let AssetGenerator;
let AssetParser;
let AssetSourceGenerator;
const getGeneratorSchema = memorize(() =>
require("../../schemas/plugins/AssetModulesPluginGenerator.json")
);
const getParserSchema = memorize(() =>
require("../../schemas/plugins/AssetModulesPluginParser.json")
);
const getAssetGenerator = memorize(() => require("./AssetGenerator"));
const getAssetParser = memorize(() => require("./AssetParser"));
const getAssetSourceGenerator = memorize(() =>
require("./AssetSourceGenerator")
);
const type = "asset";
const plugin = "AssetModulesPlugin";
@ -34,10 +41,7 @@ class AssetModulesPlugin {
normalModuleFactory.hooks.createParser
.for("asset")
.tap(plugin, parserOptions => {
if (parserSchema === undefined) {
parserSchema = require("../../schemas/plugins/AssetModulesPluginParser.json");
}
validateOptions(parserSchema, parserOptions, {
validateOptions(getParserSchema(), parserOptions, {
name: "Asset Modules Plugin",
baseDataPath: "parser"
});
@ -50,33 +54,29 @@ class AssetModulesPlugin {
};
}
if (AssetParser === undefined) {
AssetParser = require("./AssetParser");
}
const AssetParser = getAssetParser();
return new AssetParser(dataUrlCondition);
});
normalModuleFactory.hooks.createParser
.for("asset/inline")
.tap(plugin, parserOptions => {
if (AssetParser === undefined) {
AssetParser = require("./AssetParser");
}
const AssetParser = getAssetParser();
return new AssetParser(true);
});
normalModuleFactory.hooks.createParser
.for("asset/resource")
.tap(plugin, parserOptions => {
if (AssetParser === undefined) {
AssetParser = require("./AssetParser");
}
const AssetParser = getAssetParser();
return new AssetParser(false);
});
normalModuleFactory.hooks.createParser
.for("asset/source")
.tap(plugin, parserOptions => {
if (AssetParser === undefined) {
AssetParser = require("./AssetParser");
}
const AssetParser = getAssetParser();
return new AssetParser(false);
});
@ -85,10 +85,7 @@ class AssetModulesPlugin {
.for(type)
// eslint-disable-next-line no-loop-func
.tap(plugin, generatorOptions => {
if (generatorSchema === undefined) {
generatorSchema = require("../../schemas/plugins/AssetModulesPluginGenerator.json");
}
validateOptions(generatorSchema, generatorOptions, {
validateOptions(getGeneratorSchema(), generatorOptions, {
name: "Asset Modules Plugin",
baseDataPath: "generator"
});
@ -102,26 +99,23 @@ class AssetModulesPlugin {
};
}
if (AssetGenerator === undefined) {
AssetGenerator = require("./AssetGenerator");
}
const AssetGenerator = getAssetGenerator();
return new AssetGenerator(compilation, dataUrl);
});
}
normalModuleFactory.hooks.createGenerator
.for("asset/resource")
.tap(plugin, () => {
if (AssetGenerator === undefined) {
AssetGenerator = require("./AssetGenerator");
}
const AssetGenerator = getAssetGenerator();
return new AssetGenerator(compilation);
});
normalModuleFactory.hooks.createGenerator
.for("asset/source")
.tap(plugin, () => {
if (AssetSourceGenerator === undefined) {
AssetSourceGenerator = require("./AssetSourceGenerator");
}
const AssetSourceGenerator = getAssetSourceGenerator();
return new AssetSourceGenerator();
});

View File

@ -6,12 +6,15 @@
"use strict";
const validateOptions = require("schema-utils");
const memorize = require("../util/memorize");
const JsonGenerator = require("./JsonGenerator");
const JsonParser = require("./JsonParser");
/** @typedef {import("../Compiler")} Compiler */
let parserSchema;
const getParserSchema = memorize(() =>
require("../../schemas/plugins/JsonModulesPluginParser.json")
);
class JsonModulesPlugin {
/**
@ -26,10 +29,7 @@ class JsonModulesPlugin {
normalModuleFactory.hooks.createParser
.for("json")
.tap("JsonModulesPlugin", parserOptions => {
if (parserSchema === undefined) {
parserSchema = require("../../schemas/plugins/JsonModulesPluginParser.json");
}
validateOptions(parserSchema, parserOptions, {
validateOptions(getParserSchema(), parserOptions, {
name: "Json Modules Plugin",
baseDataPath: "parser"
});

View File

@ -11,6 +11,7 @@ const Generator = require("../Generator");
const { tryRunOrWebpackError } = require("../HookWebpackError");
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
const { compareModulesByIdentifier } = require("../util/comparators");
const memorize = require("../util/memorize");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Compilation")} Compilation */
@ -20,9 +21,15 @@ const { compareModulesByIdentifier } = require("../util/comparators");
/** @typedef {import("../Template").RenderManifestEntry} RenderManifestEntry */
/** @typedef {import("../Template").RenderManifestOptions} RenderManifestOptions */
let AsyncWebAssemblyGenerator;
let AsyncWebAssemblyJavascriptGenerator;
let AsyncWebAssemblyParser;
const getAsyncWebAssemblyGenerator = memorize(() =>
require("./AsyncWebAssemblyGenerator")
);
const getAsyncWebAssemblyJavascriptGenerator = memorize(() =>
require("./AsyncWebAssemblyJavascriptGenerator")
);
const getAsyncWebAssemblyParser = memorize(() =>
require("./AsyncWebAssemblyParser")
);
/**
* @typedef {Object} RenderContext
@ -90,20 +97,16 @@ class AsyncWebAssemblyModulesPlugin {
normalModuleFactory.hooks.createParser
.for("webassembly/async")
.tap("AsyncWebAssemblyModulesPlugin", () => {
if (AsyncWebAssemblyParser === undefined) {
AsyncWebAssemblyParser = require("./AsyncWebAssemblyParser");
}
const AsyncWebAssemblyParser = getAsyncWebAssemblyParser();
return new AsyncWebAssemblyParser();
});
normalModuleFactory.hooks.createGenerator
.for("webassembly/async")
.tap("AsyncWebAssemblyModulesPlugin", () => {
if (AsyncWebAssemblyGenerator === undefined) {
AsyncWebAssemblyGenerator = require("./AsyncWebAssemblyGenerator");
}
if (AsyncWebAssemblyJavascriptGenerator === undefined) {
AsyncWebAssemblyJavascriptGenerator = require("./AsyncWebAssemblyJavascriptGenerator");
}
const AsyncWebAssemblyJavascriptGenerator = getAsyncWebAssemblyJavascriptGenerator();
const AsyncWebAssemblyGenerator = getAsyncWebAssemblyGenerator();
return Generator.byType({
javascript: new AsyncWebAssemblyJavascriptGenerator(
compilation.outputOptions.webassemblyModuleFilename

View File

@ -9,6 +9,7 @@ const Generator = require("../Generator");
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
const { compareModulesByIdentifier } = require("../util/comparators");
const memorize = require("../util/memorize");
const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
/** @typedef {import("webpack-sources").Source} Source */
@ -17,9 +18,13 @@ const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError
/** @typedef {import("../ModuleTemplate")} ModuleTemplate */
/** @typedef {import("../ModuleTemplate").RenderContext} RenderContext */
let WebAssemblyGenerator;
let WebAssemblyJavascriptGenerator;
let WebAssemblyParser;
const getWebAssemblyGenerator = memorize(() =>
require("./WebAssemblyGenerator")
);
const getWebAssemblyJavascriptGenerator = memorize(() =>
require("./WebAssemblyJavascriptGenerator")
);
const getWebAssemblyParser = memorize(() => require("./WebAssemblyParser"));
class WebAssemblyModulesPlugin {
constructor(options) {
@ -47,21 +52,17 @@ class WebAssemblyModulesPlugin {
normalModuleFactory.hooks.createParser
.for("webassembly/sync")
.tap("WebAssemblyModulesPlugin", () => {
if (WebAssemblyParser === undefined) {
WebAssemblyParser = require("./WebAssemblyParser");
}
const WebAssemblyParser = getWebAssemblyParser();
return new WebAssemblyParser();
});
normalModuleFactory.hooks.createGenerator
.for("webassembly/sync")
.tap("WebAssemblyModulesPlugin", () => {
if (WebAssemblyGenerator === undefined) {
WebAssemblyGenerator = require("./WebAssemblyGenerator");
}
if (WebAssemblyJavascriptGenerator === undefined) {
WebAssemblyJavascriptGenerator = require("./WebAssemblyJavascriptGenerator");
}
const WebAssemblyJavascriptGenerator = getWebAssemblyJavascriptGenerator();
const WebAssemblyGenerator = getWebAssemblyGenerator();
return Generator.byType({
javascript: new WebAssemblyJavascriptGenerator(),
webassembly: new WebAssemblyGenerator(this.options)