add source asset type

This commit is contained in:
Tobias Koppers 2019-11-26 14:05:07 +01:00
parent 317da38171
commit 459fa25015
4 changed files with 101 additions and 0 deletions

View File

@ -11,6 +11,7 @@ const parserSchema = require("../../schemas/plugins/AssetModulesPluginParser.jso
const { compareModulesByIdentifier } = require("../util/comparators");
const AssetGenerator = require("./AssetGenerator");
const AssetParser = require("./AssetParser");
const AssetSourceGenerator = require("./AssetSourceGenerator");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Chunk")} Chunk */
@ -57,6 +58,11 @@ class AssetModulesPlugin {
.tap(plugin, parserOptions => {
return new AssetParser(false);
});
normalModuleFactory.hooks.createParser
.for("asset/source")
.tap(plugin, parserOptions => {
return new AssetParser(false);
});
for (const type of ["asset", "asset/inline"]) {
normalModuleFactory.hooks.createGenerator
@ -84,6 +90,11 @@ class AssetModulesPlugin {
.tap(plugin, () => {
return new AssetGenerator(compilation);
});
normalModuleFactory.hooks.createGenerator
.for("asset/source")
.tap(plugin, () => {
return new AssetSourceGenerator();
});
compilation.hooks.renderManifest.tap(plugin, (result, options) => {
const { chunkGraph } = compilation;

View File

@ -0,0 +1,71 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sergey Melyukov @smelukov
*/
"use strict";
const { RawSource } = require("webpack-sources");
const Generator = require("../Generator");
const RuntimeGlobals = require("../RuntimeGlobals");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../NormalModule")} NormalModule */
const TYPES = new Set(["javascript"]);
class AssetSourceGenerator extends Generator {
/**
* @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 }) {
runtimeRequirements.add(RuntimeGlobals.module);
const originalSource = module.originalSource();
if (!originalSource) {
return new RawSource("");
}
const content = originalSource.source();
let encodedSource;
if (typeof content === "string") {
encodedSource = content;
} else {
encodedSource = content.toString("utf-8");
}
return new RawSource(
`${RuntimeGlobals.module}.exports = ${JSON.stringify(encodedSource)};`
);
}
/**
* @param {NormalModule} module fresh module
* @returns {Set<string>} available types (do not mutate)
*/
getTypes(module) {
return 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();
if (!originalSource) {
return 0;
}
// Example: m.exports="abcd"
return originalSource.size() + 12;
}
}
module.exports = AssetSourceGenerator;

View File

@ -0,0 +1,5 @@
import svg from "../_images/file.svg";
it("should receive asset source", () => {
expect(svg).toMatch(/^<svg.+<\/svg>\s*$/);
});

View File

@ -0,0 +1,14 @@
module.exports = {
mode: "development",
module: {
rules: [
{
test: /\.svg$/,
type: "asset/source"
}
]
},
experiments: {
asset: true
}
};