Skip to content

Commit f327251

Browse files
committed
Update esbuild strip meta plugin to handle more joined arrays
1 parent 342009a commit f327251

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

tasks/compress_attributes.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
const fs = require('fs');
22

33
/**
4-
* ESBuild plugin that strips out meta attributes
5-
* of the plotly.js bundles
4+
* ESBuild plugin that strips out meta attributes of the plotly.js bundles. This helps reduce the file size for the build used by CI.
65
*/
76

8-
var WHITESPACE_BEFORE = '\\s*';
9-
var OPTIONAL_COMMA = ',?';
7+
const WHITESPACE_BEFORE = '\\s*';
8+
const OPTIONAL_COMMA = ',?';
109

11-
// one line string with or without trailing comma
12-
function makeStringRegex(attr) {
13-
return makeRegex(WHITESPACE_BEFORE + attr + ": '.*'" + OPTIONAL_COMMA);
14-
}
10+
// Match one line string
11+
const makeStringRegex = (attr) => makeRegex(attr + ": '.*'");
1512

16-
// joined array of strings with or without trailing comma
17-
function makeJoinedArrayRegex(attr) {
18-
return makeRegex(WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + OPTIONAL_COMMA);
19-
}
13+
// Match joined array of strings
14+
const makeJoinedArrayRegex = (attr) => makeRegex(attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\([\\s\\S]*?\\)');
2015

21-
// array with or without trailing comma
22-
function makeArrayRegex(attr) {
23-
return makeRegex(WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + OPTIONAL_COMMA);
24-
}
16+
// Match array
17+
const makeArrayRegex = (attr) => makeRegex(attr + ': \\[[\\s\\S]*?\\]');
2518

26-
function makeRegex(regexStr) {
27-
return new RegExp(regexStr, 'g');
28-
}
19+
const makeRegex = (regexStr) => new RegExp(WHITESPACE_BEFORE + regexStr + OPTIONAL_COMMA, 'g');
2920

3021
const allRegexes = [
3122
makeStringRegex('description'),
@@ -39,15 +30,14 @@ const allRegexes = [
3930
const esbuildPluginStripMeta = {
4031
name: 'strip-meta-attributes',
4132
setup(build) {
42-
const loader = 'js';
4333
build.onLoad({ filter: /\.js$/ }, async (file) => ({
4434
contents: await fs.promises.readFile(file.path, 'utf-8').then((c) => {
4535
allRegexes.forEach((r) => {
4636
c = c.replace(r, '');
4737
});
4838
return c;
4939
}),
50-
loader
40+
loader: 'js'
5141
}));
5242
}
5343
};

0 commit comments

Comments
 (0)