|
1 | | -var through = require('through2'); |
| 1 | +const fs = require('fs'); |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Browserify transform that strips meta attributes out |
| 4 | + * ESBuild plugin that strips out meta attributes |
5 | 5 | * of the plotly.js bundles |
6 | 6 | */ |
7 | 7 |
|
@@ -35,22 +35,26 @@ function makeRegex(regexStr) { |
35 | 35 | ); |
36 | 36 | } |
37 | 37 |
|
38 | | -module.exports = function() { |
39 | | - var allChunks = []; |
40 | | - return through(function(chunk, enc, next) { |
41 | | - allChunks.push(chunk); |
42 | | - next(); |
43 | | - }, function(done) { |
44 | | - var str = Buffer.concat(allChunks).toString('utf-8'); |
45 | | - this.push( |
46 | | - str |
47 | | - .replace(makeStringRegex('description'), '') |
48 | | - .replace(makeJoinedArrayRegex('description'), '') |
49 | | - .replace(makeArrayRegex('requiredOpts'), '') |
50 | | - .replace(makeArrayRegex('otherOpts'), '') |
51 | | - .replace(makeStringRegex('role'), '') |
52 | | - .replace(makeStringRegex('hrName'), '') |
53 | | - ); |
54 | | - done(); |
55 | | - }); |
| 38 | +const allRegexes = [ |
| 39 | + makeStringRegex('description'), |
| 40 | + makeJoinedArrayRegex('description'), |
| 41 | + makeArrayRegex('requiredOpts'), |
| 42 | + makeArrayRegex('otherOpts'), |
| 43 | + makeStringRegex('role'), |
| 44 | + makeStringRegex('hrName') |
| 45 | +]; |
| 46 | + |
| 47 | +var esbuildPluginStripMeta = { |
| 48 | + name: 'strip-meta-attributes', |
| 49 | + setup(build) { |
| 50 | + const loader = 'js'; |
| 51 | + build.onLoad({ filter: /\.js$/ }, async file => ({ |
| 52 | + contents: await fs.promises.readFile(file.path, 'utf-8').then(c => { |
| 53 | + allRegexes.forEach(r => { c = c.replace(r, ''); }); |
| 54 | + return c; |
| 55 | + }), loader |
| 56 | + })); |
| 57 | + } |
56 | 58 | }; |
| 59 | + |
| 60 | +module.exports = esbuildPluginStripMeta; |
0 commit comments