Skip to content

Commit

Permalink
add emit option for asset modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Mar 11, 2021
1 parent 85a6eee commit 7572217
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -2584,6 +2584,10 @@ export interface AssetParserOptions {
* Generator options for asset/resource modules.
*/
export interface AssetResourceGeneratorOptions {
/**
* Emit an output asset from this asset module. This can be set to 'false' to omit emitting e. g. for SSR.
*/
emit?: boolean;
/**
* Specifies the filename template of output files on disk. You must **not** specify an absolute path here, but the path may contain folders separated by '/'! The specified path is joined with the value of the 'output.path' option to determine the location on disk.
*/
Expand Down
6 changes: 4 additions & 2 deletions lib/asset/AssetGenerator.js
Expand Up @@ -31,11 +31,13 @@ class AssetGenerator extends Generator {
/**
* @param {AssetGeneratorOptions["dataUrl"]=} dataUrlOptions the options for the data url
* @param {string=} filename override for output.assetModuleFilename
* @param {boolean=} emit generate output asset
*/
constructor(dataUrlOptions, filename) {
constructor(dataUrlOptions, filename, emit) {
super();
this.dataUrlOptions = dataUrlOptions;
this.filename = filename;
this.emit = emit;
}

/**
Expand Down Expand Up @@ -174,7 +176,7 @@ class AssetGenerator extends Generator {
* @returns {Set<string>} available types (do not mutate)
*/
getTypes(module) {
if (module.buildInfo.dataUrl) {
if (module.buildInfo.dataUrl || this.emit === false) {
return JS_TYPES;
} else {
return JS_AND_ASSET_TYPES;
Expand Down
6 changes: 5 additions & 1 deletion lib/asset/AssetModulesPlugin.js
Expand Up @@ -124,7 +124,11 @@ class AssetModulesPlugin {

const AssetGenerator = getAssetGenerator();

return new AssetGenerator(dataUrl, filename);
return new AssetGenerator(
dataUrl,
filename,
generatorOptions.emit !== false
);
});
}
normalModuleFactory.hooks.createGenerator
Expand Down
8 changes: 8 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -101,6 +101,10 @@
"dataUrl": {
"$ref": "#/definitions/AssetGeneratorDataUrl"
},
"emit": {
"description": "Emit an output asset from this asset module. This can be set to 'false' to omit emitting e. g. for SSR.",
"type": "boolean"
},
"filename": {
"$ref": "#/definitions/FilenameTemplate"
}
Expand Down Expand Up @@ -168,6 +172,10 @@
"type": "object",
"additionalProperties": false,
"properties": {
"emit": {
"description": "Emit an output asset from this asset module. This can be set to 'false' to omit emitting e. g. for SSR.",
"type": "boolean"
},
"filename": {
"$ref": "#/definitions/FilenameTemplate"
}
Expand Down
11 changes: 11 additions & 0 deletions test/configCases/asset-modules/emit/index.js
@@ -0,0 +1,11 @@
import url from "../_images/file.png";
import fs from "fs";

it("should output asset with path", () => {
expect(url).toEqual("images/file.png");
expect(() => fs.statSync(url)).toThrowError(
expect.objectContaining({
code: "ENOENT"
})
);
});
18 changes: 18 additions & 0 deletions test/configCases/asset-modules/emit/webpack.config.js
@@ -0,0 +1,18 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
output: {
assetModuleFilename: "images/file[ext]"
},
module: {
rules: [
{
test: /\.png$/,
type: "asset",
generator: {
emit: false
}
}
]
}
};
5 changes: 5 additions & 0 deletions types.d.ts
Expand Up @@ -299,6 +299,11 @@ declare interface AssetParserOptions {
* Generator options for asset/resource modules.
*/
declare interface AssetResourceGeneratorOptions {
/**
* Emit an output asset from this asset module. This can be set to 'false' to omit emitting e. g. for SSR.
*/
emit?: boolean;

/**
* Specifies the filename template of output files on disk. You must **not** specify an absolute path here, but the path may contain folders separated by '/'! The specified path is joined with the value of the 'output.path' option to determine the location on disk.
*/
Expand Down

0 comments on commit 7572217

Please sign in to comment.