Skip to content

Commit 8209b80

Browse files
hwiemsalisdemos
andauthored
refactor: remove vue [do not merge] (#534)
* chore: remove vue It shouldn't be neccessary as instance. It should be available from outside * build: update vue(x) dependencies - vuex and vue/compat are added to peerDependencies, they are not needed as regular dependencies - vue and vue-template-compiler (the latter needed for vue 2, but not vue 3) could be removed as dependencies - to be able to run the library in a vue 3 project, vuex is required in ^v4 * refactor: remove vuex usage to be able to remove vuex as a direct dependency, we need to remove its usages in the library * docs: update documentation for vue 3 * refactor: remove vue usage to be able to remove vue as a dependency, we need to remove all its usages * build: add missing dev dependencies these are needed for running the tests * fix: fix deleting of an item - since state.items is an object, using `findIndex` on it throws an error - instead, we can just use `delete` to delete the item * build: add missing vue-jest peer dependencies * build: update jest dependencies to work with vue3 * fix: use dot notation * refactor: delete unused file * refactor: remove commented out code --------- Co-authored-by: Florian Salis <[email protected]>
1 parent dde4775 commit 8209b80

19 files changed

+2208
-3317
lines changed

docs/README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,47 @@ a magical experience.
1515
In your entrypoint do something like:
1616

1717
```js
18-
import Vue from 'vue/dist/vue.esm'
18+
import { createApp } from 'vue'
19+
import { createStore } from 'vuex'
1920

2021
import {
21-
createVueInstance,
22-
createVuexStore,
2322
Api,
2423
StaticRouter
2524
} from '@efrane/vuex-json-api'
2625

27-
import App from './App'
28-
2926
import PlaygroundRoutes from '@efrane/vuex-json-api/misc/JsonApiPlaygroundRoutes'
3027

3128
Api.setBaseUrl('https://jsonapiplayground.reyesoft.com')
3229

3330
const router = new StaticRouter(PlaygroundRoutes)
3431

35-
createVuexStore({}, router)
36-
.then((store) => {
37-
return createVueInstance(store, { App })
32+
const storeOptions = {} // your store options
33+
34+
const store = createStore(storeOptions, router)
35+
.then(store => {
36+
const app = createApp(store, { App })
37+
38+
app.use(store)
39+
app.mount('#app')
3840
})
39-
.then(instance => instance.$mount('#app'))
4041
```
4142

4243
Then, in `App.vue`:
4344

44-
```js
45+
```vue
4546
{
4647
// ...
47-
computed: ...mapState('books', {
48-
books: state => state.items
49-
}),
50-
methods: ...mapActions('books', ['list']),
51-
mounted() {
48+
computed: {
49+
...mapState('books', {
50+
books: state => state.items
51+
})
52+
},
53+
54+
methods: {
55+
...mapActions('books', ['list'])
56+
},
57+
58+
mounted () {
5259
this.list()
5360
}
5461
// ...

docs/usage/setup.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
# Example Setup
2-
## prepare the store
2+
## Prepare the store
33

44
``` js
5-
import Vue from 'vue'
6-
import Vuex from 'vuex'
5+
import { createApp } from 'vue'
6+
import { createStore } from 'vuex'
77
...
88
import { initJsonApiPlugin, prepareModuleHashMap, StaticRouter } from '@efrane/vuex-json-api'
99
import VuexApiRoutes from '<path-to>/VuexApiRoutes'
1010

11-
Vue.use(Vuex)
12-
13-
// create The Router
1411
const router = new StaticRouter(VuexApiRoutes)
15-
16-
// set the baseUrl
1712
let baseUrl = '/my-base/api/1.0'
13+
const app = createApp(rootComponent)
1814

19-
// create a store instance
2015
const store = router
2116
.updateRoutes()
2217
.then(router => {
23-
const store = new Vuex.Store({
18+
const store = createStore({
2419
plugins: [
2520
initJsonApiPlugin({
2621
router: router,
2722
baseUrl: baseUrl,
28-
headers: { }, // you might want set some custum headers
23+
headers: { }, // you might want to set some custom headers
2924
preprocessingCallbacks: [], // you can pass an array of promise resolving methods to manipulate the response before passing it to the store
3025
errorCallbacks: [], // you can pass an array of promise resolving methods to manipulate the response if an error gets returned (e.g. statuscode >= 400)
3126
}),
@@ -37,6 +32,8 @@ const store = router
3732

3833
return store
3934
})
35+
36+
app.use(store)
4037

4138
export { store }
4239

@@ -47,6 +44,7 @@ export { store }
4744

4845

4946
``` js
47+
import { createApp } from 'vue'
5048
import { store } from '<the-place-of-the-above-file>/Store'
5149

5250
// The Module names has
@@ -65,7 +63,7 @@ const presetModules = [
6563
]
6664

6765
store.then(store => {
68-
let instance = new Vue({
66+
let instance = createApp({
6967
components: [], // do the regular stuff you do, when using vue
7068
store,
7169
}).then(() => {
@@ -86,5 +84,5 @@ store.then(store => {
8684
})
8785
})
8886
```
89-
Thats it.
90-
now you should see the registered modules in the vue devTools.
87+
That's it.
88+
Now you should see the registered modules in the vue devTools.

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { createVuexStore, prepareModuleHashMap } from './src/init/createVuexStore'
1+
import { prepareModuleHashMap } from './src/init/prepareModuleHashMap'
22
import { initJsonApiPlugin } from './src/init/initJsonApiPlugin'
33
import { StaticRouter } from './src/route/StaticRouter'
44
import { JsonApiRouter } from './src/route/JsonApiRouter'
55
import { FosJsRoutingRouter } from './src/route/FosJsRoutingRouter'
66

77
export {
8-
createVuexStore,
98
initJsonApiPlugin,
109
StaticRouter,
1110
JsonApiRouter,

jest.config.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ module.exports = {
3131
testTimeout: 10000,
3232

3333
transform: {
34-
'^.+\\.vue$': 'vue-jest',
34+
'^.+\\.vue$': '@vue/vue3-jest',
3535
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$':
3636
'jest-transform-stub',
3737
'^.+\\.(js|jsx)?$': 'babel-jest'
38-
},
39-
40-
setupFiles: ['./test/setup.js']
38+
}
4139
};

package.json

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,43 @@
1717
"dependencies": {
1818
"deep-object-diff": "^1.1.0",
1919
"json-api-normalizer": "^1.0",
20-
"qs": "~6.10.3",
21-
"vuex": "^3.1.1"
20+
"qs": "~6.10.3"
2221
},
2322
"peerDependencies": {
24-
"vue": "2.*"
23+
"@vue/compat": "3.2.0",
24+
"vuex": "^4.0.2"
2525
},
2626
"devDependencies": {
2727
"@babel/core": "^7.16",
2828
"@babel/plugin-proposal-object-rest-spread": "^7.16.7",
2929
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
30-
"@babel/preset-env": "^7.16.4",
31-
"babel-jest": "^27.3.1",
30+
"@babel/preset-env": "^7.20.2",
31+
"@vue/compat": "^3.2.0",
32+
"@vue/vue3-jest": "29",
33+
"babel-core": "^7.0.0-bridge.0",
34+
"babel-jest": "^29.4.3",
3235
"eslint": "7.32.0",
3336
"eslint-config-standard": "16.0.3",
3437
"eslint-friendly-formatter": "^4.0.1",
3538
"eslint-plugin-import": "^2.25.4",
36-
"eslint-plugin-jest": "^25.2.4",
39+
"eslint-plugin-jest": "^27.2.1",
3740
"eslint-plugin-node": "^11.0.0",
3841
"eslint-plugin-promise": "^5.1.1",
3942
"eslint-plugin-vue": "^8.3.0",
4043
"faker": "^5.5.3",
4144
"fetch-mock": "^9.11.0",
4245
"husky": "^7.0.4",
43-
"jest": "^26.0.1",
46+
"jest": "^29.4.3",
47+
"jest-environment-jsdom": "^29.7.0",
4448
"jest-transform-stub": "^2.0.0",
4549
"lint-staged": "^12.3.2",
4650
"node-fetch": "^2.6.1",
47-
"vue": "^2.6.14",
48-
"vue-jest": "^3.0.5",
49-
"vue-template-compiler": "^2.6.14"
51+
"node-notifier": "^10.0.1",
52+
"vue": "^3.2.0",
53+
"vuex": "^4.0.2"
5054
},
5155
"engines": {
52-
"node": ">= 8.0.0",
56+
"node": ">= 10.0.0",
5357
"npm": ">= 3.0.0"
5458
},
5559
"lint-staged": {

src/init/createVueInstance.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/init/createVuexStore.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/init/prepareModuleHashMap.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { hasOwn } from '../shared/utils'
2+
3+
/**
4+
* Converts a module listing object (e.g. `{ myModule: myModule }`)
5+
* to the expected syntax for module registration.
6+
*
7+
* By default, this Vuex usage interpretation expects non-api-bound
8+
* modules to have a `name`-property which defines their namespaced
9+
* name. This is necessary to facilitate auto-registration of the modules.
10+
*
11+
* N.b.: There is no checking done to avoid overwrites of these modules
12+
* by later-to-be-initialized api-bound modules.
13+
*
14+
* @param {object|array} modules
15+
*/
16+
export function prepareModuleHashMap (modules) {
17+
const moduleHashMap = {}
18+
19+
for (const idx in modules) {
20+
if (hasOwn(modules, idx)) {
21+
const module = modules[idx]
22+
moduleHashMap[module.name] = module
23+
}
24+
}
25+
26+
return moduleHashMap
27+
}

src/module/mutations/removeMutation.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Vue from 'vue'
2-
31
/**
42
* Remove a ResourceObject from a module's item(s)
53
*
@@ -18,9 +16,9 @@ export function removeMutation (isCollection) {
1816
return new Proxy((state, payload) => {}, {
1917
apply (target, thisArg, [state, id]) {
2018
if (isCollection) {
21-
Vue.delete(state.items, id)
19+
delete state.items[id]
2220
} else {
23-
Vue.set(state, 'item', {})
21+
state.item = {}
2422
}
2523
}
2624
})

src/module/mutations/resetItemsMutation.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Vue from 'vue'
21
import { initialState } from '../State'
32

43
/**
@@ -12,13 +11,13 @@ export function resetItemsMutation (isCollection) {
1211
const initial = initialState(isCollection)
1312

1413
if (isCollection) {
15-
Vue.set(state, 'items', initial.items)
16-
Vue.set(state, 'initial', initial.initial)
14+
state.items = initial.items
15+
state.initial = initial.initial
1716
}
1817

1918
if (!isCollection) {
20-
Vue.set(state, 'item', initial.item)
21-
Vue.set(state, 'item', initial.initial)
19+
state.item = initial.item
20+
state.item = initial.initial
2221
}
2322
}
2423
})

0 commit comments

Comments
 (0)