From 10a0d5e4a14c408e9598b072a4889757f1735845 Mon Sep 17 00:00:00 2001 From: ZeroLing Date: Wed, 4 Jan 2023 18:35:26 +0800 Subject: [PATCH] feat: update env to support import.meta --- api-config.js | 2 +- src/packages/base/env/src/index.ts | 7 +++++-- tsconfig.json | 2 +- types/@uni/env.d.ts | 3 +++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/api-config.js b/api-config.js index 404622e7..44922e28 100644 --- a/api-config.js +++ b/api-config.js @@ -17,7 +17,7 @@ module.exports = { needCommonUtil: false, pkgInfo: [ { - version: '1.1.0', + version: '2.0.0', name: '@uni/env', exports: '', }, diff --git a/src/packages/base/env/src/index.ts b/src/packages/base/env/src/index.ts index 865c2349..862fcafc 100644 --- a/src/packages/base/env/src/index.ts +++ b/src/packages/base/env/src/index.ts @@ -14,9 +14,12 @@ declare const navigator: { swuserAgent?: string; }; -export const isWeb = typeof window !== 'undefined' && 'onload' in window; +// This requires webpack >= 5 to identify the import.meta object. +const hasImportMeta = typeof import.meta !== 'undefined'; + +export const isWeb = hasImportMeta ? import.meta.target === 'web' : (typeof window !== 'undefined' && 'onload' in window); export const isNode = typeof process !== 'undefined' && !!(process.versions && process.versions.node); -export const isWeex = typeof WXEnvironment !== 'undefined' && WXEnvironment.platform !== 'Web'; +export const isWeex = hasImportMeta ? import.meta.target === 'weex' : (typeof WXEnvironment !== 'undefined' && WXEnvironment.platform !== 'Web'); export const isKraken = typeof __kraken__ !== 'undefined'; export const isMiniApp = typeof my !== 'undefined' && my !== null && typeof my.alert !== 'undefined'; export const isByteDanceMicroApp = typeof tt !== 'undefined' && tt !== null && typeof tt.showToast !== 'undefined'; diff --git a/tsconfig.json b/tsconfig.json index 7ebf8046..3f55f057 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ - // "module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ + "module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "moduleResolution": "node", "jsx": "react", "jsxFactory": "createElement", diff --git a/types/@uni/env.d.ts b/types/@uni/env.d.ts index 64bd7c26..a6dce8d0 100644 --- a/types/@uni/env.d.ts +++ b/types/@uni/env.d.ts @@ -1 +1,4 @@ declare module '@uni/env'; +interface ImportMeta { + target: string; +}