【问题标题】:Console error on import of non-existent file in vitevite导入不存在的文件控制台报错
【发布时间】:2022-12-01 09:53:46
【问题描述】:

我正在使用 Vue2.7 和@vitejs/plugin-vue2 从 webpack 迁移。 所以有一个问题。

await import(`${path}/${config || 'config'}`).catch(e => ({default: {}}))).default

到此为止,上面的代码执行时,如果导入的文件丢失,控制台不会报错,会妥善处理。

但是当我移动到 vite 时,一旦我导入,我就会收到一个 Not Found 错误,然后运行 ​​catch。 像这样。

http://localhost:14400/src/User/config net::ERR_ABORTED 404 (Not Found)

vite 有没有办法在控制台不显示错误的情况下处理丢失的文件?

【问题讨论】:

    标签: javascript vue.js vuejs2 vite dynamic-import


    【解决方案1】:

    默认情况下,rollup 中的导入(Vite 的打包器)被提升(放在文件的顶部,忽略 try/catch)所以你需要传递 rollup 选项来覆盖这里描述的默认行为:

    https://github.com/rollup/plugins/tree/master/packages/commonjs#ignoretrycatch

    commonjs 选项可以在 Vite 的配置文件中的构建部分进行配置:

    https://vitejs.dev/config/build-options.html#build-commonjsoptions

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 2022-11-14
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2012-04-03
      • 2017-08-27
      相关资源
      最近更新 更多