forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalConfigPlugin.js
151 lines (138 loc) · 4.92 KB
/
globalConfigPlugin.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
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
const path = require('path')
const resolve = require('resolve')
const { findExisting } = require('./util')
const { loadPartialConfigSync } = require('@babel/core')
module.exports = function createConfigPlugin (context, entry, asLib) {
return {
id: '@vue/cli-service-global-config',
apply: (api, options) => {
const _entry = path.resolve(context, entry)
api.chainWebpack(config => {
// entry is *.vue file, create alias for built-in js entry
if (/\.vue$/.test(entry)) {
config.resolve
.alias
.set('~entry', _entry)
entry = require.resolve('../template/main.js')
} else {
// make sure entry is relative
if (!/^\.\//.test(entry)) {
entry = `./${entry}`
}
}
// ensure core-js polyfills can be imported
config.resolve
.alias
.set('core-js', path.dirname(require.resolve('core-js')))
.set('regenerator-runtime', path.dirname(require.resolve('regenerator-runtime')))
// ensure loaders can be resolved properly
// this is done by locating vue's install location (which is a
// dependency of the global service)
const modulePath = path.resolve(require.resolve('vue'), '../../../')
config.resolveLoader
.modules
.add(modulePath)
// add resolve alias for vue and vue-hot-reload-api
// but prioritize versions installed locally.
try {
resolve.sync('vue', { basedir: context })
} catch (e) {
const vuePath = path.dirname(require.resolve('vue'))
config.resolve.alias
.set('vue$', `${vuePath}/${options.compiler ? `vue.esm.js` : `vue.runtime.esm.js`}`)
}
try {
resolve.sync('vue-hot-reload-api', { basedir: context })
} catch (e) {
config.resolve.alias
// eslint-disable-next-line node/no-extraneous-require
.set('vue-hot-reload-api', require.resolve('vue-hot-reload-api'))
}
// set entry
config
.entry('app')
.clear()
.add(entry)
const babelOptions = {
presets: [require.resolve('@vue/cli-plugin-babel/preset')]
}
// set inline babel options
config.module
.rule('js')
.include
.clear()
.end()
.exclude
.add(/node_modules/)
.add(/@vue\/cli-service/)
.end()
.uses
.delete('cache-loader')
.end()
.use('babel-loader')
.tap(() => babelOptions)
// check eslint config presence
// otherwise eslint-loader goes all the way up to look for eslintrc, can be
// messed up when the project is inside another project.
const ESLintConfigFile = findExisting(context, [
'.eslintrc.js',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslintrc.json',
'.eslintrc',
'package.json'
])
const hasESLintConfig = ESLintConfigFile === 'package.json'
? !!(require(path.join(context, 'package.json')).eslintConfig)
: !!ESLintConfigFile
const babelConfig = loadPartialConfigSync({ filename: _entry })
const hasBabelConfig = !!babelConfig && babelConfig.hasFilesystemConfig()
// set inline eslint options
config.module
.rule('eslint')
.include
.clear()
.end()
.exclude
.add(/node_modules/)
.end()
.use('eslint-loader')
.tap(loaderOptions => Object.assign({}, loaderOptions, {
useEslintrc: hasESLintConfig,
baseConfig: {
extends: [
'plugin:vue/essential',
'eslint:recommended'
],
parserOptions: {
parser: '@babel/eslint-parser',
requireConfigFile: hasBabelConfig,
babelOptions
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}
}))
if (!asLib) {
// set html plugin template
const indexFile = findExisting(context, [
'index.html',
'public/index.html'
]) || path.resolve(__dirname, '../template/index.html')
config
.plugin('html')
.tap(args => {
args[0].template = indexFile
return args
})
}
// disable copy plugin if no public dir
if (asLib || !findExisting(context, ['public'])) {
config.plugins.delete('copy')
}
})
}
}
}