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] 使用约定式路由的时候,如果同时在页面中使用了routeProps会导致默认的以路由为分界拆分 chunk失效 #12953

Open
RichardDFang opened this issue Mar 7, 2025 · 1 comment

Comments

@RichardDFang
Copy link

What happens?

约定式路由,页面内使用了routeProps导致路由拆分chunk失效

Mini Showcase Repository(REQUIRED)

.umirc.ts

import { defineConfig } from "umi";

export default defineConfig({
  // routes: [
  //   { path: "/", component: "index" },
  //   { path: "/docs", component: "docs" },
  // ],
  routes: false,
  conventionRoutes: {
    exclude: [/\/hooks\//, /\/components\//, /\/models\//, /\/apis\//, /\/constants\//, /.js$/]
  },
});

pages/index.tsx // 当前页面讲会被打包进umi.js,而不会有单独的chunk文件

import { useRouteProps } from 'umi';
import yayJpg from '../assets/yay.jpg';

export default function HomePage() {
  const routeProps = useRouteProps();
  return (
    <div>
      <h2>Yay! Welcome to umi!</h2>
      <p>
        <img src={yayJpg} width="388" />
      </p>
      <p>
        To get started, edit <code>pages/index.tsx</code> and save to reload.
      </p>
    </div>
  );
}

export const routeProps = {
  title: 'false',
}

bad case 仓库

How To Reproduce

将上述代码拉下来后直接打包就行

Expected behavior 1. 2.

打包后的文件有dist\p__index.async.js

122.82 kB dist\umi.js
679 B dist\p__index.async.js
646 B dist\layouts__index.async.js
530 B dist\p__docs.async.js
100 B dist\layouts__index.chunk.css

Context

  • Umi Version: 4.4.4
  • Node Version: v14.18.0
  • Platform: windows

简单排查问题可能所在

打包生成的routeProps文件直接引入了源文件,如下

// @ts-nocheck
// This file is generated by Umi automatically
// DO NOT CHANGE IT MANUALLY!
import { routeProps as routeProps_1 } from 'D:/gitlab/my-app/src/pages/index.tsx';
export default {
  'index': routeProps_1,
};

如果把页面内的routeProps代码直接挪过来,而不是引用的方式应该能解决

@RichardDFang
Copy link
Author

实际发现是routeProps生成时分了入口文件routeProps.ts和生成文件routeProps.js。目前umi项目优先取的ts文件,ts是直接引用的文件,导致按路由拆包失败
改动下面这个文件可修复
packages\preset-umi\src\features\tmpFiles\tmpFiles.ts

headerImports.push(`import routeProps from './routeProps';`); -> headerImports.push(`import routeProps from './routeProps.js';`);

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

No branches or pull requests

1 participant