forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebpackConfigurationUpdater.ts
More file actions
137 lines (123 loc) · 4.23 KB
/
WebpackConfigurationUpdater.ts
File metadata and controls
137 lines (123 loc) · 4.23 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import * as Webpack from 'webpack';
import * as SetPublicPathPluginPackageType from '@rushstack/set-webpack-public-path-plugin';
import { Constants } from './utilities/Constants';
import { LocalizationPlugin } from './LocalizationPlugin';
import { ILocLoaderOptions } from './loaders/LocLoader';
import { IBaseLoaderOptions } from './loaders/LoaderFactory';
export interface IWebpackConfigurationUpdaterOptions {
pluginInstance: LocalizationPlugin;
configuration: Webpack.Configuration;
filesToIgnore: Set<string>;
localeNameOrPlaceholder: string;
}
export class WebpackConfigurationUpdater {
public static amendWebpackConfigurationForMultiLocale(options: IWebpackConfigurationUpdaterOptions): void {
const loader: string = path.resolve(__dirname, 'loaders', 'LocLoader.js');
const loaderOptions: ILocLoaderOptions = {
pluginInstance: options.pluginInstance
};
WebpackConfigurationUpdater._addLoadersForLocFiles(options, loader, loaderOptions);
WebpackConfigurationUpdater._tryUpdateLocaleTokenInPublicPathPlugin(options);
}
public static amendWebpackConfigurationForInPlaceLocFiles(options: IWebpackConfigurationUpdaterOptions): void {
const loader: string = path.resolve(__dirname, 'loaders', 'InPlaceLocFileLoader.js');
const loaderOptions: IBaseLoaderOptions = {}
WebpackConfigurationUpdater._addRulesToConfiguration(
options.configuration,
[
{
test: Constants.LOC_JSON_REGEX,
use: [{
loader: loader,
options: loaderOptions
}]
},
{
test: Constants.RESX_REGEX,
use: [{
loader: loader,
options: loaderOptions
}],
type: 'json'
}
]
);
}
private static _tryUpdateLocaleTokenInPublicPathPlugin(options: IWebpackConfigurationUpdaterOptions): void {
let setPublicPathPlugin: typeof SetPublicPathPluginPackageType.SetPublicPathPlugin | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pluginPackage: typeof SetPublicPathPluginPackageType = require('@rushstack/set-webpack-public-path-plugin');
setPublicPathPlugin = pluginPackage.SetPublicPathPlugin;
} catch (e) {
// public path plugin isn't present - ignore
}
if (setPublicPathPlugin && options.configuration.plugins) {
for (const plugin of options.configuration.plugins) {
if (plugin instanceof setPublicPathPlugin) {
if (
plugin.options &&
plugin.options.scriptName &&
plugin.options.scriptName.isTokenized &&
plugin.options.scriptName.name
) {
plugin.options.scriptName.name = plugin.options.scriptName.name.replace(
/\[locale\]/g,
options.localeNameOrPlaceholder
);
}
}
}
}
}
private static _addLoadersForLocFiles(
options: IWebpackConfigurationUpdaterOptions,
loader: string,
loaderOptions: IBaseLoaderOptions
): void {
WebpackConfigurationUpdater._addRulesToConfiguration(
options.configuration,
[
{
test: {
and: [
(filePath: string) => !options.filesToIgnore.has(filePath),
Constants.LOC_JSON_REGEX
]
},
use: [{
loader: loader,
options: loaderOptions
}]
},
{
test: {
and: [
(filePath: string) => !options.filesToIgnore.has(filePath),
Constants.RESX_REGEX
]
},
use: [{
loader: loader,
options: loaderOptions
}],
type: 'json'
}
]
);
}
private static _addRulesToConfiguration(configuration: Webpack.Configuration, rules: Webpack.RuleSetRule[]): void {
if (!configuration.module) {
configuration.module = {
rules: []
};
}
if (!configuration.module.rules) {
configuration.module.rules = [];
}
configuration.module.rules.push(...rules);
}
}