forked from ProtonMail/WebClients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
101 lines (88 loc) · 3.41 KB
/
webpack.config.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
import HtmlWebpackPlugin from 'html-webpack-plugin';
import template from 'lodash.template';
import { HrefLang } from 'proton-account/pages/interface';
import { getPages } from 'proton-account/pages/pages';
import { Parameters } from 'proton-account/src/pages/interface';
import { Configuration } from 'webpack';
import 'webpack-dev-server';
import getConfig from '@proton/pack/webpack.config';
import CopyIndexHtmlWebpackPlugin from '@proton/pack/webpack/copy-index-html-webpack-plugin';
const getTemplateParameters = (
originalTemplateParameters: any,
hreflangs: HrefLang[],
shortLocalizedPathname: string,
parameters: Parameters & { pathname: string }
) => {
let url = originalTemplateParameters.url;
const origin = url.replace(/\/$/, '');
if (parameters.pathname) {
url = `${origin}${parameters.pathname}`;
}
return {
...originalTemplateParameters,
...parameters,
url,
hreflangs: hreflangs.map(({ hreflang, pathname }) => {
return {
hreflang,
href: `${origin}${pathname}${parameters.pathname.replace(shortLocalizedPathname, '')}`,
};
}),
};
};
const result = async (env: any): Promise<Configuration> => {
const pagesPromise = getPages();
const config = getConfig(env);
const plugins = config.plugins || [];
config.plugins = plugins;
const htmlPlugin = plugins.find((plugin): plugin is HtmlWebpackPlugin => {
return plugin instanceof HtmlWebpackPlugin;
});
if (!htmlPlugin) {
throw new Error('Missing html plugin');
}
const rewrites: any[] = [];
// @ts-ignore
config.devServer.historyApiFallback.rewrites = rewrites;
const originalTemplateParameters = htmlPlugin.userOptions.templateParameters as { [key: string]: any };
const { pages, hreflangs } = await pagesPromise;
pages.forEach(({ rewrite }) => {
rewrites.push(rewrite);
});
plugins.push(
new CopyIndexHtmlWebpackPlugin((source) => {
const compiled = template(
source,
// Note: We use two different template interpolations, due to <%= require('./favicon.svg' %>, which requires
// a lot more effort to support properly, so we use the default loader for that and our own loader for this.
{
evaluate: /\{\{([\s\S]+?)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
escape: /\{\{-([\s\S]+?)\}\}/g,
},
undefined
);
const index = {
name: 'index.html',
data: compiled(
getTemplateParameters(originalTemplateParameters, hreflangs, '', {
title: originalTemplateParameters.appName,
description: originalTemplateParameters.description,
pathname: '/',
})
),
};
const rest = pages.map(({ shortLocalizedPathname, filename, parameters }) => {
return {
name: filename,
data: compiled(
getTemplateParameters(originalTemplateParameters, hreflangs, shortLocalizedPathname, parameters)
),
};
});
return [index, ...rest];
})
);
return config;
};
export default result;