Skip to content

Commit

Permalink
feat(version): throw error when webpack 2 is installed (#157)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kent C. Dodds committed Mar 24, 2017
1 parent 6c7cc40 commit f0721d1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"joi": "9.0.0-0",
"lodash": "4.11.1",
"npmlog": "2.0.3",
"semver": "^5.3.0",
"shelljs": "0.7.0",
"yargs": "4.7.1"
},
Expand All @@ -61,15 +62,15 @@
"brace-expansion": "1.1.3",
"codecov": "1.0.1",
"commitizen": "^2.7.6",
"compression-webpack-plugin": "*",
"copy-webpack-plugin": "*",
"compression-webpack-plugin": "^0.3.2",
"copy-webpack-plugin": "^4.0.1",
"cz-conventional-changelog": "^1.1.5",
"eslint": "2.8.0",
"eslint-config-jonathanewerner": "1.0.1",
"extract-text-webpack-plugin": "*",
"extract-text-webpack-plugin": "^2.1.0",
"ghooks": "1.2.1",
"glob": "7.0.3",
"html-webpack-plugin": "*",
"html-webpack-plugin": "^2.28.0",
"mocha": "2.4.5",
"npm-run-all": "1.8.0",
"nyc": "6.4.0",
Expand All @@ -80,9 +81,9 @@
"semantic-release": "^4.3.5",
"sinon": "1.17.3",
"validate-commit-msg": "2.6.1",
"webpack": "*",
"webpack-md5-hash": "*",
"webpack-notifier": "*",
"webpack": "^1",
"webpack-md5-hash": "^0.0.5",
"webpack-notifier": "^1.5.0",
"with-package": "0.2.0"
},
"keywords": [
Expand Down
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import Joi from 'joi'
import chalk from 'chalk'
import moduleSchemaFn from './properties/module'
Expand All @@ -15,6 +16,7 @@ import performanceSchema from './properties/performance'
import { looksLikeAbsolutePath } from './types'
import _merge from 'lodash/merge'
import sh from 'shelljs'
import semver from 'semver'

sh.config.silent = true

Expand Down Expand Up @@ -69,6 +71,26 @@ function makeSchema(schemaOptions, schemaExtension) {
return schemaExtension ? schema.concat(schemaExtension) : schema
}

function throwForWebpack2() {
const cwd = process.cwd()
let satisifies = true
try {
const webpackPackagePath = path.join(cwd, 'node_modules', 'webpack', 'package.json')
const { version } = require(webpackPackagePath)
satisifies = semver.satisfies(version, '^1.x')
} catch (error) {
// ignore...
}
if (!satisifies) {
throw new Error(
'It looks like you\'re using version 2 or greater of webpack. ' +
'The official release of 2 of webpack was released with built-in validation. ' +
'So webpack-validator does not support that version. ' +
'Please uninstall webpack-validator and remove it from your project!'
)
}
}

function validate(config, options = {}) {
const {
// Don't return the config object and throw on error, but just return the validation result
Expand All @@ -77,6 +99,7 @@ function validate(config, options = {}) {
schemaExtension, // Internal schema will be `Joi.concat`-ted with this schema if supplied
rules,
} = options
throwForWebpack2()

const schemaOptions = _merge(defaultSchemaOptions, { rules })

Expand Down
30 changes: 30 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import sinon from 'sinon'
import configs from '../test/passing-configs'
import failingConfigs from '../test/failing-configs'
Expand Down Expand Up @@ -65,6 +66,35 @@ describe('.', () => {
})
})

describe('version-validation', () => {
const cwd = process.cwd()

afterEach(() => {
process.chdir(cwd)
})

it('throws when the project is using webpack 2', () => {
const dir = path.resolve('./test/version-validation/fail')
process.chdir(dir)
try {
validate({ entry: './here.js', output: { filename: 'bundle.js' } })
throw new Error(`validate should throw when cwd is: ${dir}`)
} catch (error) {
if (error.message.indexOf('version 2') === -1) {
throw error
}
}
})

it('does not throw when the project is using webpack 1', () => {
const dir = path.resolve('./test/version-validation/pass')
process.chdir(dir)
// validate should not throw an error...
validate({ entry: './here.js', output: { filename: 'bundle.js' } })
})
})


it('should allow console output to be muted', () => {
validate({}, { quiet: true })

Expand Down

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

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

0 comments on commit f0721d1

Please sign in to comment.