-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
.webpack.config.js
235 lines (215 loc) · 7.46 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const { globSync } = require('glob');
const path = require('path');
const libOutputPath = 'public/lib';
const scssOutputPath = 'css/lib';
/*
* External libraries files build configuration.
*/
const config = {
entry: function () {
// Create an entry per file in lib/bundle directory.
// Entry name will be name of the file (without ext).
const entries = {};
for (const ext of ['.js', '.scss']) {
const files = globSync(path.resolve(__dirname, 'lib/bundles') + '/!(*.min)' + ext);
for (const file of files) {
const entry_name = path.basename(file, ext);
if (entry_name in entries) {
throw new Error(`Duplicate bundle entry: '${entry_name}'.`);
}
entries[entry_name] = file;
}
}
return entries;
},
output: {
path: path.resolve(__dirname, libOutputPath),
publicPath: '', // keep URLs relative to output path
},
module: {
rules: [
{
// Load scripts with no compilation for packages that are directly providing "dist" files.
// This prevents useless compilation pass and can also
// prevents incompatibility issues with the webpack require feature.
// It also removes existing sourcemaps that cannot be used correctly.
test: /\.js$/,
include: [
path.resolve(__dirname, 'node_modules/@fullcalendar'),
path.resolve(__dirname, 'node_modules/cystoscape'),
path.resolve(__dirname, 'node_modules/cytoscape-context-menus'),
path.resolve(__dirname, 'node_modules/jquery-migrate'),
path.resolve(__dirname, 'node_modules/rrule'),
path.resolve(__dirname, 'lib/blueimp/jquery-file-upload'),
],
use: ['script-loader', 'strip-sourcemap-loader'],
},
{
test: /\.json$/,
type: 'json'
},
{
// Test for a polyfill (or any file) and it won't be included in your
// bundle
test: path.resolve(__dirname, 'node_modules/jquery.fancytree/dist/modules/jquery.fancytree.ui-deps.js'),
use: 'null-loader',
},
{
// Build styles
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
// Copy images and fonts
test: /\.((gif|png|jp(e?)g)|(eot|ttf|svg|woff2?))$/,
type: 'asset/resource',
generator: {
filename: function (pathData) {
// Keep only relative path
let sanitizedPath = path.relative(__dirname, pathData.filename);
// Sanitize name
sanitizedPath = sanitizedPath.replace(/[^\\/\w-.]/, '');
// Remove the first directory (lib, node_modules, ...) and empty parts
// and replace directory separator by '/' (windows case)
sanitizedPath = sanitizedPath.split(path.sep)
.filter((part, index) => {
return '' != part && index != 0;
}).join('/');
return sanitizedPath;
},
},
},
{
// Build SCSS files
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
],
},
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
new webpack.ProvidePlugin(
{
process: 'process/browser', // required by some libs (including `popper.js`)
}
),
new CleanWebpackPlugin(
{
cleanOnceBeforeBuildPatterns: [
path.join(process.cwd(), libOutputPath + '/**/*'),
path.join(process.cwd(), scssOutputPath + '/**/*')
]
}
), // Clean lib dir content
new MiniCssExtractPlugin(), // Extract styles into CSS files
new MonacoWebpackPlugin({
'languages': ['html', 'javascript', 'typescript', 'json', 'markdown', 'twig', 'css', 'scss', 'shell'],
'publicPath': '/lib/'
}),
],
resolve: {
fallback: {
"path": require.resolve("path-browserify"),
},
// Use only main file in requirement resolution as we do not yet handle modules correctly
mainFields: [
'main',
],
},
mode: 'none', // Force 'none' mode, as optimizations will be done on release process
devtool: 'source-map', // Add sourcemap to files
stats: {
// Limit verbosity to only usefull information
all: false,
errors: true,
errorDetails: true,
warnings: true,
entrypoints: true,
timings: true,
},
};
// Copy raw JS and SCSS files
const filesToCopy = [
// JS files
{
package: '@fullcalendar/core',
from: 'locales/*.js',
},
{
package: 'flatpickr',
context: 'dist',
from: 'l10n/*.js',
},
{
package: 'flatpickr',
context: 'dist',
from: 'themes/*.css',
},
{
package: 'select2',
context: 'dist',
from: 'js/i18n/*.js',
},
{
package: 'tinymce',
from: 'skins/**/*',
},
{
package: 'tinymce-i18n',
from: 'langs6/*.js',
},
// SCSS files
{
package: 'bootstrap',
from: 'scss/vendor/_rfs.scss',
to: scssOutputPath,
},
{
package: 'select2',
from: 'src/scss/**/*.scss',
to: scssOutputPath,
},
{
package: 'tinymce',
from: 'skins/ui/oxide*/skin.css',
to: scssOutputPath,
},
{
package: 'swagger-ui-dist',
from: 'oauth2-redirect.html'
}
];
const copyPatterns = [];
// See https://github.com/glpi-project/glpi/issues/17745
copyPatterns.push({
from: path.resolve(__dirname, 'node_modules/flatpickr/dist/l10n/cat.js'),
to: path.resolve(__dirname, libOutputPath + '/flatpickr/l10n/ca.js'),
toType: 'file',
});
for (let s = 0; s < filesToCopy.length; s++) {
const specs = filesToCopy[s];
const to = (specs.to || libOutputPath) + '/' + specs.package.replace(/^@/, ''); // remove leading @ in case of prefixed package
let context = 'node_modules/' + specs.package;
if (Object.prototype.hasOwnProperty.call(specs, 'context')) {
context += '/' + specs.context;
}
const copyParams = {
context: path.resolve(__dirname, context),
from: specs.from,
to: path.resolve(__dirname, to),
toType: 'dir',
};
if (Object.prototype.hasOwnProperty.call(specs, 'ignore')) {
copyParams.ignore = specs.ignore;
}
copyPatterns.push(copyParams);
}
config.plugins.push(new CopyWebpackPlugin({patterns:copyPatterns}));
module.exports = config;