This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathloaders.ts
168 lines (150 loc) · 4.19 KB
/
loaders.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as path from 'path'
import Config from 'webpack-chain'
import frontmatter from 'remark-frontmatter'
import remarkDocz from 'remark-docz'
import rehypeDocz from 'rehype-docz'
import slug from 'rehype-slug'
import * as paths from '../config/paths'
import { Config as Args } from '../config/argv'
import { BabelRC } from '../config/babel'
const excludeNodeModules = (filepath: string) =>
/node_modules/.test(filepath) || /@babel(?:\/|\\{1,2})runtime/.test(filepath)
export const sourceMaps = (config: Config, args: Args) => {
const srcPath = path.resolve(paths.root, args.src)
config.module
.rule('sourcemaps')
.test(/\.(js|mjs|jsx|ts|tsx|md|mdx)$/)
.include.add(srcPath)
.add(paths.app)
.end()
.exclude.add(excludeNodeModules)
.end()
.use('sourcemaps')
.loader(require.resolve('source-map-loader'))
.end()
.enforce('pre')
}
export interface AddScriptLoaderOpts {
threadLoader?: boolean
rule: Config.Rule
babelrc: BabelRC
args: Args
}
const addScriptLoaders = (opts: AddScriptLoaderOpts) => {
const { rule, threadLoader = true, babelrc, args } = opts
return rule
.when(!args.debug, rule =>
rule
.use('cache-loader')
.loader(require.resolve('cache-loader'))
.options({
cacheDirectory: paths.cache,
})
)
.when(Boolean(threadLoader), rule =>
rule
.use('thread-loader')
.loader(require.resolve('thread-loader'))
.options({
workers: require('os').cpus().length - 1,
})
)
.use('babel-loader')
.loader(require.resolve('babel-loader'))
.options(babelrc)
.end()
}
export const js = (config: Config, args: Args, babelrc: BabelRC) => {
const srcPath = path.resolve(paths.root, args.src)
const rule = config.module
.rule('js')
.test(/\.(jsx?|mjs)$/)
.include.add(srcPath)
.add(paths.app)
.end()
.exclude.add(excludeNodeModules)
.end()
addScriptLoaders({ rule, babelrc, args })
}
export const ts = (config: Config, args: Args, babelrc: BabelRC) => {
const srcPath = path.resolve(paths.root, args.src)
const rule = config.module
.rule('ts')
.test(/\.tsx?$/)
.include.add(srcPath)
.add(paths.app)
.end()
.exclude.add(excludeNodeModules)
.end()
addScriptLoaders({ rule, babelrc, args })
}
export const mdx = (config: Config, args: Args, babelrc: BabelRC) => {
const { mdPlugins, hastPlugins } = args
const srcPath = path.resolve(paths.root, args.src)
const rule = config.module
.rule('mdx')
.test(/\.(md|markdown|mdx)$/)
.include.add(srcPath)
.add(paths.root)
.add(paths.app)
.end()
.exclude.add(excludeNodeModules)
.end()
addScriptLoaders({ rule, babelrc, args, threadLoader: false })
.use('mdx-loader')
.loader(require.resolve('@mdx-js/loader'))
.options({
mdPlugins: mdPlugins.concat([
[frontmatter, { type: 'yaml', marker: '-' }],
remarkDocz,
]),
hastPlugins: hastPlugins.concat([
[rehypeDocz, { root: paths.root, useCodeSandbox: args.codeSandbox }],
slug,
]),
})
}
const INLINE_LIMIT = 10000
export const images = (config: Config) => {
config.module
.rule('images')
.test(/\.(png|jpe?g|gif)(\?.*)?$/)
.use('url-loader')
.loader(require.resolve('url-loader'))
.options({
limit: INLINE_LIMIT,
name: `static/img/[name].[hash:8].[ext]`,
})
}
export const svg = (config: Config) => {
config.module
.rule('svg')
.test(/\.(svg)(\?.*)?$/)
.use('file-loader')
.loader(require.resolve('file-loader'))
.options({
name: `static/img/[name].[hash:8].[ext]`,
})
}
export const media = (config: Config) => {
config.module
.rule('media')
.test(/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/)
.use('url-loader')
.loader(require.resolve('url-loader'))
.options({
limit: INLINE_LIMIT,
name: `static/media/[name].[hash:8].[ext]`,
})
}
export const fonts = (config: Config) => {
config.module
.rule('fonts')
.test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
.use('url-loader')
.loader(require.resolve('url-loader'))
.options({
limit: INLINE_LIMIT,
name: `static/fonts/[name].[hash:8].[ext]`,
})
}