我想创建将 js 和 css 保存在同一文件夹中的 React 组件(对于每个组件):
- 对话
- 容器.jsx
- Presentation.jsx
- styles.less
这是我的 webpack 配置的一部分:
module: {
loaders: [
{
test: /\.(js|jsx)$/,
loader: 'babel',
exclude: /(node_modules)/,
query: {
presets: [
['react'],
['es2015'],
['stage-0']
],
plugins: [
['transform-decorators-legacy'],
['transform-runtime'],
['transform-react-remove-prop-types'],
['transform-react-constant-elements'],
['transform-react-inline-elements']
]
}
},
{
test: /\.less$/,
loader: ExtractTextPlugin.extract(['css-loader', 'less-loader']),
}
],
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new webpack.ProvidePlugin({
'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
}),
new webpack.optimize.UglifyJsPlugin({
beautify: false,
comments: false,
sourceMap: true,
compress: {
sequences: true,
booleans: true,
loops: true,
unused: true,
warnings: false,
drop_console: true,
unsafe: true
}
}),
new ExtractTextPlugin({
filename: 'bundle.css',
allChunks: true
}),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorOptions: {
discardComments: {
removeAll: true
}
},
canPrint: true
})
],
Webpack 创建 bundle.js
+ bundle.css
文件。我会说它按预期工作,除了一瞬间。我发现 bundle.js
包含来自 bundle.css
的 css。
例如,如果 bundle.css
的大小为 50KB,则 bundle.js
= 自身大小 + 50KB。
我的 js 包中不需要重复的 css 代码。那么如何解决呢?
最佳答案
当我从 laravel-elixir-webpack-official
切换到原生 webpack
(webpack-stream) 时修复
关于css - 如何从 js 包中删除 css?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46373461/