[英]Building es6 classes into separate files with webpack
我正在嘗試為常用的React組件建立一個存儲庫,然后可以將其添加到諸如component-kit
項目中並重復使用這些component-kit
,所以類似
import MyComponent from 'component-kit/MyComponent';
目前,我具有以下項目結構:
component-kit/
src/
MyComponent/
index.js
index.js
package.json
webpack.config.js
index.js
文件通過以下方式導入所有組件:
import('./MyComponent');
然后,我使用webpack將這些導入中的每一個構建到單獨的文件中,這是根據此處提供的代碼拆分文檔的配置
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
output: {
filename: 'dist.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env', 'es2015', 'stage-0', 'react'],
plugins: ['syntax-dynamic-import']
}
}
}
]
}
};
目前,這將輸出2個文件dist.js
和0.dist.js
,我不知道如何設置輸出文件具有組件名稱即MyComponent.js
因此我可以像上面提到的那樣包含它。 理想情況下,它們將不包含任何反應,因為它將始終存在於父組件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.