-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
104 lines (97 loc) · 3.61 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Required packages */
require('dotenv').config({path: __dirname + '/./../../.env'})
let path = require('path'),
webpack = require('webpack'),
browserSyncPlugin = require('browser-sync-webpack-plugin'),
glob = require('glob'),
miniCSSExtractPlugin = require('mini-css-extract-plugin'),
PurgecssPlugin = require('purgecss-webpack-plugin'),
{CleanWebpackPlugin} = require('clean-webpack-plugin'),
copyPlugin = require('copy-webpack-plugin'),
fs = require('fs'),
yaml = require('js-yaml'),
CssnanoPlugin = require('cssnano-webpack-plugin'),
htmlParser = require('./modules/html-parser.js'),
TerserPlugin = require('terser-webpack-plugin')
async function config()
{
// Generate the new theme path folder form boilerplate.yaml
let themeConfig = fs.readFileSync(path.join(__dirname, 'boilerplate.yaml'), 'utf8'),
data = yaml.safeLoad(themeConfig),
from = path.resolve(`../${data.themeName}-src`),
to = path.resolve(`../${data.themeName}`),
publicPath = path.join('themes', data.themeName)
let HTMLPlugins = await htmlParser(from, to, ['htm', 'html', 'txt'])
let filesToCopy = [{from: path.join(from, 'new-theme.yaml'), to: path.join(to, 'theme.yaml')}, {from: path.join(from, 'version.yaml')}]
let pathToThemePreviewFile = path.join(from, 'assets/images/theme-preview.png')
if (fs.existsSync(pathToThemePreviewFile)) {
filesToCopy.push({from: pathToThemePreviewFile})
}
let pathToThemeConfigDir = path.join(from, 'config')
if (fs.existsSync(pathToThemeConfigDir)) {
filesToCopy.push({from: pathToThemeConfigDir, to: 'config'})
}
return {
entry: {
context: from,
},
output: {
filename: 'assets/javascript/theme.js',
path: to,
publicPath: publicPath
},
resolveLoader: {
modules: ['node_modules', 'custom_loaders']
},
plugins: [
new webpack.ProgressPlugin(),
new browserSyncPlugin({
host: 'localhost',
port: 3000,
proxy: process.env.APP_URL + ':80'
}),
new miniCSSExtractPlugin({
filename: 'assets/css/theme.css',
chunkFilename: '[id]-[hash].css',
}),
... process.env.NODE_ENV === 'production' ? [
new PurgecssPlugin(require(`${from}/purgecss.config.js`)),
new CleanWebpackPlugin()
] : [],
new copyPlugin(filesToCopy, {copyUnmodified: true})
].concat(HTMLPlugins),
module: {
rules: [{
test: /\.p?css$/,
use: [ miniCSSExtractPlugin.loader, 'css-loader', 'postcss-loader' ],
},{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: (url, resourcePath, context) => path.relative(from, resourcePath)
}
},{
test: /\.(webp|png|jpe?g|gif|svg|ico)$/i,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: (url, resourcePath, context) => path.relative(from, resourcePath)
}
},{
test: /\.(html?|txt)$/i,
loader: 'theme-filter-loader',
options: {
from: from
}
}],
},
optimization: {
minimizer: [
new CssnanoPlugin(),
new TerserPlugin()
]
}
}
}
module.exports = config();