-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
326 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const chalk = require('chalk') | ||
const { gzipSync } = require('zlib') | ||
const { compress } = require('brotli') | ||
|
||
function checkFileSize(filePath) { | ||
if (!fs.existsSync(filePath)) { | ||
return | ||
} | ||
const file = fs.readFileSync(filePath) | ||
const minSize = (file.length / 1024).toFixed(2) + 'kb' | ||
const gzipped = gzipSync(file) | ||
const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb' | ||
const compressed = compress(file) | ||
const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb' | ||
console.log( | ||
`${chalk.gray( | ||
chalk.bold(path.basename(filePath)) | ||
)} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}` | ||
) | ||
} | ||
|
||
checkFileSize(path.resolve(__dirname, '../size-checks/dist/small.js')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import path from 'path' | ||
import ts from 'rollup-plugin-typescript2' | ||
import replace from '@rollup/plugin-replace' | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import { terser } from 'rollup-plugin-terser' | ||
|
||
/** @type {import('rollup').RollupOptions} */ | ||
const config = { | ||
external: ['vue'], | ||
output: { | ||
file: path.resolve(__dirname, './dist/small.js'), | ||
format: 'es', | ||
}, | ||
input: path.resolve(__dirname, './small.js'), | ||
plugins: [ | ||
replace({ | ||
__DEV__: false, | ||
// this is only used during tests | ||
__TEST__: false, | ||
// If the build is expected to run directly in the browser (global / esm builds) | ||
__BROWSER__: true, | ||
// is targeting bundlers? | ||
__BUNDLER__: false, | ||
__GLOBAL__: false, | ||
// is targeting Node (SSR)? | ||
__NODE_JS__: false, | ||
__VUE_PROD_DEVTOOLS__: false, | ||
}), | ||
ts({ | ||
check: false, | ||
tsconfig: path.resolve(__dirname, '../tsconfig.json'), | ||
cacheRoot: path.resolve(__dirname, '../node_modules/.rts2_cache'), | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
sourceMap: false, | ||
declaration: false, | ||
declarationMap: false, | ||
}, | ||
exclude: ['__tests__', 'test-dts'], | ||
}, | ||
}), | ||
resolve(), | ||
commonjs(), | ||
terser({ | ||
format: { | ||
comments: false, | ||
}, | ||
module: true, | ||
compress: { | ||
ecma: 2015, | ||
pure_getters: true, | ||
}, | ||
}), | ||
], | ||
} | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createPinia, defineStore } from '../dist/pinia.esm-bundler' | ||
|
||
createPinia() | ||
// @ts-ignore | ||
export default defineStore() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { | ||
CustomInspectorNode, | ||
CustomInspectorState, | ||
setupDevtoolsPlugin, | ||
} from '@vue/devtools-api' | ||
import { App } from 'vue' | ||
import { getRegisteredStores, registerStore } from './rootStore' | ||
import { GenericStore, NonNullObject } from './types' | ||
|
||
function formatDisplay(display: string) { | ||
return { | ||
_custom: { | ||
display, | ||
}, | ||
} | ||
} | ||
|
||
let isAlreadyInstalled: boolean | undefined | ||
|
||
export function addDevtools(app: App, store: GenericStore, req: NonNullObject) { | ||
registerStore(store) | ||
setupDevtoolsPlugin( | ||
{ | ||
id: 'pinia', | ||
label: 'Pinia 🍍', | ||
app, | ||
}, | ||
(api) => { | ||
api.on.inspectComponent((payload, ctx) => { | ||
if (payload.instanceData) { | ||
payload.instanceData.state.push({ | ||
type: '🍍 ' + store.id, | ||
key: 'state', | ||
editable: false, | ||
value: store.state, | ||
}) | ||
} | ||
}) | ||
|
||
// watch(router.currentRoute, () => { | ||
// // @ts-ignore | ||
// api.notifyComponentUpdate() | ||
// }) | ||
|
||
const mutationsLayerId = 'pinia:mutations' | ||
const piniaInspectorId = 'pinia' | ||
|
||
if (!isAlreadyInstalled) { | ||
api.addTimelineLayer({ | ||
id: mutationsLayerId, | ||
label: `Pinia 🍍`, | ||
color: 0xe5df88, | ||
}) | ||
|
||
api.addInspector({ | ||
id: piniaInspectorId, | ||
label: 'Pinia 🍍', | ||
icon: 'storage', | ||
treeFilterPlaceholder: 'Search stores', | ||
}) | ||
|
||
isAlreadyInstalled = true | ||
} else { | ||
// @ts-ignore | ||
api.notifyComponentUpdate() | ||
api.sendInspectorTree(piniaInspectorId) | ||
api.sendInspectorState(piniaInspectorId) | ||
} | ||
|
||
store.subscribe((mutation, state) => { | ||
// rootStore.state[store.id] = state | ||
const data: Record<string, any> = { | ||
store: formatDisplay(mutation.storeName), | ||
type: formatDisplay(mutation.type), | ||
} | ||
|
||
if (mutation.payload) { | ||
data.payload = mutation.payload | ||
} | ||
|
||
// @ts-ignore | ||
api.notifyComponentUpdate() | ||
api.sendInspectorState(piniaInspectorId) | ||
|
||
api.addTimelineEvent({ | ||
layerId: mutationsLayerId, | ||
event: { | ||
time: Date.now(), | ||
data, | ||
// TODO: remove when fixed | ||
meta: {}, | ||
}, | ||
}) | ||
}) | ||
|
||
api.on.getInspectorTree((payload) => { | ||
if (payload.app === app && payload.inspectorId === piniaInspectorId) { | ||
const stores = Array.from(getRegisteredStores()) | ||
|
||
payload.rootNodes = (payload.filter | ||
? stores.filter((store) => | ||
store.id.toLowerCase().includes(payload.filter.toLowerCase()) | ||
) | ||
: stores | ||
).map(formatStoreForInspectorTree) | ||
} | ||
}) | ||
|
||
api.on.getInspectorState((payload) => { | ||
if (payload.app === app && payload.inspectorId === piniaInspectorId) { | ||
const stores = Array.from(getRegisteredStores()) | ||
const store = stores.find((store) => store.id === payload.nodeId) | ||
|
||
if (store) { | ||
payload.state = { | ||
options: formatStoreForInspectorState(store), | ||
} | ||
} else { | ||
__VUE_DEVTOOLS_TOAST__( | ||
`🍍 store "${payload.nodeId}" not found`, | ||
'error' | ||
) | ||
} | ||
} | ||
}) | ||
|
||
// trigger an update so it can display new registered stores | ||
// @ts-ignore | ||
api.notifyComponentUpdate() | ||
__VUE_DEVTOOLS_TOAST__(`🍍 "${store.id}" store installed`) | ||
} | ||
) | ||
} | ||
|
||
function formatStoreForInspectorTree(store: GenericStore): CustomInspectorNode { | ||
return { | ||
id: store.id, | ||
label: store.id, | ||
tags: [], | ||
} | ||
} | ||
|
||
function formatStoreForInspectorState( | ||
store: GenericStore | ||
): CustomInspectorState[string] { | ||
const fields: CustomInspectorState[string] = [ | ||
{ editable: false, key: 'id', value: formatDisplay(store.id) }, | ||
{ editable: true, key: 'state', value: store.state }, | ||
] | ||
|
||
return fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Global compile-time constants | ||
declare var __DEV__: boolean | ||
declare var __FEATURE_PROD_DEVTOOLS__: boolean | ||
declare var __BROWSER__: boolean | ||
declare var __CI__: boolean | ||
declare var __VUE_DEVTOOLS_TOAST__: ( | ||
message: string, | ||
type?: 'normal' | 'error' | 'warning' | ||
) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export { createStore } from './store' | ||
export { setActiveReq, setStateProvider, getRootState } from './rootStore' | ||
import { createStore } from './store' | ||
export { | ||
setActiveReq, | ||
setStateProvider, | ||
getRootState, | ||
createPinia, | ||
} from './rootStore' | ||
export { StateTree, StoreGetter, Store } from './types' | ||
// TODO: deprecate createStore | ||
export { createStore, createStore as defineStore } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.