diff --git a/README.md b/README.md index 14b373db..b78ce12d 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,7 @@ module.exports = { | Name | Type | Default | Description | | :-----------------------------: | :------------------: | :--------------------------------: | :-------------------------------------------------------------------------------- | | **[`publicPath`](#publicPath)** | `{String\|Function}` | `webpackOptions.output.publicPath` | Specifies a custom public path for the external resources like images, files, etc | +| **[`emit`](#emit)** | `{Boolean}` | `true` | If false, the plugin will extract the CSS but **will not** emit the file | | **[`esModule`](#esModule)** | `{Boolean}` | `true` | Use ES modules syntax | | **[`modules`](#modules)** | `{Object}` | `undefined` | Configuration CSS Modules | @@ -344,6 +345,14 @@ module.exports = { }; ``` +#### `emit` + +Type: `Boolean` +Default: `true` + +If true, emits a file (writes a file to the filesystem). If false, the plugin will extract the CSS but **will not** emit the file. +It is often useful to disable this option for server-side packages. + #### `esModule` Type: `Boolean` diff --git a/src/loader-options.json b/src/loader-options.json index 7e66190d..a863e270 100644 --- a/src/loader-options.json +++ b/src/loader-options.json @@ -12,6 +12,9 @@ } ] }, + "emit": { + "type": "boolean" + }, "esModule": { "type": "boolean" }, diff --git a/src/loader.js b/src/loader.js index 15c2c045..b775f612 100644 --- a/src/loader.js +++ b/src/loader.js @@ -189,21 +189,16 @@ export function pitch(request) { childCompiler.runAsChild((error, entries, compilation) => { const assets = Object.create(null); const assetsInfo = new Map(); + const emit = typeof options.emit !== 'undefined' ? options.emit : true; - for (const asset of compilation.getAssets()) { - assets[asset.name] = asset.source; - assetsInfo.set(asset.name, asset.info); + if (emit) { + for (const asset of compilation.getAssets()) { + assets[asset.name] = asset.source; + assetsInfo.set(asset.name, asset.info); + } } const addDependencies = (dependencies) => { - if (!Array.isArray(dependencies) && dependencies != null) { - throw new Error( - `Exported value was not extracted as an array: ${JSON.stringify( - dependencies - )}` - ); - } - const identifierCountMap = new Map(); let lastDep; @@ -282,6 +277,8 @@ export function pitch(request) { if (!Array.isArray(exports)) { dependencies = [[null, exports]]; + } else if (!emit) { + dependencies = exports; } else { dependencies = exports.map(([id, content, media, sourceMap]) => { const module = findModuleById(compilation, id); diff --git a/test/__snapshots__/validate-loader-options.test.js.snap b/test/__snapshots__/validate-loader-options.test.js.snap index dd4b5ffa..784ab09f 100644 --- a/test/__snapshots__/validate-loader-options.test.js.snap +++ b/test/__snapshots__/validate-loader-options.test.js.snap @@ -1,5 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`validate options should throw an error on the "emit" option with "1" value 1`] = ` +"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. + - options.emit should be a boolean." +`; + exports[`validate options should throw an error on the "esModule" option with "1" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options.esModule should be a boolean." @@ -29,47 +34,47 @@ exports[`validate options should throw an error on the "publicPath" option with exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { publicPath?, esModule?, modules? }" + object { publicPath?, emit?, esModule?, modules? }" `; diff --git a/test/validate-loader-options.test.js b/test/validate-loader-options.test.js index 14cfeda1..097d2236 100644 --- a/test/validate-loader-options.test.js +++ b/test/validate-loader-options.test.js @@ -6,6 +6,10 @@ describe('validate options', () => { success: ['/public/path/to/'], failure: [true], }, + emit: { + success: [true, false], + failure: [1], + }, esModule: { success: [true, false], failure: [1],