forked from andywer/webpack-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (46 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Extract-text webpack block.
*
* @see https://github.com/webpack/extract-text-webpack-plugin
*/
const ExtractTextPlugin = require('extract-text-webpack-plugin')
module.exports = extractText
/**
* @param {string} outputFilePattern
* @param {string} [fileType] A MIME type used for file matching. Defaults to `text/css`.
* @return {Function}
*/
function extractText (outputFilePattern, fileType) {
outputFilePattern = outputFilePattern || 'css/[name].[contenthash:8].css'
fileType = fileType || 'text/css'
return (fileTypes, webpackConfig) => {
const cssLoader = webpackConfig.module.loaders.find((loader) => loader.test === fileTypes(fileType))
if (!cssLoader) {
throw new Error(`${fileType} loader could not be found in webpack config.`)
}
if (!cssLoader.loaders || cssLoader.loaders.length === 0) {
throw new Error(`No ${fileType} file loaders found.`)
}
if (!/^style(-loader)?$/.test(cssLoader.loaders[0])) {
throw new Error(`Expected "style-loader" to be first loader of .css files. Instead got: ${cssLoader.loaders[0]}`)
}
// `cssLoader.loaders` without the leading 'style-loader'
const nonStyleLoaders = [].concat(cssLoader.loaders)
nonStyleLoaders.shift()
return {
module: {
loaders: [
{
test: fileTypes(fileType),
exclude: cssLoader.exclude,
loader: ExtractTextPlugin.extract('style-loader', nonStyleLoaders),
loaders: undefined
}
]
},
plugins: [
new ExtractTextPlugin(outputFilePattern)
]
}
}
}