use types from schema

This commit is contained in:
Tobias Koppers 2018-09-19 13:05:22 +02:00
parent b6e81cd00e
commit d48975c948
11 changed files with 85 additions and 34 deletions

View File

@ -23,7 +23,7 @@ module.exports = {
yoda: "error", yoda: "error",
eqeqeq: "error", eqeqeq: "error",
"global-require": "off", "global-require": "off",
"brace-style": "error", "brace-style": "off",
"eol-last": "error", "eol-last": "error",
"no-extra-bind": "warn", "no-extra-bind": "warn",
"no-process-exit": "warn", "no-process-exit": "warn",

View File

@ -12,10 +12,10 @@ const Template = require("./Template");
class AmdMainTemplatePlugin { class AmdMainTemplatePlugin {
/** /**
* @param {string} name the library name * @param {string=} name the library name
*/ */
constructor(name) { constructor(name) {
/** @type {string} */ /** @type {string=} */
this.name = name; this.name = name;
} }

View File

@ -402,7 +402,8 @@ class Compilation extends Tapable {
this.inputFileSystem = compiler.inputFileSystem; this.inputFileSystem = compiler.inputFileSystem;
this.requestShortener = compiler.requestShortener; this.requestShortener = compiler.requestShortener;
const options = (this.options = compiler.options); const options = compiler.options;
this.options = options;
this.outputOptions = options && options.output; this.outputOptions = options && options.output;
/** @type {boolean=} */ /** @type {boolean=} */
this.bail = options && options.bail; this.bail = options && options.bail;

View File

@ -27,6 +27,9 @@ const RequestShortener = require("./RequestShortener");
const { makePathsRelative } = require("./util/identifier"); const { makePathsRelative } = require("./util/identifier");
const ConcurrentCompilationError = require("./ConcurrentCompilationError"); const ConcurrentCompilationError = require("./ConcurrentCompilationError");
/** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/** /**
* @typedef {Object} CompilationParams * @typedef {Object} CompilationParams
* @property {NormalModuleFactory} normalModuleFactory * @property {NormalModuleFactory} normalModuleFactory
@ -34,16 +37,6 @@ const ConcurrentCompilationError = require("./ConcurrentCompilationError");
* @property {Set<string>} compilationDependencies * @property {Set<string>} compilationDependencies
*/ */
/** @typedef {string|string[]} EntryValues */
/** @typedef {Record<string, EntryValues>} EntryOptionValues */
/**
* @callback EntryOptionValuesFunction
* @returns {EntryOptionValues | EntryValues} the computed value
*/
/** @typedef {EntryOptionValuesFunction | EntryOptionValues | EntryValues} EntryOptions */
class Compiler extends Tapable { class Compiler extends Tapable {
constructor(context) { constructor(context) {
super(); super();
@ -100,7 +93,7 @@ class Compiler extends Tapable {
afterPlugins: new SyncHook(["compiler"]), afterPlugins: new SyncHook(["compiler"]),
/** @type {SyncHook<Compiler>} */ /** @type {SyncHook<Compiler>} */
afterResolvers: new SyncHook(["compiler"]), afterResolvers: new SyncHook(["compiler"]),
/** @type {SyncBailHook<string, EntryOptions>} */ /** @type {SyncBailHook<string, Entry>} */
entryOption: new SyncBailHook(["context", "entry"]) entryOption: new SyncBailHook(["context", "entry"])
}; };
@ -182,7 +175,8 @@ class Compiler extends Tapable {
} }
}; };
this.options = {}; /** @type {WebpackOptions} */
this.options = /** @type {WebpackOptions} */ ({});
this.context = context; this.context = context;

View File

@ -10,13 +10,14 @@ const MultiModuleFactory = require("./MultiModuleFactory");
const MultiEntryPlugin = require("./MultiEntryPlugin"); const MultiEntryPlugin = require("./MultiEntryPlugin");
const SingleEntryPlugin = require("./SingleEntryPlugin"); const SingleEntryPlugin = require("./SingleEntryPlugin");
/** @typedef {import("../declarations/WebpackOptions").EntryDynamic} EntryDynamic */
/** @typedef {import("../declarations/WebpackOptions").EntryStatic} EntryStatic */
/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Compiler").EntryOptionValuesFunction} EntryOptionValuesFunction */
class DynamicEntryPlugin { class DynamicEntryPlugin {
/** /**
* @param {string} context the context path * @param {string} context the context path
* @param {EntryOptionValuesFunction} entry the entry value * @param {EntryDynamic} entry the entry value
*/ */
constructor(context, entry) { constructor(context, entry) {
this.context = context; this.context = context;
@ -50,7 +51,7 @@ class DynamicEntryPlugin {
/** /**
* @param {string|string[]} entry entry value or array of entry values * @param {string|string[]} entry entry value or array of entry values
* @param {string} name name of entry * @param {string} name name of entry
* @returns {Promise<any>} returns the promise resolving the Compilation#addEntry function * @returns {Promise<EntryStatic>} returns the promise resolving the Compilation#addEntry function
*/ */
const addEntry = (entry, name) => { const addEntry = (entry, name) => {
const dep = DynamicEntryPlugin.createDependency(entry, name); const dep = DynamicEntryPlugin.createDependency(entry, name);

View File

@ -8,11 +8,12 @@ const SingleEntryPlugin = require("./SingleEntryPlugin");
const MultiEntryPlugin = require("./MultiEntryPlugin"); const MultiEntryPlugin = require("./MultiEntryPlugin");
const DynamicEntryPlugin = require("./DynamicEntryPlugin"); const DynamicEntryPlugin = require("./DynamicEntryPlugin");
/** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler")} Compiler */
/** /**
* @param {string} context context path * @param {string} context context path
* @param {string | string[]} item entry array or single path * @param {EntryItem} item entry array or single path
* @param {string} name entry key name * @param {string} name entry key name
* @returns {SingleEntryPlugin | MultiEntryPlugin} returns either a single or multi entry plugin * @returns {SingleEntryPlugin | MultiEntryPlugin} returns either a single or multi entry plugin
*/ */

View File

@ -6,6 +6,7 @@
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin"); const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
/** @typedef {import("../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */
/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler")} Compiler */
/** /**
@ -18,12 +19,17 @@ const accessorToObjectAccess = accessor => {
/** /**
* @param {string=} base the path prefix * @param {string=} base the path prefix
* @param {string|string[]} accessor the accessor * @param {string|string[]|LibraryCustomUmdObject} accessor the accessor
* @param {"amd" | "commonjs" | "root"} umdProperty property used when a custom umd object is provided
* @param {string=} joinWith the element separator * @param {string=} joinWith the element separator
* @returns {string} the path * @returns {string} the path
*/ */
const accessorAccess = (base, accessor, joinWith = "; ") => { const accessorAccess = (base, accessor, umdProperty, joinWith = "; ") => {
const accessors = Array.isArray(accessor) ? accessor : [accessor]; const normalizedAccessor =
typeof accessor === "object" ? accessor[umdProperty] : accessor;
const accessors = Array.isArray(normalizedAccessor)
? normalizedAccessor
: [normalizedAccessor];
return accessors return accessors
.map((_, idx) => { .map((_, idx) => {
const a = base const a = base
@ -40,7 +46,7 @@ const accessorAccess = (base, accessor, joinWith = "; ") => {
class LibraryTemplatePlugin { class LibraryTemplatePlugin {
/** /**
* @param {string} name name of library * @param {string|string[]|LibraryCustomUmdObject} name name of library
* @param {string} target type of library * @param {string} target type of library
* @param {boolean} umdNamedDefine setting this to true will name the UMD module * @param {boolean} umdNamedDefine setting this to true will name the UMD module
* @param {string|TODO} auxiliaryComment comment in the UMD wrapper * @param {string|TODO} auxiliaryComment comment in the UMD wrapper
@ -68,14 +74,22 @@ class LibraryTemplatePlugin {
} }
switch (this.target) { switch (this.target) {
case "var": case "var":
if (
!this.name ||
(typeof this.name === "object" && !Array.isArray(this.name))
) {
throw new Error(
"library name must be set and not an UMD custom object for non-UMD target"
);
}
new SetVarMainTemplatePlugin( new SetVarMainTemplatePlugin(
`var ${accessorAccess(undefined, this.name)}`, `var ${accessorAccess(undefined, this.name, "root")}`,
false false
).apply(compilation); ).apply(compilation);
break; break;
case "assign": case "assign":
new SetVarMainTemplatePlugin( new SetVarMainTemplatePlugin(
accessorAccess(undefined, this.name), accessorAccess(undefined, this.name, "root"),
false false
).apply(compilation); ).apply(compilation);
break; break;
@ -84,7 +98,7 @@ class LibraryTemplatePlugin {
case "window": case "window":
if (this.name) { if (this.name) {
new SetVarMainTemplatePlugin( new SetVarMainTemplatePlugin(
accessorAccess(this.target, this.name), accessorAccess(this.target, this.name, "root"),
false false
).apply(compilation); ).apply(compilation);
} else { } else {
@ -96,7 +110,8 @@ class LibraryTemplatePlugin {
new SetVarMainTemplatePlugin( new SetVarMainTemplatePlugin(
accessorAccess( accessorAccess(
compilation.runtimeTemplate.outputOptions.globalObject, compilation.runtimeTemplate.outputOptions.globalObject,
this.name this.name,
"root"
), ),
false false
).apply(compilation); ).apply(compilation);
@ -110,7 +125,7 @@ class LibraryTemplatePlugin {
case "commonjs": case "commonjs":
if (this.name) { if (this.name) {
new SetVarMainTemplatePlugin( new SetVarMainTemplatePlugin(
accessorAccess("exports", this.name), accessorAccess("exports", this.name, "commonjs"),
false false
).apply(compilation); ).apply(compilation);
} else { } else {
@ -125,7 +140,13 @@ class LibraryTemplatePlugin {
break; break;
case "amd": { case "amd": {
const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin"); const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
new AmdMainTemplatePlugin(this.name).apply(compilation); if (this.name) {
if (typeof this.name !== "string")
throw new Error("library name must be a string for amd target");
new AmdMainTemplatePlugin(this.name).apply(compilation);
} else {
new AmdMainTemplatePlugin().apply(compilation);
}
break; break;
} }
case "umd": case "umd":
@ -140,6 +161,8 @@ class LibraryTemplatePlugin {
} }
case "jsonp": { case "jsonp": {
const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin"); const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
if (typeof this.name !== "string")
throw new Error("library name must be a string for jsonp target");
new JsonpExportMainTemplatePlugin(this.name).apply(compilation); new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
break; break;
} }

View File

@ -7,6 +7,7 @@
const { ConcatSource, OriginalSource } = require("webpack-sources"); const { ConcatSource, OriginalSource } = require("webpack-sources");
const Template = require("./Template"); const Template = require("./Template");
/** @typedef {import("../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */
/** @typedef {import("./Compilation")} Compilation */ /** @typedef {import("./Compilation")} Compilation */
/** /**
@ -38,7 +39,7 @@ const accessorAccess = (base, accessor, joinWith = ", ") => {
.join(joinWith); .join(joinWith);
}; };
/** @typedef {string | string[] | Record<string, string | string[]>} UmdMainTemplatePluginName */ /** @typedef {string | string[] | LibraryCustomUmdObject} UmdMainTemplatePluginName */
/** /**
* @typedef {Object} AuxiliaryCommentObject * @typedef {Object} AuxiliaryCommentObject

View File

@ -63,11 +63,19 @@ const DefinePlugin = require("./DefinePlugin");
const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin"); const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
const WasmFinalizeExportsPlugin = require("./wasm/WasmFinalizeExportsPlugin"); const WasmFinalizeExportsPlugin = require("./wasm/WasmFinalizeExportsPlugin");
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/** @typedef {import("./Compiler")} Compiler */
class WebpackOptionsApply extends OptionsApply { class WebpackOptionsApply extends OptionsApply {
constructor() { constructor() {
super(); super();
} }
/**
* @param {WebpackOptions} options options object
* @param {Compiler} compiler compiler object
* @returns {WebpackOptions} options object
*/
process(options, compiler) { process(options, compiler) {
let ExternalsPlugin; let ExternalsPlugin;
compiler.outputPath = options.output.path; compiler.outputPath = options.output.path;
@ -75,6 +83,8 @@ class WebpackOptionsApply extends OptionsApply {
compiler.recordsOutputPath = compiler.recordsOutputPath =
options.recordsOutputPath || options.recordsPath; options.recordsOutputPath || options.recordsPath;
compiler.name = options.name; compiler.name = options.name;
// TODO webpack 5 refactor this to MultiCompiler.setDependencies() with a WeakMap
// @ts-ignore TODO
compiler.dependencies = options.dependencies; compiler.dependencies = options.dependencies;
if (typeof options.target === "string") { if (typeof options.target === "string") {
let JsonpTemplatePlugin; let JsonpTemplatePlugin;
@ -201,7 +211,9 @@ class WebpackOptionsApply extends OptionsApply {
default: default:
throw new Error("Unsupported target '" + options.target + "'."); throw new Error("Unsupported target '" + options.target + "'.");
} }
} else if (options.target !== false) { }
// @ts-ignore This is always true, which is good this way
else if (options.target !== false) {
options.target(compiler); options.target(compiler);
} else { } else {
throw new Error("Unsupported target '" + options.target + "'."); throw new Error("Unsupported target '" + options.target + "'.");
@ -450,7 +462,11 @@ class WebpackOptionsApply extends OptionsApply {
} }
if (options.optimization.minimize) { if (options.optimization.minimize) {
for (const minimizer of options.optimization.minimizer) { for (const minimizer of options.optimization.minimizer) {
minimizer.apply(compiler); if (typeof minimizer === "function") {
minimizer.apply(compiler);
} else {
minimizer.apply(compiler);
}
} }
} }

View File

@ -7,6 +7,9 @@
const { ConcatSource } = require("webpack-sources"); const { ConcatSource } = require("webpack-sources");
class JsonpExportMainTemplatePlugin { class JsonpExportMainTemplatePlugin {
/**
* @param {string} name jsonp function name
*/
constructor(name) { constructor(name) {
this.name = name; this.name = name;
} }

View File

@ -15,6 +15,13 @@ const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
const RemovedPluginError = require("./RemovedPluginError"); const RemovedPluginError = require("./RemovedPluginError");
const version = require("../package.json").version; const version = require("../package.json").version;
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/**
* @param {WebpackOptions} options options object
* @param {function(Error=, Stats=): void=} callback callback
* @returns {Compiler | MultiCompiler} the compiler object
*/
const webpack = (options, callback) => { const webpack = (options, callback) => {
const webpackOptionsValidationErrors = validateSchema( const webpackOptionsValidationErrors = validateSchema(
webpackOptionsSchema, webpackOptionsSchema,
@ -34,7 +41,11 @@ const webpack = (options, callback) => {
new NodeEnvironmentPlugin().apply(compiler); new NodeEnvironmentPlugin().apply(compiler);
if (options.plugins && Array.isArray(options.plugins)) { if (options.plugins && Array.isArray(options.plugins)) {
for (const plugin of options.plugins) { for (const plugin of options.plugins) {
plugin.apply(compiler); if (typeof plugin === "function") {
plugin.apply(compiler);
} else {
plugin.apply(compiler);
}
} }
} }
compiler.hooks.environment.call(); compiler.hooks.environment.call();