|
1 | 1 | import * as webpack from 'webpack'; |
2 | | -type OldOrNewModule = webpack.OldModule & webpack.NewModule; |
3 | 2 |
|
4 | | -export function addReactHotModuleReplacementBabelTransform(webpackConfig: webpack.Configuration) { |
5 | | - const moduleConfig = webpackConfig.module as OldOrNewModule; |
6 | | - const moduleRules = moduleConfig.rules // Webpack >= 2.1.0 beta 23 |
7 | | - || moduleConfig.loaders; // Legacy/back-compat |
| 3 | +const reactHotLoaderWebpackLoader = 'react-hot-loader/webpack'; |
| 4 | +const reactHotLoaderPatch = 'react-hot-loader/patch'; |
| 5 | +const supportedTypeScriptLoaders = ['ts-loader', 'awesome-typescript-loader']; |
| 6 | + |
| 7 | +export function addReactHotModuleReplacementConfig(webpackConfig: webpack.Configuration) { |
| 8 | + const moduleConfig = webpackConfig.module as webpack.NewModule; |
| 9 | + const moduleRules = moduleConfig.rules; |
8 | 10 | if (!moduleRules) { |
9 | | - return; // Unknown rules list format |
| 11 | + return; // Unknown rules list format. Might be Webpack 1.x, which is not supported. |
10 | 12 | } |
11 | 13 |
|
12 | | - moduleRules.forEach(rule => { |
13 | | - // Allow rules/loaders entries to be either { loader: ... } or { use: ... } |
14 | | - // Ignore other config formats (too many combinations to support them all) |
15 | | - let loaderConfig = |
16 | | - (rule as webpack.NewUseRule).use // Recommended config format for Webpack 2.x |
17 | | - || (rule as webpack.LoaderRule).loader; // Typical config format for Webpack 1.x |
18 | | - if (!loaderConfig) { |
19 | | - return; // Not a supported rule format (e.g., an array) |
| 14 | + // Find the rule that loads TypeScript files, and prepend 'react-hot-loader/webpack' |
| 15 | + // to its array of loaders |
| 16 | + for (let ruleIndex = 0; ruleIndex < moduleRules.length; ruleIndex++) { |
| 17 | + // We only support NewUseRule (i.e., { use: ... }) because OldUseRule doesn't accept array values |
| 18 | + const rule = moduleRules[ruleIndex] as webpack.NewUseRule; |
| 19 | + if (!rule.use) { |
| 20 | + continue; |
20 | 21 | } |
21 | 22 |
|
22 | | - // Allow use/loader values to be either { loader: 'name' } or 'name' |
23 | | - // We don't need to support other possible ways of specifying loaders (e.g., arrays), |
24 | | - // so skip unrecognized formats. |
25 | | - const loaderNameString = |
26 | | - (loaderConfig as (webpack.OldLoader | webpack.NewLoader)).loader |
27 | | - || (loaderConfig as string); |
28 | | - if (!loaderNameString || (typeof loaderNameString !== 'string')) { |
29 | | - return; // Not a supported loader format (e.g., an array) |
| 23 | + // We're looking for the first 'use' value that's a TypeScript loader |
| 24 | + const loadersArray = rule.use instanceof Array ? rule.use : [rule.use]; |
| 25 | + const isTypescriptLoader = supportedTypeScriptLoaders.some(typeScriptLoaderName => containsLoader(loadersArray, typeScriptLoaderName)); |
| 26 | + if (!isTypescriptLoader) { |
| 27 | + continue; |
30 | 28 | } |
31 | 29 |
|
32 | | - // Find the babel-loader entry |
33 | | - if (loaderNameString.match(/\bbabel-loader\b/)) { |
34 | | - // If the rule is of the form { use: 'name' }, then replace it |
35 | | - // with { use: { loader: 'name' }} so we can attach options |
36 | | - if ((rule as webpack.NewUseRule).use && typeof loaderConfig === 'string') { |
37 | | - loaderConfig = (rule as webpack.NewUseRule).use = { loader: loaderConfig }; |
38 | | - } |
39 | | - |
40 | | - const configItemWithOptions = typeof loaderConfig === 'string' |
41 | | - ? rule // The rule is of the form { loader: 'name' }, so put options on the rule |
42 | | - : loaderConfig; // The rule is of the form { use/loader: { loader: 'name' }}, so put options on the use/loader |
43 | | - |
44 | | - // Ensure the config has an 'options' (or a legacy 'query') |
45 | | - let optionsObject = |
46 | | - (configItemWithOptions as webpack.NewLoader).options // Recommended config format for Webpack 2.x |
47 | | - || (configItemWithOptions as webpack.OldLoaderRule).query; // Legacy |
48 | | - if (!optionsObject) { |
49 | | - // If neither options nor query was set, define a new value, |
50 | | - // using the legacy format ('query') for compatibility with Webpack 1.x |
51 | | - optionsObject = (configItemWithOptions as webpack.OldLoaderRule).query = {}; |
52 | | - } |
| 30 | + // This is the one - prefix it with the react-hot-loader loader |
| 31 | + // (unless it's already in there somewhere) |
| 32 | + if (!containsLoader(loadersArray, reactHotLoaderWebpackLoader)) { |
| 33 | + loadersArray.unshift(reactHotLoaderWebpackLoader); |
| 34 | + rule.use = loadersArray; // In case we normalised it to an array |
| 35 | + } |
| 36 | + break; |
| 37 | + } |
53 | 38 |
|
54 | | - // Ensure Babel plugins includes 'react-transform' |
55 | | - const plugins = optionsObject['plugins'] = optionsObject['plugins'] || []; |
56 | | - const hasReactTransform = plugins.some(p => p && p[0] === 'react-transform'); |
57 | | - if (!hasReactTransform) { |
58 | | - plugins.push(['react-transform', {}]); |
59 | | - } |
| 39 | + // Ensure the entrypoint is prefixed with 'react-hot-loader/patch' (unless it's already in there). |
| 40 | + // We only support entrypoints of the form { name: value } (not just 'name' or ['name']) |
| 41 | + // because that gives us a place to prepend the new value |
| 42 | + if (!webpackConfig.entry || typeof webpackConfig.entry === 'string' || webpackConfig.entry instanceof Array) { |
| 43 | + throw new Error('Cannot enable React HMR because \'entry\' in Webpack config is not of the form { name: value }'); |
| 44 | + } |
| 45 | + const entryConfig = webpackConfig.entry as webpack.Entry; |
| 46 | + Object.getOwnPropertyNames(entryConfig).forEach(entrypointName => { |
| 47 | + if (typeof(entryConfig[entrypointName]) === 'string') { |
| 48 | + // Normalise to array |
| 49 | + entryConfig[entrypointName] = [entryConfig[entrypointName] as string]; |
| 50 | + } |
60 | 51 |
|
61 | | - // Ensure 'react-transform' plugin is configured to use 'react-transform-hmr' |
62 | | - plugins.forEach(pluginConfig => { |
63 | | - if (pluginConfig && pluginConfig[0] === 'react-transform') { |
64 | | - const pluginOpts = pluginConfig[1] = pluginConfig[1] || {}; |
65 | | - const transforms = pluginOpts.transforms = pluginOpts.transforms || []; |
66 | | - const hasReactTransformHmr = transforms.some(t => t.transform === 'react-transform-hmr'); |
67 | | - if (!hasReactTransformHmr) { |
68 | | - transforms.push({ |
69 | | - transform: 'react-transform-hmr', |
70 | | - imports: ['react'], |
71 | | - locals: ['module'] // Important for Webpack HMR |
72 | | - }); |
73 | | - } |
74 | | - } |
75 | | - }); |
| 52 | + let entryValueArray = entryConfig[entrypointName] as string[]; |
| 53 | + if (entryValueArray.indexOf(reactHotLoaderPatch) < 0) { |
| 54 | + entryValueArray.unshift(reactHotLoaderPatch); |
76 | 55 | } |
77 | 56 | }); |
78 | 57 | } |
| 58 | + |
| 59 | +function containsLoader(loadersArray: webpack.Loader[], loaderName: string) { |
| 60 | + return loadersArray.some(loader => { |
| 61 | + // Allow 'use' values to be either { loader: 'name' } or 'name' |
| 62 | + // No need to support legacy webpack.OldLoader |
| 63 | + const actualLoaderName = (loader as webpack.NewLoader).loader || (loader as string); |
| 64 | + return actualLoaderName && new RegExp(`\\b${ loaderName }\\b`).test(actualLoaderName); |
| 65 | + }); |
| 66 | +} |
0 commit comments