V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
MerlinLord
V2EX  ›  问与答

webpack4 如果一个 library 中包含异步引用组件,打包到项目中 js 文件 404 了怎么办

  •  
  •   MerlinLord · 2018-12-23 23:37:32 +08:00 · 944 次点击
    这是一个创建于 1965 天前的主题,其中的信息可能已经有所发展或是发生改变。
    //file1.js
    const func2 = function(){
    console.log('func2')
    }
    
    //index.js
    const func1 = function(){
    console.log('func1')
    }
    const func3 = function(){
    import('./file1').then(res=>{res.default()})
    }
    
    export default {func1,func3}
    

    在实际项目中引用上面打包好的 library 比如

    import test from './test.js'
    console.log(test) // {func1,func3}
    

    但是 func2 并没有被引入,而是 chunk loading failed *.chunk.js 404 已配置 publicUrl 为 dist

    尽管可以通过 require/require.esure 解决,但是难免在开发 library 时使用到 import/export,或者 lib 本身的项目需要 import 引入异步组件,人为避免?

    2 条回复    2018-12-24 13:19:48 +08:00
    exonuclease
        1
    exonuclease  
       2018-12-24 10:11:10 +08:00 via iPhone
    异步加载的语法是 import()
    MerlinLord
        2
    MerlinLord  
    OP
       2018-12-24 13:19:48 +08:00
    @exonuclease #1 是啊,然后?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1006 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:26 · PVG 04:26 · LAX 13:26 · JFK 16:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.