Skip to content

Commit 362587a

Browse files
pksunkarahaoqunjiang
authored andcommitted
feat!: make vuex a separate plugin (#4242)
fixes #2335, #4024
1 parent c0aa2bb commit 362587a

File tree

22 files changed

+98
-58
lines changed

22 files changed

+98
-58
lines changed

docs/guide/plugins-and-presets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ Here's an example preset:
105105
``` json
106106
{
107107
"useConfigFiles": true,
108-
"vuex": true,
109108
"cssPreprocessor": "sass",
110109
"plugins": {
111110
"@vue/cli-plugin-babel": {},
112111
"@vue/cli-plugin-eslint": {
113112
"config": "airbnb",
114113
"lintOn": ["save", "commit"]
115114
},
116-
"@vue/cli-plugin-router": {}
115+
"@vue/cli-plugin-router": {},
116+
"@vue/cli-plugin-vuex": {}
117117
}
118118
}
119119
```

docs/ru/guide/plugins-and-presets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ vue add @vue/eslint --config airbnb --lintOn save
105105
``` json
106106
{
107107
"useConfigFiles": true,
108-
"vuex": true,
109108
"cssPreprocessor": "sass",
110109
"plugins": {
111110
"@vue/cli-plugin-babel": {},
112111
"@vue/cli-plugin-eslint": {
113112
"config": "airbnb",
114113
"lintOn": ["save", "commit"]
115114
},
116-
"@vue/cli-plugin-router": {}
115+
"@vue/cli-plugin-router": {},
116+
"@vue/cli-plugin-vuex": {}
117117
}
118118
}
119119
```

docs/zh/guide/plugins-and-presets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ vue add @vue/eslint --config airbnb --lintOn save
105105
``` json
106106
{
107107
"useConfigFiles": true,
108-
"vuex": true,
109108
"cssPreprocessor": "sass",
110109
"plugins": {
111110
"@vue/cli-plugin-babel": {},
112111
"@vue/cli-plugin-eslint": {
113112
"config": "airbnb",
114113
"lintOn": ["save", "commit"]
115114
},
116-
"@vue/cli-plugin-router": {}
115+
"@vue/cli-plugin-router": {},
116+
"@vue/cli-plugin-vuex": {}
117117
}
118118
}
119119
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__
2+
__mocks__
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# @vue/cli-plugin-vuex
2+
3+
> vuex plugin for vue-cli
4+
5+
## Installing in an Already Created Project
6+
7+
``` sh
8+
vue add @vue/vuex
9+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin')
2+
3+
test('base', async () => {
4+
const { files, pkg } = await generateWithPlugin({
5+
id: 'vuex',
6+
apply: require('../generator'),
7+
options: {}
8+
})
9+
10+
expect(files['src/store/index.js']).toBeTruthy()
11+
expect(files['src/store/index.js']).toMatch('import Vuex')
12+
13+
expect(pkg.dependencies).toHaveProperty('vuex')
14+
})

packages/@vue/cli-service/generator/vuex/index.js renamed to packages/@vue/cli-plugin-vuex/generator/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
module.exports = (api, options) => {
1+
module.exports = (api, options = {}) => {
2+
api.assertCliVersion('^4.0.0-alpha.3')
3+
api.assertCliServiceVersion('^4.0.0-alpha.3')
4+
25
api.injectImports(api.entryFile, `import store from './store'`)
36
api.injectRootOptions(api.entryFile, `store`)
7+
48
api.extendPackage({
59
dependencies: {
610
vuex: '^3.0.1'
711
}
812
})
9-
api.render('./template')
13+
14+
api.render('./template', {
15+
})
1016

1117
if (api.invoking && api.hasPlugin('typescript')) {
1218
/* eslint-disable-next-line node/no-extraneous-require */

packages/@vue/cli-service/generator/vuex/template/src/store.js renamed to packages/@vue/cli-plugin-vuex/generator/template/src/store/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ Vue.use(Vuex)
55

66
export default new Vuex.Store({
77
state: {
8-
98
},
109
mutations: {
11-
1210
},
1311
actions: {
14-
12+
},
13+
modules: {
1514
}
1615
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (api, options = {}) => {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@vue/cli-plugin-vuex",
3+
"version": "4.0.0-alpha.3",
4+
"description": "Vuex plugin for vue-cli",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/vuejs/vue-cli.git",
9+
"directory": "packages/@vue/cli-plugin-vuex"
10+
},
11+
"keywords": [
12+
"vue",
13+
"cli",
14+
"vuex"
15+
],
16+
"author": "Evan You",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/vuejs/vue-cli/issues"
20+
},
21+
"homepage": "https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-plugin-vuex#readme",
22+
"publishConfig": {
23+
"access": "public"
24+
},
25+
"dependencies": {},
26+
"devDependencies": {
27+
"@vue/cli-test-utils": "^4.0.0-alpha.3"
28+
}
29+
}

packages/@vue/cli-service/__tests__/serve.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ test('serve with legacy router option', async () => {
7474
)
7575
})
7676

77+
test('serve with legacy vuex option', async () => {
78+
const project = await create('e2e-serve-legacy-vuex', Object.assign({}, defaultPreset, {
79+
vuex: true
80+
}))
81+
82+
await serve(
83+
() => project.run('vue-cli-service serve'),
84+
async ({ page, helpers }) => {
85+
expect(await helpers.getText('h1')).toMatch(`Welcome to Your Vue.js App`)
86+
}
87+
)
88+
})
89+
7790
test('serve with inline entry', async () => {
7891
const project = await create('e2e-serve-inline-entry', defaultPreset)
7992

packages/@vue/cli-service/generator/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ module.exports = (api, options) => {
2525
]
2626
})
2727

28-
if (options.vuex) {
29-
require('./vuex')(api, options)
30-
}
31-
3228
if (options.cssPreprocessor) {
3329
const deps = {
3430
sass: {

packages/@vue/cli-service/lib/PluginAPI.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ class PluginAPI {
6464
* @return {boolean}
6565
*/
6666
hasPlugin (id) {
67-
if (['vuex'].includes(id)) {
68-
const pkg = this.service.pkg
69-
return ((pkg.dependencies && pkg.dependencies[id]) || (pkg.devDependencies && pkg.devDependencies[id]))
70-
}
7167
return this.service.plugins.some(p => matchesPluginId(id, p.id))
7268
}
7369

packages/@vue/cli-ui/apollo-server/api/PluginApi.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const logs = require('../connectors/logs')
44
const sharedData = require('../connectors/shared-data')
55
const views = require('../connectors/views')
66
const suggestions = require('../connectors/suggestions')
7-
const folders = require('../connectors/folders')
87
const progress = require('../connectors/progress')
98
const app = require('../connectors/app')
109
// Utils
@@ -397,10 +396,6 @@ class PluginApi {
397396
* @param {string} id Plugin id or short id
398397
*/
399398
hasPlugin (id) {
400-
if (['vuex'].includes(id)) {
401-
const pkg = folders.readPackage(this.cwd, this.context, true)
402-
return ((pkg.dependencies && pkg.dependencies[id]) || (pkg.devDependencies && pkg.devDependencies[id]))
403-
}
404399
return this.plugins.some(p => matchesPluginId(id, p.id))
405400
}
406401

packages/@vue/cli-ui/tests/e2e/specs/g2-plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('Plugins', () => {
22
it('Should display the plugins', () => {
33
cy.visit('/plugins')
4-
cy.get('.project-plugin-item').should('have.length', 5)
4+
cy.get('.project-plugin-item').should('have.length', 6)
55
})
66

77
it('Should add a plugin', () => {
@@ -26,6 +26,6 @@ describe('Plugins', () => {
2626
.should('be.visible')
2727
.should('not.have.class', 'disabled')
2828
.click()
29-
cy.get('.project-plugin-item').should('have.length', 5)
29+
cy.get('.project-plugin-item').should('have.length', 6)
3030
})
3131
})

packages/@vue/cli-ui/ui-defaults/suggestions.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const { loadModule } = require('@vue/cli-shared-utils')
21
const invoke = require('@vue/cli/lib/invoke')
32

43
const ROUTER = 'org.vue.vue-router-add'
@@ -78,14 +77,7 @@ async function install (api, id) {
7877
let error
7978

8079
try {
81-
if (id === 'router') {
82-
await invoke(id, {}, context)
83-
} else {
84-
await invoke.runGenerator(context, {
85-
id: `core:${id}`,
86-
apply: loadModule(`@vue/cli-service/generator/${id}`, context)
87-
})
88-
}
80+
await invoke(id, {}, context)
8981
} catch (e) {
9082
error = e
9183
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ module.exports = class Creator extends EventEmitter {
106106
}
107107
}
108108

109+
// legacy support for vuex
110+
if (preset.vuex) {
111+
preset.plugins['@vue/cli-plugin-vuex'] = {}
112+
}
113+
109114
const packageManager = (
110115
cliOptions.packageManager ||
111116
loadOptions().packageManager ||

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,6 @@ module.exports = class Generator {
243243
}
244244

245245
hasPlugin (_id) {
246-
if (['vuex'].includes(_id)) {
247-
const pkg = this.pkg
248-
return ((pkg.dependencies && pkg.dependencies[_id]) || (pkg.devDependencies && pkg.devDependencies[_id]))
249-
}
250246
return [
251247
...this.plugins.map(p => p.id),
252248
...Object.keys(this.pkg.devDependencies || {}),

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@ const {
88
hasProjectYarn,
99
hasProjectPnpm,
1010
resolvePluginId,
11-
resolveModule,
12-
loadModule
11+
resolveModule
1312
} = require('@vue/cli-shared-utils')
1413

1514
async function add (pluginName, options = {}, context = process.cwd()) {
16-
// special internal "plugins"
17-
if (/^(@vue\/)?vuex$/.test(pluginName)) {
18-
return addVuex(context)
19-
}
20-
2115
const packageName = resolvePluginId(pluginName)
2216

2317
log()
@@ -38,13 +32,6 @@ async function add (pluginName, options = {}, context = process.cwd()) {
3832
}
3933
}
4034

41-
async function addVuex (context) {
42-
invoke.runGenerator(context, {
43-
id: 'core:vuex',
44-
apply: loadModule('@vue/cli-service/generator/vuex', context)
45-
})
46-
}
47-
4835
module.exports = (...args) => {
4936
return add(...args).catch(err => {
5037
error(err)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const rcPath = exports.rcPath = getRcPath('.vuerc')
1010
const presetSchema = createSchema(joi => joi.object().keys({
1111
bare: joi.boolean(),
1212
useConfigFiles: joi.boolean(),
13-
// TODO: Use warn for router once @hapi/joi v16 releases
13+
// TODO: Use warn for router and vuex once @hapi/joi v16 releases
1414
router: joi.boolean(),
1515
routerHistoryMode: joi.boolean(),
1616
vuex: joi.boolean(),
@@ -32,7 +32,6 @@ exports.validatePreset = preset => validate(preset, presetSchema, msg => {
3232
})
3333

3434
exports.defaultPreset = {
35-
vuex: false,
3635
useConfigFiles: false,
3736
cssPreprocessor: undefined,
3837
plugins: {

packages/@vue/cli/lib/promptModules/__tests__/vuex.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ test('vuex', async () => {
1515
]
1616

1717
const expectedOptions = {
18-
vuex: true,
19-
plugins: {}
18+
plugins: {
19+
'@vue/cli-plugin-vuex': {}
20+
}
2021
}
2122

2223
await assertPromptModule(

packages/@vue/cli/lib/promptModules/vuex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = cli => {
88

99
cli.onPromptComplete((answers, options) => {
1010
if (answers.features.includes('vuex')) {
11-
options.vuex = true
11+
options.plugins['@vue/cli-plugin-vuex'] = {}
1212
}
1313
})
1414
}

0 commit comments

Comments
 (0)