forked from lwd-technology/react-app-rewire-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
99 lines (85 loc) · 2.98 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
93
94
95
96
97
98
99
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 rewireCoffeescript(config, env, coffeescriptLoaderOptions = {}) {
// 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.
// With vendor splitting, you might have named entries, usually the index should be 'main'
const makeUniversalIndexPath = () =>
path.resolve(fs.realpathSync(process.cwd()), 'src/index')
if (config.entry instanceof Array) {
config.entry = config.entry
.slice(0, config.entry.length - 1)
.concat([makeUniversalIndexPath()])
} else {
config.entry['main'] = makeUniversalIndexPath()
}
// Add Coffeescript files to automatic file resolution for Webpack.
config.resolve.extensions = (config.resolve.extensions || []).concat([
'.coffee'
])
// Set up a Coffeescript rule.
const babelLoader = getBabelLoader(config.module.rules)
const coffeescriptRules = {
test: /\.coffee$/,
use: [
{ loader: babelLoader.loader, options: babelLoader.options },
{ loader: 'coffee-loader', options: coffeescriptLoaderOptions }
]
}
// Add the Coffeescript rule before the file-loader rule.
addBeforeRule(config.module.rules, fileLoaderMatcher, coffeescriptRules)
return config
}
module.exports = rewireCoffeescript