我设法将我的CSS和更少的资源(从JavaScript导入)到all-my-LESS|CSS
,使用ExtractTextPlugin
提取它们,并将它们与MergeFilesPlugin
合并为CC_3。
我缺少的位:如何在完成位置运行CSSNANO(例如通过PostCSS?)?(哦,当我habe inline源地图时,我没有设法生成外部combinedStyles.map
文件)。
这是我组合的webpack.config.babel.js
(忽略babel位,只是意味着,您可以在ES6中写下它,带有更高的导入语句):
import path from 'path';
const webpack = require('webpack'); //to access built-in plugins
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import MergeFilesPlugin from 'merge-files-webpack-plugin';
const extractCSS = new ExtractTextPlugin("all-my-CSS.css");
const extractLESS = new ExtractTextPlugin("all-my-LESS.css");
export default {
entry: [ './src/index_5.js' ],
output: {
path: path.resolve( 'dist')
filename: 'bundle.js',
sourceMapFilename: './bundle.js.map'
},
module: {
rules: [{
test: /.css$/,
use: extractCSS.extract(
[{ // This is basically "use"
loader: "css-loader",
options: {
minimize: false, // done later
sourceMap: true,
modules: false, // no css modules, all global styles
}
}]
)
},
{
test: /.less$/,
use: extractLESS.extract( // This is basically "use"
[// No style-loader here! We want this external!
{
loader: "css-loader", // translates CSS into CommonJS
options: {
minimize: false,
sourceMap: true
}
}, {
loader: "less-loader", // compiles Less to CSS
options: {
sourceMap: true,
}
}]
)
}
] // rules
}, // module
devtool: 'inline-source-map',
devServer: { inline: true },
plugins: [
extractCSS,
extractLESS,
new MergeFilesPlugin({
filename: 'combinedStyles.css', //output filename
test: /.css$/,
deleteSourceFiles: false // for now
})
]
};
我添加了用 css-nano
添加了 postcss-loader
,检查它是否有帮助。
我在这里看不到需要使用Mergefilesplugins(只是我的意见)。ExtractTextPlugin
在这里很有用。
只需使用一个ExtractTextPlugin,然后将所有CSS或更少的文件放在一个文件夹中(使用.css或.less。ext),将选择性地应用于它们,然后在插件中使用
,然后将其应用于它们。new ExtractTextPlugin('style.css')
提取了一个普通的CSS文件。
使用CSSNANO:
[{
test: /.css$/,
use: ExtractTextPlugin.extract(
[{ // This is basically "use"
loader: "css-loader",
options: {
minimize: false, // done later
sourceMap: true,
modules: false, // no css modules, all global styles
}
},
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('cssnano')({
autoprefixer: false,
safe: true
})
];
}
}
}]
)
},
{
test: /.less$/,
use: ExtractTextPlugin.extract( // This is basically "use"
[// No style-loader here! We want this external!
{
loader: "css-loader", // translates CSS into CommonJS
options: {
minimize: false,
sourceMap: true
}
},
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('cssnano')({
autoprefixer: false,
safe: true
})
];
}
}
},
{
loader: "less-loader", // compiles Less to CSS
options: {
sourceMap: true,
}
}]
)
}
]