From 34ac7adb064e0db4bf1317544fcecbc52662fd16 Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Tue, 16 Mar 2021 20:29:04 +0000 Subject: [PATCH 1/6] feat: emitFile option Add emitFile option (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. --- README.md | 9 ++++++++ src/index.js | 9 ++++---- src/plugin-options.json | 4 ++++ .../validate-plugin-options.test.js.snap | 22 ++++++++++++------- test/validate-plugin-options.test.js | 4 ++++ 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 14b373db..05e66384 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,15 @@ This option determines the name of non-entry chunk files. Works like [`output.chunkFilename`](https://webpack.js.org/configuration/output/#outputchunkfilename) +#### `emitFile` + +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. + #### `ignoreOrder` Type: `Boolean` diff --git a/src/index.js b/src/index.js index 5ca6dbf7..5217f2a1 100644 --- a/src/index.js +++ b/src/index.js @@ -309,7 +309,7 @@ class MiniCssExtractPlugin { this._sortedModulesCache = new WeakMap(); this.options = Object.assign( - { filename: DEFAULT_FILENAME, ignoreOrder: false }, + { filename: DEFAULT_FILENAME, ignoreOrder: false, emitFile: true }, options ); @@ -426,6 +426,7 @@ class MiniCssExtractPlugin { new CssDependencyTemplate() ); + const { emitFile } = this.options; if (isWebpack4) { compilation.mainTemplate.hooks.renderManifest.tap( pluginName, @@ -439,7 +440,7 @@ class MiniCssExtractPlugin { const filenameTemplate = chunk.filenameTemplate || this.options.filename; - if (renderedModules.length > 0) { + if (renderedModules.length > 0 && emitFile) { result.push({ render: () => this.renderContentAsset( @@ -473,7 +474,7 @@ class MiniCssExtractPlugin { const filenameTemplate = chunk.filenameTemplate || this.options.chunkFilename; - if (renderedModules.length > 0) { + if (renderedModules.length > 0 && emitFile) { result.push({ render: () => this.renderContentAsset( @@ -515,7 +516,7 @@ class MiniCssExtractPlugin { ? this.options.filename : this.options.chunkFilename; - if (renderedModules.length > 0) { + if (renderedModules.length > 0 && emitFile) { result.push({ render: () => this.renderContentAsset( diff --git a/src/plugin-options.json b/src/plugin-options.json index 04e5c9ed..33809dc7 100644 --- a/src/plugin-options.json +++ b/src/plugin-options.json @@ -22,6 +22,10 @@ } ] }, + "emitFile": { + "description": "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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emitFile).", + "type": "boolean" + }, "ignoreOrder": { "type": "boolean" }, diff --git a/test/__snapshots__/validate-plugin-options.test.js.snap b/test/__snapshots__/validate-plugin-options.test.js.snap index 8d9e1b45..ed735a89 100644 --- a/test/__snapshots__/validate-plugin-options.test.js.snap +++ b/test/__snapshots__/validate-plugin-options.test.js.snap @@ -16,6 +16,12 @@ exports[`validate options should throw an error on the "chunkFilename" option wi * options.chunkFilename should be an instance of function." `; +exports[`validate options should throw an error on the "emitFile" option with "1" value 1`] = ` +"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema. + - options.emitFile should be a boolean. + -> 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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emitFile)." +`; + exports[`validate options should throw an error on the "filename" option with "true" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema. - options.filename should be one of these: @@ -99,47 +105,47 @@ exports[`validate options should throw an error on the "linkType" option with "i exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" `; diff --git a/test/validate-plugin-options.test.js b/test/validate-plugin-options.test.js index 0638f4a8..a4382f70 100644 --- a/test/validate-plugin-options.test.js +++ b/test/validate-plugin-options.test.js @@ -20,6 +20,10 @@ describe('validate options', () => { ], failure: [true], }, + emitFile: { + success: [true, false], + failure: [1], + }, ignoreOrder: { success: [true, false], failure: [1], From 350ba1a3bf296f5e7ea9bfbd1730b0458e72860f Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Thu, 18 Mar 2021 16:45:03 +0000 Subject: [PATCH 2/6] feat: emit option Renamed from emitFile --- README.md | 2 +- src/index.js | 10 ++++----- src/plugin-options.json | 4 ++-- .../validate-plugin-options.test.js.snap | 22 +++++++++---------- test/validate-plugin-options.test.js | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 05e66384..0fb13f64 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ This option determines the name of non-entry chunk files. Works like [`output.chunkFilename`](https://webpack.js.org/configuration/output/#outputchunkfilename) -#### `emitFile` +#### `emit` Type: `Boolean` Default: `true` diff --git a/src/index.js b/src/index.js index 5217f2a1..4f5069ec 100644 --- a/src/index.js +++ b/src/index.js @@ -309,7 +309,7 @@ class MiniCssExtractPlugin { this._sortedModulesCache = new WeakMap(); this.options = Object.assign( - { filename: DEFAULT_FILENAME, ignoreOrder: false, emitFile: true }, + { filename: DEFAULT_FILENAME, ignoreOrder: false, emit: true }, options ); @@ -426,7 +426,7 @@ class MiniCssExtractPlugin { new CssDependencyTemplate() ); - const { emitFile } = this.options; + const { emit } = this.options; if (isWebpack4) { compilation.mainTemplate.hooks.renderManifest.tap( pluginName, @@ -440,7 +440,7 @@ class MiniCssExtractPlugin { const filenameTemplate = chunk.filenameTemplate || this.options.filename; - if (renderedModules.length > 0 && emitFile) { + if (renderedModules.length > 0 && emit) { result.push({ render: () => this.renderContentAsset( @@ -474,7 +474,7 @@ class MiniCssExtractPlugin { const filenameTemplate = chunk.filenameTemplate || this.options.chunkFilename; - if (renderedModules.length > 0 && emitFile) { + if (renderedModules.length > 0 && emit) { result.push({ render: () => this.renderContentAsset( @@ -516,7 +516,7 @@ class MiniCssExtractPlugin { ? this.options.filename : this.options.chunkFilename; - if (renderedModules.length > 0 && emitFile) { + if (renderedModules.length > 0 && emit) { result.push({ render: () => this.renderContentAsset( diff --git a/src/plugin-options.json b/src/plugin-options.json index 33809dc7..b6db0f98 100644 --- a/src/plugin-options.json +++ b/src/plugin-options.json @@ -22,8 +22,8 @@ } ] }, - "emitFile": { - "description": "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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emitFile).", + "emit": { + "description": "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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emit).", "type": "boolean" }, "ignoreOrder": { diff --git a/test/__snapshots__/validate-plugin-options.test.js.snap b/test/__snapshots__/validate-plugin-options.test.js.snap index ed735a89..41d99fb7 100644 --- a/test/__snapshots__/validate-plugin-options.test.js.snap +++ b/test/__snapshots__/validate-plugin-options.test.js.snap @@ -16,10 +16,10 @@ exports[`validate options should throw an error on the "chunkFilename" option wi * options.chunkFilename should be an instance of function." `; -exports[`validate options should throw an error on the "emitFile" option with "1" value 1`] = ` +exports[`validate options should throw an error on the "emit" option with "1" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema. - - options.emitFile should be a boolean. - -> 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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emitFile)." + - options.emit should be a boolean. + -> 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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emit)." `; exports[`validate options should throw an error on the "filename" option with "true" value 1`] = ` @@ -105,47 +105,47 @@ exports[`validate options should throw an error on the "linkType" option with "i exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emitFile?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" `; diff --git a/test/validate-plugin-options.test.js b/test/validate-plugin-options.test.js index a4382f70..f08481f3 100644 --- a/test/validate-plugin-options.test.js +++ b/test/validate-plugin-options.test.js @@ -20,7 +20,7 @@ describe('validate options', () => { ], failure: [true], }, - emitFile: { + emit: { success: [true, false], failure: [1], }, From 0cbea181f2de750190e34e07cb2dce542091634b Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Thu, 18 Mar 2021 23:11:56 +0000 Subject: [PATCH 3/6] feat: emit option, moved to loader --- README.md | 18 +-- src/index.js | 9 +- src/loader-options.json | 3 + src/loader.js | 108 +++++++++--------- src/plugin-options.json | 4 - .../validate-loader-options.test.js.snap | 21 ++-- .../validate-plugin-options.test.js.snap | 22 ++-- test/validate-loader-options.test.js | 4 + test/validate-plugin-options.test.js | 4 - 9 files changed, 98 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index 0fb13f64..b78ce12d 100644 --- a/README.md +++ b/README.md @@ -104,15 +104,6 @@ This option determines the name of non-entry chunk files. Works like [`output.chunkFilename`](https://webpack.js.org/configuration/output/#outputchunkfilename) -#### `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. - #### `ignoreOrder` Type: `Boolean` @@ -270,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 | @@ -353,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/index.js b/src/index.js index 4f5069ec..5ca6dbf7 100644 --- a/src/index.js +++ b/src/index.js @@ -309,7 +309,7 @@ class MiniCssExtractPlugin { this._sortedModulesCache = new WeakMap(); this.options = Object.assign( - { filename: DEFAULT_FILENAME, ignoreOrder: false, emit: true }, + { filename: DEFAULT_FILENAME, ignoreOrder: false }, options ); @@ -426,7 +426,6 @@ class MiniCssExtractPlugin { new CssDependencyTemplate() ); - const { emit } = this.options; if (isWebpack4) { compilation.mainTemplate.hooks.renderManifest.tap( pluginName, @@ -440,7 +439,7 @@ class MiniCssExtractPlugin { const filenameTemplate = chunk.filenameTemplate || this.options.filename; - if (renderedModules.length > 0 && emit) { + if (renderedModules.length > 0) { result.push({ render: () => this.renderContentAsset( @@ -474,7 +473,7 @@ class MiniCssExtractPlugin { const filenameTemplate = chunk.filenameTemplate || this.options.chunkFilename; - if (renderedModules.length > 0 && emit) { + if (renderedModules.length > 0) { result.push({ render: () => this.renderContentAsset( @@ -516,7 +515,7 @@ class MiniCssExtractPlugin { ? this.options.filename : this.options.chunkFilename; - if (renderedModules.length > 0 && emit) { + if (renderedModules.length > 0) { result.push({ render: () => this.renderContentAsset( 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..e8cef8e0 100644 --- a/src/loader.js +++ b/src/loader.js @@ -189,45 +189,49 @@ 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); - } - - const addDependencies = (dependencies) => { - if (!Array.isArray(dependencies) && dependencies != null) { - throw new Error( - `Exported value was not extracted as an array: ${JSON.stringify( - dependencies - )}` - ); + let addDependencies; + if (emit) { + for (const asset of compilation.getAssets()) { + assets[asset.name] = asset.source; + assetsInfo.set(asset.name, asset.info); } - const identifierCountMap = new Map(); + addDependencies = (dependencies) => { + if (!Array.isArray(dependencies) && dependencies != null) { + throw new Error( + `Exported value was not extracted as an array: ${JSON.stringify( + dependencies + )}` + ); + } - let lastDep; + const identifierCountMap = new Map(); - for (const dependency of dependencies) { - if (!dependency.identifier) { - // eslint-disable-next-line no-continue - continue; - } + let lastDep; + + for (const dependency of dependencies) { + if (!dependency.identifier) { + // eslint-disable-next-line no-continue + continue; + } - const count = identifierCountMap.get(dependency.identifier) || 0; - const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack); + const count = identifierCountMap.get(dependency.identifier) || 0; + const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack); - this._module.addDependency( - (lastDep = new CssDependency(dependency, dependency.context, count)) - ); - identifierCountMap.set(dependency.identifier, count + 1); - } + this._module.addDependency( + (lastDep = new CssDependency(dependency, dependency.context, count)) + ); + identifierCountMap.set(dependency.identifier, count + 1); + } - if (lastDep) { - lastDep.assets = assets; - lastDep.assetsInfo = assetsInfo; - } - }; + if (lastDep) { + lastDep.assets = assets; + lastDep.assetsInfo = assetsInfo; + } + }; + } if (error) { return callback(error); @@ -278,28 +282,30 @@ export function pitch(request) { locals = exports && exports.locals; } - let dependencies; + if (emit) { + let dependencies; - if (!Array.isArray(exports)) { - dependencies = [[null, exports]]; - } else { - dependencies = exports.map(([id, content, media, sourceMap]) => { - const module = findModuleById(compilation, id); - - return { - identifier: module.identifier(), - context: module.context, - content: Buffer.from(content), - media, - sourceMap: sourceMap - ? Buffer.from(JSON.stringify(sourceMap)) - : // eslint-disable-next-line no-undefined - undefined, - }; - }); - } + if (!Array.isArray(exports)) { + dependencies = [[null, exports]]; + } else { + dependencies = exports.map(([id, content, media, sourceMap]) => { + const module = findModuleById(compilation, id); + + return { + identifier: module.identifier(), + context: module.context, + content: Buffer.from(content), + media, + sourceMap: sourceMap + ? Buffer.from(JSON.stringify(sourceMap)) + : // eslint-disable-next-line no-undefined + undefined, + }; + }); + } - addDependencies(dependencies); + addDependencies(dependencies); + } } catch (e) { return callback(e); } diff --git a/src/plugin-options.json b/src/plugin-options.json index b6db0f98..04e5c9ed 100644 --- a/src/plugin-options.json +++ b/src/plugin-options.json @@ -22,10 +22,6 @@ } ] }, - "emit": { - "description": "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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emit).", - "type": "boolean" - }, "ignoreOrder": { "type": "boolean" }, 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/__snapshots__/validate-plugin-options.test.js.snap b/test/__snapshots__/validate-plugin-options.test.js.snap index 41d99fb7..8d9e1b45 100644 --- a/test/__snapshots__/validate-plugin-options.test.js.snap +++ b/test/__snapshots__/validate-plugin-options.test.js.snap @@ -16,12 +16,6 @@ exports[`validate options should throw an error on the "chunkFilename" option wi * options.chunkFilename should be an instance of function." `; -exports[`validate options should throw an error on the "emit" option with "1" value 1`] = ` -"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema. - - options.emit should be a boolean. - -> 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. (https://github.com/webpack-contrib/mini-css-extract-plugin#emit)." -`; - exports[`validate options should throw an error on the "filename" option with "true" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema. - options.filename should be one of these: @@ -105,47 +99,47 @@ exports[`validate options should throw an error on the "linkType" option with "i exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` "Invalid options object. Mini CSS Extract Plugin 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 { filename?, chunkFilename?, emit?, ignoreOrder?, insert?, attributes?, linkType? }" + object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }" `; 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], diff --git a/test/validate-plugin-options.test.js b/test/validate-plugin-options.test.js index f08481f3..0638f4a8 100644 --- a/test/validate-plugin-options.test.js +++ b/test/validate-plugin-options.test.js @@ -20,10 +20,6 @@ describe('validate options', () => { ], failure: [true], }, - emit: { - success: [true, false], - failure: [1], - }, ignoreOrder: { success: [true, false], failure: [1], From 46a219c52c9bf166b418d48e72d582e430b8daa2 Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Thu, 18 Mar 2021 23:46:39 +0000 Subject: [PATCH 4/6] feat: emit option Removed the unnecessary check. Codecov was complaining about it and there is only one caller for this function a few lines below, dependencies is guaranteed to be an array --- src/loader.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/loader.js b/src/loader.js index e8cef8e0..64108811 100644 --- a/src/loader.js +++ b/src/loader.js @@ -199,14 +199,6 @@ export function pitch(request) { } 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; From d6ff0bc7db51d7a154787ea88299c0af00d75181 Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Fri, 19 Mar 2021 12:29:14 +0000 Subject: [PATCH 5/6] feat: emit option Keep addDependencies to ensure watch works --- src/loader.js | 86 +++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/src/loader.js b/src/loader.js index 64108811..30cc1913 100644 --- a/src/loader.js +++ b/src/loader.js @@ -191,39 +191,38 @@ export function pitch(request) { const assetsInfo = new Map(); const emit = typeof options.emit !== 'undefined' ? options.emit : true; - let addDependencies; if (emit) { for (const asset of compilation.getAssets()) { assets[asset.name] = asset.source; assetsInfo.set(asset.name, asset.info); } + } - addDependencies = (dependencies) => { - const identifierCountMap = new Map(); + const addDependencies = (dependencies) => { + const identifierCountMap = new Map(); - let lastDep; + let lastDep; - for (const dependency of dependencies) { - if (!dependency.identifier) { - // eslint-disable-next-line no-continue - continue; - } + for (const dependency of dependencies) { + if (!dependency.identifier) { + // eslint-disable-next-line no-continue + continue; + } - const count = identifierCountMap.get(dependency.identifier) || 0; - const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack); + const count = identifierCountMap.get(dependency.identifier) || 0; + const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack); - this._module.addDependency( - (lastDep = new CssDependency(dependency, dependency.context, count)) - ); - identifierCountMap.set(dependency.identifier, count + 1); - } + this._module.addDependency( + (lastDep = new CssDependency(dependency, dependency.context, count)) + ); + identifierCountMap.set(dependency.identifier, count + 1); + } - if (lastDep) { - lastDep.assets = assets; - lastDep.assetsInfo = assetsInfo; - } - }; - } + if (lastDep) { + lastDep.assets = assets; + lastDep.assetsInfo = assetsInfo; + } + }; if (error) { return callback(error); @@ -274,30 +273,29 @@ export function pitch(request) { locals = exports && exports.locals; } - if (emit) { - let dependencies; + let dependencies; - if (!Array.isArray(exports)) { - dependencies = [[null, exports]]; - } else { - dependencies = exports.map(([id, content, media, sourceMap]) => { - const module = findModuleById(compilation, id); - - return { - identifier: module.identifier(), - context: module.context, - content: Buffer.from(content), - media, - sourceMap: sourceMap - ? Buffer.from(JSON.stringify(sourceMap)) - : // eslint-disable-next-line no-undefined - undefined, - }; - }); - } - - addDependencies(dependencies); + 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); + + return { + identifier: module.identifier(), + context: module.context, + content: Buffer.from(content), + media, + sourceMap: sourceMap + ? Buffer.from(JSON.stringify(sourceMap)) + : // eslint-disable-next-line no-undefined + undefined, + }; + }); } + addDependencies(dependencies); } catch (e) { return callback(e); } From 53909265f7b57457e43359f6e5a1c48c9508771a Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Fri, 19 Mar 2021 12:32:31 +0000 Subject: [PATCH 6/6] feat: emit option Restoring mistakenly removed formatting --- src/loader.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/loader.js b/src/loader.js index 30cc1913..b775f612 100644 --- a/src/loader.js +++ b/src/loader.js @@ -295,6 +295,7 @@ export function pitch(request) { }; }); } + addDependencies(dependencies); } catch (e) { return callback(e);