* @param {string} outputFilePattern * @return {Function}
(outputFilePattern = 'css/[name].[contenthash:8].css')
| 13 | * @return {Function} |
| 14 | */ |
| 15 | function extractText(outputFilePattern = 'css/[name].[contenthash:8].css') { |
| 16 | const plugin = new ExtractTextPlugin(outputFilePattern) |
| 17 | |
| 18 | const postHook = (context, util) => prevConfig => { |
| 19 | let nextConfig = prevConfig |
| 20 | |
| 21 | // Only apply to loaders in the same `match()` group or css loaders if there is no `match()` |
| 22 | const ruleToMatch = context.match || { test: /\.css$/ } |
| 23 | const matchingLoaderRules = getMatchingLoaderRules(ruleToMatch, prevConfig) |
| 24 | |
| 25 | if (matchingLoaderRules.length === 0) { |
| 26 | throw new Error( |
| 27 | `extractText(): No loaders found to extract contents from. Looking for loaders matching ${ |
| 28 | ruleToMatch.test |
| 29 | }` |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | const [fallbackLoaders, nonFallbackLoaders] = splitFallbackRule(matchingLoaderRules) |
| 34 | |
| 35 | const newLoaderDef = Object.assign({}, ruleToMatch, { |
| 36 | use: plugin.extract({ |
| 37 | fallback: fallbackLoaders, |
| 38 | use: nonFallbackLoaders |
| 39 | }) |
| 40 | }) |
| 41 | |
| 42 | for (const ruleToRemove of matchingLoaderRules) { |
| 43 | nextConfig = removeLoaderRule(ruleToRemove)(nextConfig) |
| 44 | } |
| 45 | |
| 46 | nextConfig = util.addPlugin(plugin)(nextConfig) |
| 47 | nextConfig = util.addLoader(newLoaderDef)(nextConfig) |
| 48 | |
| 49 | return nextConfig |
| 50 | } |
| 51 | |
| 52 | return Object.assign(() => prevConfig => prevConfig, { post: postHook }) |
| 53 | } |
| 54 | |
| 55 | function getMatchingLoaderRules(ruleToMatch, webpackConfig) { |
| 56 | return webpackConfig.module.rules.filter( |
no outgoing calls
no test coverage detected
searching dependent graphs…