Skip to content

Commit dd0b434

Browse files
🎉upload
1 parent b34236e commit dd0b434

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

generator/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = (api, options = {}, rootOptions = {}) => {
2+
const isVue3 = (rootOptions.vueVersion === '3')
3+
4+
api.injectImports(api.entryFile, `import cookie from './plugins/cookie'`)
5+
6+
if (isVue3) {
7+
api.transformScript(api.entryFile, require('./injectUseCookie'))
8+
} else {
9+
api.injectRootOptions(api.entryFile, `cookie`)
10+
}
11+
12+
api.render('./template', { ...options, })
13+
}

generator/injectUseCookie.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = (file, api) => {
2+
const j = api.jscodeshift
3+
const root = j(file.source)
4+
5+
const appRoots = root.find(j.CallExpression, (node) => {
6+
if (j.Identifier.check(node.callee) && node.callee.name === 'createApp') {
7+
return true
8+
}
9+
10+
if (
11+
j.MemberExpression.check(node.callee) &&
12+
j.Identifier.check(node.callee.object) &&
13+
node.callee.object.name === 'Vue' &&
14+
j.Identifier.check(node.callee.property) &&
15+
node.callee.property.name === 'createApp'
16+
) {
17+
return true
18+
}
19+
})
20+
21+
appRoots.replaceWith(({ node: createAppCall }) => {
22+
return j.callExpression(
23+
j.memberExpression(createAppCall, j.identifier('use')),
24+
[j.identifier('cookie')]
25+
)
26+
})
27+
28+
return root.toSource()
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const cookie = require('tiny-cookie')
2+
3+
const vueCookie = {
4+
// Main functions
5+
get: (name) => cookie.get(name),
6+
set: (name, value, options) => cookie.set(name, value, options),
7+
remove: (name, options) => cookie.remove(name, options),
8+
9+
// More functions
10+
delete: (name, options) => cookie.remove(name, options), // alias of remove
11+
getAll: () => cookie.getAll()
12+
}
13+
14+
export default (app) => {
15+
app.config.globalProperties.$cookie = vueCookie
16+
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = () => {}

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "vue-cli-plugin-cookie",
3+
"version": "1.0.0",
4+
"description": "🍪 Vue CLI 3/4 Plugin for handling browser cookies",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "BattlefieldDuck",
10+
"license": "MIT",
11+
"dependencies": {
12+
"tiny-cookie": "^1.0"
13+
}
14+
}

0 commit comments

Comments
 (0)