2019-08-08 19:39:06 +03:00
|
|
|
/*
|
2022-01-17 14:07:46 +02:00
|
|
|
* Copyright © 2016-2022 The Thingsboard Authors
|
2019-08-08 19:39:06 +03:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2020-02-04 15:14:17 +02:00
|
|
|
const CompressionPlugin = require("compression-webpack-plugin");
|
2021-12-06 12:54:48 +02:00
|
|
|
const JavaScriptOptimizerPlugin = require("@angular-devkit/build-angular/src/webpack/plugins/javascript-optimizer-plugin").JavaScriptOptimizerPlugin;
|
2020-02-04 15:14:17 +02:00
|
|
|
const webpack = require("webpack");
|
|
|
|
|
const dirTree = require("directory-tree");
|
2021-05-26 20:10:56 +03:00
|
|
|
const ngWebpack = require('@ngtools/webpack');
|
2022-05-11 15:58:15 +03:00
|
|
|
const keysTransformer = require('ts-transformer-keys/transformer').default;
|
2019-08-08 19:39:06 +03:00
|
|
|
|
|
|
|
|
var langs = [];
|
|
|
|
|
|
2020-02-04 15:14:17 +02:00
|
|
|
dirTree("./src/assets/locale/", {extensions: /\.json$/}, (item) => {
|
2019-08-08 19:39:06 +03:00
|
|
|
/* It is expected what the name of a locale file has the following format: */
|
|
|
|
|
/* 'locale.constant-LANG_CODE[_REGION_CODE].json', e.g. locale.constant-es.json or locale.constant-zh_CN.json*/
|
2020-02-04 15:14:17 +02:00
|
|
|
langs.push(item.name.slice(item.name.lastIndexOf("-") + 1, -5));
|
2019-08-08 19:39:06 +03:00
|
|
|
});
|
|
|
|
|
|
2020-12-30 17:13:01 +02:00
|
|
|
module.exports = (config, options) => {
|
|
|
|
|
config.plugins.push(
|
2019-08-08 19:39:06 +03:00
|
|
|
new webpack.DefinePlugin({
|
2020-02-04 15:14:17 +02:00
|
|
|
TB_VERSION: JSON.stringify(require("./package.json").version),
|
|
|
|
|
SUPPORTED_LANGS: JSON.stringify(langs),
|
2020-12-30 17:13:01 +02:00
|
|
|
})
|
|
|
|
|
);
|
2021-01-05 11:37:05 +02:00
|
|
|
config.plugins.push(
|
|
|
|
|
new webpack.ProvidePlugin(
|
|
|
|
|
{
|
|
|
|
|
$: "jquery"
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
2020-12-30 17:13:01 +02:00
|
|
|
config.plugins.push(
|
2019-08-08 19:39:06 +03:00
|
|
|
new CompressionPlugin({
|
2020-10-08 11:45:50 +03:00
|
|
|
filename: "[path][base].gz[query]",
|
2019-08-08 19:39:06 +03:00
|
|
|
algorithm: "gzip",
|
2019-09-06 20:17:45 +03:00
|
|
|
test: /\.js$|\.css$|\.html$|\.svg?.+$|\.jpg$|\.ttf?.+$|\.woff?.+$|\.eot?.+$|\.json$/,
|
2019-08-08 19:39:06 +03:00
|
|
|
threshold: 10240,
|
|
|
|
|
minRatio: 0.8,
|
2020-02-04 15:14:17 +02:00
|
|
|
deleteOriginalAssets: false,
|
2020-12-30 17:13:01 +02:00
|
|
|
})
|
|
|
|
|
);
|
2021-01-05 11:37:05 +02:00
|
|
|
config.plugins.push(
|
2021-12-06 12:54:48 +02:00
|
|
|
new webpack.IgnorePlugin({
|
|
|
|
|
resourceRegExp: /^\.\/locale$/,
|
|
|
|
|
contextRegExp: /moment$/,
|
|
|
|
|
})
|
2021-01-05 11:37:05 +02:00
|
|
|
);
|
2020-12-30 17:13:01 +02:00
|
|
|
|
2022-05-11 15:58:15 +03:00
|
|
|
const index = config.plugins.findIndex(p => p instanceof ngWebpack.ivy.AngularWebpackPlugin || p instanceof ngWebpack.AngularWebpackPlugin);
|
|
|
|
|
const angularWebpackPlugin = config.plugins[index];
|
|
|
|
|
|
|
|
|
|
addTransformerToAngularWebpackPlugin(angularWebpackPlugin, keysTransformer);
|
|
|
|
|
|
2020-12-30 17:13:01 +02:00
|
|
|
if (config.mode === 'production') {
|
2022-05-11 15:58:15 +03:00
|
|
|
const angularCompilerOptions = angularWebpackPlugin.pluginOptions;
|
2020-12-30 17:13:01 +02:00
|
|
|
angularCompilerOptions.emitClassMetadata = true;
|
|
|
|
|
angularCompilerOptions.emitNgModuleScope = true;
|
|
|
|
|
config.plugins.splice(index, 1);
|
2021-05-26 20:10:56 +03:00
|
|
|
config.plugins.push(new ngWebpack.ivy.AngularWebpackPlugin(angularCompilerOptions));
|
2021-12-06 12:54:48 +02:00
|
|
|
const javascriptOptimizerOptions = config.optimization.minimizer[1].options;
|
|
|
|
|
delete javascriptOptimizerOptions.define.ngJitMode;
|
2020-12-30 17:13:01 +02:00
|
|
|
config.optimization.minimizer.splice(1, 1);
|
2021-12-06 12:54:48 +02:00
|
|
|
config.optimization.minimizer.push(new JavaScriptOptimizerPlugin(javascriptOptimizerOptions));
|
2020-12-30 17:13:01 +02:00
|
|
|
}
|
|
|
|
|
return config;
|
2019-08-08 19:39:06 +03:00
|
|
|
};
|
2022-05-11 15:58:15 +03:00
|
|
|
|
|
|
|
|
function addTransformerToAngularWebpackPlugin(plugin, transformer) {
|
|
|
|
|
const originalCreateFileEmitter = plugin.createFileEmitter; // private method
|
|
|
|
|
plugin.createFileEmitter = function (program, transformers, getExtraDependencies, onAfterEmit) {
|
|
|
|
|
if (!transformers) {
|
|
|
|
|
transformers = {};
|
|
|
|
|
}
|
|
|
|
|
if (!transformers.before) {
|
|
|
|
|
transformers = { before: [] };
|
|
|
|
|
}
|
|
|
|
|
transformers.before.push(transformer(program.getProgram()));
|
|
|
|
|
return originalCreateFileEmitter.apply(plugin, [program, transformers, getExtraDependencies, onAfterEmit]);
|
|
|
|
|
};
|
|
|
|
|
}
|