-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
index.js
104 lines (92 loc) · 3.13 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
100
101
102
103
104
const path = require('path')
module.exports = (api, projectOptions) => {
const useThreads = process.env.NODE_ENV === 'production' && !!projectOptions.parallel
const { semver, loadModule } = require('@vue/cli-shared-utils')
const vue = loadModule('vue', api.service.context)
const isVue3 = (vue && semver.major(vue.version) === 3)
api.chainWebpack(config => {
config.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))
if (!projectOptions.pages) {
config.entry('app')
.clear()
.add('./src/main.ts')
}
config.resolve
.extensions
.prepend('.ts')
.prepend('.tsx')
const tsRule = config.module.rule('ts').test(/\.ts$/)
const tsxRule = config.module.rule('tsx').test(/\.tsx$/)
// add a loader to both *.ts & vue<lang="ts">
const addLoader = ({ name, loader, options }) => {
tsRule.use(name).loader(loader).options(options)
tsxRule.use(name).loader(loader).options(options)
}
try {
const cacheLoaderPath = require.resolve('cache-loader')
addLoader({
name: 'cache-loader',
loader: cacheLoaderPath,
options: api.genCacheConfig('ts-loader', {
'ts-loader': require('ts-loader/package.json').version,
'typescript': require('typescript/package.json').version,
modern: !!process.env.VUE_CLI_MODERN_BUILD
}, 'tsconfig.json')
})
} catch (e) {}
if (useThreads) {
addLoader({
name: 'thread-loader',
loader: require.resolve('thread-loader'),
options:
typeof projectOptions.parallel === 'number'
? { workers: projectOptions.parallel }
: {}
})
}
if (api.hasPlugin('babel')) {
addLoader({
name: 'babel-loader',
loader: require.resolve('babel-loader')
})
}
addLoader({
name: 'ts-loader',
loader: require.resolve('ts-loader'),
options: {
transpileOnly: true,
appendTsSuffixTo: ['\\.vue$'],
// https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
happyPackMode: useThreads
}
})
// make sure to append TSX suffix
tsxRule.use('ts-loader').loader(require.resolve('ts-loader')).tap(options => {
options = Object.assign({}, options)
delete options.appendTsSuffixTo
options.appendTsxSuffixTo = ['\\.vue$']
return options
})
// this plugin does not play well with jest + cypress setup (tsPluginE2e.spec.js) somehow
// so temporarily disabled for vue-cli tests
if (!process.env.VUE_CLI_TEST) {
config
.plugin('fork-ts-checker')
.use(require('fork-ts-checker-webpack-plugin'), [{
typescript: {
extensions: {
vue: {
enabled: true,
compiler: isVue3 ? require.resolve('vue/compiler-sfc') : require.resolve('vue-template-compiler')
}
},
diagnosticOptions: {
semantic: true,
// https://github.com/TypeStrong/ts-loader#happypackmode
syntactic: useThreads
}
}
}])
}
})
}