diff --git a/package.json b/package.json index 86d9746..6ea4121 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", @@ -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": [ diff --git a/src/index.js b/src/index.js index 933941b..d672ccd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import path from 'path' import Joi from 'joi' import chalk from 'chalk' import moduleSchemaFn from './properties/module' @@ -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 @@ -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 @@ -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 }) diff --git a/src/index.test.js b/src/index.test.js index 2194e1d..42652ae 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -1,3 +1,4 @@ +import path from 'path' import sinon from 'sinon' import configs from '../test/passing-configs' import failingConfigs from '../test/failing-configs' @@ -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 }) diff --git a/test/version-validation/fail/node_modules/webpack/package.json b/test/version-validation/fail/node_modules/webpack/package.json new file mode 100644 index 0000000..bf38d5b --- /dev/null +++ b/test/version-validation/fail/node_modules/webpack/package.json @@ -0,0 +1,3 @@ +{ + "version": "2.2.0" +} diff --git a/test/version-validation/pass/node_modules/webpack/package.json b/test/version-validation/pass/node_modules/webpack/package.json new file mode 100644 index 0000000..6076f7d --- /dev/null +++ b/test/version-validation/pass/node_modules/webpack/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.7.0" +}