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

[Bug]: umi 设定的 define 环境变量在最新版本中会造成 Uncaught ReferenceError: xxx is not defined #1549

Closed
ShotHeadman opened this issue Aug 29, 2024 · 1 comment · Fixed by #1551
Assignees

Comments

@ShotHeadman
Copy link

在 umi 的 config 的里设置了 define 之后,全局ts里增加声明,并在组件里使用

import { Layout, Row, Typography } from 'antd';
import React from 'react';
import styles from './Guide.less';

interface Props {
  _id?: string;
  name: string;
}

export const idMap: Record<string, string> = {
  dev: '4431j2',
  pre: '0asd4',
  prod: 'zxcxczxczxc',
};

// 脚手架示例组件
const Guide: React.FC<Props> = (props) => {
  const { _id = idMap[MY_ENV], name } = props;

  return (
    <Layout>
      <Row>
        <Typography.Title level={3} className={styles.title}>
          <span>{MY_ENV}</span>
          <span>{_id}</span>
          欢迎使用 <strong>{name}</strong></Typography.Title>
      </Row>
    </Layout>
  );
};

export default Guide;

会产生如下异常:
image

关闭 mako,使用 msfu,显示正常
选择使用 umi@4.3.11 附带的 mako@0.7.9,显示也正常

该问题在我使用 mako@0.8.6时可复现:https://codesandbox.io/p/github/ShotHeadman/umi-mako-env/master?import=true

@stormslowly
Copy link
Member

const { _id = idMap[MY_ENV], name } = props;

先修改成

const   defaultEnv = MY_ENV;
const { _id = idMap[defaultEnv], name } = props

先绕过下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants