Skip to content

Commit ba43900

Browse files
committed
refactor: add invoking flag for generators, fix eslint + ts
close vuejs#1972
1 parent ee85f7c commit ba43900

File tree

7 files changed

+151
-135
lines changed

7 files changed

+151
-135
lines changed

packages/@vue/cli-plugin-eslint/__tests__/eslintGenerator.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ test('typescript', async () => {
9898
'@vue/prettier',
9999
'@vue/typescript'
100100
])
101-
expect(pkg.eslintConfig).not.toHaveProperty('parserOptions')
101+
expect(pkg.eslintConfig.parserOptions).toEqual({
102+
parser: 'typescript-eslint-parser'
103+
})
102104
expect(pkg.devDependencies).toHaveProperty('@vue/eslint-config-prettier')
103105
expect(pkg.devDependencies).toHaveProperty('@vue/eslint-config-typescript')
104106
})

packages/@vue/cli-plugin-eslint/generator.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = (api, { config, lintOn = [] }) => {
1+
module.exports = (api, { config, lintOn = [] }, _, invoking) => {
22
if (typeof lintOn === 'string') {
33
lintOn = lintOn.split(',')
44
}
@@ -33,14 +33,6 @@ module.exports = (api, { config, lintOn = [] }) => {
3333
eslintConfig.extends.push('eslint:recommended')
3434
}
3535

36-
// typescript support
37-
if (api.hasPlugin('typescript')) {
38-
eslintConfig.extends.push('@vue/typescript')
39-
Object.assign(pkg.devDependencies, {
40-
'@vue/eslint-config-typescript': '^3.0.0-rc.8'
41-
})
42-
}
43-
4436
if (!lintOn.includes('save')) {
4537
pkg.vue = {
4638
lintOnSave: false // eslint-loader configured in runtime plugin
@@ -62,30 +54,20 @@ module.exports = (api, { config, lintOn = [] }) => {
6254

6355
api.extendPackage(pkg)
6456

65-
if (api.hasPlugin('unit-mocha')) {
66-
const config = {
67-
env: { mocha: true }
68-
}
69-
if (config === 'airbnb') {
70-
config.rules = {
71-
'import/no-extraneous-dependencies': 'off'
72-
}
73-
}
74-
api.render(files => {
75-
files['tests/unit/.eslintrc.js'] = api.genJSConfig(config)
76-
})
77-
} else if (api.hasPlugin('unit-jest')) {
78-
const config = {
79-
env: { jest: true }
80-
}
81-
if (config === 'airbnb') {
82-
config.rules = {
83-
'import/no-extraneous-dependencies': 'off'
84-
}
57+
// typescript support
58+
if (api.hasPlugin('typescript')) {
59+
applyTS(api)
60+
}
61+
62+
// invoking only
63+
if (invoking) {
64+
if (api.hasPlugin('unit-mocha')) {
65+
// eslint-disable-next-line node/no-extraneous-require
66+
require('@vue/cli-plugin-unit-mocha/generator').applyESLint(api)
67+
} else if (api.hasPlugin('unit-jest')) {
68+
// eslint-disable-next-line node/no-extraneous-require
69+
require('@vue/cli-plugin-unit-jest/generator').applyESLint(api)
8570
}
86-
api.render(files => {
87-
files['tests/unit/.eslintrc.js'] = api.genJSConfig(config)
88-
})
8971
}
9072

9173
// lint & fix after create to ensure files adhere to chosen config
@@ -95,3 +77,17 @@ module.exports = (api, { config, lintOn = [] }) => {
9577
})
9678
}
9779
}
80+
81+
const applyTS = module.exports.applyTS = api => {
82+
api.extendPackage({
83+
eslintConfig: {
84+
extends: ['@vue/typescript'],
85+
parserOptions: {
86+
parser: 'typescript-eslint-parser'
87+
}
88+
},
89+
devDependencies: {
90+
'@vue/eslint-config-typescript': '^3.0.0-rc.8'
91+
}
92+
})
93+
}

packages/@vue/cli-plugin-typescript/__tests__/tsGenerator.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ test('compat with unit-mocha', async () => {
116116
const { pkg } = await generateWithPlugin([
117117
{
118118
id: '@vue/cli-plugin-unit-mocha',
119-
apply: () => {},
119+
apply: require('@vue/cli-plugin-unit-mocha/generator'),
120120
options: {}
121121
},
122122
{
123-
id: 'ts',
123+
id: '@vue/cli-plugin-typescript',
124124
apply: require('../generator'),
125125
options: {
126126
lint: true,
@@ -137,11 +137,11 @@ test('compat with unit-jest', async () => {
137137
const { pkg } = await generateWithPlugin([
138138
{
139139
id: '@vue/cli-plugin-unit-jest',
140-
apply: () => {},
140+
apply: require('@vue/cli-plugin-unit-jest/generator'),
141141
options: {}
142142
},
143143
{
144-
id: 'ts',
144+
id: '@vue/cli-plugin-typescript',
145145
apply: require('../generator'),
146146
options: {
147147
lint: true,

packages/@vue/cli-plugin-typescript/generator/index.js

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = (api, {
22
classComponent,
33
tsLint,
44
lintOn = []
5-
}) => {
5+
}, _, invoking) => {
66
if (typeof lintOn === 'string') {
77
lintOn = lintOn.split(',')
88
}
@@ -58,43 +58,28 @@ module.exports = (api, {
5858
})
5959
}
6060

61-
// inject necessary typings for other plugins
62-
63-
const hasMocha = api.hasPlugin('unit-mocha')
64-
if (hasMocha) {
65-
api.extendPackage({
66-
devDependencies: {
67-
'@types/mocha': '^5.2.4',
68-
'@types/chai': '^4.1.0'
69-
}
70-
})
71-
}
61+
// late invoke compat
62+
if (invoking) {
63+
if (api.hasPlugin('unit-mocha')) {
64+
// eslint-disable-next-line node/no-extraneous-require
65+
require('@vue/cli-plugin-unit-mocha/generator').applyTS(api)
66+
}
7267

73-
const hasJest = api.hasPlugin('unit-jest')
74-
if (hasJest) {
75-
api.extendPackage({
76-
devDependencies: {
77-
'@types/jest': '^23.1.4'
78-
}
79-
})
80-
}
68+
if (api.hasPlugin('unit-jest')) {
69+
// eslint-disable-next-line node/no-extraneous-require
70+
require('@vue/cli-plugin-unit-jest/generator').applyTS(api)
71+
}
8172

82-
const hasESLint = api.hasPlugin('eslint')
83-
if (hasESLint) {
84-
api.extendPackage({
85-
devDependencies: {
86-
'@vue/eslint-config-typescript': '^3.0.0-rc.8'
87-
},
88-
eslintConfig: {
89-
extends: ['@vue/typescript']
90-
}
91-
})
73+
if (api.hasPlugin('eslint')) {
74+
// eslint-disable-next-line node/no-extraneous-require
75+
require('@vue/cli-plugin-eslint/generator').applyTS(api)
76+
}
9277
}
9378

9479
api.render('./template', {
9580
isTest: process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG,
96-
hasMocha,
97-
hasJest
81+
hasMocha: api.hasPlugin('unit-mocha'),
82+
hasJest: api.hasPlugin('unit-jest')
9883
})
9984

10085
require('./convert')(api, { tsLint })

packages/@vue/cli-plugin-unit-jest/generator/index.js

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,44 @@ module.exports = api => {
66
},
77
devDependencies: {
88
'@vue/test-utils': '^1.0.0-beta.20'
9+
},
10+
jest: {
11+
'moduleFileExtensions': [
12+
'js',
13+
'jsx',
14+
'json',
15+
// tell Jest to handle *.vue files
16+
'vue'
17+
],
18+
'transform': {
19+
// process *.vue files with vue-jest
20+
'^.+\\.vue$': 'vue-jest',
21+
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub'
22+
},
23+
// support the same @ -> src alias mapping in source code
24+
'moduleNameMapper': {
25+
'^@/(.*)$': '<rootDir>/src/$1'
26+
},
27+
// serializer for snapshots
28+
'snapshotSerializers': [
29+
'jest-serializer-vue'
30+
],
31+
'testMatch': [
32+
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
33+
],
34+
// https://github.com/facebook/jest/issues/6766
35+
'testURL': 'http://localhost/'
936
}
1037
})
1138

12-
const jestConfig = {
13-
'moduleFileExtensions': [
14-
'js',
15-
'jsx',
16-
'json',
17-
// tell Jest to handle *.vue files
18-
'vue'
19-
],
20-
'transform': {
21-
// process *.vue files with vue-jest
22-
'^.+\\.vue$': 'vue-jest',
23-
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub'
24-
},
25-
// support the same @ -> src alias mapping in source code
26-
'moduleNameMapper': {
27-
'^@/(.*)$': '<rootDir>/src/$1'
28-
},
29-
// serializer for snapshots
30-
'snapshotSerializers': [
31-
'jest-serializer-vue'
32-
],
33-
'testMatch': [
34-
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
35-
],
36-
// https://github.com/facebook/jest/issues/6766
37-
'testURL': 'http://localhost/'
38-
}
39-
4039
if (!api.hasPlugin('typescript')) {
41-
jestConfig.transform['^.+\\.jsx?$'] = 'babel-jest'
40+
api.extendPackage({
41+
jest: {
42+
transform: {
43+
'^.+\\.jsx?$': 'babel-jest'
44+
}
45+
}
46+
})
4247
if (api.hasPlugin('babel')) {
4348
api.extendPackage({
4449
devDependencies: {
@@ -55,33 +60,45 @@ module.exports = api => {
5560
})
5661
}
5762
} else {
58-
jestConfig.moduleFileExtensions.unshift('ts', 'tsx')
59-
jestConfig.transform['^.+\\.tsx?$'] = 'ts-jest'
63+
applyTS(api)
64+
}
65+
66+
if (api.hasPlugin('eslint')) {
67+
applyESLint(api)
68+
}
69+
}
70+
71+
const applyTS = module.exports.applyTS = api => {
72+
// TODO inject type into tsconfig.json
73+
api.extendPackage({
74+
jest: {
75+
moduleFileExtensions: ['ts', 'tsx'],
76+
transform: {
77+
'^.+\\.tsx?$': 'ts-jest'
78+
}
79+
},
80+
devDependencies: {
81+
'@types/jest': '^23.1.4',
82+
'ts-jest': '^23.0.0'
83+
}
84+
})
85+
if (api.hasPlugin('babel')) {
6086
api.extendPackage({
6187
devDependencies: {
62-
'ts-jest': '^23.0.0'
88+
// this is for now necessary to force ts-jest and vue-jest to use babel 7
89+
'babel-core': '7.0.0-bridge.0'
6390
}
6491
})
65-
if (api.hasPlugin('babel')) {
66-
api.extendPackage({
67-
devDependencies: {
68-
// this is for now necessary to force ts-jest and vue-jest to use babel 7
69-
'babel-core': '7.0.0-bridge.0'
70-
}
71-
})
72-
}
7392
}
93+
}
7494

75-
api.extendPackage({ jest: jestConfig })
76-
77-
if (api.hasPlugin('eslint')) {
78-
api.render(files => {
79-
files['tests/unit/.eslintrc.js'] = api.genJSConfig({
80-
env: { jest: true },
81-
rules: {
82-
'import/no-extraneous-dependencies': 'off'
83-
}
84-
})
95+
const applyESLint = module.exports.applyESLint = api => {
96+
api.render(files => {
97+
files['tests/unit/.eslintrc.js'] = api.genJSConfig({
98+
env: { jest: true },
99+
rules: {
100+
'import/no-extraneous-dependencies': 'off'
101+
}
85102
})
86-
}
103+
})
87104
}
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
module.exports = api => {
22
api.render('./template')
33

4-
const devDependencies = {
5-
'@vue/test-utils': '^1.0.0-beta.20',
6-
'chai': '^4.1.2'
7-
}
8-
94
api.extendPackage({
10-
devDependencies,
5+
devDependencies: {
6+
'@vue/test-utils': '^1.0.0-beta.20',
7+
'chai': '^4.1.2'
8+
},
119
scripts: {
1210
'test:unit': 'vue-cli-service test:unit'
1311
}
1412
})
1513

1614
if (api.hasPlugin('eslint')) {
17-
api.render(files => {
18-
files['tests/unit/.eslintrc.js'] = api.genJSConfig({
19-
env: { mocha: true },
20-
rules: {
21-
'import/no-extraneous-dependencies': 'off'
22-
}
23-
})
24-
})
15+
applyESLint(api)
2516
}
17+
18+
if (api.hasPlugin('typescript')) {
19+
applyTS(api)
20+
}
21+
}
22+
23+
const applyESLint = module.exports.applyESLint = api => {
24+
api.render(files => {
25+
files['tests/unit/.eslintrc.js'] = api.genJSConfig({
26+
env: { mocha: true },
27+
rules: {
28+
'import/no-extraneous-dependencies': 'off'
29+
}
30+
})
31+
})
32+
}
33+
34+
const applyTS = module.exports.applyTS = api => {
35+
// TODO inject type into tsconfig.json
36+
api.extendPackage({
37+
devDependencies: {
38+
'@types/mocha': '^5.2.4',
39+
'@types/chai': '^4.1.0'
40+
}
41+
})
2642
}

packages/@vue/cli/lib/Generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module.exports = class Generator {
9393
// apply generators from plugins
9494
plugins.forEach(({ id, apply, options }) => {
9595
const api = new GeneratorAPI(id, this, options, rootOptions)
96-
apply(api, options, rootOptions)
96+
apply(api, options, rootOptions, invoking)
9797
})
9898
}
9999

0 commit comments

Comments
 (0)