]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(devtools): use simpler expression to tree shake it fix/tree-shake-devtools
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 13 Sep 2022 17:06:57 +0000 (19:06 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 13 Sep 2022 17:07:37 +0000 (19:07 +0200)
packages/pinia/src/createPinia.ts
packages/pinia/src/env.ts
packages/pinia/src/global.d.ts
packages/pinia/src/vue2-plugin.ts
rollup.config.js
vite.config.ts

index f71497fd31a865c6bbfe2eb124d4b38bcaf5d0ba..309321721cac01914f72100c5c04cc3a45757959 100644 (file)
@@ -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)
   }
 
index d54ffbbdfb9e21cba323ccd75b3329c19798669d..bb089835950315a3ac7f8aa04cd1d318f072f6f8 100644 (file)
@@ -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
index ecff968b79667bcd966af53c4f84c98b44757fca..e93b35f70586235ffdd1272a1f68c4450379b6a4 100644 (file)
@@ -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
index 03647fd61ed3b7c77e51936719745efdc66a39ec..70a0d1f00b67874c98971abb317e27f5069458f0 100644 (file)
@@ -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) {
index 0b300fbb740c3354bdb8a1b212c130ff072e295a..5b6a80d865e8c5724abd914c692df36ba5b1d1be 100644 (file)
@@ -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?
index 3144ae85154e42db631a20ea4c53d5607a189d20..d0d20c3adf23ec869943dc43b61bd97055349e16 100644 (file)
@@ -4,6 +4,7 @@ import { defineConfig } from 'vite'
 export default defineConfig({
   define: {
     __DEV__: true,
+    __USE_DEVTOOLS__: false,
     __TEST__: true,
     __BROWSER__: true,
   },