2019-03-31 22:12:19 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
2019-11-19 22:36:11 +08:00
|
|
|
Author Sergey Melyukov @smelukov
|
2019-03-31 22:12:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-11-19 22:36:11 +08:00
|
|
|
const mime = require("mime");
|
|
|
|
const path = require("path");
|
2019-11-18 21:29:19 +08:00
|
|
|
const { RawSource } = require("webpack-sources");
|
2019-03-31 22:12:19 +08:00
|
|
|
const Generator = require("../Generator");
|
2019-11-18 21:29:19 +08:00
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
2019-03-31 22:12:19 +08:00
|
|
|
|
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
2019-11-18 00:49:48 +08:00
|
|
|
/** @typedef {import("../../declarations/plugins/AssetModulesPlugin").AssetModulesPluginOptions} AssetModulesPluginOptions */
|
2019-11-18 21:29:19 +08:00
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
2019-03-31 22:12:19 +08:00
|
|
|
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
2019-11-18 21:29:19 +08:00
|
|
|
/** @typedef {import("../Module")} Module */
|
2019-03-31 22:12:19 +08:00
|
|
|
/** @typedef {import("../NormalModule")} NormalModule */
|
2019-11-18 21:29:19 +08:00
|
|
|
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
2019-03-31 22:12:19 +08:00
|
|
|
|
2019-11-19 22:36:11 +08:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
};
|
|
|
|
|
2019-11-18 21:29:19 +08:00
|
|
|
const JS_TYPES = new Set(["javascript"]);
|
|
|
|
const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]);
|
2019-03-31 22:12:19 +08:00
|
|
|
|
2019-11-19 21:32:40 +08:00
|
|
|
class AssetGenerator extends Generator {
|
2019-11-16 00:27:36 +08:00
|
|
|
/**
|
2019-11-18 21:29:19 +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
|
|
|
*/
|
2019-11-18 21:29:19 +08:00
|
|
|
constructor(compilation, options) {
|
2019-11-16 00:27:36 +08:00
|
|
|
super();
|
2019-11-18 21:29:19 +08:00
|
|
|
this.compilation = compilation;
|
2019-11-26 17:00:11 +08:00
|
|
|
this.options = options;
|
2019-11-16 00:27:36 +08:00
|
|
|
}
|
|
|
|
|
2019-03-31 22:12:19 +08:00
|
|
|
/**
|
2019-11-18 21:29:19 +08:00
|
|
|
* @param {NormalModule} module module for which the code should be generated
|
|
|
|
* @param {GenerateContext} generateContext context for generate
|
|
|
|
* @returns {Source} generated code
|
2019-03-31 22:12:19 +08:00
|
|
|
*/
|
2019-11-18 21:29:19 +08:00
|
|
|
generate(module, { chunkGraph, runtimeTemplate, runtimeRequirements, type }) {
|
|
|
|
if (type === "asset") {
|
|
|
|
return module.originalSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
runtimeRequirements.add(RuntimeGlobals.module);
|
|
|
|
|
2019-11-19 22:36:11 +08:00
|
|
|
if (!shouldEmitAsset(module, this.options)) {
|
2019-11-26 16:53:47 +08:00
|
|
|
const originalSource = module.originalSource();
|
|
|
|
let content = originalSource.source();
|
|
|
|
|
|
|
|
let encodedSource;
|
|
|
|
if (typeof this.options.dataUrl === "function") {
|
|
|
|
encodedSource = this.options.dataUrl.call(
|
|
|
|
null,
|
|
|
|
content,
|
|
|
|
module.nameForCondition()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// @ts-ignore non-false dataUrl ensures in shouldEmitAsset above
|
|
|
|
const encoding = this.options.dataUrl.encoding;
|
|
|
|
const extname = path.extname(module.nameForCondition());
|
|
|
|
// @ts-ignore non-false dataUrl ensures in shouldEmitAsset above
|
|
|
|
const mimeType = this.options.dataUrl.mimetype || mime.getType(extname);
|
|
|
|
|
|
|
|
if (encoding === "base64") {
|
|
|
|
if (typeof content === "string") {
|
|
|
|
content = Buffer.from(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
content = content.toString("base64");
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedSource = `data:${mimeType}${
|
|
|
|
encoding ? `;${encoding}` : ""
|
|
|
|
},${content}`;
|
|
|
|
}
|
2019-11-18 21:29:19 +08:00
|
|
|
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)};`
|
|
|
|
);
|
2019-03-31 22:12:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 21:29:19 +08:00
|
|
|
* @param {NormalModule} module fresh module
|
|
|
|
* @returns {Set<string>} available types (do not mutate)
|
2019-03-31 22:12:19 +08:00
|
|
|
*/
|
2019-11-18 21:29:19 +08:00
|
|
|
getTypes(module) {
|
2019-11-19 22:36:11 +08:00
|
|
|
if (shouldEmitAsset(module, this.options)) {
|
2019-11-18 21:29:19 +08:00
|
|
|
return JS_AND_ASSET_TYPES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TYPES;
|
2019-03-31 22:12:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {NormalModule} module the module
|
|
|
|
* @param {string=} type source type
|
|
|
|
* @returns {number} estimate size of the module
|
|
|
|
*/
|
2019-11-18 21:29:19 +08:00
|
|
|
getSize(module, type = module.type) {
|
2019-03-31 22:12:19 +08:00
|
|
|
const originalSource = module.originalSource();
|
2019-06-06 05:29:53 +08:00
|
|
|
|
2019-03-31 22:12:19 +08:00
|
|
|
if (!originalSource) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-18 21:29:19 +08:00
|
|
|
|
|
|
|
if (type === "asset") {
|
|
|
|
return originalSource.size();
|
|
|
|
}
|
|
|
|
|
2019-11-19 22:36:11 +08:00
|
|
|
if (shouldEmitAsset(module, this.options)) {
|
2019-11-18 21:29:19 +08:00
|
|
|
// 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-03-31 22:12:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 21:32:40 +08:00
|
|
|
module.exports = AssetGenerator;
|