123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- "use strict";
- const mimeTypes = require("mime-types");
- const path = require("path");
- const { RawSource } = require("webpack-sources");
- const ConcatenationScope = require("../ConcatenationScope");
- const Generator = require("../Generator");
- const {
- NO_TYPES,
- ASSET_TYPES,
- ASSET_AND_JS_TYPES,
- ASSET_AND_JS_AND_CSS_URL_TYPES,
- ASSET_AND_CSS_URL_TYPES,
- JS_TYPES,
- JS_AND_CSS_URL_TYPES,
- CSS_URL_TYPES
- } = require("../ModuleSourceTypesConstants");
- const { ASSET_MODULE_TYPE } = require("../ModuleTypeConstants");
- const RuntimeGlobals = require("../RuntimeGlobals");
- const CssUrlDependency = require("../dependencies/CssUrlDependency");
- const createHash = require("../util/createHash");
- const { makePathsRelative } = require("../util/identifier");
- const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
- const mergeMaybeArrays = (a, b) => {
- const set = new Set();
- if (Array.isArray(a)) for (const item of a) set.add(item);
- else set.add(a);
- if (Array.isArray(b)) for (const item of b) set.add(item);
- else set.add(b);
- return Array.from(set);
- };
- /**
- * @template {object} T
- * @template {object} U
- * @param {TODO} a a
- * @param {TODO} b b
- * @returns {T & U} object
- */
- const mergeAssetInfo = (a, b) => {
- const result = { ...a, ...b };
- for (const key of Object.keys(a)) {
- if (key in b) {
- if (a[key] === b[key]) continue;
- switch (key) {
- case "fullhash":
- case "chunkhash":
- case "modulehash":
- case "contenthash":
- result[key] = mergeMaybeArrays(a[key], b[key]);
- break;
- case "immutable":
- case "development":
- case "hotModuleReplacement":
- case "javascriptModule":
- result[key] = a[key] || b[key];
- break;
- case "related":
- result[key] = mergeRelatedInfo(a[key], b[key]);
- break;
- default:
- throw new Error(`Can't handle conflicting asset info for ${key}`);
- }
- }
- }
- return result;
- };
- /**
- * @template {object} T
- * @template {object} U
- * @param {TODO} a a
- * @param {TODO} b b
- * @returns {T & U} object
- */
- const mergeRelatedInfo = (a, b) => {
- const result = { ...a, ...b };
- for (const key of Object.keys(a)) {
- if (key in b) {
- if (a[key] === b[key]) continue;
- result[key] = mergeMaybeArrays(a[key], b[key]);
- }
- }
- return result;
- };
- /**
- * @param {"base64" | false} encoding encoding
- * @param {Source} source source
- * @returns {string} encoded data
- */
- const encodeDataUri = (encoding, source) => {
- /** @type {string | undefined} */
- let encodedContent;
- switch (encoding) {
- case "base64": {
- encodedContent = source.buffer().toString("base64");
- break;
- }
- case false: {
- const content = source.source();
- if (typeof content !== "string") {
- encodedContent = content.toString("utf-8");
- }
- encodedContent = encodeURIComponent(
- /** @type {string} */
- (encodedContent)
- ).replace(
- /[!'()*]/g,
- character =>
- `%${/** @type {number} */ (character.codePointAt(0)).toString(16)}`
- );
- break;
- }
- default:
- throw new Error(`Unsupported encoding '${encoding}'`);
- }
- return encodedContent;
- };
- /**
- * @param {string} encoding encoding
- * @param {string} content content
- * @returns {Buffer} decoded content
- */
- const decodeDataUriContent = (encoding, content) => {
- const isBase64 = encoding === "base64";
- if (isBase64) {
- return Buffer.from(content, "base64");
- }
- // If we can't decode return the original body
- try {
- return Buffer.from(decodeURIComponent(content), "ascii");
- } catch (_) {
- return Buffer.from(content, "ascii");
- }
- };
- const DEFAULT_ENCODING = "base64";
- class AssetGenerator extends Generator {
-
- constructor(
- moduleGraph,
- dataUrlOptions,
- filename,
- publicPath,
- outputPath,
- emit
- ) {
- super();
- this.dataUrlOptions = dataUrlOptions;
- this.filename = filename;
- this.publicPath = publicPath;
- this.outputPath = outputPath;
- this.emit = emit;
- this._moduleGraph = moduleGraph;
- }
-
- getSourceFileName(module, runtimeTemplate) {
- return makePathsRelative(
- runtimeTemplate.compilation.compiler.context,
- module.matchResource || module.resource,
- runtimeTemplate.compilation.compiler.root
- ).replace(/^\.\//, "");
- }
-
- getConcatenationBailoutReason(module, context) {
- return undefined;
- }
-
- getMimeType(module) {
- if (typeof this.dataUrlOptions === "function") {
- throw new Error(
- "This method must not be called when dataUrlOptions is a function"
- );
- }
-
- let mimeType =
-
- (this.dataUrlOptions).mimetype;
- if (mimeType === undefined) {
- const ext = path.extname(
-
- (module.nameForCondition())
- );
- if (
- module.resourceResolveData &&
- module.resourceResolveData.mimetype !== undefined
- ) {
- mimeType =
- module.resourceResolveData.mimetype +
- module.resourceResolveData.parameters;
- } else if (ext) {
- mimeType = mimeTypes.lookup(ext);
- if (typeof mimeType !== "string") {
- throw new Error(
- "DataUrl can't be generated automatically, " +
- `because there is no mimetype for "${ext}" in mimetype database. ` +
- 'Either pass a mimetype via "generator.mimetype" or ' +
- 'use type: "asset/resource" to create a resource file instead of a DataUrl'
- );
- }
- }
- }
- if (typeof mimeType !== "string") {
- throw new Error(
- "DataUrl can't be generated automatically. " +
- 'Either pass a mimetype via "generator.mimetype" or ' +
- 'use type: "asset/resource" to create a resource file instead of a DataUrl'
- );
- }
- return (mimeType);
- }
-
- generateDataUri(module) {
- const source = (module.originalSource());
- let encodedSource;
- if (typeof this.dataUrlOptions === "function") {
- encodedSource = this.dataUrlOptions.call(null, source.source(), {
- filename: module.matchResource || module.resource,
- module
- });
- } else {
-
- let encoding =
-
- (this.dataUrlOptions).encoding;
- if (
- encoding === undefined &&
- module.resourceResolveData &&
- module.resourceResolveData.encoding !== undefined
- ) {
- encoding = module.resourceResolveData.encoding;
- }
- if (encoding === undefined) {
- encoding = DEFAULT_ENCODING;
- }
- const mimeType = this.getMimeType(module);
- let encodedContent;
- if (
- module.resourceResolveData &&
- module.resourceResolveData.encoding === encoding &&
- decodeDataUriContent(
- module.resourceResolveData.encoding,
- module.resourceResolveData.encodedContent
- ).equals(source.buffer())
- ) {
- encodedContent = module.resourceResolveData.encodedContent;
- } else {
- encodedContent = encodeDataUri(encoding, source);
- }
- encodedSource = `data:${mimeType}${
- encoding ? `;${encoding}` : ""
- },${encodedContent}`;
- }
- return encodedSource;
- }
-
- _getFilenameWithInfo(
- module,
- { runtime, runtimeTemplate, chunkGraph },
- contentHash
- ) {
- const assetModuleFilename =
- this.filename ||
-
- (runtimeTemplate.outputOptions.assetModuleFilename);
- const sourceFilename = this.getSourceFileName(module, runtimeTemplate);
- let { path: filename, info: assetInfo } =
- runtimeTemplate.compilation.getAssetPathWithInfo(assetModuleFilename, {
- module,
- runtime,
- filename: sourceFilename,
- chunkGraph,
- contentHash
- });
- const originalFilename = filename;
- if (this.outputPath) {
- const { path: outputPath, info } =
- runtimeTemplate.compilation.getAssetPathWithInfo(this.outputPath, {
- module,
- runtime,
- filename: sourceFilename,
- chunkGraph,
- contentHash
- });
- filename = path.posix.join(outputPath, filename);
- assetInfo = mergeAssetInfo(assetInfo, info);
- }
- return { originalFilename, filename, assetInfo };
- }
-
- _getAssetPathWithInfo(
- module,
- { runtimeTemplate, runtime, chunkGraph, type, runtimeRequirements },
- filename,
- assetInfo,
- contentHash
- ) {
- const sourceFilename = this.getSourceFileName(module, runtimeTemplate);
- let assetPath;
- if (this.publicPath !== undefined && type === "javascript") {
- const { path, info } = runtimeTemplate.compilation.getAssetPathWithInfo(
- this.publicPath,
- {
- module,
- runtime,
- filename: sourceFilename,
- chunkGraph,
- contentHash
- }
- );
- assetInfo = mergeAssetInfo(assetInfo, info);
- assetPath = JSON.stringify(path + filename);
- } else if (this.publicPath !== undefined && type === "css-url") {
- const { path, info } = runtimeTemplate.compilation.getAssetPathWithInfo(
- this.publicPath,
- {
- module,
- runtime,
- filename: sourceFilename,
- chunkGraph,
- contentHash
- }
- );
- assetInfo = mergeAssetInfo(assetInfo, info);
- assetPath = path + filename;
- } else if (type === "javascript") {
-
- runtimeRequirements.add(RuntimeGlobals.publicPath);
- assetPath = runtimeTemplate.concatenation(
- { expr: RuntimeGlobals.publicPath },
- filename
- );
- } else if (type === "css-url") {
- const compilation = runtimeTemplate.compilation;
- const path =
- compilation.outputOptions.publicPath === "auto"
- ? CssUrlDependency.PUBLIC_PATH_AUTO
- : compilation.getAssetPath(
-
- (compilation.outputOptions.publicPath),
- {
- hash: compilation.hash
- }
- );
- assetPath = path + filename;
- }
- return {
-
- assetPath: (assetPath),
- assetInfo: { sourceFilename, ...assetInfo }
- };
- }
-
- generate(module, generateContext) {
- const {
- type,
- getData,
- runtimeTemplate,
- runtimeRequirements,
- concatenationScope
- } = generateContext;
- let content;
- const needContent = type === "javascript" || type === "css-url";
- const data = getData ? getData() : undefined;
- if (
-
- (module.buildInfo).dataUrl &&
- needContent
- ) {
- const encodedSource = this.generateDataUri(module);
- content =
- type === "javascript" ? JSON.stringify(encodedSource) : encodedSource;
- if (data) {
- data.set("url", { [type]: content, ...data.get("url") });
- }
- } else {
- const hash = createHash(
-
- (runtimeTemplate.outputOptions.hashFunction)
- );
- if (runtimeTemplate.outputOptions.hashSalt) {
- hash.update(runtimeTemplate.outputOptions.hashSalt);
- }
- hash.update( (module.originalSource()).buffer());
- const fullHash =
-
- (hash.digest(runtimeTemplate.outputOptions.hashDigest));
- if (data) {
- data.set("fullContentHash", fullHash);
- }
-
- (module.buildInfo).fullContentHash = fullHash;
-
- const contentHash = nonNumericOnlyHash(
- fullHash,
-
- (generateContext.runtimeTemplate.outputOptions.hashDigestLength)
- );
- if (data) {
- data.set("contentHash", contentHash);
- }
- const { originalFilename, filename, assetInfo } =
- this._getFilenameWithInfo(module, generateContext, contentHash);
- if (data) {
- data.set("filename", filename);
- }
- let { assetPath, assetInfo: newAssetInfo } = this._getAssetPathWithInfo(
- module,
- generateContext,
- originalFilename,
- assetInfo,
- contentHash
- );
- if (data && (type === "javascript" || type === "css-url")) {
- data.set("url", { [type]: assetPath, ...data.get("url") });
- }
- if (data && data.get("assetInfo")) {
- newAssetInfo = mergeAssetInfo(data.get("assetInfo"), newAssetInfo);
- }
- if (data) {
- data.set("assetInfo", newAssetInfo);
- }
-
-
-
-
- (module.buildInfo).filename = filename;
-
- (module.buildInfo).assetInfo = newAssetInfo;
- content = assetPath;
- }
- if (type === "javascript") {
- if (concatenationScope) {
- concatenationScope.registerNamespaceExport(
- ConcatenationScope.NAMESPACE_OBJECT_EXPORT
- );
- return new RawSource(
- `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
- ConcatenationScope.NAMESPACE_OBJECT_EXPORT
- } = ${content};`
- );
- }
- runtimeRequirements.add(RuntimeGlobals.module);
- return new RawSource(`${RuntimeGlobals.module}.exports = ${content};`);
- } else if (type === "css-url") {
- return null;
- }
- return (module.originalSource());
- }
-
- getTypes(module) {
- const sourceTypes = new Set();
- const connections = this._moduleGraph.getIncomingConnections(module);
- for (const connection of connections) {
- if (!connection.originModule) {
- continue;
- }
- sourceTypes.add(connection.originModule.type.split("/")[0]);
- }
- if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) {
- if (sourceTypes) {
- if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
- return JS_AND_CSS_URL_TYPES;
- } else if (sourceTypes.has("javascript")) {
- return JS_TYPES;
- } else if (sourceTypes.has("css")) {
- return CSS_URL_TYPES;
- }
- }
- return NO_TYPES;
- }
- if (sourceTypes) {
- if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
- return ASSET_AND_JS_AND_CSS_URL_TYPES;
- } else if (sourceTypes.has("javascript")) {
- return ASSET_AND_JS_TYPES;
- } else if (sourceTypes.has("css")) {
- return ASSET_AND_CSS_URL_TYPES;
- }
- }
- return ASSET_TYPES;
- }
-
- getSize(module, type) {
- switch (type) {
- case ASSET_MODULE_TYPE: {
- const originalSource = module.originalSource();
- if (!originalSource) {
- return 0;
- }
- return originalSource.size();
- }
- default:
- if (module.buildInfo && module.buildInfo.dataUrl) {
- const originalSource = module.originalSource();
- if (!originalSource) {
- return 0;
- }
-
-
-
-
- return originalSource.size() * 1.34 + 36;
- }
-
-
- return 42;
- }
- }
-
- updateHash(hash, updateHashContext) {
- const { module } = updateHashContext;
- if (
-
- (module.buildInfo).dataUrl
- ) {
- hash.update("data-url");
-
-
- if (typeof this.dataUrlOptions === "function") {
- const ident = (this.dataUrlOptions)
- .ident;
- if (ident) hash.update(ident);
- } else {
- const dataUrlOptions =
-
- (this.dataUrlOptions);
- if (
- dataUrlOptions.encoding &&
- dataUrlOptions.encoding !== DEFAULT_ENCODING
- ) {
- hash.update(dataUrlOptions.encoding);
- }
- if (dataUrlOptions.mimetype) hash.update(dataUrlOptions.mimetype);
-
- }
- } else {
- hash.update("resource");
- const { module, chunkGraph, runtime } = updateHashContext;
- const runtimeTemplate =
-
- (updateHashContext.runtimeTemplate);
- const pathData = {
- module,
- runtime,
- filename: this.getSourceFileName(module, runtimeTemplate),
- chunkGraph,
- contentHash: runtimeTemplate.contentHashReplacement
- };
- if (typeof this.publicPath === "function") {
- hash.update("path");
- const assetInfo = {};
- hash.update(this.publicPath(pathData, assetInfo));
- hash.update(JSON.stringify(assetInfo));
- } else if (this.publicPath) {
- hash.update("path");
- hash.update(this.publicPath);
- } else {
- hash.update("no-path");
- }
- const assetModuleFilename =
- this.filename ||
-
- (runtimeTemplate.outputOptions.assetModuleFilename);
- const { path: filename, info } =
- runtimeTemplate.compilation.getAssetPathWithInfo(
- assetModuleFilename,
- pathData
- );
- hash.update(filename);
- hash.update(JSON.stringify(info));
- }
- }
- }
- module.exports = AssetGenerator;
|