diff --git a/docs/BLOCK-CREATION.md b/docs/BLOCK-CREATION.md index aff48be5..886dfb3f 100644 --- a/docs/BLOCK-CREATION.md +++ b/docs/BLOCK-CREATION.md @@ -108,13 +108,7 @@ function sampleBlock () { The context object is a metadata object that is passed to every block. It is meant to contain any kind of data that is needed for webpack config creation, but not part of the webpack config itself. -Initially it will contain the `fileType` mapping and a `webpack` instance. If you are using [hooks](#hooks) you might want to put custom metadata into the context and use it in the `post` hook. - -### context.fileType - -*Deprecated. This feature will be removed soon. Use `match()` and `context.match` instead.* - -The `context.fileType` is a mapping from MIME type (`application/javascript`, `text/css`, ...) to a regular expression used for matching filenames. You can find the default file types and the extensions they match [here](https://github.com/andywer/webpack-blocks/blob/master/packages/core/lib/defaultFileTypes.js). +Initially it will contain a `webpack` instance. If you are using [hooks](#hooks) you might want to put custom metadata into the context and use it in the `post` hook. ### context.match diff --git a/packages/assets/CHANGELOG.md b/packages/assets/CHANGELOG.md index d9fd9aff..79c2cf14 100644 --- a/packages/assets/CHANGELOG.md +++ b/packages/assets/CHANGELOG.md @@ -1,5 +1,9 @@ # @webpack-blocks/assets - Changelog +## Next Release + +- Remove deprecated `fileType` API ([#260](https://github.com/andywer/webpack-blocks/issues/260)) + ## 1.0.0-rc - Added a `styleLoader` option to `css()` and `css.modules()` blocks. diff --git a/packages/assets/__tests__/css.test.js b/packages/assets/__tests__/css.test.js index 16b23b9b..58840e1a 100644 --- a/packages/assets/__tests__/css.test.js +++ b/packages/assets/__tests__/css.test.js @@ -96,46 +96,3 @@ test('style-loader can be disabled', t => { } ]) }) - -test('deprecated css() still works', t => { - const config = createConfig({}, [ - css('text/x-sass') - ]) - - t.deepEqual(config.module.rules, [ - { - test: /\.(sass|scss)$/, - use: [ - { - loader: 'style-loader', - options: {} - }, { - loader: 'css-loader', - options: {} - } - ] - } - ]) -}) - -test('deprecated css(, { exclude }) still works', t => { - const config = createConfig({}, [ - css('text/x-sass', { exclude: /node_modules/ }) - ]) - - t.deepEqual(config.module.rules, [ - { - test: /\.(sass|scss)$/, - exclude: /node_modules/, - use: [ - { - loader: 'style-loader', - options: {} - }, { - loader: 'css-loader', - options: {} - } - ] - } - ]) -}) diff --git a/packages/assets/__tests__/file.test.js b/packages/assets/__tests__/file.test.js index f94f88af..2813c464 100644 --- a/packages/assets/__tests__/file.test.js +++ b/packages/assets/__tests__/file.test.js @@ -46,42 +46,3 @@ test('file() works with options and match()', t => { } ]) }) - -test('deprecated file() still works', t => { - const config = createConfig({}, [ - file('image') - ]) - - t.deepEqual(config.module.rules, [ - { - test: /\.(gif|ico|jpg|jpeg|png|svg|webp)$/, - use: [ - { - loader: 'file-loader', - options: {} - } - ] - } - ]) -}) - -test('deprecated file(, { exclude }) still works', t => { - const config = createConfig({}, [ - file('image', { exclude: /node_modules/ }) - ]) - - t.deepEqual(config.module.rules, [ - { - test: /\.(gif|ico|jpg|jpeg|png|svg|webp)$/, - exclude: /node_modules/, - use: [ - { - loader: 'file-loader', - options: { - exclude: /node_modules/ - } - } - ] - } - ]) -}) diff --git a/packages/assets/__tests__/url.test.js b/packages/assets/__tests__/url.test.js index b0a66b73..5cbbfc08 100644 --- a/packages/assets/__tests__/url.test.js +++ b/packages/assets/__tests__/url.test.js @@ -46,42 +46,3 @@ test('url() works with options and match()', t => { } ]) }) - -test('deprecated url() still works', t => { - const config = createConfig({}, [ - url('image') - ]) - - t.deepEqual(config.module.rules, [ - { - test: /\.(gif|ico|jpg|jpeg|png|svg|webp)$/, - use: [ - { - loader: 'url-loader', - options: {} - } - ] - } - ]) -}) - -test('deprecated url(, { exclude }) still works', t => { - const config = createConfig({}, [ - url('image', { exclude: /node_modules/ }) - ]) - - t.deepEqual(config.module.rules, [ - { - test: /\.(gif|ico|jpg|jpeg|png|svg|webp)$/, - exclude: /node_modules/, - use: [ - { - loader: 'url-loader', - options: { - exclude: /node_modules/ - } - } - ] - } - ]) -}) diff --git a/packages/assets/lib/compat.js b/packages/assets/lib/compat.js deleted file mode 100644 index 63305f87..00000000 --- a/packages/assets/lib/compat.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = { - synthesizeMatch -} - -function synthesizeMatch (fileType, options) { - const match = {} - - if (fileType) { - match.test = fileType - } - if (options && options.exclude) { - match.exclude = options.exclude - } - if (options && options.include) { - match.include = options.include - } - - return match -} diff --git a/packages/assets/lib/css.js b/packages/assets/lib/css.js index f2e42891..dca5127d 100644 --- a/packages/assets/lib/css.js +++ b/packages/assets/lib/css.js @@ -1,26 +1,16 @@ const _ = require('lodash') -const { synthesizeMatch } = require('./compat') module.exports = css module.exports.modules = cssModules /** - * @param {string} [fileType] Deprecated. * @param {object} [options] You can pass all css-loader options. * @param {object} [options.styleLoader] style-loader options. If set to 'false' the 'style-loader' won't be added. * @return {Function} * @see https://github.com/webpack-contrib/css-loader */ -function css (fileType, options = {}) { - if (fileType && typeof fileType === 'object' && Object.keys(options).length === 0) { - options = fileType - fileType = null - } - if (fileType || options.exclude || options.include) { - console.warn(`css(): You are using the deprecated 'fileType' parameter, 'options.exclude' or 'options.include'. Use match() instead.`) - } - - const cssOptions = _.omit(options, ['exclude', 'include', 'styleLoader']) +function css (options = {}) { + const cssOptions = _.omit(options, ['styleLoader']) const loaders = [{ loader: 'css-loader', options: cssOptions }] if (options.styleLoader !== false) { @@ -33,15 +23,12 @@ function css (fileType, options = {}) { test: /\.css$/, use: loaders }, - // for API backwards compatibility only - synthesizeMatch(context.fileType(fileType || 'text/css'), options), context.match ) ) } /** - * @param {string} [fileType] Deprecated. * @param {object} [options] You can pass all css-loader options. * @param {number} [options.importLoaders] Defaults to 1. * @param {string} [options.localIdentName] Defaults to '[hash:base64:10]' in production, '[name]--[local]--[hash:base64:5]' in development. @@ -49,15 +36,7 @@ function css (fileType, options = {}) { * @return {Function} * @see https://github.com/webpack-contrib/css-loader */ -function cssModules (fileType, options = {}) { - if (fileType && typeof fileType === 'object' && Object.keys(options).length === 0) { - options = fileType - fileType = null - } - if (fileType || options.exclude || options.include) { - console.warn(`css.modules(): You are using the deprecated 'fileType' parameter, 'options.exclude' or 'options.include'. Use match() instead.`) - } - +function cssModules (options = {}) { const defaultCssOptions = { modules: true, importLoaders: 1, @@ -78,8 +57,6 @@ function cssModules (fileType, options = {}) { test: /\.css$/, use: loaders }, - // for API backwards compatibility only - synthesizeMatch(context.fileType(fileType || 'text/css'), options), context.match ) ) diff --git a/packages/assets/lib/file.js b/packages/assets/lib/file.js index 1f2f2946..d399b204 100644 --- a/packages/assets/lib/file.js +++ b/packages/assets/lib/file.js @@ -1,22 +1,13 @@ -const { synthesizeMatch } = require('./compat') - module.exports = file /** - * @param {string} [fileType] Deprecated. * @param {object} [options] You can pass all file-loader options. * @return {Function} * @see https://github.com/webpack-contrib/file-loader */ -function file (fileType, options = {}) { +function file (options = {}) { return (context, util) => { - if (fileType && typeof fileType === 'object' && Object.keys(options).length === 0) { - options = fileType - fileType = null - } - if (fileType || options.exclude || options.include) { - console.warn(`file(): You are using the deprecated 'fileType' parameter, 'options.exclude' or 'options.include'. Use match() instead.`) - } else if (!context.match) { + if (!context.match) { throw new Error( `The file() block can only be used in combination with match(). ` + `Use match() to state on which files to apply the file loader.` @@ -30,8 +21,6 @@ function file (fileType, options = {}) { { loader: 'file-loader', options } ] }, - // for API backwards compatibility only - synthesizeMatch(fileType && context.fileType(fileType), options), context.match ) ) diff --git a/packages/assets/lib/url.js b/packages/assets/lib/url.js index fa988543..9e98fc83 100644 --- a/packages/assets/lib/url.js +++ b/packages/assets/lib/url.js @@ -1,22 +1,13 @@ -const { synthesizeMatch } = require('./compat') - module.exports = url /** - * @param {string} [fileType] Deprecated. * @param {object} [options] You can pass all url-loader options. * @return {Function} * @see https://github.com/webpack-contrib/url-loader */ -function url (fileType, options = {}) { +function url (options = {}) { return (context, util) => { - if (fileType && typeof fileType === 'object' && Object.keys(options).length === 0) { - options = fileType - fileType = null - } - if (fileType || options.exclude || options.include) { - console.warn(`url(): You are using the deprecated 'fileType' parameter, 'options.exclude' or 'options.include'. Use match() instead.`) - } else if (!context.match) { + if (!context.match) { throw new Error( `The url() block can only be used in combination with match(). ` + `Use match() to state on which files to apply the url loader.` @@ -30,8 +21,6 @@ function url (fileType, options = {}) { { loader: 'url-loader', options } ] }, - // for API backwards compatibility only - synthesizeMatch(fileType && context.fileType(fileType), options), context.match ) ) diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 9926bd09..cffc02dd 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -1,5 +1,9 @@ # @webpack-blocks/core - Changelog +## Next Release + +- Remove deprecated `fileType` API ([#260](https://github.com/andywer/webpack-blocks/issues/260)) + ## 1.0.0 - Added `when()` ([#242](https://github.com/andywer/webpack-blocks/issues/242)) diff --git a/packages/core/lib/__tests__/fileTypes.test.js b/packages/core/lib/__tests__/fileTypes.test.js deleted file mode 100644 index c577e164..00000000 --- a/packages/core/lib/__tests__/fileTypes.test.js +++ /dev/null @@ -1,88 +0,0 @@ -import test from 'ava' -import { createConfig } from '../index' - -const webpack = {} - -test('fileType() works with existing type', (t) => { - t.plan(1) - - createConfig(webpack, [ - context => prevConfig => { - const regex = context.fileType('application/javascript') - t.is(String(regex), String(/\.(js|jsx)$/)) - return prevConfig - } - ]) -}) - -test('fileType() throws on non-existing type', (t) => { - t.plan(1) - - createConfig(webpack, [ - context => prevConfig => { - t.throws(() => context.fileType('application/not-existent'), /Type is not registered/) - return prevConfig - } - ]) -}) - -test('fileType.get() works with existing type', (t) => { - t.plan(1) - - createConfig(webpack, [ - context => prevConfig => { - const regex = context.fileType.get('application/javascript') - t.is(String(regex), String(/\.(js|jsx)$/)) - return prevConfig - } - ]) -}) - -test('fileType.get() throws on non-existing type', (t) => { - t.plan(1) - - createConfig(webpack, [ - context => prevConfig => { - t.throws(() => context.fileType.get('application/not-existent'), /Type is not registered/) - return prevConfig - } - ]) -}) - -test('fileType.has() works with existing types', (t) => { - t.plan(1) - - createConfig(webpack, [ - context => prevConfig => { - t.true(context.fileType.has('application/javascript')) - return prevConfig - } - ]) -}) - -test('fileType.has() works with non-existing types', (t) => { - t.plan(1) - - createConfig(webpack, [ - context => prevConfig => { - t.false(context.fileType.has('application/not-existent')) - return prevConfig - } - ]) -}) - -test('fileType.add() works', (t) => { - t.plan(2) - - createConfig(webpack, [ - context => prevConfig => { - t.throws(() => context.fileType.get('application/new-thing'), /Type is not registered/) - context.fileType.add('application/new-thing', '*.new') - return prevConfig - }, - context => prevConfig => { - t.is(context.fileType.get('application/new-thing'), '*.new') - return prevConfig - } - ]) -}) diff --git a/packages/core/lib/configSetters.js b/packages/core/lib/configSetters.js index 036696e7..5009092c 100644 --- a/packages/core/lib/configSetters.js +++ b/packages/core/lib/configSetters.js @@ -28,15 +28,6 @@ function invokeConfigSetters (configSetters, context, baseConfig) { return configSetters.reduce( (config, setter) => { const updateFunction = setter(context, blockUtils) - - if (typeof updateFunction !== 'function') { - throw new Error( - `Expected a function, instead got a ${typeof updateFunction}. ` + - 'Beware that the block API changed since version 0.x.\n\n' + - `Dump of what should have been a function: ${JSON.stringify(updateFunction)}` - ) - } - return updateFunction(config) }, baseConfig diff --git a/packages/core/lib/createConfig.js b/packages/core/lib/createConfig.js index e8acda61..87b88975 100644 --- a/packages/core/lib/createConfig.js +++ b/packages/core/lib/createConfig.js @@ -1,5 +1,3 @@ -const createFileTypesMapping = require('./createFileTypesMapping') -const defaultFileTypes = require('./defaultFileTypes') const { assertConfigSetters, invokeConfigSetters } = require('./configSetters') const { invokePreHooks, invokePostHooks } = require('./hooks') @@ -23,10 +21,6 @@ function createConfig (initialContext, configSetters) { } assertConfigSetters(configSetters) - /** @deprecated context.fileType */ - const fileType = createFileTypesMapping(defaultFileTypes) - const context = Object.assign({ fileType }, initialContext) - const baseConfig = { resolve: { // Explicitly define default extensions, otherwise blocks will overwrite them instead of extending @@ -45,9 +39,9 @@ function createConfig (initialContext, configSetters) { plugins: [] } - invokePreHooks(configSetters, context) - const config = invokeConfigSetters(configSetters, context, baseConfig) - const postProcessedConfig = invokePostHooks(configSetters, context, config) + invokePreHooks(configSetters, initialContext) + const config = invokeConfigSetters(configSetters, initialContext, baseConfig) + const postProcessedConfig = invokePostHooks(configSetters, initialContext, config) return postProcessedConfig } diff --git a/packages/core/lib/createFileTypesMapping.js b/packages/core/lib/createFileTypesMapping.js deleted file mode 100644 index b3f54584..00000000 --- a/packages/core/lib/createFileTypesMapping.js +++ /dev/null @@ -1,69 +0,0 @@ -module.exports = createFileTypesMapping - -function createFileTypesMapping (initialMapping) { - let currentMapping = initialMapping - - const mapperMethods = { - all () { - return currentMapping - }, - - /** - * @param {string} type MIME type. - * @return {RegExp|Function|string|array} - */ - get (type) { - if (!(type in currentMapping)) { - throw new Error(`FileTypes:get(): Type is not registered: ${type}`) - } - return currentMapping[ type ] - }, - - /** - * @param {string} type MIME type. - * @return {boolean} - */ - has (type) { - return type in currentMapping - }, - - /** - * @param {string|object} type - * @param {RegExp|Function|string|array} [condition] Only used if param `type` is a string. - * @return {FileTypesMapping} this - * @see https://webpack.github.io/docs/configuration.html#module-loaders - * @example `fileType.add('application/javascript', /\.jsx?$/)` - * @example `fileType.add({ 'application/javascript': [ /\.js$/, /\.jsx$/ ] })` - */ - add (type, condition) { - if (typeof type === 'string') { - currentMapping = addOne(type, condition) - } else if (typeof type === 'object') { - currentMapping = addMultiple(type) - } else { - throw new Error(`FileTypes:add(): Expected 1st param to be a string or object, but got: ${typeof type}`) - } - return mapper - } - } - - function FileTypeMapping (type) { - return mapper.get(type) - } - - function addOne (type, condition) { - if (!condition) { - throw new Error(`FileTypes:add(): Expected a 'condition' as 2nd param if 1st param is a string.`) - } - return Object.assign({}, currentMapping, { - [ type ]: condition - }) - } - - function addMultiple (types) { - return Object.assign({}, currentMapping, types) - } - - const mapper = Object.assign(FileTypeMapping, mapperMethods) - return mapper -} diff --git a/packages/core/lib/defaultFileTypes.js b/packages/core/lib/defaultFileTypes.js deleted file mode 100644 index e8a86f08..00000000 --- a/packages/core/lib/defaultFileTypes.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - 'application/font': /\.(eot|ttf|woff|woff2)(\?.*)?$/, - 'application/javascript': /\.(js|jsx)$/, - 'application/x-typescript': /\.(ts|tsx)$/, - 'application/x-elm': /\.elm$/, - 'application/json': /\.json$/, - 'audio': /\.(aac|m4a|mp3|oga|ogg|wav)$/, - 'image': /\.(gif|ico|jpg|jpeg|png|svg|webp)$/, - 'text/css': /\.css$/, - 'text/html': /\.html$/, - 'text/x-less': /\.less$/, - 'text/x-sass': /\.(sass|scss)$/, - 'video': /\.(mp4|webm)$/ -} diff --git a/packages/extract-text/CHANGELOG.md b/packages/extract-text/CHANGELOG.md index 3515251c..b6ee55ee 100644 --- a/packages/extract-text/CHANGELOG.md +++ b/packages/extract-text/CHANGELOG.md @@ -1,5 +1,9 @@ # @webpack-blocks/extract-text - Changelog +## Next Release + +- Remove deprecated `fileType` API ([#260](https://github.com/andywer/webpack-blocks/issues/260)) + ## 1.0.0-rc - Breaking change: drop webpack 2 support, update extract-text-webpack-plugin to v3 diff --git a/packages/extract-text/__tests__/integration.test.js b/packages/extract-text/__tests__/integration.test.js index c1c2d91c..4459db3b 100644 --- a/packages/extract-text/__tests__/integration.test.js +++ b/packages/extract-text/__tests__/integration.test.js @@ -100,27 +100,6 @@ test('fails properly if nothing to extract can be found', t => { ]), /No loaders found to extract contents from/) }) -test('deprecated fileType parameter does still work', t => { - const config = createConfig({}, [ - sass(), - extractText('css/styles.css', 'text/x-sass') - ]) - - const id = config.plugins[0].id - const plugin = Object.assign(new ExtractTextPlugin('css/styles.css'), { id }) - - t.deepEqual(config.plugins, [ plugin ]) - t.deepEqual(config.module.rules, [ - { - test: /\.(sass|scss)$/, - use: plugin.extract({ - fallback: [ 'style-loader' ], - use: [ 'css-loader', 'sass-loader' ] - }) - } - ]) -}) - function css () { return (context, util) => util.addLoader( Object.assign({ diff --git a/packages/extract-text/index.js b/packages/extract-text/index.js index e2833fa7..870b1fca 100644 --- a/packages/extract-text/index.js +++ b/packages/extract-text/index.js @@ -10,21 +10,16 @@ module.exports = extractText /** * @param {string} outputFilePattern - * @param {string} [fileType] Deprecated * @return {Function} */ -function extractText (outputFilePattern = 'css/[name].[contenthash:8].css', fileType = null) { +function extractText (outputFilePattern = 'css/[name].[contenthash:8].css') { const plugin = new ExtractTextPlugin(outputFilePattern) - if (fileType) { - console.warn(`extractText(): You are using the deprecated 'fileType' parameter. Use match() instead.`) - } - const postHook = (context, util) => prevConfig => { let nextConfig = prevConfig // Only apply to loaders in the same `match()` group or css loaders if there is no `match()` - const ruleToMatch = context.match || { test: fileType ? context.fileType(fileType) : /\.css$/ } + const ruleToMatch = context.match || { test: /\.css$/ } const matchingLoaderRules = getMatchingLoaderRules(ruleToMatch, prevConfig) if (matchingLoaderRules.length === 0) { diff --git a/packages/webpack/CHANGELOG.md b/packages/webpack/CHANGELOG.md index b070cea5..bb2949c2 100644 --- a/packages/webpack/CHANGELOG.md +++ b/packages/webpack/CHANGELOG.md @@ -1,5 +1,9 @@ # @webpack-blocks/webpack - Changelog +## Next Release + +- Remove deprecated `fileType` API ([#260](https://github.com/andywer/webpack-blocks/issues/260)) + ## 1.0.0 - Added `when()` ([#242](https://github.com/andywer/webpack-blocks/issues/242)) diff --git a/packages/webpack/__tests__/integration.test.js b/packages/webpack/__tests__/integration.test.js index 75a78668..04710d18 100644 --- a/packages/webpack/__tests__/integration.test.js +++ b/packages/webpack/__tests__/integration.test.js @@ -166,17 +166,10 @@ test('createConfig() creates a minimal configuration', t => { }) test('context contains necessary properties', t => { - t.plan(10) + t.plan(5) createConfig([ context => { - // context.fileType - t.is(typeof context.fileType, 'function') - t.is(typeof context.fileType.add, 'function') - t.is(typeof context.fileType.all, 'function') - t.is(typeof context.fileType.get, 'function') - t.is(typeof context.fileType.has, 'function') - // context.webpack t.is(typeof context.webpack, 'function') t.is(typeof context.webpack.EnvironmentPlugin, 'function')