Skip to content

Commit bceef55

Browse files
committed
fix(useCookie): Cookie code should not be imported when useCookie is not used
1 parent a8b8b84 commit bceef55

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

package-lock.json

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"vue-hooks",
1111
"vue-use-hooks"
1212
],
13+
"main": "dist/vue-use-kit.umd.js",
1314
"module": "dist/vue-use-kit.es5.js",
1415
"typings": "dist/types/vue-use-kit.d.ts",
1516
"files": [
@@ -27,7 +28,8 @@
2728
"scripts": {
2829
"lint": "eslint 'src/**/*.ts'",
2930
"prebuild": "rimraf dist && rimraf docs",
30-
"build": "npm run storybook:build && tsc --module commonjs -p ./tsconfig.build.json && rollup -c rollup.config.ts && npm run build:clean",
31+
"build": "npm run storybook:build && npm run build:code && npm run build:clean",
32+
"build:code": "tsc --module commonjs -p ./tsconfig.build.json && rollup -c rollup.config.ts",
3133
"build:clean": "ts-node tools/build-clean",
3234
"dev": "start-storybook -p 3000 -s ./public",
3335
"storybook:build": "build-storybook --quiet -c .storybook -s ./public -o docs",
@@ -91,6 +93,7 @@
9193
"@storybook/addon-viewport": "^5.3.13",
9294
"@storybook/theming": "^5.3.13",
9395
"@storybook/vue": "^5.3.13",
96+
"@types/cookie": "^0.3.3",
9497
"@types/jest": "^23.3.2",
9598
"@types/node": "^10.11.0",
9699
"@types/throttle-debounce": "^2.1.0",
@@ -103,7 +106,6 @@
103106
"bulma": "^0.8.0",
104107
"colors": "^1.3.2",
105108
"commitizen": "^3.0.0",
106-
"cookie-universal": "^2.1.3",
107109
"cross-env": "^5.2.0",
108110
"cz-conventional-changelog": "^2.1.0",
109111
"eslint": "^6.8.0",
@@ -140,6 +142,7 @@
140142
"vue-template-compiler": "^2.6.11"
141143
},
142144
"dependencies": {
145+
"cookie": "^0.4.0",
143146
"throttle-debounce": "^2.1.0"
144147
}
145148
}

rollup.config.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable */
22
import resolve from 'rollup-plugin-node-resolve'
33
import commonjs from 'rollup-plugin-commonjs'
4+
import camelCase from 'lodash.camelcase'
45
import typescript from 'rollup-plugin-typescript2'
56
import json from 'rollup-plugin-json'
67
import ttypescript from 'ttypescript'
@@ -24,16 +25,29 @@ const banner = `/**
2425

2526
export default {
2627
input: `src/${libraryName}.ts`,
27-
output: [{ banner, file: pkg.module, format: 'es' }],
28+
output: [
29+
{
30+
banner,
31+
file: pkg.main,
32+
name: camelCase(libraryName),
33+
format: 'umd',
34+
globals: {
35+
vue: 'Vue',
36+
'@vue/composition-api': 'vueCompositionApi'
37+
}
38+
},
39+
{ banner, file: pkg.module, format: 'es' }
40+
],
2841
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
29-
external: ['vue', '@vue/composition-api', 'cookie-universal'],
42+
external: ['vue', '@vue/composition-api'],
3043
watch: { include: 'src/**' },
3144
plugins: [
3245
// Allow json resolution
3346
json(),
3447
// Compile TypeScript files
3548
typescript({
36-
// Fix typescript definition paths
49+
// Transform typescript aliases because otherwise type definitions
50+
// will have the wrong import path
3751
typescript: ttypescript,
3852
tsconfigDefaults: {
3953
compilerOptions: {

src/functions/useCookie/useCookie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Cookies from 'cookie-universal'
21
import { CookieSerializeOptions } from 'cookie'
2+
import { Cookies } from '@src/shared/cookies'
33
import {
44
createSerializer,
55
createDeserializer,
@@ -30,7 +30,7 @@ export function useCookie(
3030
const serializer = createSerializer(opts.serializer)
3131
const deserializer = createDeserializer(opts.deserializer)
3232

33-
const cookieLib = Cookies(undefined, undefined, false)
33+
const cookieLib = Cookies()
3434
const cookie: Ref<any> = ref(null)
3535

3636
const getCookie = () => {

src/shared/cookies.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Cookie from 'cookie'
2+
3+
export const Cookies = () => {
4+
const state: any = {
5+
set(name = '', value = '', opts = { path: '/' }) {
6+
value = typeof value === 'object' ? JSON.stringify(value) : value
7+
document.cookie = Cookie.serialize(name, value, opts)
8+
},
9+
10+
get(name = '') {
11+
const cookies = Cookie.parse(document.cookie)
12+
const cookie = cookies[name]
13+
return cookie
14+
},
15+
16+
remove(name = '', opts = { path: '/', expires: new Date(0) }) {
17+
const cookie = state.get(name)
18+
opts.expires = new Date(0)
19+
if (typeof cookie !== 'undefined') state.set(name, '', opts)
20+
}
21+
}
22+
23+
return state
24+
}

tools/build-clean.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const handleError = (err: Error) => {
66
if (err) return console.log(err)
77
}
88

9+
// Remove lib folder
10+
rimraf(`${distDir}/lib`, handleError)
11+
912
// Remove stories folder
1013
rimraf(`${distDir}/**/stories/**`, handleError)
1114

0 commit comments

Comments
 (0)