-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
92 lines (80 loc) · 2.71 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs')
const path = require('path')
const { getBabelLoader } = require('react-app-rewired')
/**
* @param {Object} rule
* @return {Array}
*/
const ruleChildren = rule =>
rule.use || rule.oneOf || (Array.isArray(rule.loader) && rule.loader) || []
const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result
const rules = Array.isArray(rulesSource)
? rulesSource
: ruleChildren(rulesSource)
rules.some(
(rule, index) =>
(result = ruleMatcher(rule)
? { index, rules }
: findIndexAndRules(ruleChildren(rule), ruleMatcher))
)
return result
}
/**
* Given a rule, return if it uses a specific loader.
*/
const createLoaderMatcher = loader => rule =>
rule.loader && rule.loader.indexOf(`${path.sep}${loader}${path.sep}`) !== -1
/**
* Get the existing file-loader config.
*/
const fileLoaderMatcher = createLoaderMatcher('file-loader')
/**
* Add one rule before another in the list of rules.
*/
const addBeforeRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher)
rules.splice(index, 0, value)
}
/**
* @param {object} config
* @param {object} config.resolve
* @param {string[]} config.resolve.extensions
* @param {object} config.module
* @param {any[]} config.module.rules
* @param {string[]} config.entry
*/
function rewireTypescript(config, env, typescriptLoaderOptions = {}) {
// Monkey patch react-scripts paths to use just `src` instead of
// `src/index.js` specifically. Hopefully this can get removed at some point.
// @see https://github.com/facebookincubator/create-react-app/issues/3052
let paths = require('react-scripts/config/paths')
if (paths) {
paths.appIndexJs = path.resolve(fs.realpathSync(process.cwd()), 'src')
}
// Change the hardcoded `index.js` to just `index`, so that it will resolve as
// whichever file is available. The use of `fs` is to handle things like
// symlinks.
config.entry = config.entry
.slice(0, config.entry.length - 1)
.concat([path.resolve(fs.realpathSync(process.cwd()), 'src/index')])
// Add Typescript files to automatic file resolution for Webpack.
config.resolve.extensions = (config.resolve.extensions || []).concat([
'.web.ts',
'.ts',
'.tsx'
])
// Set up a Typescript rule.
const babelLoader = getBabelLoader(config.module.rules)
const typescriptRules = {
test: /\.tsx?$/,
use: [
{ loader: babelLoader.loader, options: babelLoader.options },
{ loader: 'ts-loader', options: typescriptLoaderOptions }
]
}
// Add the Typescript rule before the file-loader rule.
addBeforeRule(config.module.rules, fileLoaderMatcher, typescriptRules)
return config
}
module.exports = rewireTypescript