You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
webpack/index.js

134 lines
3.8 KiB

8 months ago
const webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { PurgeCSSPlugin } = require('purgecss-webpack-plugin')
2 months ago
const ManifestPlugin = require('./plugins/manifest')
8 months ago
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts')
const SvgSpritemapPlugin = require('svg-spritemap-webpack-plugin')
const path = require('path')
const glob = require('glob')
2 months ago
/**
* function generate config for webpack
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://git.node001.net/tiny-components/webpack.git
*
*/
8 months ago
module.exports = function tinyComponentsWebpack(files, options = {}) {
// merge options with defaults
const defaults = Object.assign({
2 months ago
context: path.resolve(process.cwd(), ''),
destination: path.resolve(process.cwd(), 'public'),
2 months ago
publicPath: '/',
7 months ago
purge: {
src: path.join(__dirname, 'js'),
safelist: []
7 months ago
}
8 months ago
}, options)
2 months ago
const config = {
2 months ago
context: defaults.context,
8 months ago
entry: files,
output: {
2 months ago
path: defaults.destination,
filename: 'js/[name].js',
publicPath: defaults.publicPath
8 months ago
},
resolve: {
modules: ['node_modules'],
},
8 months ago
optimization: {
removeEmptyChunks: true,
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
output: {
comments: false,
},
},
extractComments: false
})
]
},
module: {
rules: [{
test: /\.(css|scss)$/,
7 months ago
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { url: false }
},
7 months ago
'sass-loader'
]
},{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset/resource',
generator: {
filename: "fonts/[name].[ext]",
}
7 months ago
}]
8 months ago
},
plugins: [
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
new PurgeCSSPlugin({
paths: glob.sync(`${defaults.purge.src}/**/*`, { nodir: true }),
safelist: defaults.purge.safelist
8 months ago
}),
2 months ago
new ManifestPlugin({
filename: 'assets-manifest.json',
context: defaults.context
})
8 months ago
],
}
7 months ago
// if rules exists add
if (defaults.rules) {
2 months ago
defaults.rules.forEach((rule) => {
7 months ago
config.module.rules.push(rule)
})
}
// adding svg src
8 months ago
if (defaults.svg.src) {
config.plugins.push(new SvgSpritemapPlugin(defaults.svg.src, {
output: {
filename: 'symbol-defs.svg',
chunk: {
keep: true
},
svgo: {
plugins: [{
name: 'convertStyleToAttrs',
active: true
},{
name: 'removeStyleElement',
active: true
}, {
name: 'removeAttrs',
params: {
attrs: 'fill'
}
}]
}
},
sprite: {
prefix: 'icon-'
}
}))
}
return config
}