diff --git a/packages/babel/__tests__/babel.test.js b/packages/babel/__tests__/babel.test.js index eeb45b6f..99ac0f0a 100644 --- a/packages/babel/__tests__/babel.test.js +++ b/packages/babel/__tests__/babel.test.js @@ -51,7 +51,7 @@ test('Babel options work', t => { test('using custom match() works', t => { const config = createConfig({}, [ - match('*.js', { exclude: [] }, [ + match('*.js', { exclude: null }, [ babel({ cacheDirectory: false }) @@ -61,7 +61,7 @@ test('using custom match() works', t => { t.deepEqual(config.module.rules, [ { test: /^.*\.js$/, - exclude: [], + exclude: null, use: [ { loader: 'babel-loader', diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 55471946..fc963770 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next version - Support negations in `match()` to set `exclude` ([#252](https://github.com/andywer/webpack-blocks/issues/252)) +- Change `match()` to pass all [webpack rule options](https://webpack.js.org/configuration/module/) to context. ([#250](https://github.com/andywer/webpack-blocks/pull/250)) ## 1.0.0-beta.2 diff --git a/packages/core/lib/__tests__/match.test.js b/packages/core/lib/__tests__/match.test.js index d4328dbf..770cd832 100644 --- a/packages/core/lib/__tests__/match.test.js +++ b/packages/core/lib/__tests__/match.test.js @@ -3,8 +3,6 @@ import createConfig from '../createConfig' import match from '../match' test('match() sets context.match', t => { - t.plan(9) - const matchedLoaderBlock = context => config => { t.is(typeof context.match, 'object') t.deepEqual(Object.keys(context.match), [ 'test' ]) @@ -36,11 +34,9 @@ test('match() sets context.match', t => { }) test('match() supports options and extended regexps', t => { - t.plan(3) - const loaderBlock = context => config => { - t.deepEqual(Object.keys(context.match).sort(), [ 'exclude', 'test' ]) - t.is(context.match.test.toString(), '/^(.*\\.js|.*\\.jsx)$/') + t.deepEqual(Object.keys(context.match).sort(), [ 'enforce', 'exclude', 'test' ]) + t.is(context.match.test.toString(), '/^.*\\.(js|jsx)$/') t.is(context.match.exclude, 'node_modules') return config } @@ -49,15 +45,13 @@ test('match() supports options and extended regexps', t => { // or the space will become part of the regex... createConfig({}, [ - match('{*.js,*.jsx}', { exclude: 'node_modules' }, [ + match('*.{js,jsx}', { exclude: 'node_modules', enforce: 'pre' }, [ loaderBlock ]) ]) }) test('match() supports negations', t => { - t.plan(3) - const loaderBlock = context => config => { t.deepEqual(Object.keys(context.match).sort(), [ 'exclude', 'test' ]) t.is(context.match.test.toString(), '/^.*\\.js$/') @@ -73,8 +67,6 @@ test('match() supports negations', t => { }) test('match() returns derived context that propagates mutations', t => { - t.plan(1) - const mutatingBlock = context => config => { context.foo = 'bar' return config @@ -92,3 +84,9 @@ test('match() returns derived context that propagates mutations', t => { readingBlock ]) }) + +test('match() throws if `options` argument has `test`', t => { + t.throws(() => createConfig({}, [ + match(['*.js'], {test: /\.jsx/}, []) + ])) +}) diff --git a/packages/core/lib/match.js b/packages/core/lib/match.js index 626fedbf..c5002d92 100644 --- a/packages/core/lib/match.js +++ b/packages/core/lib/match.js @@ -14,9 +14,7 @@ const toArray = thing => Array.isArray(thing) ? thing : [ thing ] * like `group()`, but adds the file matching information to the context. * * @param {string|RegExp|Function|Array} test A glob like `*.css` or `{*.js, *.jsx}` or something else to use as `loader.test`. - * @param {object} [options] Optional advanced matching options. - * @param {string|Function|RegExp|Array|object} [options.include] - * @param {string|Function|RegExp|Array|object} [options.exclude] + * @param {object} [options] Rule options. See https://webpack.js.org/configuration/module/ * @param {Function[]} configSetters Array of functions as returned by webpack blocks. * @return {Function} */ @@ -28,6 +26,10 @@ function match (test, options, configSetters) { assertConfigSetters(configSetters) + if (options.test) { + throw new Error(`match(): Setting 'test' in options is not supported; use the argument instead.`) + } + const { inclusions, exclusions } = splitPatterns(toArray(test)) const match = Object.assign({}, options, { // The `.test` is usually just one pattern, so stripping the array feels more natural @@ -53,16 +55,14 @@ function normalizeMatchers (fileMatchTests) { return fileMatchTests.map(test => { if (typeof test === 'string') { return regexify(test) - } else { - return test } + return test }) } function splitPatterns (patterns) { const isNegation = pattern => typeof pattern === 'string' && pattern.startsWith('!') const stripLeadingExclam = pattern => pattern.startsWith('!') ? pattern.substr(1) : pattern - return { inclusions: patterns.filter(pattern => !isNegation(pattern)), exclusions: patterns.filter(pattern => isNegation(pattern)).map(stripLeadingExclam)