-
Notifications
You must be signed in to change notification settings - Fork 7
/
createConfig.js
119 lines (106 loc) · 3.58 KB
/
createConfig.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
// Copyright 1999-2018. Plesk International GmbH. All rights reserved.
const fs = require('fs-extra');
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VirtualModulesPlugin = require('webpack-virtual-modules');
const { resolveAppPath } = require('../utils');
const resolveLibPath = relativePath => path.resolve(__dirname, '..', relativePath);
const createBabelConfig = require('../babel/createConfig');
const { prepareRoutes, generateRoutesModule } = require('../routes');
const loadExtensionConfig = () => {
const configPath = resolveAppPath('extension.config.js');
const defaults = {
routes: [],
};
if (fs.pathExistsSync(configPath)) {
return {
...defaults,
...require(configPath),
};
}
return defaults;
};
const applyCustomConfig = (config, params, customConfig) => {
if (typeof customConfig !== 'function') {
return config;
}
return customConfig(config, params);
};
module.exports = ({ isDev }) => {
const cleanOptions = {
verbose: true,
dangerouslyAllowCleanPatternsOutsideProject: true,
dry: isDev,
};
const output = resolveAppPath('src/htdocs/dist');
const extensionConfig = loadExtensionConfig();
const routes = prepareRoutes(extensionConfig.routes);
const routesPlugin = new VirtualModulesPlugin({
routes: generateRoutesModule(routes),
});
const baseConfig = {
mode: isDev ? 'development' : 'production',
watch: isDev,
context: resolveAppPath('src/frontend'),
entry: resolveLibPath('templates/index'),
output: {
filename: 'main.js',
path: output,
libraryTarget: 'amd',
},
devtool: isDev ? 'cheap-module-source-map' : 'source-map',
module: {
rules: [
{
test: /\.js$/i,
include: [
resolveAppPath('src/frontend'),
resolveLibPath('..'),
],
loader: require.resolve('babel-loader'),
options: {
cacheDirectory: true,
...createBabelConfig(),
},
},
{
test: /\.pug$/,
use: {
loader: 'pug-loader',
options: {
pretty: true,
},
},
},
],
},
plugins: [
new CleanWebpackPlugin(cleanOptions),
new HtmlWebpackPlugin({
filename: resolveAppPath('src/htdocs/index.php'),
template: resolveLibPath('templates/index.pug'),
inject: false,
minify: false,
templateParameters: {
routes,
},
}),
],
resolve: {
modules: [
resolveAppPath('src/frontend'),
'node_modules',
],
},
externals: {
'@plesk/ui-library': { amd: 'plesk-ui-library' },
},
};
const config = applyCustomConfig(baseConfig, { isDev }, extensionConfig.webpack);
if (!config.plugins || !config.plugins.find(plugin => plugin?.constructor?.name === 'VirtualModulesPlugin')) {
config.plugins = config.plugins ?? [];
config.plugins.push(routesPlugin);
}
return config;
};