Skip to content

Commit

Permalink
fix: schema validation (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 25, 2019
1 parent 903a56e commit b197757
Show file tree
Hide file tree
Showing 34 changed files with 456 additions and 18 deletions.
40 changes: 31 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"jest": "^24.8.0",
"jest-junit": "^10.0.0",
"lint-staged": "^9.5.0",
"memory-fs": "^0.4.1",
"memfs": "^3.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"standard-version": "^7.0.1",
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import webpack from 'webpack';
import sources from 'webpack-sources';

import validateOptions from 'schema-utils';

import CssDependency from './CssDependency';
import schema from './plugin-options.json';

const { ConcatSource, SourceMapSource, OriginalSource } = sources;
const {
Expand Down Expand Up @@ -97,6 +100,8 @@ class CssModuleFactory {

class MiniCssExtractPlugin {
constructor(options = {}) {
validateOptions(schema, options, 'Mini CSS Extract Plugin');

this.options = Object.assign(
{
filename: DEFAULT_FILENAME,
Expand Down Expand Up @@ -491,6 +496,7 @@ class MiniCssExtractPlugin {
// use list with fewest failed deps
// and emit a warning
const fallbackModule = bestMatch.pop();

if (!this.options.ignoreOrder) {
const reasons = moduleDependenciesReasons.get(fallbackModule);
compilation.warnings.push(
Expand Down
10 changes: 5 additions & 5 deletions src/options.json → src/loader-options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "object",
"additionalProperties": true,
"properties": {
"publicPath": {
Expand All @@ -13,10 +14,9 @@
},
"esModule": {
"type": "boolean"
},
"hmr": {
"type": "boolean"
}
},
"errorMessages": {
"publicPath": "should be {String} or {Function} (https://github.com/webpack-contrib/mini-css-extract-plugin#publicpath)"
},
"type": "object"
}
}
2 changes: 1 addition & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import validateOptions from 'schema-utils';

import CssDependency from './CssDependency';

import schema from './options.json';
import schema from './loader-options.json';

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

Expand Down
18 changes: 18 additions & 0 deletions src/plugin-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "object",
"additionalProperties": true,
"properties": {
"filename": {
"type": "string"
},
"chunkFilename": {
"type": "string"
},
"moduleFilename": {
"instanceof": "Function"
},
"ignoreOrder": {
"type": "boolean"
}
}
}
14 changes: 12 additions & 2 deletions test/TestMemoryFS.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import MemoryFS from 'memory-fs';
import { createFsFromVolume, Volume } from 'memfs';
import webpack from 'webpack';

const assetsNames = (json) => json.assets.map((asset) => asset.name);
Expand All @@ -20,20 +20,30 @@ describe('TestMemoryFS', () => {
context: directoryForCase,
cache: false,
});
compiler.outputFileSystem = new MemoryFS();
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

compiler.outputFileSystem = outputFileSystem;

compiler.run((err1, stats1) => {
if (err1) {
done(err1);

return;
}

compiler.run((err2, stats2) => {
if (err2) {
done(err2);

return;
}

expect(assetsNames(stats1.toJson())).toEqual(
assetsNames(stats2.toJson())
);

done();
});
});
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/validate-loader-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validate options should throw an error on the "esModule" option with "1" value 1`] = `
"Mini CSS Extract Plugin Loader Invalid Options
options.esModule should be boolean
"
`;

exports[`validate options should throw an error on the "hmr" option with "1" value 1`] = `
"Mini CSS Extract Plugin Loader Invalid Options
options.hmr should be boolean
"
`;

exports[`validate options should throw an error on the "publicPath" option with "true" value 1`] = `
"Mini CSS Extract Plugin Loader Invalid Options
options.publicPath should be string
options.publicPath should pass \\"instanceof\\" keyword validation
options.publicPath should match some schema in anyOf
"
`;
29 changes: 29 additions & 0 deletions test/__snapshots__/validate-plugin-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validate options should throw an error on the "chunkFilename" option with "true" value 1`] = `
"Mini CSS Extract Plugin Invalid Options
options.chunkFilename should be string
"
`;

exports[`validate options should throw an error on the "filename" option with "true" value 1`] = `
"Mini CSS Extract Plugin Invalid Options
options.filename should be string
"
`;

exports[`validate options should throw an error on the "ignoreOrder" option with "1" value 1`] = `
"Mini CSS Extract Plugin Invalid Options
options.ignoreOrder should be boolean
"
`;

exports[`validate options should throw an error on the "moduleFilename" option with "true" value 1`] = `
"Mini CSS Extract Plugin Invalid Options
options.moduleFilename should pass \\"instanceof\\" keyword validation
"
`;
3 changes: 3 additions & 0 deletions test/cases/chunkFilename/async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.async {
color: red;
}
4 changes: 4 additions & 0 deletions test/cases/chunkFilename/expected/1.async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.async {
color: red;
}

4 changes: 4 additions & 0 deletions test/cases/chunkFilename/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: red;
}

4 changes: 4 additions & 0 deletions test/cases/chunkFilename/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './style.css';

/* eslint-disable-next-line no-unused-expressions */
import(/* webpackChunkName: "async" */ './async.css');
3 changes: 3 additions & 0 deletions test/cases/chunkFilename/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
19 changes: 19 additions & 0 deletions test/cases/chunkFilename/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
plugins: [
new Self({
filename: '[name].css',
chunkFilename: '[id].[name].css',
}),
],
};
3 changes: 3 additions & 0 deletions test/cases/filename-with-template/async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.async {
color: red;
}
4 changes: 4 additions & 0 deletions test/cases/filename-with-template/expected/async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.async {
color: red;
}

4 changes: 4 additions & 0 deletions test/cases/filename-with-template/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: red;
}

4 changes: 4 additions & 0 deletions test/cases/filename-with-template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './style.css';

/* eslint-disable-next-line no-unused-expressions */
import(/* webpackChunkName: "async" */ './async.css');
3 changes: 3 additions & 0 deletions test/cases/filename-with-template/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
18 changes: 18 additions & 0 deletions test/cases/filename-with-template/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
3 changes: 3 additions & 0 deletions test/cases/filename-without-template/async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.async {
color: red;
}
4 changes: 4 additions & 0 deletions test/cases/filename-without-template/expected/1.main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.async {
color: red;
}

4 changes: 4 additions & 0 deletions test/cases/filename-without-template/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: red;
}

4 changes: 4 additions & 0 deletions test/cases/filename-without-template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './style.css';

/* eslint-disable-next-line no-unused-expressions */
import(/* webpackChunkName: "async" */ './async.css');
3 changes: 3 additions & 0 deletions test/cases/filename-without-template/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
18 changes: 18 additions & 0 deletions test/cases/filename-without-template/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
plugins: [
new Self({
filename: 'main.css',
}),
],
};
3 changes: 3 additions & 0 deletions test/fixtures/simple.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: red;
}
3 changes: 3 additions & 0 deletions test/fixtures/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './simple.css';

console.log('HERE');
Loading

0 comments on commit b197757

Please sign in to comment.