Skip to content

Commit 6646c02

Browse files
committed
Embed the ansi-regex code into the project to fix an import dependency issue
1 parent 561320a commit 6646c02

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const get = require('lodash.get');
2020
const each = require('lodash.foreach');
2121
const fromPairs = require('lodash.frompairs');
2222
const toPairs = require('lodash.topairs');
23-
const stripAnsi = require('strip-ansi');
23+
const stripAnsi = require('./utils/stripAnsi');
2424

2525
function getAssetPath(compilation, name) {
2626
return path.join(compilation.getPath(compilation.compiler.outputPath), name.split('?')[0]);

lib/utils/stripAnsi.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This code is based on the strip-ansi library by Chalk.
3+
* Source: https://github.com/chalk/strip-ansi
4+
*/
5+
6+
function ansiRegex({ onlyFirst = false } = {}) {
7+
const pattern = [
8+
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
9+
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
10+
].join('|');
11+
12+
return new RegExp(pattern, onlyFirst ? undefined : 'g');
13+
}
14+
15+
function stripAnsi(string) {
16+
if (typeof string !== 'string') {
17+
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
18+
}
19+
20+
// Even though the regex is global, we don't need to reset the `.lastIndex`
21+
// because unlike `.exec()` and `.test()`, `.replace()` does it automatically
22+
// and doing it manually has a performance penalty.
23+
const regex = ansiRegex();
24+
return string.replace(regex, '');
25+
}
26+
27+
module.exports = stripAnsi;

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
"lodash.foreach": "^4.5.0",
4444
"lodash.frompairs": "^4.0.1",
4545
"lodash.get": "^4.4.2",
46-
"lodash.topairs": "^4.3.0",
47-
"strip-ansi": "^6.0.1"
46+
"lodash.topairs": "^4.3.0"
4847
},
4948
"devDependencies": {
5049
"@types/babel__traverse": "7.0.6",

0 commit comments

Comments
 (0)