Skip to content

Commit 19d2f35

Browse files
sadafieKent C. Dodds
authored and
Kent C. Dodds
committed
fix: support webpack multi-compiler (#119)
* fix: support webpack multi-compiler when provided with an array of configurations, iterate array and validate each configuration individually fixes #117 * refactor: use ES6 module import use import instead of require for validate module
1 parent fcd4f53 commit 19d2f35

File tree

6 files changed

+78
-18
lines changed

6 files changed

+78
-18
lines changed

src/bin/validate-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path')
2-
const validate = require('../')
2+
const validate = require('../index').validateRoot
33
const argv = require('yargs').argv
44

55
module.exports = function validateConfig(webpackConfigFile, quiet) {

src/index.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ import sh from 'shelljs'
1717

1818
sh.config.silent = true
1919

20-
const makeSchema = (schemaOptions, schemaExtension) => {
20+
const defaultSchemaOptions = {
21+
rules: {
22+
'no-root-files-node-modules-nameclash': true,
23+
'loader-enforce-include-or-exclude': false,
24+
'loader-prefer-include': false,
25+
},
26+
}
27+
28+
function makeSchema(schemaOptions, schemaExtension) {
2129
const resolveSchema = resolveSchemaFn(schemaOptions)
2230
const moduleSchema = moduleSchemaFn(schemaOptions)
2331

@@ -59,20 +67,10 @@ const makeSchema = (schemaOptions, schemaExtension) => {
5967
return schemaExtension ? schema.concat(schemaExtension) : schema
6068
}
6169

62-
const defaultSchemaOptions = {
63-
rules: {
64-
'no-root-files-node-modules-nameclash': true,
65-
'loader-enforce-include-or-exclude': false,
66-
'loader-prefer-include': false,
67-
},
68-
}
69-
70-
// Easier consumability for require (default use case for non-transpiled webpack configs)
71-
module.exports = function validate(config, options = {}) {
70+
function validate(config, options) {
7271
const {
7372
// Don't return the config object and throw on error, but just return the validation result
7473
returnValidation, // bool
75-
quiet, // bool
7674
schema: overrideSchema, // Don't take internal schema, but override with this one
7775
schemaExtension, // Internal schema will be `Joi.concat`-ted with this schema if supplied
7876
rules,
@@ -92,11 +90,36 @@ module.exports = function validate(config, options = {}) {
9290
process.exit(1)
9391
}
9492

93+
return config
94+
}
95+
96+
// Easier consumability for require (default use case for non-transpiled webpack configs)
97+
function validateRoot(config, options = {}) {
98+
const {
99+
quiet,
100+
} = options
101+
102+
let validationResult,
103+
multiValidationResults
104+
105+
if (Array.isArray(config)) {
106+
multiValidationResults = []
107+
config.forEach((cfg) => {
108+
multiValidationResults.push(
109+
validate(cfg, options)
110+
)
111+
})
112+
} else {
113+
validationResult = validate(config, options)
114+
}
115+
95116
if (!quiet) {
96117
console.info(chalk.green('[webpack-validator] Config is valid.'))
97118
}
98119

99-
return config
120+
return validationResult || multiValidationResults
100121
}
101122

102-
module.exports.Joi = Joi
123+
exports.validate = validate
124+
exports.validateRoot = validateRoot
125+
exports.Joi = Joi

src/index.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import sinon from 'sinon'
22
import configs from '../test/passing-configs'
33
import failingConfigs from '../test/failing-configs'
4-
import validate, { Joi } from './'
4+
import { Joi } from './'
5+
6+
const validate = require('./index').validateRoot
57

68
describe('.', () => {
79
let sandbox
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
* @link https://github.com/webpack/webpack/blob/master/examples/multi-compiler/webpack.config.js
4+
*
5+
*/
6+
7+
var path = require("path");
8+
var webpack = require('webpack')
9+
10+
module.exports = [
11+
{
12+
entry: "./example",
13+
output: {
14+
path: path.join(__dirname, "js"),
15+
filename: "mobile.js"
16+
},
17+
plugins: [
18+
new webpack.DefinePlugin({
19+
ENV: JSON.stringify("mobile")
20+
})
21+
]
22+
},
23+
{
24+
entry: "./example",
25+
output: {
26+
path: path.join(__dirname, "js"),
27+
filename: "desktop.js"
28+
},
29+
plugins: [
30+
new webpack.DefinePlugin({
31+
ENV: JSON.stringify("desktop")
32+
})
33+
]
34+
}
35+
];

test/utils/allInvalid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import validate from '../../src/index'
21
import chalk from 'chalk'
32
import util from 'util'
3+
import { validate } from '../../src'
44

55
/**
66
* For all supplied configs (array of objects), check that they are invalid given a schema.

test/utils/allValid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import validate from '../../src/index'
21
import chalk from 'chalk'
32
import util from 'util'
3+
import { validate } from '../../src'
44

55
/**
66
* For all supplied configs (array of objects), check that they are valid given a schema.

0 commit comments

Comments
 (0)