Skip to content

Commit a6bfbb3

Browse files
Akryumposvakiaking
authored
dx: add devtools integration (#1942) (#1949)
close #1942 Co-authored-by: Eduardo San Martin Morote <[email protected]> Co-authored-by: Kia King Ishii <[email protected]>
1 parent db8c476 commit a6bfbb3

File tree

12 files changed

+617
-330
lines changed

12 files changed

+617
-330
lines changed

.babelrc

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
{
2-
"presets": ["@babel/preset-env"]
2+
"presets": [
3+
["@babel/preset-env", {
4+
"exclude": [
5+
"transform-regenerator"
6+
]
7+
}]
8+
]
39
}

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"plugin:vue-libs/recommended"
55
],
66
"globals": {
7-
"__DEV__": true
7+
"__DEV__": true,
8+
"__VUE_PROD_DEVTOOLS__": true
89
}
910
}

examples/classic/shopping-cart/api/shop.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@ const _products = [
88
]
99

1010
export default {
11-
getProducts (cb) {
12-
setTimeout(() => cb(_products), 100)
11+
async getProducts () {
12+
await wait(100)
13+
return _products
1314
},
1415

15-
buyProducts (products, cb, errorCb) {
16-
setTimeout(() => {
16+
async buyProducts (products) {
17+
await wait(100)
18+
if (
1719
// simulate random checkout failure.
1820
(Math.random() > 0.5 || navigator.webdriver)
19-
? cb()
20-
: errorCb()
21-
}, 100)
21+
) {
22+
return
23+
} else {
24+
throw new Error('Checkout error')
25+
}
2226
}
2327
}
28+
29+
function wait (ms) {
30+
return new Promise(resolve => {
31+
setTimeout(resolve, ms)
32+
})
33+
}

examples/classic/shopping-cart/store/modules/cart.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import shop from '../../api/shop'
2+
import nested from './nested'
23

34
// initial state
45
// shape: [{ id, quantity }]
@@ -29,20 +30,20 @@ const getters = {
2930

3031
// actions
3132
const actions = {
32-
checkout ({ commit, state }, products) {
33+
async checkout ({ commit, state }, products) {
3334
const savedCartItems = [...state.items]
3435
commit('setCheckoutStatus', null)
3536
// empty cart
3637
commit('setCartItems', { items: [] })
37-
shop.buyProducts(
38-
products,
39-
() => commit('setCheckoutStatus', 'successful'),
40-
() => {
41-
commit('setCheckoutStatus', 'failed')
42-
// rollback to the cart saved before sending the request
43-
commit('setCartItems', { items: savedCartItems })
44-
}
45-
)
38+
try {
39+
await shop.buyProducts(products)
40+
commit('setCheckoutStatus', 'successful')
41+
} catch (e) {
42+
console.error(e)
43+
commit('setCheckoutStatus', 'failed')
44+
// rollback to the cart saved before sending the request
45+
commit('setCartItems', { items: savedCartItems })
46+
}
4647
},
4748

4849
addProductToCart ({ state, commit }, product) {
@@ -88,5 +89,8 @@ export default {
8889
state,
8990
getters,
9091
actions,
91-
mutations
92+
mutations,
93+
modules: {
94+
nested
95+
}
9296
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
namespaced: true,
3+
state: () => ({
4+
foo: 'bar'
5+
}),
6+
getters: {
7+
twoBars: state => state.foo.repeat(2)
8+
}
9+
}

examples/classic/shopping-cart/store/modules/products.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ const getters = {}
1010

1111
// actions
1212
const actions = {
13-
getAllProducts ({ commit }) {
14-
shop.getProducts(products => {
15-
commit('setProducts', products)
16-
})
13+
async getAllProducts ({ commit }) {
14+
const products = await shop.getProducts()
15+
commit('setProducts', products)
1716
}
1817
}
1918

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
"peerDependencies": {
5555
"vue": "^3.0.2"
5656
},
57+
"dependencies": {
58+
"@vue/devtools-api": "^6.0.0-beta.10"
59+
},
5760
"devDependencies": {
5861
"@babel/core": "^7.12.10",
5962
"@babel/preset-env": "^7.12.11",

rollup.config.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ function createEntries() {
2525
}
2626

2727
function createEntry(config) {
28+
const isGlobalBuild = config.format === 'iife'
29+
const isBunderBuild = config.format !== 'iife' && !config.browser
30+
2831
const c = {
2932
external: ['vue'],
3033
input: config.input,
@@ -44,15 +47,20 @@ function createEntry(config) {
4447
}
4548
}
4649

47-
if (config.format === 'iife' || config.format === 'umd') {
50+
if (isGlobalBuild) {
4851
c.output.name = c.output.name || 'Vuex'
4952
}
5053

54+
if (!isGlobalBuild) {
55+
c.external.push('@vue/devtools-api')
56+
}
57+
5158
c.plugins.push(replace({
5259
__VERSION__: pkg.version,
53-
__DEV__: config.format !== 'iife' && !config.browser
60+
__DEV__: isBunderBuild
5461
? `(process.env.NODE_ENV !== 'production')`
55-
: config.env !== 'production'
62+
: config.env !== 'production',
63+
__VUE_PROD_DEVTOOLS__: isBunderBuild ? '__VUE_PROD_DEVTOOLS__' : 'false'
5664
}))
5765

5866
if (config.transpile !== false) {

0 commit comments

Comments
 (0)