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

增加 import.meta.env.PUBLIC_PATH 变量 #5787

Open
luhc228 opened this issue Jan 5, 2023 · 1 comment
Open

增加 import.meta.env.PUBLIC_PATH 变量 #5787

luhc228 opened this issue Jan 5, 2023 · 1 comment
Assignees
Labels

Comments

@luhc228
Copy link
Collaborator

luhc228 commented Jan 5, 2023

背景

假设现在在 public 目录下有一些静态资源,要在 Document.tsx 中引入,比如:

function Document() {
  return (
    <html lang="en">
      <head>
        <script src="/abc.js"></script>
      </head>
    </html>
  );
}

在本地环境下是可以正常访问到 localhost:3000/abc.js 静态资源的。但是如果部署到跨端应用后,跨端应用部署后访问 public 下的静态资源是通过 https://example.com/abc.js ,而 abc.js 仅仅被发到 cdn 上,导致访问失败。

ice.js 3 需要提供跟 rax-app 类似的能力,通过变量的形式修改。

使用方式

function Document() {
  return (
    <html lang="en">
      <head>
        <script src={import.meta.env.PUBLIC_PATH + "abc.js"}></script>
      </head>
    </html>
  );
}

在构建时统一替换 import.meta.env.PUBLIC_PATH 的值为 publicPath

@luhc228 luhc228 changed the title 增加 import.meta.env.PUBLIC_URL 变量 增加 import.meta.env.PUBLIC_PATH 变量 Jan 5, 2023
@wssgcg1213 wssgcg1213 added the rfc label Jan 5, 2023
@wssgcg1213
Copy link
Collaborator

import.meta.publicPath 会更好一些, env 这里应该是这个变量的输入, 消费的地方已经不是环境变量了, 而是 context.publicPath

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

No branches or pull requests

2 participants