-
Notifications
You must be signed in to change notification settings - Fork 1
/
carbonio.webpack.ts
144 lines (135 loc) · 3.69 KB
/
carbonio.webpack.ts
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
/*
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { execSync } from 'child_process';
import CopyPlugin from 'copy-webpack-plugin';
import dotenv from 'dotenv';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import path from 'path';
import { ContextReplacementPlugin, DefinePlugin } from 'webpack';
import type { WebpackConfiguration } from 'webpack-cli';
import { SUPPORTED_LOCALES } from './src/constants/locales';
dotenv.config();
const commitHash = execSync('git rev-parse HEAD').toString().trim();
const baseStaticPath = `/static/iris/carbonio-shell-ui/${commitHash}/`;
const supportedLocalesList = Object.values(SUPPORTED_LOCALES);
// these have to match with the ones available in date-fns/locale
const supportedLocalesDateFns = supportedLocalesList.map(
(locale) => ('dateFnsLocale' in locale && locale.dateFnsLocale) || locale.value
);
const tinymceLocales = supportedLocalesList.map(
(locale) => ('tinymceLocale' in locale && locale.tinymceLocale) || locale.value
);
const configFn = (
initialConf: WebpackConfiguration,
pkg: string,
options: { host: string; port: number },
mode: 'development' | 'production'
): WebpackConfiguration => {
const conf: WebpackConfiguration = { ...initialConf };
const server = `https://${options.host}`;
const root = 'carbonio';
conf.entry = {
index: path.resolve(process.cwd(), 'src', 'index.tsx')
};
conf.output = {
...conf.output,
filename: mode === 'development' ? 'zapp-shell.bundle.js' : '[name].[chunkhash:8].js'
};
const extensions = conf.resolve?.extensions ?? [];
extensions.push('.d.ts');
conf.resolve = {
...conf.resolve,
extensions
};
conf.plugins = conf.plugins ?? [];
conf.plugins.push(
new CopyPlugin({
patterns: [
{
from: 'assets/',
to: ''
},
{
from: `plugins/help/js/i18n/**/(${tinymceLocales.join('|')}).js`,
to: '',
context: 'node_modules/tinymce/'
}
]
}),
new DefinePlugin({
COMMIT_ID: JSON.stringify(commitHash.toString().trim()),
BASE_PATH: JSON.stringify(baseStaticPath),
POSTHOG_API_KEY: JSON.stringify(process.env.POSTHOG_API_KEY),
POSTHOG_API_HOST: JSON.stringify(process.env.POSTHOG_API_HOST)
}),
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(process.cwd(), 'src', 'index.template.html'),
chunks: ['index'],
BASE_PATH: baseStaticPath
}),
new HtmlWebpackPlugin({
inject: false,
template: path.resolve(process.cwd(), 'commit.template'),
filename: 'commit',
COMMIT_ID: commitHash
}),
new ContextReplacementPlugin(
/^date-fns[/\\]locale$/,
new RegExp(`\\.[/\\\\](${supportedLocalesDateFns.join('|')})[/\\\\]index\\.js$`)
)
);
conf.devServer = {
port: options.port,
historyApiFallback: {
index: `${baseStaticPath}/index.html`,
rewrites: [
{
from: new RegExp(`/${root}/*`),
to: `${baseStaticPath}/index.html`
}
]
},
server: 'https',
open: [`/${root}/`],
proxy: [
{
context: ['/static/login/**'],
target: server,
secure: false,
cookieDomainRewrite: {
'*': server,
[server]: `localhost:${options.port}`
}
},
{
context: ['!/static/iris/carbonio-shell-ui/**/*', `!/${root}/`, `!/${root}/**/*`],
target: server,
secure: false,
logLevel: 'debug',
cookieDomainRewrite: {
'*': server,
[server]: `localhost:${options.port}`
}
}
]
};
conf.externals = {};
const rules: NonNullable<WebpackConfiguration['module']>['rules'] = conf.module?.rules ?? [];
rules.push({
test: /\.(woff(2)?|ttf|eot)$/,
type: 'asset/resource',
generator: {
filename: './files/[name][ext]'
}
});
conf.module = {
...conf.module,
rules
};
return conf;
};
export = configFn;