pwgen-for-bios/webpack.base.js
Slava Bacherikov b99bdd7c81 Update modules and dependencies, change to jsbi
* Drop gulp support
* Migrate long.js to JSBI library (for long math)
* Update karma targets
2020-05-29 20:25:50 +03:00

119 lines
3.7 KiB
JavaScript

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { DefinePlugin } = require('webpack');
const { exec } = require('child_process');
const Terser = require('terser-webpack-plugin');
const fs = require('fs');
class VersionInfoPlugin {
constructor(options = {}) {
if (options.filename == void 0) {
throw new Error('[VersionInfoPlugin] filename should be set');
}
this.options = options;
}
apply(compiler) {
const plugin = { name: 'VersionInfoPlugin' }
compiler.hooks.afterEmit.tapAsync(plugin, (compilation, callback) => {
exec('git describe --tags --always', (error, stdout, stderr) => {
if (error) {
compilation.errors.push(error);
callback();
return;
}
var version_info = `version: ${stdout.trim()}\ntime: ${new Date().toISOString()}\n`;
if (process.env.TRAVIS) {
version_info += `build id: TRAVIS ${process.env.TRAVIS_JOB_NUMBER} (${process.env.TRAVIS_BUILD_ID})\n`;
}
const filename = path.join(compiler.options.output.path, this.options.filename);
fs.writeFile(filename, version_info, (err) => {
if (err) {
compilation.errors.push(err);
}
callback();
})
});
});
}
}
function getWebpackConfig(production, gtag) {
var webpackMode;
production = (typeof(production) === 'undefined') ? false : production;
gtag = (typeof(gtag) === 'undefined') ? "" : gtag;
var plugins = [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([
{from: 'assets/bootstrap.min.css', to: 'assets/'},
{from: 'assets/images/favicon.ico', to: 'favicon.ico'},
{from: 'assets/images/', to: 'assets/images/'},
]),
new DefinePlugin({
GOOGLE_ANALYTICS_TAG: JSON.stringify(gtag)
}),
new HtmlWebpackPlugin({
minify: {
collapseWhitespace: true,
conservativeCollapse: true,
removeComments: true,
},
template: 'html/index.html',
}),
new VersionInfoPlugin({filename: 'version-info.txt'})
];
if (production) {
webpackMode = "production";
} else {
webpackMode = "development";
}
return {
entry: "./src/ui.ts",
output: {
filename: "assets/bundle.[hash].js",
path: path.join(__dirname, "dist")
},
plugins: plugins,
optimization: {
concatenateModules: false,
minimizer: [
new Terser({})
]
},
devtool: "source-map",
devServer: {
contentBase: path.join(__dirname, "dist"),
port: 9000
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
mode: webpackMode,
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [{loader: 'ts-loader', options: {transpileOnly: false}}]
},
{
test: /\.m?js$/,
exclude: /node_modules/,
use: [{loader: 'babel-loader', options: {presets: ['@babel/preset-env']}}]
}
]
}
}
}
exports.getWebpackConfig = getWebpackConfig