2017-10-06 106 views
0

我正在構建要在React應用程序中導入的組件庫。我想公開這些組件,以便它們可以動態地導入到應用程序中。公開組件,以便它們可以動態導入應用程序

我試了一下:

LIB> index.js

export const Component1 = import(/* webpackChunkName: "components/Component1" */ './components/Component1') 
export const Component2 = import(/* webpackChunkName: "components/Component2" */ './components/Component2') 

LIB> webpack.config.js

entry: path.resolve('src/index.js'), 
output: { 
    path: path.resolve('build'), 
    filename: 'index.js', 
    chunkFilename: '[name].js', 
    library: 'lib', 
    libraryTarget: 'umd' 
}, 

構建結果

80.13 KB build/index.js 
69.68 KB build/components/Component1.js 
12.4 KB build/components/Component2.js 

我想達到的目標:

應用> index.js

import('lib/components/Component1').then(module => { 
    console.log(module) // Empty object so far 
}) 

我怎麼想導出的組件,使它們可一個接一個?還是有另一種方式?

+0

從你的構建結果輸出看來,這正是你所取得的成就?我錯過了什麼?你有沒有一個「組件」目錄,其中包含兩個組件?您可能還需要在您的配置中設置'libraryExport:default'選項。 https://webpack.js.org/configuration/output/#output-libraryexport –

+0

我在輸出中有組件,但當我想在應用中檢索它們時,它們不可用。不過,我會嘗試設置libraryExport選項。 – untemps

回答

相關問題