From 8cd1e4c51958a50c03c67a836578a23e416cdf6d Mon Sep 17 00:00:00 2001 From: Ludovico Fischer <43557+ludofischer@users.noreply.github.com> Date: Tue, 22 Feb 2022 11:34:41 +0100 Subject: [PATCH] chore: embed strip-ansi dependency (#4306) --- client-src/index.js | 2 +- client-src/modules/strip-ansi/index.js | 3 --- client-src/utils/stripAnsi.js | 26 +++++++++++++++++++ client-src/webpack.config.js | 7 ----- package-lock.json | 7 +++-- package.json | 1 - test/client/index.test.js | 1 - .../multi-compiler.test.js.snap.webpack5 | 12 ++++----- 8 files changed, 38 insertions(+), 21 deletions(-) delete mode 100644 client-src/modules/strip-ansi/index.js create mode 100644 client-src/utils/stripAnsi.js diff --git a/client-src/index.js b/client-src/index.js index dc85fdcff0..6f361db83b 100644 --- a/client-src/index.js +++ b/client-src/index.js @@ -1,7 +1,7 @@ /* global __resourceQuery, __webpack_hash__ */ /// import webpackHotLog from "webpack/hot/log.js"; -import stripAnsi from "./modules/strip-ansi/index.js"; +import stripAnsi from "./utils/stripAnsi.js"; import parseURL from "./utils/parseURL.js"; import socket from "./socket.js"; import { formatProblem, show, hide } from "./overlay.js"; diff --git a/client-src/modules/strip-ansi/index.js b/client-src/modules/strip-ansi/index.js deleted file mode 100644 index 011c0e0053..0000000000 --- a/client-src/modules/strip-ansi/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import stripAnsi from "strip-ansi"; - -export default stripAnsi; diff --git a/client-src/utils/stripAnsi.js b/client-src/utils/stripAnsi.js new file mode 100644 index 0000000000..45792950f9 --- /dev/null +++ b/client-src/utils/stripAnsi.js @@ -0,0 +1,26 @@ +const ansiRegex = new RegExp( + [ + "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)", + "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", + ].join("|"), + "g" +); + +/** + * + * Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) from a string. + * Adapted from code originally released by Sindre Sorhus + * Licensed the MIT License + * + * @param {string} string + * @return {string} + */ +function stripAnsi(string) { + if (typeof string !== "string") { + throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``); + } + + return string.replace(ansiRegex, ""); +} + +export default stripAnsi; diff --git a/client-src/webpack.config.js b/client-src/webpack.config.js index 6242831a94..e85a93cee9 100644 --- a/client-src/webpack.config.js +++ b/client-src/webpack.config.js @@ -78,13 +78,6 @@ module.exports = [ ), ], }), - merge(baseForModules, { - entry: path.join(__dirname, "modules/strip-ansi/index.js"), - output: { - // @ts-ignore - filename: "strip-ansi/index.js", - }, - }), merge(baseForModules, { entry: path.join(__dirname, "modules/sockjs-client/index.js"), output: { diff --git a/package-lock.json b/package-lock.json index 7f1528c7a7..8112789608 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,7 +36,6 @@ "serve-index": "^1.9.1", "sockjs": "^0.3.21", "spdy": "^4.0.2", - "strip-ansi": "^7.0.0", "webpack-dev-middleware": "^5.3.1", "ws": "^8.4.2" }, @@ -14190,6 +14189,7 @@ "version": "7.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.0.1.tgz", "integrity": "sha512-cXNxvT8dFNRVfhVME3JAe98mkXDYN2O1l7jmcwMnOslDeESg1rF/OZMtK0nRAhiari1unG5cD4jG3rapUAkLbw==", + "dev": true, "dependencies": { "ansi-regex": "^6.0.1" }, @@ -14214,6 +14214,7 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.0.1.tgz", "integrity": "sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==", + "dev": true, "engines": { "node": ">=12" }, @@ -26479,6 +26480,7 @@ "version": "7.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.0.1.tgz", "integrity": "sha512-cXNxvT8dFNRVfhVME3JAe98mkXDYN2O1l7jmcwMnOslDeESg1rF/OZMtK0nRAhiari1unG5cD4jG3rapUAkLbw==", + "dev": true, "requires": { "ansi-regex": "^6.0.1" }, @@ -26486,7 +26488,8 @@ "ansi-regex": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.0.1.tgz", - "integrity": "sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==" + "integrity": "sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==", + "dev": true } } }, diff --git a/package.json b/package.json index 6e3200b747..5dde77d985 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "serve-index": "^1.9.1", "sockjs": "^0.3.21", "spdy": "^4.0.2", - "strip-ansi": "^7.0.0", "webpack-dev-middleware": "^5.3.1", "ws": "^8.4.2" }, diff --git a/test/client/index.test.js b/test/client/index.test.js index ee3c64a947..2546281b4e 100644 --- a/test/client/index.test.js +++ b/test/client/index.test.js @@ -27,7 +27,6 @@ describe("index", () => { }, setLogLevel: jest.fn(), }); - jest.setMock("strip-ansi", require("strip-ansi-v6")); log = require("../../client-src/utils/log"); diff --git a/test/e2e/__snapshots__/multi-compiler.test.js.snap.webpack5 b/test/e2e/__snapshots__/multi-compiler.test.js.snap.webpack5 index 2f577cc5ed..81b56819eb 100644 --- a/test/e2e/__snapshots__/multi-compiler.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/multi-compiler.test.js.snap.webpack5 @@ -55,13 +55,13 @@ Array [ "[HMR] Cannot apply update. Need to do a full reload!", "[HMR] Error: Aborted because ./browser.js is not accepted Update propagation: ./browser.js - at applyHandler (http://127.0.0.1:8103/browser.js:1017:31) - at http://127.0.0.1:8103/browser.js:717:21 + at applyHandler (http://127.0.0.1:8103/browser.js:1018:31) + at http://127.0.0.1:8103/browser.js:718:21 at Array.map () - at internalApply (http://127.0.0.1:8103/browser.js:716:54) - at http://127.0.0.1:8103/browser.js:690:26 - at waitForBlockingPromises (http://127.0.0.1:8103/browser.js:643:55) - at http://127.0.0.1:8103/browser.js:688:24", + at internalApply (http://127.0.0.1:8103/browser.js:717:54) + at http://127.0.0.1:8103/browser.js:691:26 + at waitForBlockingPromises (http://127.0.0.1:8103/browser.js:644:55) + at http://127.0.0.1:8103/browser.js:689:24", "[HMR] Waiting for update signal from WDS...", "Hello from the browser", "[webpack-dev-server] Hot Module Replacement enabled.",