webpack/lib/asset/AssetGenerator.js

210 lines
5.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sergey Melyukov @smelukov
*/
"use strict";
const mime = require("mime");
const path = require("path");
const { RawSource } = require("webpack-sources");
const Generator = require("../Generator");
const RuntimeGlobals = require("../RuntimeGlobals");
/** @typedef {import("webpack-sources").Source} Source */
2019-11-18 00:49:48 +08:00
/** @typedef {import("../../declarations/plugins/AssetModulesPlugin").AssetModulesPluginOptions} AssetModulesPluginOptions */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/**
* @type {Map<string|Buffer, string|null>}
*/
const dataUrlFnCache = new Map();
/**
* @param {NormalModule} module the module
* @param {AssetModulesPluginOptions} options the options to encode
* @returns {boolean} should emit additional asset for the module
*/
const shouldEmitAsset = (module, options) => {
const originalSource = module.originalSource();
if (typeof options.dataUrl === "function") {
return (
options.dataUrl.call(null, module, module.nameForCondition()) === false
);
}
if (options.dataUrl === false) {
return true;
}
return originalSource.size() > options.dataUrl.maxSize;
};
/**
* @param {AssetModulesPluginOptions} options the options to the encoder
* @returns {AssetModulesPluginOptions} normalized options
*/
const prepareOptions = (options = {}) => {
const dataUrl = options.dataUrl || {};
if (options.dataUrl === false) {
return {
dataUrl: false
};
}
if (typeof dataUrl === "function") {
return {
dataUrl: (source, resourcePath) => {
if (dataUrlFnCache.has(source)) {
return dataUrlFnCache.get(source);
}
const encoded = dataUrl.call(null, source, resourcePath);
dataUrlFnCache.set(source, encoded);
return encoded;
}
};
}
return {
dataUrl: {
encoding: "base64",
maxSize: 8192,
...dataUrl
}
};
};
/**
* @param {NormalModule} module a module to encode
* @param {AssetModulesPluginOptions} options the options to the encoder
* @returns {string|null} encoded source
*/
const encode = (module, options) => {
if (shouldEmitAsset(module, options)) {
return null;
}
const originalSource = module.originalSource();
let content = originalSource.source();
if (typeof options.dataUrl === "function") {
return options.dataUrl.call(null, content, module.nameForCondition());
}
// @ts-ignore non-false dataUrl ensures in shouldEmitAsset above
const encoding = options.dataUrl.encoding;
const extname = path.extname(module.nameForCondition());
// @ts-ignore non-false dataUrl ensures in shouldEmitAsset above
const mimeType = options.dataUrl.mimetype || mime.getType(extname);
if (encoding === "base64") {
if (typeof content === "string") {
content = Buffer.from(content);
}
content = content.toString("base64");
}
return `data:${mimeType}${encoding ? `;${encoding}` : ""},${content}`;
};
const JS_TYPES = new Set(["javascript"]);
const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]);
2019-11-19 21:32:40 +08:00
class AssetGenerator extends Generator {
2019-11-16 00:27:36 +08:00
/**
* @param {Compilation} compilation the compilation
2019-11-18 00:49:48 +08:00
* @param {AssetModulesPluginOptions} options the options
2019-11-16 00:27:36 +08:00
*/
constructor(compilation, options) {
2019-11-16 00:27:36 +08:00
super();
this.compilation = compilation;
this.options = prepareOptions(options);
2019-11-16 00:27:36 +08:00
}
/**
* @param {NormalModule} module module for which the code should be generated
* @param {GenerateContext} generateContext context for generate
* @returns {Source} generated code
*/
generate(module, { chunkGraph, runtimeTemplate, runtimeRequirements, type }) {
if (type === "asset") {
return module.originalSource();
}
runtimeRequirements.add(RuntimeGlobals.module);
if (!shouldEmitAsset(module, this.options)) {
const encodedSource = encode(module, this.options);
return new RawSource(
`${RuntimeGlobals.module}.exports = ${JSON.stringify(encodedSource)};`
);
}
const filename = module.nameForCondition();
const { assetModuleFilename } = runtimeTemplate.outputOptions;
const url = this.compilation.getAssetPath(assetModuleFilename, {
module,
filename,
chunkGraph
});
runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p
// TODO: (hiroppy) use ESM
return new RawSource(
`${RuntimeGlobals.module}.exports = ${
RuntimeGlobals.publicPath
} + ${JSON.stringify(url)};`
);
}
/**
* @param {NormalModule} module fresh module
* @returns {Set<string>} available types (do not mutate)
*/
getTypes(module) {
if (shouldEmitAsset(module, this.options)) {
return JS_AND_ASSET_TYPES;
}
return JS_TYPES;
}
/**
* @param {NormalModule} module the module
* @param {string=} type source type
* @returns {number} estimate size of the module
*/
getSize(module, type = module.type) {
const originalSource = module.originalSource();
2019-06-06 05:29:53 +08:00
if (!originalSource) {
return 0;
}
if (type === "asset") {
return originalSource.size();
}
if (shouldEmitAsset(module, this.options)) {
// it's only estimated so this number is probably fine
// Example: m.exports=r.p+"0123456789012345678901.ext"
return 42;
} else {
// roughly for data url (a little bit tricky)
return originalSource.size() * 1.5;
}
}
}
2019-11-19 21:32:40 +08:00
module.exports = AssetGenerator;