Skip to content

Commit

Permalink
Remove the old fileType API (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitmel authored and Vlad Zhukov committed Apr 21, 2018
1 parent f4a0754 commit 5644112
Show file tree
Hide file tree
Showing 20 changed files with 30 additions and 424 deletions.
8 changes: 1 addition & 7 deletions docs/BLOCK-CREATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions packages/assets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
43 changes: 0 additions & 43 deletions packages/assets/__tests__/css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,3 @@ test('style-loader can be disabled', t => {
}
])
})

test('deprecated css(<fileType>) 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(<fileType>, { 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: {}
}
]
}
])
})
39 changes: 0 additions & 39 deletions packages/assets/__tests__/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,3 @@ test('file() works with options and match()', t => {
}
])
})

test('deprecated file(<fileType>) 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(<fileType>, { 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/
}
}
]
}
])
})
39 changes: 0 additions & 39 deletions packages/assets/__tests__/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,3 @@ test('url() works with options and match()', t => {
}
])
})

test('deprecated url(<fileType>) 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(<fileType>, { 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/
}
}
]
}
])
})
19 changes: 0 additions & 19 deletions packages/assets/lib/compat.js

This file was deleted.

29 changes: 3 additions & 26 deletions packages/assets/lib/css.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -33,31 +23,20 @@ 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.
* @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 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,
Expand All @@ -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
)
)
Expand Down
15 changes: 2 additions & 13 deletions packages/assets/lib/file.js
Original file line number Diff line number Diff line change
@@ -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.`
Expand All @@ -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
)
)
Expand Down
15 changes: 2 additions & 13 deletions packages/assets/lib/url.js
Original file line number Diff line number Diff line change
@@ -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.`
Expand All @@ -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
)
)
Expand Down
4 changes: 4 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
Loading

0 comments on commit 5644112

Please sign in to comment.