-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.server.config.js
146 lines (133 loc) · 3.75 KB
/
webpack.server.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
145
146
const path = require('path');
const glob = require('glob');
const R = require('ramda');
const {ProvidePlugin} = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const {
setEntriesForPath,
addRule,
addPlugin,
prependExtensions
} = require('./util/compose');
const env = require('./util/env');
const RESOURCES_PATH = 'src/main/resources';
// ----------------------------------------------------------------------------
// Base config
// ----------------------------------------------------------------------------
const config = {
context: path.join(__dirname, RESOURCES_PATH),
entry: {},
externals: [
/^\/lib\/(.+|\$)$/i
],
output: {
path: path.join(__dirname, '/build/resources/main'),
filename: '[name].js',
libraryTarget: 'commonjs'
},
resolve: {
extensions: [],
},
externals: {
// TS externals
'@enonic-types/lib-event': '/lib/xp/event',
// JS externals
'/lib/xp/event': '/lib/xp/event',
'/lib/enonic/static': '/lib/enonic/static',
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: false,
},
keep_classnames: true,
keep_fnames: true,
}
}),
],
splitChunks: {
minSize: 30000,
},
},
mode: env.type,
// Source maps are not usable in server scripts
devtool: false,
};
// ----------------------------------------------------------------------------
// JavaScript loaders
// ----------------------------------------------------------------------------
function listEntries(extensions, ignoreList) {
const CLIENT_FILES = glob.sync(`${RESOURCES_PATH}/assets/**/*.${extensions}`);
const IGNORED_FILES = R.pipe(
R.map(entry => path.join(RESOURCES_PATH, entry)),
R.concat(CLIENT_FILES)
)(ignoreList);
const SERVER_FILES = glob.sync(`${RESOURCES_PATH}/**/*.${extensions}`, { absolute: false, ignore: IGNORED_FILES });
return SERVER_FILES.map(entry => path.relative(RESOURCES_PATH, entry));
}
// TYPESCRIPT
function addTypeScriptSupport(cfg) {
const rule = {
test: /\.ts$/,
exclude: /node_modules/,
loader: 'ts-loader',
options: {
configFile: 'tsconfig.json',
}
};
const entries = listEntries('ts', [
// Add additional files to the ignore list.
// The following path will be transformed to 'src/main/resources/types.ts:
'types.ts'
]).filter(entry => entry.indexOf('.d.ts') === -1);
return R.pipe(
setEntriesForPath(entries),
addRule(rule),
addPlugin(new ProvidePlugin({
'Object.assign': [path.join(__dirname, RESOURCES_PATH, 'polyfills'), 'assign']
})),
prependExtensions(['.ts', '.json'])
)(cfg);
}
// BABEL
function addBabelSupport(cfg) {
const rule = {
test: /\.(es6?|js)$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
babelrc: false,
plugins: [],
presets: [
[
'@babel/preset-env',
{
// Use custom Browserslist config
targets: 'node 0.10',
// Polyfills are not required in runtime
useBuiltIns: false
},
],
]
}
};
const entries = listEntries('{js,es,es6}', [
// Add additional files to the ignore list.
// The following path will be transformed to 'src/main/resources/lib/observe/observe.es6':
'lib/observe/observe.es6'
]);
return R.pipe(
setEntriesForPath(entries),
addRule(rule),
prependExtensions(['.js', '.es', '.es6', '.json'])
)(cfg);
}
// ----------------------------------------------------------------------------
// Result config
// ----------------------------------------------------------------------------
module.exports = R.pipe(
addBabelSupport,
addTypeScriptSupport
)(config);