Skip to content

Commit f623b22

Browse files
committed
Update esbuild strip meta plugin to handle more joined arrays
1 parent 52c29cc commit f623b22

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

tasks/compress_attributes.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
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
5+
* bundles. This helps reduce the file size for the build.
66
*/
77

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

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

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-
}
14+
// Match joined array of strings
15+
const makeJoinedArrayRegex = (attr) => makeRegex(attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\([\\s\\S]*?\\)');
2016

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

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

3022
const allRegexes = [
3123
makeStringRegex('description'),
@@ -39,15 +31,14 @@ const allRegexes = [
3931
const esbuildPluginStripMeta = {
4032
name: 'strip-meta-attributes',
4133
setup(build) {
42-
const loader = 'js';
4334
build.onLoad({ filter: /\.js$/ }, async (file) => ({
4435
contents: await fs.promises.readFile(file.path, 'utf-8').then((c) => {
4536
allRegexes.forEach((r) => {
4637
c = c.replace(r, '');
4738
});
4839
return c;
4940
}),
50-
loader
41+
loader: 'js'
5142
}));
5243
}
5344
};

0 commit comments

Comments
 (0)