我设法使样式表的工作。
主要插件是:
ExtractTextPlugin
- 对于合并多个CSS文件 CopyWebpackPlugin
- 复制入资产价值到dist PurifyCSSPlugin
- 删除未使用的CSS OptimizeCssAssetsPlugin
- 压缩CSS文件ENV =建立
下面是webpack.config.js
文件:
const path = require('path');
// To remove unused css
const PurifyCSSPlugin = require('purifycss-webpack');
// Copy Assests to dist
const CopyWebpackPlugin = require('copy-webpack-plugin');
// For combining multiple css files
const ExtractTextPlugin = require('extract-text-webpack-plugin')
// Minify css files for env=build
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
let buildPlugins = [];
let basePath = path.join(__dirname, '/../');
if (env === EnvEnum.BUILD) {
// minify css files if env is build i.e. production
buildPlugins.push(new OptimizeCssAssetsPlugin());
}
module.exports = {
// Entry, files to be bundled
entry: {
'myStyle.min.css': [
basePath + '/src/styles/app.css',
basePath + '/src/styles/other.css',
basePath + '/src/bower_components/abc.min.css'
]
},
devtool: '',
output: {
// Output directory
path: basePath + '/dist/styles/',
// [hash:6] with add a SHA based on file changes if the env is build
filename: env === EnvEnum.BUILD ? 'myStyle-[hash:6].min.css' : '[name]'
},
// Rules for bundling
module: {
rules: [{
test: /\.css$/i,
use: ExtractTextPlugin.extract({
use: {
loader: 'css-loader',
options: {
// ExtractTextPlugin tries to process url like in backgroun-image, url(), etc. We need to stop that behavior so we need this option
url: false
}
}
})
}, {
// Load all possible images included in css
test: /\.(jpe?g|png|gif|svg|ico)$/i,
loaders: [
'file-loader?name=images/[sha512:hash:base64:7].[ext]',
'image-webpack-loader?progressive=true&optimizationLevel=7&interlaced=true'
]
}, {
// Load all possible fonts format files included in css
test: /\.(ttf|eot|svg|woff2?)(\?v=[a-z0-9=\.]+)?$/i,
include: basePath + '/src/bower_components',
loader: 'file-loader?name=fonts/[name].[ext]'
}]
},
resolve: {
alias: {},
modules: [
'src/bower_components/',
],
extensions: ['.css', '.eot', '.woff', '.svg']
},
plugins: [
// Bundling of entry files
new ExtractTextPlugin((env === EnvEnum.BUILD ? 'myStyle-[hash:6].min.css' : '[name]')),
// Copy css/images/fonts/js file(s) to dist
new CopyWebpackPlugin([{
from: basePath + '/src/bower_components/components-font-awesome/fonts',
to: basePath + '/dist/fonts/'
}, {
from: basePath + '/src/fonts',
to: basePath + '/dist/fonts/'
}]),
// To remove unused CSS by looking in corresponding html files
new PurifyCSSPlugin({
// Give paths to parse for rules. These should be absolute!
paths: glob.sync([
path.join(basePath, 'src/*.html'),
path.join(basePath, 'src/*.js')
])
})
].concat(buildPlugins)
};
让我知道如果您有任何进一步的概念RNS。
是不是应该使用'loaders'键?测试:/\.scss$/, 加载器:['to-string-loader','css-loader','sass-loader'] }' – VadimB
@VadimB根据[docs](https) ://webpack.js.org/guides/migrating/#chaining-loaders)时,应该使用规则'use:[loader1,loader2]'。 – user3344978