嘿,我需要以下方面的帮助:
编辑显然这种方法需要在父应用程序项目中手动加载CSS。我想避免哪些。有没有其他方法可以提供在不需要手动加载的情况下在组件级别上解析样式的方案?
这是我的生产webpack配置:
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.join(__dirname, '../lib'),
libraryTarget: 'commonjs',
},
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader']
})
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader']
})
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.svg/,
use: {
loader: 'svg-url-loader',
options: {}
}
}
]
},
externals: {
'react': 'commonjs react',
'react-dom': 'commonjs react-dom',
},
resolve: {
modules: [
path.resolve('./src'),
path.resolve('./node_modules')
]
},
plugins: [
new ExtractTextPlugin({
filename: 'ui-library.css'
})
]
};
你可以简单地不使用ExtractTextPlugin
。
Webpack的整个目的是将资产分组,而不是基于文件类型,而是通过组件透视图。
因此,如果你删除ExtractTextPlugin
,你的CSS将包含在你的.js包中。