Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions src/loader-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
}
]
},
"emit": {
"type": "boolean"
},
"esModule": {
"type": "boolean"
},
Expand Down
19 changes: 8 additions & 11 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 13 additions & 8 deletions test/__snapshots__/validate-loader-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -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."
Expand Down Expand Up @@ -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? }"
`;
4 changes: 4 additions & 0 deletions test/validate-loader-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down