From aabe430e78fb719adb37e146a6636e03c75389a5 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 13 Sep 2022 19:06:57 +0200 Subject: [PATCH] fix(devtools): use simpler expression to tree shake it --- packages/pinia/src/createPinia.ts | 5 ++--- packages/pinia/src/env.ts | 5 +++-- packages/pinia/src/global.d.ts | 1 + packages/pinia/src/vue2-plugin.ts | 4 ++-- rollup.config.js | 32 ++++++++++++++++++------------- vite.config.ts | 1 + 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/pinia/src/createPinia.ts b/packages/pinia/src/createPinia.ts index f71497fd..30932172 100644 --- a/packages/pinia/src/createPinia.ts +++ b/packages/pinia/src/createPinia.ts @@ -1,7 +1,6 @@ import { Pinia, PiniaPlugin, setActivePinia, piniaSymbol } from './rootStore' import { ref, App, markRaw, effectScope, isVue2, Ref } from 'vue-demi' import { registerPiniaDevtools, devtoolsPlugin } from './devtools' -import { USE_DEVTOOLS } from './env' import { StateTree, StoreGeneric } from './types' /** @@ -29,7 +28,7 @@ export function createPinia(): Pinia { app.provide(piniaSymbol, pinia) app.config.globalProperties.$pinia = pinia /* istanbul ignore else */ - if (USE_DEVTOOLS) { + if (__USE_DEVTOOLS__) { registerPiniaDevtools(app, pinia) } toBeInstalled.forEach((plugin) => _p.push(plugin)) @@ -57,7 +56,7 @@ export function createPinia(): Pinia { // pinia devtools rely on dev only features so they cannot be forced unless // the dev build of Vue is used. Avoid old browsers like IE11. - if (USE_DEVTOOLS && typeof Proxy !== 'undefined') { + if (__USE_DEVTOOLS__ && typeof Proxy !== 'undefined') { pinia.use(devtoolsPlugin) } diff --git a/packages/pinia/src/env.ts b/packages/pinia/src/env.ts index d54ffbbd..bb089835 100644 --- a/packages/pinia/src/env.ts +++ b/packages/pinia/src/env.ts @@ -5,6 +5,7 @@ export const IS_CLIENT = typeof window !== 'undefined' * - only if dev mode or forced through the prod devtools flag * - not in test * - only if window exists (could change in the future) + * NOTE: Moved to a build time replacement */ -export const USE_DEVTOOLS = - (__DEV__ || __FEATURE_PROD_DEVTOOLS__) && !__TEST__ && IS_CLIENT +// export const USE_DEVTOOLS = +// (__DEV__ || __FEATURE_PROD_DEVTOOLS__) && !__TEST__ && IS_CLIENT diff --git a/packages/pinia/src/global.d.ts b/packages/pinia/src/global.d.ts index ecff968b..e93b35f7 100644 --- a/packages/pinia/src/global.d.ts +++ b/packages/pinia/src/global.d.ts @@ -1,5 +1,6 @@ // Global compile-time constants declare var __DEV__: boolean +declare var __USE_DEVTOOLS__: boolean declare var __TEST__: boolean declare var __FEATURE_PROD_DEVTOOLS__: boolean declare var __BROWSER__: boolean diff --git a/packages/pinia/src/vue2-plugin.ts b/packages/pinia/src/vue2-plugin.ts index 03647fd6..70a0d1f0 100644 --- a/packages/pinia/src/vue2-plugin.ts +++ b/packages/pinia/src/vue2-plugin.ts @@ -1,6 +1,6 @@ import type { Plugin } from 'vue-demi' import { registerPiniaDevtools } from './devtools' -import { IS_CLIENT, USE_DEVTOOLS } from './env' +import { IS_CLIENT } from './env' import { Pinia, piniaSymbol, setActivePinia } from './rootStore' /** @@ -57,7 +57,7 @@ export const PiniaVuePlugin: Plugin = function (_Vue) { // installing pinia's plugin setActivePinia(pinia) } - if (USE_DEVTOOLS) { + if (__USE_DEVTOOLS__) { registerPiniaDevtools(pinia._a, pinia) } } else if (!this.$pinia && options.parent && options.parent.$pinia) { diff --git a/rollup.config.js b/rollup.config.js index 0b300fbb..5b6a80d8 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -163,23 +163,29 @@ function createReplacePlugin( isGlobalBuild, isNodeBuild ) { + const __DEV__ = + (isBundlerESMBuild && !isRawESMBuild) || (isNodeBuild && !isProduction) + ? // preserve to be handled by bundlers + `(process.env.NODE_ENV !== 'production')` + : // hard coded dev/prod builds + JSON.stringify(!isProduction) + const __FEATURE_PROD_DEVTOOLS__ = isBundlerESMBuild + ? `(typeof __VUE_PROD_DEVTOOLS__ !== 'undefined' && __VUE_PROD_DEVTOOLS__)` + : 'false' + + const __TEST__ = + (isBundlerESMBuild && !isRawESMBuild) || isNodeBuild + ? `(process.env.NODE_ENV === 'test')` + : 'false' + const replacements = { __COMMIT__: `"${process.env.COMMIT}"`, __VERSION__: `"${pkg.version}"`, - __DEV__: - (isBundlerESMBuild && !isRawESMBuild) || (isNodeBuild && !isProduction) - ? // preserve to be handled by bundlers - `(process.env.NODE_ENV !== 'production')` - : // hard coded dev/prod builds - JSON.stringify(!isProduction), + __DEV__, // this is only used during tests - __TEST__: - (isBundlerESMBuild && !isRawESMBuild) || isNodeBuild - ? `(process.env.NODE_ENV === 'test')` - : 'false', - __FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild - ? `(typeof __VUE_PROD_DEVTOOLS__ !== 'undefined' && __VUE_PROD_DEVTOOLS__)` - : 'false', + __TEST__, + __FEATURE_PROD_DEVTOOLS__, + __USE_DEVTOOLS__: `((${__DEV__} || ${__FEATURE_PROD_DEVTOOLS__}) && !${__TEST__} && typeof window !== 'undefined')`, // If the build is expected to run directly in the browser (global / esm builds) __BROWSER__: JSON.stringify(isRawESMBuild), // is targeting bundlers? diff --git a/vite.config.ts b/vite.config.ts index 3144ae85..d0d20c3a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -4,6 +4,7 @@ import { defineConfig } from 'vite' export default defineConfig({ define: { __DEV__: true, + __USE_DEVTOOLS__: false, __TEST__: true, __BROWSER__: true, }, -- 2.47.2