1- var through = require ( 'through2 ' ) ;
1+ const fs = require ( 'fs ' ) ;
22
33/**
4- * Browserify transform that strips meta attributes out
4+ * ESBuild plugin that strips out meta attributes
55 * of the plotly.js bundles
66 */
77
@@ -10,47 +10,46 @@ var OPTIONAL_COMMA = ',?';
1010
1111// one line string with or without trailing comma
1212function makeStringRegex ( attr ) {
13- return makeRegex (
14- WHITESPACE_BEFORE + attr + ': \'.*\'' + OPTIONAL_COMMA
15- ) ;
13+ return makeRegex ( WHITESPACE_BEFORE + attr + ": '.*'" + OPTIONAL_COMMA ) ;
1614}
1715
1816// joined array of strings with or without trailing comma
1917function makeJoinedArrayRegex ( attr ) {
20- return makeRegex (
21- WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + OPTIONAL_COMMA
22- ) ;
18+ return makeRegex ( WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + OPTIONAL_COMMA ) ;
2319}
2420
2521// array with or without trailing comma
2622function makeArrayRegex ( attr ) {
27- return makeRegex (
28- WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + OPTIONAL_COMMA
29- ) ;
23+ return makeRegex ( WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + OPTIONAL_COMMA ) ;
3024}
3125
3226function makeRegex ( regexStr ) {
33- return (
34- new RegExp ( regexStr , 'g' )
35- ) ;
27+ return new RegExp ( regexStr , 'g' ) ;
3628}
3729
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- } ) ;
30+ const allRegexes = [
31+ makeStringRegex ( 'description' ) ,
32+ makeJoinedArrayRegex ( 'description' ) ,
33+ makeArrayRegex ( 'requiredOpts' ) ,
34+ makeArrayRegex ( 'otherOpts' ) ,
35+ makeStringRegex ( 'role' ) ,
36+ makeStringRegex ( 'hrName' )
37+ ] ;
38+
39+ const esbuildPluginStripMeta = {
40+ name : 'strip-meta-attributes' ,
41+ setup ( build ) {
42+ const loader = 'js' ;
43+ build . onLoad ( { filter : / \. j s $ / } , async ( file ) => ( {
44+ contents : await fs . promises . readFile ( file . path , 'utf-8' ) . then ( ( c ) => {
45+ allRegexes . forEach ( ( r ) => {
46+ c = c . replace ( r , '' ) ;
47+ } ) ;
48+ return c ;
49+ } ) ,
50+ loader
51+ } ) ) ;
52+ }
5653} ;
54+
55+ module . exports = esbuildPluginStripMeta ;
0 commit comments