2020-08-07 21:01:48 +00:00
|
|
|
const { environment, config } = require('@rails/webpacker')
|
2020-05-27 22:05:25 +00:00
|
|
|
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
2020-08-07 21:01:48 +00:00
|
|
|
const webpack = require('webpack');
|
2020-05-27 22:05:25 +00:00
|
|
|
const path = require("path");
|
2019-11-18 21:51:37 +00:00
|
|
|
const erb = require('./loaders/erb')
|
2020-08-07 21:01:48 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const hash = require('object-hash')
|
|
|
|
|
|
|
|
function getTerser() {
|
|
|
|
if(environment.config &&
|
|
|
|
environment.config.optimization &&
|
|
|
|
environment.config.optimization.minimizer &&
|
|
|
|
environment.config.optimization.minimizer instanceof Array &&
|
|
|
|
environment.config.optimization.minimizer.length === 1)
|
|
|
|
return environment.config.optimization.minimizer[0]
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function outputLicenseFile(file, outputDir) {
|
|
|
|
const contents = fs.readFileSync(file);
|
|
|
|
const name = path.basename(file) + "-" + hash(contents) + ".txt";
|
|
|
|
if (!fs.existsSync(outputDir)) {
|
|
|
|
fs.mkdirSync(outputDir, {recursive: true});
|
|
|
|
}
|
|
|
|
fs.copyFileSync(file, path.join(outputDir, name));
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-05 22:39:13 +00:00
|
|
|
|
2019-11-15 22:28:55 +00:00
|
|
|
environment.loaders.prepend('erb', erb)
|
2020-05-27 22:05:25 +00:00
|
|
|
environment.plugins.append(
|
2020-06-23 21:53:28 +00:00
|
|
|
"ForkTsCheckerWebpackPlugin",
|
|
|
|
new ForkTsCheckerWebpackPlugin({
|
|
|
|
typescript: {
|
2020-05-27 22:05:25 +00:00
|
|
|
tsconfig: path.resolve(__dirname, "../../tsconfig.json"),
|
2020-06-23 21:53:28 +00:00
|
|
|
},
|
|
|
|
async: false,
|
|
|
|
})
|
|
|
|
);
|
2020-08-07 21:01:48 +00:00
|
|
|
|
|
|
|
environment.plugins.prepend("BannerPlugin", new webpack.BannerPlugin(
|
|
|
|
{banner: `@hlicense License information is available at ${config.publicPath}${outputLicenseFile('NOTICE-js', config.outputPath)}`,
|
|
|
|
entryOnly: false}))
|
|
|
|
|
|
|
|
|
|
|
|
const terser = getTerser()
|
|
|
|
if (terser) {
|
|
|
|
terser.options.terserOptions = terser.options.terserOptions || {}
|
|
|
|
terser.options.terserOptions.output = terser.options.terserOptions.output || {}
|
|
|
|
terser.options.terserOptions.output.comments = /@hlicense/i
|
|
|
|
|
|
|
|
// we don't want terser to print out license headers, we'll handle that ourselves
|
|
|
|
terser.options.extractComments = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-05 22:39:13 +00:00
|
|
|
module.exports = environment
|