Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update env to support import.meta #293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
needCommonUtil: false,
pkgInfo: [
{
version: '1.1.0',
version: '2.0.0',
name: '@uni/env',
exports: '',
},
Expand Down
7 changes: 5 additions & 2 deletions src/packages/base/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

兜底判断条件没变化吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没变化的

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确认一下 ssr 场景下是否可用

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';
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions types/@uni/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
declare module '@uni/env';
interface ImportMeta {
target: string;
}