From 233e16799dc351738ed00783f371683e440a3259 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Fri, 7 Aug 2020 16:01:48 -0500 Subject: [PATCH] Webpack copies the NOTICE-js to the output folder and puts a header with url to the notice in every webpack file --- NOTICE-js | 29 +++++++++++++++++++++ config/webpack/environment.js | 47 ++++++++++++++++++++++++++++++++++- config/webpack/production.js | 5 +--- package.json | 1 + yarn.lock | 5 ++++ 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/NOTICE-js b/NOTICE-js index 8b4ec3bd..3585650a 100644 --- a/NOTICE-js +++ b/NOTICE-js @@ -12296,6 +12296,35 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------ + +** object-hash; version 2.0.3 -- https://github.com/puleos/object-hash +Copyright (c) 2014 object-hash contributors + +The MIT License (MIT) + +Copyright (c) 2014 object-hash contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + + ------ ** v8-compile-cache; version 2.0.3 -- https://github.com/zertosh/v8-compile-cache#readme diff --git a/config/webpack/environment.js b/config/webpack/environment.js index b1a4dbd3..3a218612 100644 --- a/config/webpack/environment.js +++ b/config/webpack/environment.js @@ -1,7 +1,35 @@ -const { environment } = require('@rails/webpacker') +const { environment, config } = require('@rails/webpacker') const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin"); +const webpack = require('webpack'); const path = require("path"); const erb = require('./loaders/erb') +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; + +} + + environment.loaders.prepend('erb', erb) environment.plugins.append( @@ -13,4 +41,21 @@ environment.plugins.append( async: false, }) ); + +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; +} + + module.exports = environment diff --git a/config/webpack/production.js b/config/webpack/production.js index 3e3269a6..0f8a087e 100644 --- a/config/webpack/production.js +++ b/config/webpack/production.js @@ -1,7 +1,5 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'production' - const environment = require('./environment') -const modifyTerserPlugin = require('./plugins/modifyTerserPlugin'); environment.splitChunks((config) => { const excludeDonateButtonFromSplit = { optimization: @@ -21,7 +19,6 @@ environment.splitChunks((config) => { return Object.assign({}, config, excludeDonateButtonFromSplit) }) -// we don't want terser to print out license headers, we'll handle that ourselves -environment.config.optimization.minimizer[0].options.extractComments = false; + module.exports = environment.toWebpackConfig() diff --git a/package.json b/package.json index 898437de..ec12620f 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "jsdom": "^11.10.0", "mini-css-extract-plugin": "^0.9.0", "node-sass": "^4.12.0", + "object-hash": "^2.0.3", "phantomjs-prebuilt": "^2.1.16", "react-test-renderer": "^16.13.1", "resolve-url-loader": "^2.3.0", diff --git a/yarn.lock b/yarn.lock index e1990c56..8a4d44f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11145,6 +11145,11 @@ object-copy@^0.1.0: define-property "^0.2.5" kind-of "^3.0.3" +object-hash@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/object-hash/-/object-hash-2.0.3.tgz#d12db044e03cd2ca3d77c0570d87225b02e1e6ea" + integrity sha512-JPKn0GMu+Fa3zt3Bmr66JhokJU5BaNBIh4ZeTlaCBzrBsOeXzwcKKAK1tbLiPKgvwmPXsDvvLHoWh5Bm7ofIYg== + object-inspect@^1.7.0: version "1.8.0" resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.8.0.tgz#df807e5ecf53a609cc6bfe93eac3cc7be5b3a9d0"