MerlinLord
V2EX  ›  问与答

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

  •  
  •   MerlinLord · Dec 23, 2018 · 1384 views
    This topic created in 2705 days ago, the information mentioned may be changed or developed.
    //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 replies    2018-12-24 13:19:48 +08:00
    exonuclease
        1
    exonuclease  
       Dec 24, 2018 via iPhone
    异步加载的语法是 import()
    MerlinLord
        2
    MerlinLord  
    OP
       Dec 24, 2018
    @exonuclease #1 是啊,然后?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1493 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 16:59 · PVG 00:59 · LAX 09:59 · JFK 12:59
    ♥ Do have faith in what you're doing.