2013-01-31 01:49:25 +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-11 11:16:18 +08:00
|
|
|
"use strict";
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2020-06-26 05:14:50 +08:00
|
|
|
const http = require("http");
|
|
|
|
const https = require("https");
|
2019-11-21 21:36:31 +08:00
|
|
|
const parseJson = require("json-parse-better-errors");
|
2018-07-30 23:08:51 +08:00
|
|
|
const { getContext, runLoaders } = require("loader-runner");
|
2019-11-21 21:36:31 +08:00
|
|
|
const querystring = require("querystring");
|
|
|
|
const validateOptions = require("schema-utils");
|
2018-11-12 21:13:55 +08:00
|
|
|
const { SyncHook } = require("tapable");
|
2020-06-26 05:14:50 +08:00
|
|
|
const { URL, fileURLToPath } = require("url");
|
2018-03-22 19:05:58 +08:00
|
|
|
const {
|
|
|
|
CachedSource,
|
|
|
|
OriginalSource,
|
|
|
|
RawSource,
|
|
|
|
SourceMapSource
|
|
|
|
} = require("webpack-sources");
|
2018-11-12 21:13:55 +08:00
|
|
|
const Compilation = require("./Compilation");
|
2017-02-12 21:08:41 +08:00
|
|
|
const Module = require("./Module");
|
2017-02-11 11:16:18 +08:00
|
|
|
const ModuleBuildError = require("./ModuleBuildError");
|
|
|
|
const ModuleError = require("./ModuleError");
|
2018-07-30 23:08:51 +08:00
|
|
|
const ModuleParseError = require("./ModuleParseError");
|
2017-02-11 11:16:18 +08:00
|
|
|
const ModuleWarning = require("./ModuleWarning");
|
2018-11-17 01:11:51 +08:00
|
|
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
2020-06-26 05:14:50 +08:00
|
|
|
const UnsupportedSchemeError = require("./UnsupportedSchemeError");
|
2018-07-30 23:08:51 +08:00
|
|
|
const WebpackError = require("./WebpackError");
|
2020-04-16 20:05:40 +08:00
|
|
|
const { decodeDataURI } = require("./util/DataURI");
|
2020-06-29 17:24:44 +08:00
|
|
|
const { getScheme } = require("./util/URLAbsoluteSpecifier");
|
2018-10-18 15:20:59 +08:00
|
|
|
const {
|
2018-12-20 15:51:54 +08:00
|
|
|
compareLocations,
|
2018-10-18 15:20:59 +08:00
|
|
|
concatComparators,
|
|
|
|
compareSelect,
|
|
|
|
keepOriginalOrder
|
|
|
|
} = require("./util/comparators");
|
2018-04-27 20:24:43 +08:00
|
|
|
const createHash = require("./util/createHash");
|
2019-11-11 22:25:03 +08:00
|
|
|
const { contextify } = require("./util/identifier");
|
2018-10-09 20:30:59 +08:00
|
|
|
const makeSerializable = require("./util/makeSerializable");
|
2017-02-11 11:16:18 +08:00
|
|
|
|
2019-11-11 22:25:03 +08:00
|
|
|
/** @typedef {import("source-map").RawSourceMap} SourceMap */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
2020-02-17 17:27:46 +08:00
|
|
|
/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
2018-08-23 01:23:48 +08:00
|
|
|
/** @typedef {import("./ChunkGraph")} ChunkGraph */
|
2018-07-25 18:12:17 +08:00
|
|
|
/** @typedef {import("./Compilation")} Compilation */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
2019-11-20 17:58:58 +08:00
|
|
|
/** @typedef {import("./Generator")} Generator */
|
2019-10-09 04:29:46 +08:00
|
|
|
/** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
|
|
|
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
2018-07-20 22:24:35 +08:00
|
|
|
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
|
2018-09-26 15:14:44 +08:00
|
|
|
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
2019-11-30 03:24:13 +08:00
|
|
|
/** @typedef {import("./Parser")} Parser */
|
2018-07-25 18:12:17 +08:00
|
|
|
/** @typedef {import("./RequestShortener")} RequestShortener */
|
2019-11-11 22:25:03 +08:00
|
|
|
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
2018-07-25 18:12:17 +08:00
|
|
|
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
2019-07-17 22:02:33 +08:00
|
|
|
/** @typedef {import("./util/Hash")} Hash */
|
2019-11-11 22:25:03 +08:00
|
|
|
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
2018-07-11 19:05:13 +08:00
|
|
|
|
2020-01-16 21:48:52 +08:00
|
|
|
/**
|
|
|
|
* @typedef {Object} LoaderItem
|
|
|
|
* @property {string} loader
|
|
|
|
* @property {any} options
|
|
|
|
* @property {string?} ident
|
|
|
|
*/
|
|
|
|
|
2019-11-11 22:25:03 +08:00
|
|
|
/**
|
|
|
|
* @param {string} context absolute context path
|
|
|
|
* @param {string} source a source path
|
|
|
|
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
|
|
|
|
* @returns {string} new source path
|
|
|
|
*/
|
|
|
|
const contextifySourceUrl = (context, source, associatedObjectForCache) => {
|
2019-12-27 22:14:59 +08:00
|
|
|
if (source.startsWith("webpack://")) return source;
|
2019-11-11 22:25:03 +08:00
|
|
|
return `webpack://${contextify(context, source, associatedObjectForCache)}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} context absolute context path
|
|
|
|
* @param {SourceMap} sourceMap a source map
|
|
|
|
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
|
|
|
|
* @returns {SourceMap} new source map
|
|
|
|
*/
|
|
|
|
const contextifySourceMap = (context, sourceMap, associatedObjectForCache) => {
|
|
|
|
if (!Array.isArray(sourceMap.sources)) return sourceMap;
|
2020-01-19 15:58:22 +08:00
|
|
|
const { sourceRoot } = sourceMap;
|
2019-12-27 22:14:59 +08:00
|
|
|
/** @type {function(string): string} */
|
|
|
|
const mapper = !sourceRoot
|
|
|
|
? source => source
|
|
|
|
: sourceRoot.endsWith("/")
|
|
|
|
? source =>
|
|
|
|
source.startsWith("/")
|
|
|
|
? `${sourceRoot.slice(0, -1)}${source}`
|
|
|
|
: `${sourceRoot}${source}`
|
|
|
|
: source =>
|
|
|
|
source.startsWith("/")
|
|
|
|
? `${sourceRoot}${source}`
|
|
|
|
: `${sourceRoot}/${source}`;
|
2019-11-11 22:25:03 +08:00
|
|
|
const newSources = sourceMap.sources.map(source =>
|
2019-12-27 22:14:59 +08:00
|
|
|
contextifySourceUrl(context, mapper(source), associatedObjectForCache)
|
2019-11-11 22:25:03 +08:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
...sourceMap,
|
2019-12-27 22:14:59 +08:00
|
|
|
file: "x",
|
|
|
|
sourceRoot: undefined,
|
2019-11-11 22:25:03 +08:00
|
|
|
sources: newSources
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-11-07 21:03:25 +08:00
|
|
|
/**
|
|
|
|
* @param {string | Buffer} input the input
|
|
|
|
* @returns {string} the converted string
|
|
|
|
*/
|
|
|
|
const asString = input => {
|
|
|
|
if (Buffer.isBuffer(input)) {
|
|
|
|
return input.toString("utf-8");
|
2016-01-04 04:42:56 +08:00
|
|
|
}
|
2018-11-07 21:03:25 +08:00
|
|
|
return input;
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2016-01-04 04:42:56 +08:00
|
|
|
|
2018-11-07 21:03:25 +08:00
|
|
|
/**
|
|
|
|
* @param {string | Buffer} input the input
|
|
|
|
* @returns {Buffer} the converted buffer
|
|
|
|
*/
|
|
|
|
const asBuffer = input => {
|
|
|
|
if (!Buffer.isBuffer(input)) {
|
|
|
|
return Buffer.from(input, "utf-8");
|
2017-10-30 20:56:57 +08:00
|
|
|
}
|
2018-11-07 21:03:25 +08:00
|
|
|
return input;
|
2017-10-30 20:56:57 +08:00
|
|
|
};
|
|
|
|
|
2020-06-26 05:14:50 +08:00
|
|
|
/**
|
|
|
|
* @param {InputFileSystem} fs fs
|
|
|
|
* @returns {(resource: string, cb: (err: Error|null, data?: string|Buffer) => void) => void} read function
|
|
|
|
*/
|
2020-04-16 20:05:40 +08:00
|
|
|
const readResourceFn = fs => {
|
|
|
|
return (resource, callback) => {
|
2020-06-26 05:14:50 +08:00
|
|
|
const scheme = getScheme(resource);
|
2020-06-29 17:24:44 +08:00
|
|
|
if (scheme) {
|
2020-06-26 05:14:50 +08:00
|
|
|
switch (scheme) {
|
|
|
|
case "data":
|
|
|
|
return process.nextTick(() => {
|
|
|
|
callback(null, decodeDataURI(resource));
|
|
|
|
});
|
|
|
|
case "file":
|
|
|
|
return fs.readFile(fileURLToPath(new URL(resource)), callback);
|
|
|
|
case "http":
|
|
|
|
return http.get(new URL(resource), res => {
|
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
res.destroy();
|
|
|
|
return callback(
|
|
|
|
new ModuleBuildError(resource, {
|
|
|
|
from: `http request status code = ${res.statusCode}`
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufferArr = [];
|
2020-04-16 20:05:40 +08:00
|
|
|
|
2020-06-26 05:14:50 +08:00
|
|
|
res.on("data", chunk => {
|
|
|
|
bufferArr.push(chunk);
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on("end", () => {
|
|
|
|
if (!res.complete) {
|
|
|
|
return callback(
|
|
|
|
new ModuleBuildError(resource, {
|
|
|
|
from: "http request was terminated"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, Buffer.concat(bufferArr));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
case "https":
|
|
|
|
return https.get(new URL(resource), res => {
|
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
res.destroy();
|
|
|
|
return callback(
|
|
|
|
new ModuleBuildError(resource, {
|
|
|
|
from: `https request status code = ${res.statusCode}`
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufferArr = [];
|
|
|
|
|
|
|
|
res.on("data", chunk => {
|
|
|
|
bufferArr.push(chunk);
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on("end", () => {
|
|
|
|
if (!res.complete) {
|
|
|
|
return callback(
|
|
|
|
new ModuleBuildError(resource, {
|
|
|
|
from: "https request was terminated"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, Buffer.concat(bufferArr));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
callback(new UnsupportedSchemeError(scheme, resource));
|
|
|
|
}
|
2020-04-16 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.readFile(resource, callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-25 05:17:47 +08:00
|
|
|
class NonErrorEmittedError extends WebpackError {
|
2017-03-22 20:00:57 +08:00
|
|
|
constructor(error) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "NonErrorEmittedError";
|
|
|
|
this.message = "(Emitted value instead of an instance of Error) " + error;
|
|
|
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:49:51 +08:00
|
|
|
makeSerializable(
|
|
|
|
NonErrorEmittedError,
|
|
|
|
"webpack/lib/NormalModule",
|
|
|
|
"NonErrorEmittedError"
|
|
|
|
);
|
|
|
|
|
2018-11-12 21:13:55 +08:00
|
|
|
/**
|
|
|
|
* @typedef {Object} NormalModuleCompilationHooks
|
2018-12-09 19:54:17 +08:00
|
|
|
* @property {SyncHook<[object, NormalModule]>} loader
|
2018-11-12 21:13:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type {WeakMap<Compilation, NormalModuleCompilationHooks>} */
|
|
|
|
const compilationHooksMap = new WeakMap();
|
|
|
|
|
2017-02-11 11:16:18 +08:00
|
|
|
class NormalModule extends Module {
|
2018-11-12 21:13:55 +08:00
|
|
|
/**
|
|
|
|
* @param {Compilation} compilation the compilation
|
|
|
|
* @returns {NormalModuleCompilationHooks} the attached hooks
|
|
|
|
*/
|
|
|
|
static getCompilationHooks(compilation) {
|
|
|
|
if (!(compilation instanceof Compilation)) {
|
|
|
|
throw new TypeError(
|
|
|
|
"The 'compilation' argument must be an instance of Compilation"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let hooks = compilationHooksMap.get(compilation);
|
|
|
|
if (hooks === undefined) {
|
|
|
|
hooks = {
|
|
|
|
loader: new SyncHook(["loaderContext", "module"])
|
|
|
|
};
|
|
|
|
compilationHooksMap.set(compilation, hooks);
|
|
|
|
}
|
|
|
|
return hooks;
|
|
|
|
}
|
|
|
|
|
2019-11-20 17:58:58 +08:00
|
|
|
/**
|
|
|
|
* @param {Object} options options object
|
|
|
|
* @param {string} options.type module type
|
|
|
|
* @param {string} options.request request string
|
2020-03-10 09:59:46 +08:00
|
|
|
* @param {string} options.userRequest request intended by user (without loaders from config)
|
2019-11-20 17:58:58 +08:00
|
|
|
* @param {string} options.rawRequest request without resolving
|
2020-01-16 21:48:52 +08:00
|
|
|
* @param {LoaderItem[]} options.loaders list of loaders
|
2019-11-20 17:58:58 +08:00
|
|
|
* @param {string} options.resource path + query of the real resource
|
2020-01-19 16:01:37 +08:00
|
|
|
* @param {string | undefined} options.matchResource path + query of the matched resource (virtual)
|
2019-11-30 03:24:13 +08:00
|
|
|
* @param {Parser} options.parser the parser used
|
2019-11-20 17:58:58 +08:00
|
|
|
* @param {Generator} options.generator the generator used
|
2020-06-26 05:14:50 +08:00
|
|
|
* @param {string=} options.scheme scheme
|
2019-11-20 17:58:58 +08:00
|
|
|
* @param {Object} options.resolveOptions options used for resolving requests from this module
|
|
|
|
*/
|
2018-01-24 06:09:26 +08:00
|
|
|
constructor({
|
|
|
|
type,
|
|
|
|
request,
|
|
|
|
userRequest,
|
|
|
|
rawRequest,
|
|
|
|
loaders,
|
|
|
|
resource,
|
2018-06-01 20:54:54 +08:00
|
|
|
matchResource,
|
2018-01-24 06:09:26 +08:00
|
|
|
parser,
|
|
|
|
generator,
|
2020-06-26 05:14:50 +08:00
|
|
|
resolveOptions,
|
|
|
|
scheme
|
2018-01-24 06:09:26 +08:00
|
|
|
}) {
|
2018-01-31 04:40:44 +08:00
|
|
|
super(type, getContext(resource));
|
2017-11-06 20:02:35 +08:00
|
|
|
|
|
|
|
// Info from Factory
|
2018-11-07 21:03:25 +08:00
|
|
|
/** @type {string} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.request = request;
|
2018-11-07 21:03:25 +08:00
|
|
|
/** @type {string} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.userRequest = userRequest;
|
2018-11-07 21:03:25 +08:00
|
|
|
/** @type {string} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.rawRequest = rawRequest;
|
2018-11-07 21:03:25 +08:00
|
|
|
/** @type {boolean} */
|
2019-07-16 19:16:27 +08:00
|
|
|
this.binary = /^(asset|webassembly)\b/.test(type);
|
2019-11-30 03:24:13 +08:00
|
|
|
/** @type {Parser} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.parser = parser;
|
2019-11-20 17:58:58 +08:00
|
|
|
/** @type {Generator} */
|
2018-01-24 06:09:26 +08:00
|
|
|
this.generator = generator;
|
2019-11-20 17:58:58 +08:00
|
|
|
/** @type {string} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.resource = resource;
|
2019-11-20 17:58:58 +08:00
|
|
|
/** @type {string | undefined} */
|
2018-06-01 20:54:54 +08:00
|
|
|
this.matchResource = matchResource;
|
2020-06-26 05:14:50 +08:00
|
|
|
/** @type {string | undefined} */
|
|
|
|
this.scheme = scheme;
|
2020-01-16 21:48:52 +08:00
|
|
|
/** @type {LoaderItem[]} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.loaders = loaders;
|
2018-11-07 21:03:25 +08:00
|
|
|
if (resolveOptions !== undefined) {
|
2019-11-20 17:58:58 +08:00
|
|
|
// already declared in super class
|
2018-11-07 21:03:25 +08:00
|
|
|
this.resolveOptions = resolveOptions;
|
|
|
|
}
|
2017-11-06 20:02:35 +08:00
|
|
|
|
|
|
|
// Info from Build
|
2018-11-07 21:03:25 +08:00
|
|
|
/** @type {WebpackError=} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this.error = null;
|
2018-11-07 21:03:25 +08:00
|
|
|
/** @private @type {Source=} */
|
2017-02-11 11:16:18 +08:00
|
|
|
this._source = null;
|
2019-11-08 19:46:37 +08:00
|
|
|
/** @private @type {Map<string, number> | undefined} **/
|
|
|
|
this._sourceSizes = undefined;
|
2019-10-09 19:36:44 +08:00
|
|
|
/** @private @type {string} */
|
|
|
|
this._cachedCodeGenerationHash = "";
|
|
|
|
/** @private @type {CodeGenerationResult} */
|
2019-10-09 04:29:46 +08:00
|
|
|
this._cachedCodeGeneration = undefined;
|
2017-11-06 20:02:35 +08:00
|
|
|
|
2017-12-07 00:26:02 +08:00
|
|
|
// Cache
|
|
|
|
this._lastSuccessfulBuildMeta = {};
|
2018-09-12 00:47:55 +08:00
|
|
|
this._forceBuild = true;
|
2018-11-12 16:15:06 +08:00
|
|
|
|
|
|
|
// TODO refactor this -> options object filled from Factory
|
|
|
|
this.useSourceMap = false;
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2016-09-14 18:04:42 +08:00
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* @returns {string} a unique identifier of the module
|
|
|
|
*/
|
2017-02-11 11:16:18 +08:00
|
|
|
identifier() {
|
|
|
|
return this.request;
|
|
|
|
}
|
2016-01-04 04:42:56 +08:00
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* @param {RequestShortener} requestShortener the request shortener
|
|
|
|
* @returns {string} a user readable identifier of the module
|
|
|
|
*/
|
2017-02-11 11:16:18 +08:00
|
|
|
readableIdentifier(requestShortener) {
|
|
|
|
return requestShortener.shorten(this.userRequest);
|
|
|
|
}
|
2016-01-04 04:42:56 +08:00
|
|
|
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
|
|
|
* @param {LibIdentOptions} options options
|
|
|
|
* @returns {string | null} an identifier for library inclusion
|
|
|
|
*/
|
2017-02-11 11:16:18 +08:00
|
|
|
libIdent(options) {
|
2019-01-19 20:12:43 +08:00
|
|
|
return contextify(
|
|
|
|
options.context,
|
|
|
|
this.userRequest,
|
|
|
|
options.associatedObjectForCache
|
|
|
|
);
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2016-01-04 04:42:56 +08:00
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
|
|
|
|
*/
|
2017-02-11 11:16:18 +08:00
|
|
|
nameForCondition() {
|
2018-06-01 20:54:54 +08:00
|
|
|
const resource = this.matchResource || this.resource;
|
|
|
|
const idx = resource.indexOf("?");
|
|
|
|
if (idx >= 0) return resource.substr(0, idx);
|
|
|
|
return resource;
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* Assuming this module is in the cache. Update the (cached) module with
|
|
|
|
* the fresh module from the factory. Usually updates internal references
|
|
|
|
* and properties.
|
|
|
|
* @param {Module} module fresh module
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-03-28 22:19:15 +08:00
|
|
|
updateCacheModule(module) {
|
2018-10-09 20:30:59 +08:00
|
|
|
super.updateCacheModule(module);
|
2018-07-25 18:12:17 +08:00
|
|
|
const m = /** @type {NormalModule} */ (module);
|
|
|
|
this.request = m.request;
|
|
|
|
this.userRequest = m.userRequest;
|
|
|
|
this.rawRequest = m.rawRequest;
|
|
|
|
this.parser = m.parser;
|
|
|
|
this.generator = m.generator;
|
|
|
|
this.resource = m.resource;
|
|
|
|
this.matchResource = m.matchResource;
|
|
|
|
this.loaders = m.loaders;
|
2020-06-26 05:14:50 +08:00
|
|
|
this.scheme = m.scheme;
|
2018-03-28 22:19:15 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:03:25 +08:00
|
|
|
/**
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {string} context the compilation context
|
2018-11-07 21:03:25 +08:00
|
|
|
* @param {string} name the asset name
|
|
|
|
* @param {string} content the content
|
|
|
|
* @param {string | TODO} sourceMap an optional source map
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {Object=} associatedObjectForCache object for caching
|
2018-11-07 21:03:25 +08:00
|
|
|
* @returns {Source} the created source
|
|
|
|
*/
|
2019-11-11 22:25:03 +08:00
|
|
|
createSourceForAsset(
|
|
|
|
context,
|
|
|
|
name,
|
|
|
|
content,
|
|
|
|
sourceMap,
|
|
|
|
associatedObjectForCache
|
|
|
|
) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!sourceMap) {
|
2017-02-11 14:07:01 +08:00
|
|
|
return new RawSource(content);
|
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (typeof sourceMap === "string") {
|
2019-11-11 22:25:03 +08:00
|
|
|
return new OriginalSource(
|
|
|
|
content,
|
|
|
|
contextifySourceUrl(context, sourceMap, associatedObjectForCache)
|
|
|
|
);
|
2017-02-11 14:07:01 +08:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:25:03 +08:00
|
|
|
return new SourceMapSource(
|
|
|
|
content,
|
|
|
|
name,
|
|
|
|
contextifySourceMap(context, sourceMap, associatedObjectForCache)
|
|
|
|
);
|
2017-02-11 14:07:01 +08:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:25:03 +08:00
|
|
|
/**
|
|
|
|
* @param {ResolverWithOptions} resolver a resolver
|
|
|
|
* @param {WebpackOptions} options webpack options
|
|
|
|
* @param {Compilation} compilation the compilation
|
|
|
|
* @param {InputFileSystem} fs file system from reading
|
|
|
|
* @returns {any} loader context
|
|
|
|
*/
|
2017-02-11 12:32:48 +08:00
|
|
|
createLoaderContext(resolver, options, compilation, fs) {
|
2020-02-08 05:38:53 +08:00
|
|
|
const { requestShortener } = compilation.runtimeTemplate;
|
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
|
|
|
const getCurrentLoaderName = () => {
|
|
|
|
const currentLoader = this.getCurrentLoader(loaderContext);
|
|
|
|
if (!currentLoader) return "(not in loader scope)";
|
|
|
|
return requestShortener.shorten(currentLoader.loader);
|
|
|
|
};
|
2019-11-26 05:10:47 +08:00
|
|
|
const getResolveContext = () => {
|
|
|
|
return {
|
|
|
|
fileDependencies: {
|
|
|
|
add: d => loaderContext.addDependency(d)
|
|
|
|
},
|
|
|
|
contextDependencies: {
|
|
|
|
add: d => loaderContext.addContextDependency(d)
|
|
|
|
},
|
|
|
|
missingDependencies: {
|
|
|
|
add: d => loaderContext.addMissingDependency(d)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2017-02-11 11:16:18 +08:00
|
|
|
const loaderContext = {
|
|
|
|
version: 2,
|
2020-01-16 21:48:52 +08:00
|
|
|
getOptions: schema => {
|
|
|
|
const loader = this.getCurrentLoader(loaderContext);
|
2019-11-21 21:36:31 +08:00
|
|
|
|
2020-01-16 21:48:52 +08:00
|
|
|
let { options } = loader;
|
2019-11-21 21:36:31 +08:00
|
|
|
|
2020-01-16 21:48:52 +08:00
|
|
|
if (typeof options === "string") {
|
|
|
|
if (options.substr(0, 1) === "{" && options.substr(-1) === "}") {
|
2019-11-21 21:36:31 +08:00
|
|
|
try {
|
2020-01-16 21:48:52 +08:00
|
|
|
options = parseJson(options);
|
2019-11-21 21:36:31 +08:00
|
|
|
} catch (e) {
|
2020-01-16 21:48:52 +08:00
|
|
|
throw new Error(`Cannot parse string options: ${e.message}`);
|
2019-11-21 21:36:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-16 21:48:52 +08:00
|
|
|
options = querystring.parse(options, "&", "=", {
|
|
|
|
maxKeys: 0
|
|
|
|
});
|
2019-11-21 21:36:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:48:52 +08:00
|
|
|
if (options === null || options === undefined) {
|
|
|
|
options = {};
|
2019-11-21 21:36:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 21:48:52 +08:00
|
|
|
if (schema) {
|
2020-01-17 19:17:53 +08:00
|
|
|
let name = "Loader";
|
|
|
|
let baseDataPath = "options";
|
|
|
|
let match;
|
|
|
|
if (schema.title && (match = /^(.+) (.+)$/.exec(schema.title))) {
|
|
|
|
[, name, baseDataPath] = match;
|
|
|
|
}
|
2020-01-16 21:48:52 +08:00
|
|
|
validateOptions(schema, options, {
|
2020-01-17 19:17:53 +08:00
|
|
|
name,
|
|
|
|
baseDataPath
|
2020-01-16 21:48:52 +08:00
|
|
|
});
|
|
|
|
}
|
2019-11-21 21:36:31 +08:00
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
emitWarning: warning => {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!(warning instanceof Error)) {
|
2017-03-22 20:00:57 +08:00
|
|
|
warning = new NonErrorEmittedError(warning);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2019-11-08 19:43:05 +08:00
|
|
|
this.addWarning(
|
2018-10-30 05:18:08 +08:00
|
|
|
new ModuleWarning(warning, {
|
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
|
|
|
from: getCurrentLoaderName()
|
2018-03-02 17:57:46 +08:00
|
|
|
})
|
|
|
|
);
|
2017-02-11 11:16:18 +08:00
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
emitError: error => {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!(error instanceof Error)) {
|
|
|
|
error = new NonErrorEmittedError(error);
|
|
|
|
}
|
2019-11-08 19:43:05 +08:00
|
|
|
this.addError(
|
2018-10-30 05:18:08 +08:00
|
|
|
new ModuleError(error, {
|
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
|
|
|
from: getCurrentLoaderName()
|
2018-03-02 17:57:46 +08:00
|
|
|
})
|
|
|
|
);
|
2017-02-11 11:16:18 +08:00
|
|
|
},
|
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
|
|
|
getLogger: name => {
|
|
|
|
const currentLoader = this.getCurrentLoader(loaderContext);
|
|
|
|
return compilation.getLogger(() =>
|
|
|
|
[currentLoader && currentLoader.loader, name, this.identifier()]
|
|
|
|
.filter(Boolean)
|
2019-07-23 15:28:06 +08:00
|
|
|
.join("|")
|
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
|
|
|
);
|
|
|
|
},
|
2017-02-12 21:08:41 +08:00
|
|
|
resolve(context, request, callback) {
|
2019-11-26 05:10:47 +08:00
|
|
|
resolver.resolve({}, context, request, getResolveContext(), callback);
|
2017-02-11 11:16:18 +08:00
|
|
|
},
|
2018-12-03 19:42:28 +08:00
|
|
|
getResolve(options) {
|
|
|
|
const child = options ? resolver.withOptions(options) : resolver;
|
|
|
|
return (context, request, callback) => {
|
|
|
|
if (callback) {
|
2019-11-26 05:10:47 +08:00
|
|
|
child.resolve({}, context, request, getResolveContext(), callback);
|
2018-12-03 19:42:28 +08:00
|
|
|
} else {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-26 05:10:47 +08:00
|
|
|
child.resolve(
|
|
|
|
{},
|
|
|
|
context,
|
|
|
|
request,
|
|
|
|
getResolveContext(),
|
|
|
|
(err, result) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(result);
|
|
|
|
}
|
|
|
|
);
|
2018-12-03 19:42:28 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2019-09-11 17:13:46 +08:00
|
|
|
emitFile: (name, content, sourceMap, assetInfo) => {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!this.buildInfo.assets) {
|
|
|
|
this.buildInfo.assets = Object.create(null);
|
2019-09-11 17:13:46 +08:00
|
|
|
this.buildInfo.assetsInfo = new Map();
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
this.buildInfo.assets[name] = this.createSourceForAsset(
|
2019-11-11 22:25:03 +08:00
|
|
|
options.context,
|
2018-02-25 09:00:20 +08:00
|
|
|
name,
|
|
|
|
content,
|
2019-11-11 22:25:03 +08:00
|
|
|
sourceMap,
|
|
|
|
compilation.compiler.root
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2019-09-11 17:13:46 +08:00
|
|
|
this.buildInfo.assetsInfo.set(name, assetInfo);
|
2017-02-11 12:32:48 +08:00
|
|
|
},
|
2017-09-14 15:00:22 +08:00
|
|
|
rootContext: options.context,
|
2017-02-11 12:32:48 +08:00
|
|
|
webpack: true,
|
|
|
|
sourceMap: !!this.useSourceMap,
|
2019-05-17 03:50:31 +08:00
|
|
|
mode: options.mode || "production",
|
2017-02-11 12:32:48 +08:00
|
|
|
_module: this,
|
|
|
|
_compilation: compilation,
|
|
|
|
_compiler: compilation.compiler,
|
2018-02-25 09:00:20 +08:00
|
|
|
fs: fs
|
2017-02-11 11:16:18 +08:00
|
|
|
};
|
2017-02-11 12:32:48 +08:00
|
|
|
|
2018-11-12 21:13:55 +08:00
|
|
|
NormalModule.getCompilationHooks(compilation).loader.call(
|
|
|
|
loaderContext,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
2018-05-29 20:50:40 +08:00
|
|
|
if (options.loader) {
|
|
|
|
Object.assign(loaderContext, options.loader);
|
|
|
|
}
|
2017-02-11 12:32:48 +08:00
|
|
|
|
|
|
|
return loaderContext;
|
|
|
|
}
|
|
|
|
|
2018-06-05 17:58:10 +08:00
|
|
|
getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
|
2018-03-02 17:57:46 +08:00
|
|
|
if (
|
|
|
|
this.loaders &&
|
|
|
|
this.loaders.length &&
|
2018-06-05 17:58:10 +08:00
|
|
|
index < this.loaders.length &&
|
|
|
|
index >= 0 &&
|
|
|
|
this.loaders[index]
|
2018-03-02 17:57:46 +08:00
|
|
|
) {
|
2018-06-05 17:58:10 +08:00
|
|
|
return this.loaders[index];
|
2018-03-02 17:57:46 +08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:03:25 +08:00
|
|
|
/**
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {string} context the compilation context
|
2018-11-07 21:03:25 +08:00
|
|
|
* @param {string | Buffer} content the content
|
|
|
|
* @param {string | TODO} sourceMap an optional source map
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {Object=} associatedObjectForCache object for caching
|
2018-11-07 21:03:25 +08:00
|
|
|
* @returns {Source} the created source
|
|
|
|
*/
|
2019-11-11 22:25:03 +08:00
|
|
|
createSource(context, content, sourceMap, associatedObjectForCache) {
|
2018-11-07 21:03:25 +08:00
|
|
|
if (Buffer.isBuffer(content)) {
|
|
|
|
return new RawSource(content);
|
|
|
|
}
|
|
|
|
|
2017-02-11 12:57:52 +08:00
|
|
|
// if there is no identifier return raw source
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!this.identifier) {
|
2018-11-07 21:03:25 +08:00
|
|
|
return new RawSource(content);
|
2017-02-11 12:57:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// from here on we assume we have an identifier
|
|
|
|
const identifier = this.identifier();
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.useSourceMap && sourceMap) {
|
2019-11-11 22:25:03 +08:00
|
|
|
return new SourceMapSource(
|
|
|
|
content,
|
|
|
|
contextifySourceUrl(context, identifier, associatedObjectForCache),
|
|
|
|
contextifySourceMap(context, sourceMap, associatedObjectForCache)
|
|
|
|
);
|
2017-02-11 12:57:52 +08:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:25:03 +08:00
|
|
|
return new OriginalSource(
|
|
|
|
content,
|
|
|
|
contextifySourceUrl(context, identifier, associatedObjectForCache)
|
|
|
|
);
|
2017-02-11 12:57:52 +08:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:25:03 +08:00
|
|
|
/**
|
|
|
|
* @param {WebpackOptions} options webpack options
|
|
|
|
* @param {Compilation} compilation the compilation
|
|
|
|
* @param {ResolverWithOptions} resolver the resolver
|
|
|
|
* @param {InputFileSystem} fs the file system
|
|
|
|
* @param {function(WebpackError=): void} callback callback function
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-11 12:32:48 +08:00
|
|
|
doBuild(options, compilation, resolver, fs, callback) {
|
2018-02-25 09:00:20 +08:00
|
|
|
const loaderContext = this.createLoaderContext(
|
|
|
|
resolver,
|
|
|
|
options,
|
|
|
|
compilation,
|
|
|
|
fs
|
|
|
|
);
|
|
|
|
|
2019-01-09 20:23:26 +08:00
|
|
|
const startTime = Date.now();
|
|
|
|
|
|
|
|
const processResult = (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
if (!(err instanceof Error)) {
|
|
|
|
err = new NonErrorEmittedError(err);
|
|
|
|
}
|
|
|
|
const currentLoader = this.getCurrentLoader(loaderContext);
|
|
|
|
const error = new ModuleBuildError(err, {
|
|
|
|
from:
|
|
|
|
currentLoader &&
|
|
|
|
compilation.runtimeTemplate.requestShortener.shorten(
|
|
|
|
currentLoader.loader
|
|
|
|
)
|
|
|
|
});
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const source = result[0];
|
|
|
|
const sourceMap = result.length >= 1 ? result[1] : null;
|
|
|
|
const extraInfo = result.length >= 2 ? result[2] : null;
|
|
|
|
|
|
|
|
if (!Buffer.isBuffer(source) && typeof source !== "string") {
|
|
|
|
const currentLoader = this.getCurrentLoader(loaderContext, 0);
|
|
|
|
const err = new Error(
|
|
|
|
`Final loader (${
|
|
|
|
currentLoader
|
|
|
|
? compilation.runtimeTemplate.requestShortener.shorten(
|
|
|
|
currentLoader.loader
|
|
|
|
)
|
|
|
|
: "unknown"
|
|
|
|
}) didn't return a Buffer or String`
|
|
|
|
);
|
|
|
|
const error = new ModuleBuildError(err);
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._source = this.createSource(
|
2019-11-11 22:25:03 +08:00
|
|
|
options.context,
|
2019-01-09 20:23:26 +08:00
|
|
|
this.binary ? asBuffer(source) : asString(source),
|
2019-11-11 22:25:03 +08:00
|
|
|
sourceMap,
|
|
|
|
compilation.compiler.root
|
2019-01-09 20:23:26 +08:00
|
|
|
);
|
2019-11-08 19:46:37 +08:00
|
|
|
if (this._sourceSizes !== undefined) this._sourceSizes.clear();
|
2019-01-09 20:23:26 +08:00
|
|
|
this._ast =
|
|
|
|
typeof extraInfo === "object" &&
|
|
|
|
extraInfo !== null &&
|
|
|
|
extraInfo.webpackAST !== undefined
|
|
|
|
? extraInfo.webpackAST
|
|
|
|
: null;
|
|
|
|
return callback();
|
|
|
|
};
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
runLoaders(
|
|
|
|
{
|
|
|
|
resource: this.resource,
|
|
|
|
loaders: this.loaders,
|
|
|
|
context: loaderContext,
|
2020-04-16 20:05:40 +08:00
|
|
|
readResource: readResourceFn(fs)
|
2018-02-25 09:00:20 +08:00
|
|
|
},
|
|
|
|
(err, result) => {
|
2019-01-09 20:23:26 +08:00
|
|
|
if (!result) {
|
|
|
|
processResult(
|
|
|
|
err || new Error("No result from loader-runner processing"),
|
|
|
|
null
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
}
|
2019-08-09 20:46:42 +08:00
|
|
|
for (const loader of this.loaders) {
|
2019-08-14 00:05:26 +08:00
|
|
|
compilation.buildDependencies.add(loader.loader);
|
2019-08-09 20:46:42 +08:00
|
|
|
}
|
2019-01-09 20:23:26 +08:00
|
|
|
this.buildInfo.fileDependencies = new Set(result.fileDependencies);
|
|
|
|
this.buildInfo.contextDependencies = new Set(
|
|
|
|
result.contextDependencies
|
|
|
|
);
|
|
|
|
this.buildInfo.missingDependencies = new Set(
|
|
|
|
result.missingDependencies
|
|
|
|
);
|
|
|
|
if (!result.cacheable) {
|
|
|
|
this.buildInfo.cacheable = false;
|
|
|
|
processResult(err, result.result);
|
|
|
|
return;
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
2019-01-09 20:23:26 +08:00
|
|
|
this.buildInfo.cacheable = true;
|
|
|
|
compilation.fileSystemInfo.createSnapshot(
|
|
|
|
startTime,
|
|
|
|
result.fileDependencies,
|
|
|
|
result.contextDependencies,
|
|
|
|
result.missingDependencies,
|
|
|
|
null,
|
|
|
|
(err2, snapshot) => {
|
|
|
|
this.buildInfo.snapshot = snapshot;
|
|
|
|
processResult(err || err2, result.result);
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2014-05-17 06:31:52 +08:00
|
|
|
}
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2018-11-07 21:03:25 +08:00
|
|
|
/**
|
|
|
|
* @param {WebpackError} error the error
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-13 18:38:55 +08:00
|
|
|
markModuleAsErrored(error) {
|
2018-02-26 10:48:51 +08:00
|
|
|
// Restore build meta from successful build to keep importing state
|
2019-06-19 19:16:05 +08:00
|
|
|
this.buildMeta = { ...this._lastSuccessfulBuildMeta };
|
2017-02-13 18:38:55 +08:00
|
|
|
this.error = error;
|
2019-11-08 19:43:05 +08:00
|
|
|
this.addError(error);
|
2017-02-11 11:41:26 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 18:14:57 +08:00
|
|
|
applyNoParseRule(rule, content) {
|
2017-02-11 13:59:58 +08:00
|
|
|
// must start with "rule" if rule is a string
|
2018-02-25 09:00:20 +08:00
|
|
|
if (typeof rule === "string") {
|
2020-01-19 15:59:39 +08:00
|
|
|
return content.startsWith(rule);
|
2017-02-11 13:59:58 +08:00
|
|
|
}
|
2017-06-03 08:28:54 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (typeof rule === "function") {
|
2017-06-03 08:28:54 +08:00
|
|
|
return rule(content);
|
|
|
|
}
|
2017-02-11 13:59:58 +08:00
|
|
|
// we assume rule is a regexp
|
2017-02-14 18:14:57 +08:00
|
|
|
return rule.test(content);
|
2017-02-11 13:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if module should not be parsed
|
|
|
|
// returns "true" if the module should !not! be parsed
|
|
|
|
// returns "false" if the module !must! be parsed
|
2017-02-16 05:01:09 +08:00
|
|
|
shouldPreventParsing(noParseRule, request) {
|
2017-02-11 13:59:58 +08:00
|
|
|
// if no noParseRule exists, return false
|
|
|
|
// the module !must! be parsed.
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!noParseRule) {
|
2017-02-11 13:59:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only have one rule to check
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!Array.isArray(noParseRule)) {
|
2017-02-11 13:59:58 +08:00
|
|
|
// returns "true" if the module is !not! to be parsed
|
|
|
|
return this.applyNoParseRule(noParseRule, request);
|
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
for (let i = 0; i < noParseRule.length; i++) {
|
2017-02-11 13:59:58 +08:00
|
|
|
const rule = noParseRule[i];
|
|
|
|
// early exit on first truthy match
|
|
|
|
// this module is !not! to be parsed
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.applyNoParseRule(rule, request)) {
|
2017-02-11 13:59:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no match found, so this module !should! be parsed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-27 20:24:43 +08:00
|
|
|
_initBuildHash(compilation) {
|
|
|
|
const hash = createHash(compilation.outputOptions.hashFunction);
|
|
|
|
if (this._source) {
|
|
|
|
hash.update("source");
|
2018-11-07 21:03:25 +08:00
|
|
|
this._source.updateHash(/** @type {TODO} */ (hash));
|
2018-04-27 20:24:43 +08:00
|
|
|
}
|
|
|
|
hash.update("meta");
|
|
|
|
hash.update(JSON.stringify(this.buildMeta));
|
2019-11-27 04:24:41 +08:00
|
|
|
this.generator.updateHash(hash, {
|
|
|
|
module: this,
|
|
|
|
compilation
|
|
|
|
});
|
2019-07-17 22:02:33 +08:00
|
|
|
this.buildInfo.hash = /** @type {string} */ (hash.digest("hex"));
|
2018-04-27 20:24:43 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {WebpackOptions} options webpack options
|
2018-07-25 18:12:17 +08:00
|
|
|
* @param {Compilation} compilation the compilation
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {ResolverWithOptions} resolver the resolver
|
|
|
|
* @param {InputFileSystem} fs the file system
|
2018-09-12 00:47:55 +08:00
|
|
|
* @param {function(WebpackError=): void} callback callback function
|
2018-07-25 18:12:17 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-11 11:16:18 +08:00
|
|
|
build(options, compilation, resolver, fs, callback) {
|
2018-09-12 00:47:55 +08:00
|
|
|
this._forceBuild = false;
|
2017-02-11 11:16:18 +08:00
|
|
|
this._source = null;
|
2019-11-08 19:46:37 +08:00
|
|
|
if (this._sourceSizes !== undefined) this._sourceSizes.clear();
|
2017-11-03 18:12:45 +08:00
|
|
|
this._ast = null;
|
2017-02-11 11:16:18 +08:00
|
|
|
this.error = null;
|
2019-11-08 19:43:05 +08:00
|
|
|
this.clearWarningsAndErrors();
|
2019-11-08 22:39:44 +08:00
|
|
|
this.clearDependenciesAndBlocks();
|
2017-12-06 19:09:17 +08:00
|
|
|
this.buildMeta = {};
|
|
|
|
this.buildInfo = {
|
|
|
|
cacheable: false,
|
2018-11-17 01:18:44 +08:00
|
|
|
parsed: true,
|
2018-12-31 19:50:45 +08:00
|
|
|
fileDependencies: undefined,
|
|
|
|
contextDependencies: undefined,
|
2019-01-09 20:23:26 +08:00
|
|
|
missingDependencies: undefined,
|
2018-12-31 19:50:45 +08:00
|
|
|
hash: undefined,
|
2019-09-11 17:13:46 +08:00
|
|
|
assets: undefined,
|
|
|
|
assetsInfo: undefined
|
2017-12-06 19:09:17 +08:00
|
|
|
};
|
2016-06-21 03:46:27 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
return this.doBuild(options, compilation, resolver, fs, err => {
|
2019-10-09 19:36:44 +08:00
|
|
|
this._cachedCodeGenerationHash = "";
|
2019-10-09 04:29:46 +08:00
|
|
|
this._cachedCodeGeneration = undefined;
|
2017-02-11 13:59:58 +08:00
|
|
|
|
|
|
|
// if we have an error mark module as failed and exit
|
2018-02-25 09:00:20 +08:00
|
|
|
if (err) {
|
2017-02-13 18:38:55 +08:00
|
|
|
this.markModuleAsErrored(err);
|
2018-04-27 20:24:43 +08:00
|
|
|
this._initBuildHash(compilation);
|
2017-02-11 11:41:26 +08:00
|
|
|
return callback();
|
|
|
|
}
|
2017-02-11 13:59:58 +08:00
|
|
|
|
|
|
|
// check if this module should !not! be parsed.
|
|
|
|
// if so, exit here;
|
|
|
|
const noParseRule = options.module && options.module.noParse;
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.shouldPreventParsing(noParseRule, this.request)) {
|
2018-11-17 01:11:51 +08:00
|
|
|
// We assume that we need module and exports
|
2018-11-17 01:18:44 +08:00
|
|
|
this.buildInfo.parsed = false;
|
2018-04-27 20:24:43 +08:00
|
|
|
this._initBuildHash(compilation);
|
2017-02-11 13:59:58 +08:00
|
|
|
return callback();
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2017-02-11 13:59:58 +08:00
|
|
|
|
2017-12-14 08:21:44 +08:00
|
|
|
const handleParseError = e => {
|
2020-04-29 02:22:50 +08:00
|
|
|
const source = this._source.source();
|
2019-06-03 21:23:13 +08:00
|
|
|
const loaders = this.loaders.map(item =>
|
2020-01-15 06:14:47 +08:00
|
|
|
contextify(options.context, item.loader, compilation.compiler.root)
|
2019-06-03 21:23:13 +08:00
|
|
|
);
|
2020-04-29 02:22:50 +08:00
|
|
|
const error = new ModuleParseError(source, e, loaders, this.type);
|
2017-12-14 08:21:44 +08:00
|
|
|
this.markModuleAsErrored(error);
|
2018-04-27 20:24:43 +08:00
|
|
|
this._initBuildHash(compilation);
|
2017-12-14 08:21:44 +08:00
|
|
|
return callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleParseResult = result => {
|
2018-10-18 15:20:59 +08:00
|
|
|
this.dependencies.sort(
|
|
|
|
concatComparators(
|
|
|
|
compareSelect(a => a.loc, compareLocations),
|
|
|
|
keepOriginalOrder(this.dependencies)
|
|
|
|
)
|
|
|
|
);
|
2017-12-14 08:21:44 +08:00
|
|
|
this._lastSuccessfulBuildMeta = this.buildMeta;
|
2018-04-27 20:24:43 +08:00
|
|
|
this._initBuildHash(compilation);
|
2017-12-14 08:21:44 +08:00
|
|
|
return callback();
|
|
|
|
};
|
|
|
|
|
2019-11-30 03:24:13 +08:00
|
|
|
let result;
|
2017-02-11 11:16:18 +08:00
|
|
|
try {
|
2019-11-30 03:24:13 +08:00
|
|
|
result = this.parser.parse(this._ast || this._source.source(), {
|
|
|
|
current: this,
|
|
|
|
module: this,
|
|
|
|
compilation: compilation,
|
|
|
|
options: options
|
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
} catch (e) {
|
2017-12-14 08:21:44 +08:00
|
|
|
handleParseError(e);
|
2019-11-30 03:24:13 +08:00
|
|
|
return;
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2019-11-30 03:24:13 +08:00
|
|
|
handleParseResult(result);
|
2015-07-13 06:20:09 +08:00
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2018-07-11 19:05:13 +08:00
|
|
|
/**
|
2018-08-23 01:23:48 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph the chunk graph
|
2018-07-11 19:05:13 +08:00
|
|
|
* @param {DependencyTemplates} dependencyTemplates dependency templates
|
|
|
|
* @returns {string} hash
|
|
|
|
*/
|
2018-11-15 00:31:32 +08:00
|
|
|
_getHashDigest(chunkGraph, dependencyTemplates) {
|
2018-08-23 01:23:48 +08:00
|
|
|
const hash = chunkGraph.getModuleHash(this);
|
|
|
|
const dtHash = dependencyTemplates.getHash();
|
|
|
|
return `${hash}-${dtHash}`;
|
2017-02-13 19:09:16 +08:00
|
|
|
}
|
2017-02-11 11:16:18 +08:00
|
|
|
|
2018-12-03 22:00:32 +08:00
|
|
|
/**
|
2020-03-10 09:59:46 +08:00
|
|
|
* @returns {Set<string>} types available (do not mutate)
|
2018-12-03 22:00:32 +08:00
|
|
|
*/
|
|
|
|
getSourceTypes() {
|
2019-11-18 21:29:19 +08:00
|
|
|
return this.generator.getTypes(this);
|
2018-12-03 22:00:32 +08:00
|
|
|
}
|
|
|
|
|
2018-11-15 00:31:32 +08:00
|
|
|
/**
|
2019-10-09 04:29:46 +08:00
|
|
|
* @param {CodeGenerationContext} context context for code generation
|
|
|
|
* @returns {CodeGenerationResult} result
|
2018-11-15 00:31:32 +08:00
|
|
|
*/
|
2019-10-09 04:29:46 +08:00
|
|
|
codeGeneration({
|
2018-07-24 23:35:36 +08:00
|
|
|
dependencyTemplates,
|
|
|
|
runtimeTemplate,
|
|
|
|
moduleGraph,
|
2019-10-09 04:29:46 +08:00
|
|
|
chunkGraph
|
2018-07-24 23:35:36 +08:00
|
|
|
}) {
|
2018-11-15 00:31:32 +08:00
|
|
|
const hashDigest = this._getHashDigest(chunkGraph, dependencyTemplates);
|
2019-10-09 19:36:44 +08:00
|
|
|
if (this._cachedCodeGenerationHash === hashDigest) {
|
2018-11-15 00:31:32 +08:00
|
|
|
// We can reuse the cached data
|
2019-10-09 19:36:44 +08:00
|
|
|
return this._cachedCodeGeneration;
|
2018-01-24 06:09:26 +08:00
|
|
|
}
|
2017-02-16 05:01:09 +08:00
|
|
|
|
2018-11-15 00:31:32 +08:00
|
|
|
/** @type {Set<string>} */
|
|
|
|
const runtimeRequirements = new Set();
|
|
|
|
|
2018-11-17 01:18:44 +08:00
|
|
|
if (!this.buildInfo.parsed) {
|
|
|
|
runtimeRequirements.add(RuntimeGlobals.module);
|
|
|
|
runtimeRequirements.add(RuntimeGlobals.exports);
|
2019-08-27 02:21:07 +08:00
|
|
|
runtimeRequirements.add(RuntimeGlobals.thisAsExports);
|
2018-11-17 01:18:44 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 04:29:46 +08:00
|
|
|
const sources = new Map();
|
2019-11-18 21:29:19 +08:00
|
|
|
for (const type of this.generator.getTypes(this)) {
|
2019-10-09 04:29:46 +08:00
|
|
|
const source = this.error
|
|
|
|
? new RawSource(
|
|
|
|
"throw new Error(" + JSON.stringify(this.error.message) + ");"
|
|
|
|
)
|
|
|
|
: this.generator.generate(this, {
|
|
|
|
dependencyTemplates,
|
|
|
|
runtimeTemplate,
|
|
|
|
moduleGraph,
|
|
|
|
chunkGraph,
|
|
|
|
runtimeRequirements,
|
|
|
|
type
|
|
|
|
});
|
|
|
|
|
2019-11-20 17:48:38 +08:00
|
|
|
if (source) {
|
|
|
|
sources.set(type, new CachedSource(source));
|
|
|
|
}
|
2019-10-09 04:29:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {CodeGenerationResult} */
|
2018-11-15 00:31:32 +08:00
|
|
|
const resultEntry = {
|
2019-10-09 04:29:46 +08:00
|
|
|
sources,
|
|
|
|
runtimeRequirements
|
|
|
|
};
|
2019-10-09 19:36:44 +08:00
|
|
|
this._cachedCodeGeneration = resultEntry;
|
|
|
|
this._cachedCodeGenerationHash = hashDigest;
|
2018-11-15 00:31:32 +08:00
|
|
|
return resultEntry;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:20:35 +08:00
|
|
|
/**
|
|
|
|
* @returns {Source | null} the original source for the module before webpack transformation
|
|
|
|
*/
|
2017-04-03 15:48:55 +08:00
|
|
|
originalSource() {
|
|
|
|
return this._source;
|
|
|
|
}
|
2017-04-03 15:33:11 +08:00
|
|
|
|
2018-09-12 00:47:55 +08:00
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
invalidateBuild() {
|
|
|
|
this._forceBuild = true;
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:38:34 +08:00
|
|
|
/**
|
2018-09-26 15:14:44 +08:00
|
|
|
* @param {NeedBuildContext} context context info
|
|
|
|
* @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
|
|
|
* @returns {void}
|
2018-07-25 18:38:34 +08:00
|
|
|
*/
|
2018-09-27 13:22:19 +08:00
|
|
|
needBuild({ fileSystemInfo }, callback) {
|
2018-09-12 00:47:55 +08:00
|
|
|
// build if enforced
|
2018-09-26 15:14:44 +08:00
|
|
|
if (this._forceBuild) return callback(null, true);
|
2018-09-12 00:47:55 +08:00
|
|
|
|
2018-09-26 15:14:44 +08:00
|
|
|
// always try to build in case of an error
|
|
|
|
if (this.error) return callback(null, true);
|
2017-11-06 20:02:35 +08:00
|
|
|
|
2020-06-26 05:14:50 +08:00
|
|
|
// always build when module resource is an absolute url, except data url
|
|
|
|
if (this.scheme && this.scheme !== "data") return callback(null, true);
|
|
|
|
|
2018-09-26 15:14:44 +08:00
|
|
|
// always build when module is not cacheable
|
|
|
|
if (!this.buildInfo.cacheable) return callback(null, true);
|
|
|
|
|
2020-01-28 21:01:19 +08:00
|
|
|
// build when there is no snapshot to check
|
|
|
|
if (!this.buildInfo.snapshot) return callback(null, true);
|
|
|
|
|
2019-01-09 20:23:26 +08:00
|
|
|
// check snapshot for validity
|
|
|
|
fileSystemInfo.checkSnapshotValid(this.buildInfo.snapshot, (err, valid) => {
|
|
|
|
callback(err, !valid);
|
|
|
|
});
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
2018-12-04 18:23:40 +08:00
|
|
|
* @param {string=} type the source type for which the size should be estimated
|
2019-05-13 18:29:29 +08:00
|
|
|
* @returns {number} the estimated size of the module (must be non-zero)
|
2018-07-25 18:12:17 +08:00
|
|
|
*/
|
2018-12-04 18:23:40 +08:00
|
|
|
size(type) {
|
2019-11-08 19:46:37 +08:00
|
|
|
const cachedSize =
|
|
|
|
this._sourceSizes === undefined ? undefined : this._sourceSizes.get(type);
|
2019-09-26 05:51:38 +08:00
|
|
|
if (cachedSize !== undefined) {
|
|
|
|
return cachedSize;
|
2019-09-20 08:44:49 +08:00
|
|
|
}
|
2019-09-26 05:51:38 +08:00
|
|
|
const size = Math.max(1, this.generator.getSize(this, type));
|
2019-11-08 19:46:37 +08:00
|
|
|
if (this._sourceSizes === undefined) {
|
|
|
|
this._sourceSizes = new Map();
|
|
|
|
}
|
2019-09-26 05:51:38 +08:00
|
|
|
this._sourceSizes.set(type, size);
|
|
|
|
return size;
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:17:51 +08:00
|
|
|
/**
|
|
|
|
* @param {Hash} hash the hash used to track dependencies
|
2018-08-23 23:07:23 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph the chunk graph
|
2018-07-21 00:17:51 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-23 23:07:23 +08:00
|
|
|
updateHash(hash, chunkGraph) {
|
2018-12-30 20:45:20 +08:00
|
|
|
hash.update(this.buildInfo.hash);
|
2018-08-23 23:07:23 +08:00
|
|
|
super.updateHash(hash, chunkGraph);
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
2018-10-09 20:30:59 +08:00
|
|
|
|
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
|
|
|
// constructor
|
|
|
|
write(this.type);
|
|
|
|
write(this.resource);
|
|
|
|
// deserialize
|
|
|
|
write(this._source);
|
2019-09-26 05:51:38 +08:00
|
|
|
write(this._sourceSizes);
|
2018-10-09 20:30:59 +08:00
|
|
|
write(this.error);
|
2019-10-09 19:36:44 +08:00
|
|
|
write(this._cachedCodeGenerationHash);
|
2019-10-09 04:29:46 +08:00
|
|
|
write(this._cachedCodeGeneration);
|
2018-10-09 20:30:59 +08:00
|
|
|
write(this._lastSuccessfulBuildMeta);
|
|
|
|
write(this._forceBuild);
|
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
const obj = new NormalModule({
|
|
|
|
type: read(),
|
|
|
|
resource: read(),
|
|
|
|
// will be filled by updateCacheModule
|
|
|
|
request: null,
|
|
|
|
userRequest: null,
|
|
|
|
rawRequest: null,
|
|
|
|
loaders: null,
|
|
|
|
matchResource: null,
|
|
|
|
parser: null,
|
|
|
|
generator: null,
|
|
|
|
resolveOptions: null
|
|
|
|
});
|
|
|
|
obj.deserialize(context);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
this._source = read();
|
2019-09-26 05:51:38 +08:00
|
|
|
this._sourceSizes = read();
|
2018-10-09 20:30:59 +08:00
|
|
|
this.error = read();
|
2019-10-09 19:36:44 +08:00
|
|
|
this._cachedCodeGenerationHash = read();
|
2019-10-09 04:29:46 +08:00
|
|
|
this._cachedCodeGeneration = read();
|
2018-10-09 20:30:59 +08:00
|
|
|
this._lastSuccessfulBuildMeta = read();
|
|
|
|
this._forceBuild = read();
|
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2017-02-11 11:16:18 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 20:30:59 +08:00
|
|
|
makeSerializable(NormalModule, "webpack/lib/NormalModule");
|
|
|
|
|
2017-02-11 11:16:18 +08:00
|
|
|
module.exports = NormalModule;
|