From bfe5ce3addf0ff62c8dbf21ea35df05aa5b7ef8d Mon Sep 17 00:00:00 2001 From: Brennan Date: Fri, 28 Jan 2022 11:20:51 -0800 Subject: [PATCH 1/2] [SignalR] Only minify .min.js files --- src/SignalR/clients/ts/signalr/package.json | 5 +---- src/SignalR/clients/ts/signalr/webpack.config.js | 4 ++-- src/SignalR/clients/ts/webpack.config.base.js | 13 +++++++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/SignalR/clients/ts/signalr/package.json b/src/SignalR/clients/ts/signalr/package.json index d259ff9d180a..682c900106af 100644 --- a/src/SignalR/clients/ts/signalr/package.json +++ b/src/SignalR/clients/ts/signalr/package.json @@ -16,15 +16,12 @@ "preclean": "cd ../common && yarn install --mutex network --frozen-lockfile", "clean": "node ../common/node_modules/rimraf/bin.js ./dist", "prebuild": "yarn run clean && yarn install --mutex network --frozen-lockfile", - "build": "yarn run build:lint && yarn run build:esm && yarn run build:cjs && yarn run build:browser && yarn run build:webworker && yarn run build:uglify", + "build": "yarn run build:lint && yarn run build:esm && yarn run build:cjs && yarn run build:browser && yarn run build:webworker", "build:lint": "node ../common/node_modules/eslint/bin/eslint ./src --ext .ts --resolve-plugins-relative-to ../common", "build:esm": "node ../common/node_modules/typescript/bin/tsc --project ./tsconfig.json --module es2015 --outDir ./dist/esm -d && node ./build/process-dts.js", "build:cjs": "node ../common/node_modules/typescript/bin/tsc --project ./tsconfig.json --module commonjs --outDir ./dist/cjs", "build:browser": "node ../common/node_modules/webpack-cli/bin/cli.js", "build:webworker": "node ../common/node_modules/webpack-cli/bin/cli.js --env platform=webworker", - "build:uglify": "yarn run build:uglify:browser && yarn run build:uglify:webworker", - "build:uglify:browser": "node ../common/node_modules/terser/bin/terser -m -c --ecma 2019 --module --source-map \"url='signalr.min.js.map',content='./dist/browser/signalr.js.map'\" --comments -o ./dist/browser/signalr.min.js ./dist/browser/signalr.js", - "build:uglify:webworker": "node ../common/node_modules/terser/bin/terser -m -c --ecma 2019 --module--source-map \"url='signalr.min.js.map',content='./dist/webworker/signalr.js.map'\" --comments -o ./dist/webworker/signalr.min.js ./dist/webworker/signalr.js", "prepack": "node ../build/embed-version.js", "test": "echo \"Run 'yarn test' in the 'clients/ts' folder to test this package\" && exit 1" }, diff --git a/src/SignalR/clients/ts/signalr/webpack.config.js b/src/SignalR/clients/ts/signalr/webpack.config.js index 47346a404fc1..5852b6cb8417 100644 --- a/src/SignalR/clients/ts/signalr/webpack.config.js +++ b/src/SignalR/clients/ts/signalr/webpack.config.js @@ -4,8 +4,8 @@ const baseConfig = require("../webpack.config.base"); module.exports = env => baseConfig(__dirname, "signalr", { // These are only used in Node environments // so we tell webpack not to pull them in for the browser - target: env && env.platform ? env.platform : undefined, - platformDist: env && env.platform ? env.platform : undefined, + target: env && env.platform ? env.platform : undefined, + platformDist: env && env.platform ? env.platform : undefined, externals: [ "ws", "eventsource", diff --git a/src/SignalR/clients/ts/webpack.config.base.js b/src/SignalR/clients/ts/webpack.config.base.js index 7d8ca7287c1a..0bf3a9d0ee1b 100644 --- a/src/SignalR/clients/ts/webpack.config.base.js +++ b/src/SignalR/clients/ts/webpack.config.base.js @@ -11,8 +11,8 @@ module.exports = function (modulePath, browserBaseName, options) { options = options || {}; - return { - entry: path.resolve(modulePath, "src", "browser-index.ts"), + const webpackOptions = { + entry: {}, mode: "none", node: { global: true @@ -45,7 +45,7 @@ module.exports = function (modulePath, browserBaseName, options) { } }, output: { - filename: `${browserBaseName}.js`, + filename: '[name].js', path: path.resolve(modulePath, "dist", options.platformDist || "browser"), library: { root: pkg.umd_name.split("."), @@ -55,7 +55,7 @@ module.exports = function (modulePath, browserBaseName, options) { }, plugins: [ new webpack.SourceMapDevToolPlugin({ - filename: `${browserBaseName}.js.map`, + filename: '[name].js.map', moduleFilenameTemplate(info) { let resourcePath = info.resourcePath; @@ -88,6 +88,7 @@ module.exports = function (modulePath, browserBaseName, options) { innerGraph: true, minimize: true, minimizer: [new TerserJsPlugin({ + include: /\.min\.js$/, terserOptions: { ecma: 2019, compress: {}, @@ -114,4 +115,8 @@ module.exports = function (modulePath, browserBaseName, options) { }, externals: options.externals, }; + + webpackOptions.entry[browserBaseName] = path.resolve(modulePath, "src", "browser-index.ts"); + webpackOptions.entry[`${browserBaseName}.min`] = path.resolve(modulePath, "src", "browser-index.ts"); + return webpackOptions; } \ No newline at end of file From 4963b907826d567862f77e783ac7a68218187bc7 Mon Sep 17 00:00:00 2001 From: Brennan Date: Fri, 28 Jan 2022 17:10:45 -0800 Subject: [PATCH 2/2] test --- .../ts/signalr/tests/OutputSize.test.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/SignalR/clients/ts/signalr/tests/OutputSize.test.ts diff --git a/src/SignalR/clients/ts/signalr/tests/OutputSize.test.ts b/src/SignalR/clients/ts/signalr/tests/OutputSize.test.ts new file mode 100644 index 000000000000..67a4f3d741bc --- /dev/null +++ b/src/SignalR/clients/ts/signalr/tests/OutputSize.test.ts @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +import * as _fs from "fs"; +import * as path from "path"; +import { promisify } from "util"; + +const fs = { + stat: promisify(_fs.stat), +}; + +// Regression tests to make sure we are building the .min and non .min js files differently. +// It's ok to have to modify these values as long as we know why they changed. + +describe("Output files", () => { + it(".min.js file is small", async () => { + const size = await (await fs.stat(path.resolve(__dirname, "..", "dist/browser/signalr.min.js"))).size; + expect(size).toBeLessThan(45000); + }); + + it("non .min.js file is big", async () => { + const size = await (await fs.stat(path.resolve(__dirname, "..", "dist/browser/signalr.js"))).size; + expect(size).toBeGreaterThan(120000); + }); +}); \ No newline at end of file