Skip to content

Commit b197757

Browse files
fix: schema validation (#480)
1 parent 903a56e commit b197757

34 files changed

+456
-18
lines changed

package-lock.json

+31-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"jest": "^24.8.0",
6969
"jest-junit": "^10.0.0",
7070
"lint-staged": "^9.5.0",
71-
"memory-fs": "^0.4.1",
71+
"memfs": "^3.0.2",
7272
"npm-run-all": "^4.1.5",
7373
"prettier": "^1.19.1",
7474
"standard-version": "^7.0.1",

src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import webpack from 'webpack';
44
import sources from 'webpack-sources';
55

6+
import validateOptions from 'schema-utils';
7+
68
import CssDependency from './CssDependency';
9+
import schema from './plugin-options.json';
710

811
const { ConcatSource, SourceMapSource, OriginalSource } = sources;
912
const {
@@ -97,6 +100,8 @@ class CssModuleFactory {
97100

98101
class MiniCssExtractPlugin {
99102
constructor(options = {}) {
103+
validateOptions(schema, options, 'Mini CSS Extract Plugin');
104+
100105
this.options = Object.assign(
101106
{
102107
filename: DEFAULT_FILENAME,
@@ -491,6 +496,7 @@ class MiniCssExtractPlugin {
491496
// use list with fewest failed deps
492497
// and emit a warning
493498
const fallbackModule = bestMatch.pop();
499+
494500
if (!this.options.ignoreOrder) {
495501
const reasons = moduleDependenciesReasons.get(fallbackModule);
496502
compilation.warnings.push(

src/options.json renamed to src/loader-options.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"type": "object",
23
"additionalProperties": true,
34
"properties": {
45
"publicPath": {
@@ -13,10 +14,9 @@
1314
},
1415
"esModule": {
1516
"type": "boolean"
17+
},
18+
"hmr": {
19+
"type": "boolean"
1620
}
17-
},
18-
"errorMessages": {
19-
"publicPath": "should be {String} or {Function} (https://github.com/webpack-contrib/mini-css-extract-plugin#publicpath)"
20-
},
21-
"type": "object"
21+
}
2222
}

src/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import validateOptions from 'schema-utils';
1212

1313
import CssDependency from './CssDependency';
1414

15-
import schema from './options.json';
15+
import schema from './loader-options.json';
1616

1717
const pluginName = 'mini-css-extract-plugin';
1818

src/plugin-options.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"type": "object",
3+
"additionalProperties": true,
4+
"properties": {
5+
"filename": {
6+
"type": "string"
7+
},
8+
"chunkFilename": {
9+
"type": "string"
10+
},
11+
"moduleFilename": {
12+
"instanceof": "Function"
13+
},
14+
"ignoreOrder": {
15+
"type": "boolean"
16+
}
17+
}
18+
}

test/TestMemoryFS.test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22

3-
import MemoryFS from 'memory-fs';
3+
import { createFsFromVolume, Volume } from 'memfs';
44
import webpack from 'webpack';
55

66
const assetsNames = (json) => json.assets.map((asset) => asset.name);
@@ -20,20 +20,30 @@ describe('TestMemoryFS', () => {
2020
context: directoryForCase,
2121
cache: false,
2222
});
23-
compiler.outputFileSystem = new MemoryFS();
23+
const outputFileSystem = createFsFromVolume(new Volume());
24+
// Todo remove when we drop webpack@4 support
25+
outputFileSystem.join = path.join.bind(path);
26+
27+
compiler.outputFileSystem = outputFileSystem;
28+
2429
compiler.run((err1, stats1) => {
2530
if (err1) {
2631
done(err1);
32+
2733
return;
2834
}
35+
2936
compiler.run((err2, stats2) => {
3037
if (err2) {
3138
done(err2);
39+
3240
return;
3341
}
42+
3443
expect(assetsNames(stats1.toJson())).toEqual(
3544
assetsNames(stats2.toJson())
3645
);
46+
3747
done();
3848
});
3949
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`validate options should throw an error on the "esModule" option with "1" value 1`] = `
4+
"Mini CSS Extract Plugin Loader Invalid Options
5+
6+
options.esModule should be boolean
7+
"
8+
`;
9+
10+
exports[`validate options should throw an error on the "hmr" option with "1" value 1`] = `
11+
"Mini CSS Extract Plugin Loader Invalid Options
12+
13+
options.hmr should be boolean
14+
"
15+
`;
16+
17+
exports[`validate options should throw an error on the "publicPath" option with "true" value 1`] = `
18+
"Mini CSS Extract Plugin Loader Invalid Options
19+
20+
options.publicPath should be string
21+
options.publicPath should pass \\"instanceof\\" keyword validation
22+
options.publicPath should match some schema in anyOf
23+
"
24+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`validate options should throw an error on the "chunkFilename" option with "true" value 1`] = `
4+
"Mini CSS Extract Plugin Invalid Options
5+
6+
options.chunkFilename should be string
7+
"
8+
`;
9+
10+
exports[`validate options should throw an error on the "filename" option with "true" value 1`] = `
11+
"Mini CSS Extract Plugin Invalid Options
12+
13+
options.filename should be string
14+
"
15+
`;
16+
17+
exports[`validate options should throw an error on the "ignoreOrder" option with "1" value 1`] = `
18+
"Mini CSS Extract Plugin Invalid Options
19+
20+
options.ignoreOrder should be boolean
21+
"
22+
`;
23+
24+
exports[`validate options should throw an error on the "moduleFilename" option with "true" value 1`] = `
25+
"Mini CSS Extract Plugin Invalid Options
26+
27+
options.moduleFilename should pass \\"instanceof\\" keyword validation
28+
"
29+
`;

test/cases/chunkFilename/async.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.async {
2+
color: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.async {
2+
color: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+

test/cases/chunkFilename/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './style.css';
2+
3+
/* eslint-disable-next-line no-unused-expressions */
4+
import(/* webpackChunkName: "async" */ './async.css');

test/cases/chunkFilename/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [Self.loader, 'css-loader'],
10+
},
11+
],
12+
},
13+
plugins: [
14+
new Self({
15+
filename: '[name].css',
16+
chunkFilename: '[id].[name].css',
17+
}),
18+
],
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.async {
2+
color: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.async {
2+
color: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './style.css';
2+
3+
/* eslint-disable-next-line no-unused-expressions */
4+
import(/* webpackChunkName: "async" */ './async.css');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [Self.loader, 'css-loader'],
10+
},
11+
],
12+
},
13+
plugins: [
14+
new Self({
15+
filename: '[name].css',
16+
}),
17+
],
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.async {
2+
color: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.async {
2+
color: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './style.css';
2+
3+
/* eslint-disable-next-line no-unused-expressions */
4+
import(/* webpackChunkName: "async" */ './async.css');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [Self.loader, 'css-loader'],
10+
},
11+
],
12+
},
13+
plugins: [
14+
new Self({
15+
filename: 'main.css',
16+
}),
17+
],
18+
};

test/fixtures/simple.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: red;
3+
}

test/fixtures/simple.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './simple.css';
2+
3+
console.log('HERE');

0 commit comments

Comments
 (0)