taro icon indicating copy to clipboard operation
taro copied to clipboard

h5执行打包操作报错

Open yamlling opened this issue 1 year ago • 7 comments

相关平台

H5

复现仓库

https://gitee.com/yamlling_admin/taro-react-taroui.git 浏览器版本: edge127.0.2651.98 使用框架: React

复现步骤

npm run build:h5

期望结果

打包成功

实际结果

vite v4.5.3 building for production... ✓ 3 modules transformed. ✓ built in 72ms [vite]: Rollup failed to resolve import "\app.config.ts" from "C:/Users/Notx200/Desktop/react-app/src/index.html". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to build.rollupOptions.external file:///C:/Users/Notx200/Desktop/react-app/node_modules/vite/dist/node/chunks/dep-41cf5ffd.js:48216 throw new Error([vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n
^

Error: [vite]: Rollup failed to resolve import "\app.config.ts" from "C:/Users/Notx200/Desktop/react-app/src/index.html". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to build.rollupOptions.external at viteWarn (file:///C:/Users/Notx200/Desktop/react-app/node_modules/vite/dist/node/chunks/dep-41cf5ffd.js:48216:27) at onwarn (C:\Users\Notx200\Desktop\react-app\node_modules@vitejs\plugin-react\dist\index.cjs:274:9) at onRollupWarning (file:///C:/Users/Notx200/Desktop/react-app/node_modules/vite/dist/node/chunks/dep-41cf5ffd.js:48245:9) at onwarn (file:///C:/Users/Notx200/Desktop/react-app/node_modules/vite/dist/node/chunks/dep-41cf5ffd.js:47976:13) at file:///C:/Users/Notx200/Desktop/react-app/node_modules/rollup/dist/es/shared/node-entry.js:24276:13 at Object.logger [as onLog] (file:///C:/Users/Notx200/Desktop/react-app/node_modules/rollup/dist/es/shared/node-entry.js:25950:9) at ModuleLoader.handleInvalidResolvedId (file:///C:/Users/Notx200/Desktop/react-app/node_modules/rollup/dist/es/shared/node-entry.js:24862:26) at file:///C:/Users/Notx200/Desktop/react-app/node_modules/rollup/dist/es/shared/node-entry.js:24822:26 { watchFiles: [ 'taro:compiler', 'C:\Users\Notx200\Desktop\react-app\src\pages\index\index.config.ts', 'C:\Users\Notx200\Desktop\react-app\src\pages\car\index.config.ts', 'C:/Users/Notx200/Desktop/react-app/src/index.html', '\x00vite/modulepreload-polyfill' ] }

Node.js v18.20.4

环境信息

Taro v4.0.4


  Taro CLI 4.0.4 environment info:
    System:
      OS: Windows 11 10.0.22621
    Binaries:
      Node: 18.20.4 - C:\Program Files\nodejs\node.EXE
      Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD
      npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
    npmPackages:
      @tarojs/cli: 4.0.4 => 4.0.4
      @tarojs/components: 4.0.4 => 4.0.4
      @tarojs/helper: 4.0.4 => 4.0.4
      @tarojs/plugin-framework-react: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-alipay: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-h5: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-harmony-hybrid: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-jd: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-qq: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-swan: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-tt: 4.0.4 => 4.0.4
      @tarojs/plugin-platform-weapp: 4.0.4 => 4.0.4
      @tarojs/react: 4.0.4 => 4.0.4
      @tarojs/runtime: 4.0.4 => 4.0.4
      @tarojs/shared: 4.0.4 => 4.0.4
      @tarojs/taro: 4.0.4 => 4.0.4
      @tarojs/vite-runner: 4.0.4 => 4.0.4
      babel-preset-taro: 4.0.4 => 4.0.4
      eslint-config-taro: 4.0.4 => 4.0.4
      react: ^18.0.0 => 18.3.1
      taro-ui: ^3.3.0 => 3.3.0

yamlling avatar Aug 15 '24 03:08 yamlling

I was running into this too. I noticed that Gatsby seems to be dead and while in the long run, I expect to move away from Gatsby, but in the short-term, I needed a fix.

Issue

It seems like the source of the bug is in gatsby-link, where several code paths don't call withPrefix. I cloned the gatsby repo, and modified the gatsby-link package to add the appropriate lines. (Confusingly, this file seems to have an isAbsolutePath function which returns true for relative paths).

diff --git a/packages/gatsby-link/src/rewrite-link-path.js b/packages/gatsby-link/src/rewrite-link-path.js
index e1b2b49671..6668b76c9b 100644
--- a/packages/gatsby-link/src/rewrite-link-path.js
+++ b/packages/gatsby-link/src/rewrite-link-path.js
@@ -13,25 +13,26 @@ const getGlobalTrailingSlash = () =>
 function applyTrailingSlashOptionOnPathnameOnly(path, option) {
   const { pathname, search, hash } = parsePath(path)
   const output = applyTrailingSlashOption(pathname, option)
 
   return `${output}${search}${hash}`
 }
 
 function absolutify(path, current) {
   // If it's already absolute, return as-is
   if (isAbsolutePath(path)) {
-    return path
+    return withPrefix(path)
   }
 
+  const prefixed = withPrefix(path)
   const option = getGlobalTrailingSlash()
-  const absolutePath = resolve(path, current)
+  const absolutePath = resolve(prefixed, current)
 
   if (option === `always` || option === `never`) {
     return applyTrailingSlashOptionOnPathnameOnly(absolutePath, option)
   }
 
   return absolutePath
 }
 
 function applyPrefix(path) {
   const prefixed = withPrefix(path)

Fixing It

The site I'm working on uses patch-package to apply the above change to Gatsby link. To generate this yourself, you can clone the repo, apply the above change, build, and then generate a patch from that.

If you'd rather re-use my work, you can copy the patch file in this gist to patches/gatsby-link+5.10.0.patch in your repository.

I haven't tested this too thoroughly, but it seems to handle all the cases I've thrown at it so far.

rosszurowski avatar Nov 15 '24 22:11 rosszurowski

Thank you for sharing your solution with the community @rosszurowski!

Though we aren't actively investing in new features or low-priority bug fixes, Gatsby is still maintained.

If you open a PR with your bug fix, we'll take a look and get it merged so everyone can benefit! Thanks 🙂

serhalp avatar Nov 18 '24 14:11 serhalp

Hi @serhalp — good to know! I didn't see an official response in that thread, so assumed it was accurate. I'll gladly open a PR with the fix.

rosszurowski avatar Nov 18 '24 15:11 rosszurowski

Awesome, thank you!

For the record, there is a response here and a blog post here.

serhalp avatar Nov 18 '24 16:11 serhalp