-
Notifications
You must be signed in to change notification settings - Fork 6
/
webpack.config.js
144 lines (130 loc) · 4.11 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @author Extly, CB <[email protected]>
* @copyright Copyright (c)2019-2022 Extly, CB All rights reserved.
* @license MIT; see LICENSE.txt
*
* @see https://www.extly.com
*/
// Declaration of Webpack plugins
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
// Define the pages to be prototyped
const prototypePages = [
'index',
'index.es',
'74-using-composer-in-joomla-and-other-content-management-systems',
'80-desde-phpmad-2021-usando-composer-en-un-cms,-gracias-al-php-prefixing',
];
// Read the package configuration
const packageConfig = require('./package.json');
// Read the control flags
const devMode = process.env.NODE_ENV === 'development';
const productionMode = !devMode;
// The proxy mode is only used within a template package
const proxyMode = process.env.npm_lifecycle_event === 'dev-proxy'
&& packageConfig.config
&& packageConfig.config.proxyURL;
// Output filenames
const cssOutputfilename = devMode ? '[name].css' : '[name].css'; // [hash].
const cssOutputchunkFilename = devMode ? '[id].css' : '[id].css'; // [hash].
// Configure plugins for Webpack
const plugins = [
new MiniCssExtractPlugin({
filename: cssOutputfilename,
chunkFilename: cssOutputchunkFilename,
}),
];
// In Dev mode and not proxied,
// declare the pages to be processed
if (devMode && !proxyMode) {
plugins.push(
...prototypePages.map(
(page) => new HtmlWebpackPlugin({
filename: `${page}.html`,
template: `src/pages/${page}.html`,
}),
),
);
}
// In proxy mode, keep php files updated
if (proxyMode) {
// Watch php files
plugins.push(
new BrowserSyncPlugin({
proxy: {
target: packageConfig.config.proxyURL,
},
files: ['**/*.php'],
cors: true,
reloadDelay: 0,
}),
);
}
// In proxy mode or production mode,
// copy the css and js files to the template
if (proxyMode || productionMode) {
// Copy files
plugins.push(
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, './dist/main.css'),
to: path.resolve(__dirname, './css/template.css'),
},
{
from: path.resolve(__dirname, './dist/main.js'),
to: path.resolve(__dirname, './js/template.js'),
},
]
}),
);
// If there is an extra folder,
// copy also the css file to the extra destination
if (packageConfig.config && packageConfig.config.extraCCProxyFolder) {
plugins.push(
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, './dist/main.css'),
to: path.resolve(
packageConfig.config.extraCCProxyFolder,
'./css/template.css',
),
},
{
from: path.resolve(__dirname, './dist/main.js'),
to: path.resolve(
packageConfig.config.extraCCProxyFolder,
'./js/template.js',
),
},
]
}),
);
}
}
// Declare export for webpack processing
module.exports = {
entry: './src/styles.css',
mode: process.env.NODE_ENV,
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
// Load css
'css-loader',
// Load PostCss, see postcss.config.js
'postcss-loader',
],
},
],
},
plugins,
};