Skip to content

Commit f0721d1

Browse files
author
Kent C. Dodds
authored
feat(version): throw error when webpack 2 is installed (#157)
Closes #152 BREAKING CHANGE: webpack v2 is not supported and now there will be a message when you're using webpack v2 with webpack-validator.
1 parent 6c7cc40 commit f0721d1

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"joi": "9.0.0-0",
4848
"lodash": "4.11.1",
4949
"npmlog": "2.0.3",
50+
"semver": "^5.3.0",
5051
"shelljs": "0.7.0",
5152
"yargs": "4.7.1"
5253
},
@@ -61,15 +62,15 @@
6162
"brace-expansion": "1.1.3",
6263
"codecov": "1.0.1",
6364
"commitizen": "^2.7.6",
64-
"compression-webpack-plugin": "*",
65-
"copy-webpack-plugin": "*",
65+
"compression-webpack-plugin": "^0.3.2",
66+
"copy-webpack-plugin": "^4.0.1",
6667
"cz-conventional-changelog": "^1.1.5",
6768
"eslint": "2.8.0",
6869
"eslint-config-jonathanewerner": "1.0.1",
69-
"extract-text-webpack-plugin": "*",
70+
"extract-text-webpack-plugin": "^2.1.0",
7071
"ghooks": "1.2.1",
7172
"glob": "7.0.3",
72-
"html-webpack-plugin": "*",
73+
"html-webpack-plugin": "^2.28.0",
7374
"mocha": "2.4.5",
7475
"npm-run-all": "1.8.0",
7576
"nyc": "6.4.0",
@@ -80,9 +81,9 @@
8081
"semantic-release": "^4.3.5",
8182
"sinon": "1.17.3",
8283
"validate-commit-msg": "2.6.1",
83-
"webpack": "*",
84-
"webpack-md5-hash": "*",
85-
"webpack-notifier": "*",
84+
"webpack": "^1",
85+
"webpack-md5-hash": "^0.0.5",
86+
"webpack-notifier": "^1.5.0",
8687
"with-package": "0.2.0"
8788
},
8889
"keywords": [

src/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import Joi from 'joi'
23
import chalk from 'chalk'
34
import moduleSchemaFn from './properties/module'
@@ -15,6 +16,7 @@ import performanceSchema from './properties/performance'
1516
import { looksLikeAbsolutePath } from './types'
1617
import _merge from 'lodash/merge'
1718
import sh from 'shelljs'
19+
import semver from 'semver'
1820

1921
sh.config.silent = true
2022

@@ -69,6 +71,26 @@ function makeSchema(schemaOptions, schemaExtension) {
6971
return schemaExtension ? schema.concat(schemaExtension) : schema
7072
}
7173

74+
function throwForWebpack2() {
75+
const cwd = process.cwd()
76+
let satisifies = true
77+
try {
78+
const webpackPackagePath = path.join(cwd, 'node_modules', 'webpack', 'package.json')
79+
const { version } = require(webpackPackagePath)
80+
satisifies = semver.satisfies(version, '^1.x')
81+
} catch (error) {
82+
// ignore...
83+
}
84+
if (!satisifies) {
85+
throw new Error(
86+
'It looks like you\'re using version 2 or greater of webpack. ' +
87+
'The official release of 2 of webpack was released with built-in validation. ' +
88+
'So webpack-validator does not support that version. ' +
89+
'Please uninstall webpack-validator and remove it from your project!'
90+
)
91+
}
92+
}
93+
7294
function validate(config, options = {}) {
7395
const {
7496
// Don't return the config object and throw on error, but just return the validation result
@@ -77,6 +99,7 @@ function validate(config, options = {}) {
7799
schemaExtension, // Internal schema will be `Joi.concat`-ted with this schema if supplied
78100
rules,
79101
} = options
102+
throwForWebpack2()
80103

81104
const schemaOptions = _merge(defaultSchemaOptions, { rules })
82105

src/index.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import sinon from 'sinon'
23
import configs from '../test/passing-configs'
34
import failingConfigs from '../test/failing-configs'
@@ -65,6 +66,35 @@ describe('.', () => {
6566
})
6667
})
6768

69+
describe('version-validation', () => {
70+
const cwd = process.cwd()
71+
72+
afterEach(() => {
73+
process.chdir(cwd)
74+
})
75+
76+
it('throws when the project is using webpack 2', () => {
77+
const dir = path.resolve('./test/version-validation/fail')
78+
process.chdir(dir)
79+
try {
80+
validate({ entry: './here.js', output: { filename: 'bundle.js' } })
81+
throw new Error(`validate should throw when cwd is: ${dir}`)
82+
} catch (error) {
83+
if (error.message.indexOf('version 2') === -1) {
84+
throw error
85+
}
86+
}
87+
})
88+
89+
it('does not throw when the project is using webpack 1', () => {
90+
const dir = path.resolve('./test/version-validation/pass')
91+
process.chdir(dir)
92+
// validate should not throw an error...
93+
validate({ entry: './here.js', output: { filename: 'bundle.js' } })
94+
})
95+
})
96+
97+
6898
it('should allow console output to be muted', () => {
6999
validate({}, { quiet: true })
70100

test/version-validation/fail/node_modules/webpack/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/version-validation/pass/node_modules/webpack/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)