From 47622e8e77f57d9e7cc9e12e0945bde4f9fc539a Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Wed, 8 May 2024 15:24:49 +0800 Subject: [PATCH] feat: add a feature flag to turn on trusted types policy creation, and default to false --- packages/global.d.ts | 1 + packages/runtime-dom/src/nodeOps.ts | 6 +++++- rollup.config.js | 3 +++ scripts/dev.js | 1 + vitest.config.ts | 1 + 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/global.d.ts b/packages/global.d.ts index 38320b81e04..5d8e2db1127 100644 --- a/packages/global.d.ts +++ b/packages/global.d.ts @@ -18,6 +18,7 @@ declare var __FEATURE_OPTIONS_API__: boolean declare var __FEATURE_PROD_DEVTOOLS__: boolean declare var __FEATURE_SUSPENSE__: boolean declare var __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: boolean +declare var __FEATURE_PROD_TRUSTED_TYPES__: boolean // for tests declare namespace jest { diff --git a/packages/runtime-dom/src/nodeOps.ts b/packages/runtime-dom/src/nodeOps.ts index e12870b59c9..ead05b76eee 100644 --- a/packages/runtime-dom/src/nodeOps.ts +++ b/packages/runtime-dom/src/nodeOps.ts @@ -13,7 +13,11 @@ type VueTrustedTypePolicy = let policy: VueTrustedTypePolicy = undefined function getPolicy(): VueTrustedTypePolicy { const ttWindow = window as unknown as TrustedTypesWindow - if (ttWindow.trustedTypes && !policy) { + if ( + (__DEV__ || __FEATURE_PROD_TRUSTED_TYPES__) && + ttWindow.trustedTypes && + !policy + ) { try { policy = ttWindow.trustedTypes.createPolicy('vue', { createHTML: val => val, diff --git a/rollup.config.js b/rollup.config.js index e8d5984184d..dc25ffd7e86 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -192,6 +192,9 @@ function createConfig(format, output, plugins = []) { __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: isBundlerESMBuild ? `__VUE_PROD_HYDRATION_MISMATCH_DETAILS__` : `false`, + __FEATURE_PROD_TRUSTED_TYPES__: isBundlerESMBuild + ? `__VUE_PROD_TRUSTED_TYPES__` + : `false`, } if (!isBundlerESMBuild) { diff --git a/scripts/dev.js b/scripts/dev.js index 7db79442ee9..3fbce1aa428 100644 --- a/scripts/dev.js +++ b/scripts/dev.js @@ -127,6 +127,7 @@ for (const target of targets) { __FEATURE_OPTIONS_API__: `true`, __FEATURE_PROD_DEVTOOLS__: `false`, __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `false`, + __FEATURE_PROD_TRUSTED_TYPES__: `false`, }, }) .then(ctx => ctx.watch()) diff --git a/vitest.config.ts b/vitest.config.ts index e6436408adb..53f9bd4a7d3 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -17,6 +17,7 @@ export default defineConfig({ __FEATURE_SUSPENSE__: true, __FEATURE_PROD_DEVTOOLS__: false, __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: false, + __FEATURE_PROD_TRUSTED_TYPES__: false, __COMPAT__: true, }, resolve: {