Skip to content

feat!: make vuex a separate plugin #4242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/guide/plugins-and-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ Here's an example preset:
``` json
{
"useConfigFiles": true,
"vuex": true,
"cssPreprocessor": "sass",
"plugins": {
"@vue/cli-plugin-babel": {},
"@vue/cli-plugin-eslint": {
"config": "airbnb",
"lintOn": ["save", "commit"]
},
"@vue/cli-plugin-router": {}
"@vue/cli-plugin-router": {},
"@vue/cli-plugin-vuex": {}
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/ru/guide/plugins-and-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ vue add @vue/eslint --config airbnb --lintOn save
``` json
{
"useConfigFiles": true,
"vuex": true,
"cssPreprocessor": "sass",
"plugins": {
"@vue/cli-plugin-babel": {},
"@vue/cli-plugin-eslint": {
"config": "airbnb",
"lintOn": ["save", "commit"]
},
"@vue/cli-plugin-router": {}
"@vue/cli-plugin-router": {},
"@vue/cli-plugin-vuex": {}
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/zh/guide/plugins-and-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ vue add @vue/eslint --config airbnb --lintOn save
``` json
{
"useConfigFiles": true,
"vuex": true,
"cssPreprocessor": "sass",
"plugins": {
"@vue/cli-plugin-babel": {},
"@vue/cli-plugin-eslint": {
"config": "airbnb",
"lintOn": ["save", "commit"]
},
"@vue/cli-plugin-router": {}
"@vue/cli-plugin-router": {},
"@vue/cli-plugin-vuex": {}
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions packages/@vue/cli-plugin-vuex/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__tests__
__mocks__
9 changes: 9 additions & 0 deletions packages/@vue/cli-plugin-vuex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @vue/cli-plugin-vuex

> vuex plugin for vue-cli

## Installing in an Already Created Project

``` sh
vue add @vue/vuex
```
14 changes: 14 additions & 0 deletions packages/@vue/cli-plugin-vuex/__tests__/vuexGenerator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin')

test('base', async () => {
const { files, pkg } = await generateWithPlugin({
id: 'vuex',
apply: require('../generator'),
options: {}
})

expect(files['src/store/index.js']).toBeTruthy()
expect(files['src/store/index.js']).toMatch('import Vuex')

expect(pkg.dependencies).toHaveProperty('vuex')
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
module.exports = (api, options) => {
module.exports = (api, options = {}) => {
api.assertCliVersion('^4.0.0-alpha.3')
api.assertCliServiceVersion('^4.0.0-alpha.3')

api.injectImports(api.entryFile, `import store from './store'`)
api.injectRootOptions(api.entryFile, `store`)

api.extendPackage({
dependencies: {
vuex: '^3.0.1'
}
})
api.render('./template')

api.render('./template', {
})

if (api.invoking && api.hasPlugin('typescript')) {
/* eslint-disable-next-line node/no-extraneous-require */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ Vue.use(Vuex)

export default new Vuex.Store({
state: {

},
mutations: {

},
actions: {

},
modules: {
}
})
1 change: 1 addition & 0 deletions packages/@vue/cli-plugin-vuex/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (api, options = {}) => {}
29 changes: 29 additions & 0 deletions packages/@vue/cli-plugin-vuex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@vue/cli-plugin-vuex",
"version": "4.0.0-alpha.3",
"description": "Vuex plugin for vue-cli",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/vuejs/vue-cli.git",
"directory": "packages/@vue/cli-plugin-vuex"
},
"keywords": [
"vue",
"cli",
"vuex"
],
"author": "Evan You",
"license": "MIT",
"bugs": {
"url": "https://github.com/vuejs/vue-cli/issues"
},
"homepage": "https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-plugin-vuex#readme",
"publishConfig": {
"access": "public"
},
"dependencies": {},
"devDependencies": {
"@vue/cli-test-utils": "^4.0.0-alpha.3"
}
}
13 changes: 13 additions & 0 deletions packages/@vue/cli-service/__tests__/serve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ test('serve with legacy router option', async () => {
)
})

test('serve with legacy vuex option', async () => {
const project = await create('e2e-serve-legacy-vuex', Object.assign({}, defaultPreset, {
vuex: true
}))

await serve(
() => project.run('vue-cli-service serve'),
async ({ page, helpers }) => {
expect(await helpers.getText('h1')).toMatch(`Welcome to Your Vue.js App`)
}
)
})

test('serve with inline entry', async () => {
const project = await create('e2e-serve-inline-entry', defaultPreset)

Expand Down
4 changes: 0 additions & 4 deletions packages/@vue/cli-service/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ module.exports = (api, options) => {
]
})

if (options.vuex) {
require('./vuex')(api, options)
}

if (options.cssPreprocessor) {
const deps = {
sass: {
Expand Down
4 changes: 0 additions & 4 deletions packages/@vue/cli-service/lib/PluginAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ class PluginAPI {
* @return {boolean}
*/
hasPlugin (id) {
if (['vuex'].includes(id)) {
const pkg = this.service.pkg
return ((pkg.dependencies && pkg.dependencies[id]) || (pkg.devDependencies && pkg.devDependencies[id]))
}
return this.service.plugins.some(p => matchesPluginId(id, p.id))
}

Expand Down
5 changes: 0 additions & 5 deletions packages/@vue/cli-ui/apollo-server/api/PluginApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const logs = require('../connectors/logs')
const sharedData = require('../connectors/shared-data')
const views = require('../connectors/views')
const suggestions = require('../connectors/suggestions')
const folders = require('../connectors/folders')
const progress = require('../connectors/progress')
const app = require('../connectors/app')
// Utils
Expand Down Expand Up @@ -397,10 +396,6 @@ class PluginApi {
* @param {string} id Plugin id or short id
*/
hasPlugin (id) {
if (['vuex'].includes(id)) {
const pkg = folders.readPackage(this.cwd, this.context, true)
return ((pkg.dependencies && pkg.dependencies[id]) || (pkg.devDependencies && pkg.devDependencies[id]))
}
return this.plugins.some(p => matchesPluginId(id, p.id))
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-ui/tests/e2e/specs/g2-plugins.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('Plugins', () => {
it('Should display the plugins', () => {
cy.visit('/plugins')
cy.get('.project-plugin-item').should('have.length', 5)
cy.get('.project-plugin-item').should('have.length', 6)
})

it('Should add a plugin', () => {
Expand All @@ -26,6 +26,6 @@ describe('Plugins', () => {
.should('be.visible')
.should('not.have.class', 'disabled')
.click()
cy.get('.project-plugin-item').should('have.length', 5)
cy.get('.project-plugin-item').should('have.length', 6)
})
})
10 changes: 1 addition & 9 deletions packages/@vue/cli-ui/ui-defaults/suggestions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { loadModule } = require('@vue/cli-shared-utils')
const invoke = require('@vue/cli/lib/invoke')

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

try {
if (id === 'router') {
await invoke(id, {}, context)
} else {
await invoke.runGenerator(context, {
id: `core:${id}`,
apply: loadModule(`@vue/cli-service/generator/${id}`, context)
})
}
await invoke(id, {}, context)
} catch (e) {
error = e
}
Expand Down
5 changes: 5 additions & 0 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ module.exports = class Creator extends EventEmitter {
}
}

// legacy support for vuex
if (preset.vuex) {
preset.plugins['@vue/cli-plugin-vuex'] = {}
}

const packageManager = (
cliOptions.packageManager ||
loadOptions().packageManager ||
Expand Down
4 changes: 0 additions & 4 deletions packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ module.exports = class Generator {
}

hasPlugin (_id) {
if (['vuex'].includes(_id)) {
const pkg = this.pkg
return ((pkg.dependencies && pkg.dependencies[_id]) || (pkg.devDependencies && pkg.devDependencies[_id]))
}
return [
...this.plugins.map(p => p.id),
...Object.keys(this.pkg.devDependencies || {}),
Expand Down
15 changes: 1 addition & 14 deletions packages/@vue/cli/lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ const {
hasProjectYarn,
hasProjectPnpm,
resolvePluginId,
resolveModule,
loadModule
resolveModule
} = require('@vue/cli-shared-utils')

async function add (pluginName, options = {}, context = process.cwd()) {
// special internal "plugins"
if (/^(@vue\/)?vuex$/.test(pluginName)) {
return addVuex(context)
}

const packageName = resolvePluginId(pluginName)

log()
Expand All @@ -38,13 +32,6 @@ async function add (pluginName, options = {}, context = process.cwd()) {
}
}

async function addVuex (context) {
invoke.runGenerator(context, {
id: 'core:vuex',
apply: loadModule('@vue/cli-service/generator/vuex', context)
})
}

module.exports = (...args) => {
return add(...args).catch(err => {
error(err)
Expand Down
3 changes: 1 addition & 2 deletions packages/@vue/cli/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rcPath = exports.rcPath = getRcPath('.vuerc')
const presetSchema = createSchema(joi => joi.object().keys({
bare: joi.boolean(),
useConfigFiles: joi.boolean(),
// TODO: Use warn for router once @hapi/joi v16 releases
// TODO: Use warn for router and vuex once @hapi/joi v16 releases
router: joi.boolean(),
routerHistoryMode: joi.boolean(),
vuex: joi.boolean(),
Expand All @@ -32,7 +32,6 @@ exports.validatePreset = preset => validate(preset, presetSchema, msg => {
})

exports.defaultPreset = {
vuex: false,
useConfigFiles: false,
cssPreprocessor: undefined,
plugins: {
Expand Down
5 changes: 3 additions & 2 deletions packages/@vue/cli/lib/promptModules/__tests__/vuex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ test('vuex', async () => {
]

const expectedOptions = {
vuex: true,
plugins: {}
plugins: {
'@vue/cli-plugin-vuex': {}
}
}

await assertPromptModule(
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli/lib/promptModules/vuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = cli => {

cli.onPromptComplete((answers, options) => {
if (answers.features.includes('vuex')) {
options.vuex = true
options.plugins['@vue/cli-plugin-vuex'] = {}
}
})
}