1
- /* eslint-disable */
2
- // Based on https://github.com/remorses/esbuild-plugins/blob/master/node-modules-polyfill/src/index.ts
3
-
1
+ /* eslint-disable no-param-reassign */
2
+ import escapeStringRegexp from 'escape-string-regexp' ;
4
3
import fs from 'fs' ;
5
4
import path from 'path' ;
6
5
7
- import builtinsPolyfills from './node-polyfills-paths.js' ;
6
+ import builtinsPolyfills from '../../../polyfills /node-polyfills-paths.js' ;
8
7
9
- const NAME = 'node-modules-polyfills' ;
8
+ const NAME = 'vulcan- node-modules-polyfills' ;
10
9
const NAMESPACE = NAME ;
11
10
12
11
/**
@@ -22,35 +21,23 @@ function removeEndingSlash(importee) {
22
21
return importee ;
23
22
}
24
23
25
- function escapeStringRegexp ( string ) {
26
- if ( typeof string !== 'string' ) {
27
- throw new TypeError ( 'Expected a string' ) ;
28
- }
29
-
30
- // Escape characters with special meaning either inside or outside character sets.
31
- // Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
32
- return string . replace ( / [ | \\ { } ( ) [ \] ^ $ + * ? . ] / g, '\\$&' ) . replace ( / - / g, '\\x2d' ) ;
33
- }
34
-
35
24
/**
36
25
* Generates a template for commonjs with module require
37
26
* @param {string } importPath - the import path
38
27
* @returns {string } the template
39
28
*/
40
29
function commonJsTemplate ( { importPath } ) {
41
30
return `
42
- const polyfill = require('${ importPath } ')
31
+ const polyfill = require('${ importPath } ')
43
32
44
- if (polyfill && polyfill.default) {
45
- module.exports = polyfill.default
46
- for (let k in polyfill) {
47
- module.exports[k] = polyfill[k]
33
+ if (polyfill && polyfill.default) {
34
+ module.exports = polyfill.default
35
+ for (let k in polyfill) {
36
+ module.exports[k] = polyfill[k]
37
+ }
38
+ } else if (polyfill) {
39
+ module.exports = polyfill
48
40
}
49
- } else if (polyfill) {
50
- module.exports = polyfill
51
- }
52
-
53
-
54
41
` ;
55
42
}
56
43
@@ -68,7 +55,7 @@ export function NodeModulesPolyfillPlugin(options = {}) {
68
55
// require('assert') will give you import('assert').default
69
56
const commonjsNamespace = `${ namespace } -commonjs` ;
70
57
const polyfilledBuiltins = builtinsPolyfills ( ) ;
71
- const polyfilledBuiltinsNames = [ ...polyfilledBuiltins . keys ( ) ] ;
58
+ const polyfilledBuiltinsNames = [ ...polyfilledBuiltins . libs . keys ( ) ] ;
72
59
73
60
return {
74
61
name,
@@ -77,10 +64,31 @@ export function NodeModulesPolyfillPlugin(options = {}) {
77
64
if ( initialOptions ?. define && ! initialOptions . define ?. global ) {
78
65
initialOptions . define . global = 'globalThis' ;
79
66
} else if ( ! initialOptions ?. define ) {
80
- initialOptions . define = { global : 'globalThis' } ;
67
+ initialOptions . define = {
68
+ global : 'globalThis' ,
69
+ } ;
70
+ }
71
+ if ( initialOptions . external && initialOptions . external . length > 0 ) {
72
+ const changedExternal = initialOptions . external . reduce (
73
+ ( acc , current ) => {
74
+ if ( polyfilledBuiltinsNames . includes ( current ) ) {
75
+ acc [ `node:${ current } ` ] = current ;
76
+ }
77
+ acc [ current ] = current ;
78
+ return acc ;
79
+ } ,
80
+ { } ,
81
+ ) ;
82
+ initialOptions . external = Object . keys ( changedExternal ) ;
83
+ }
84
+ initialOptions . inject = initialOptions . inject || [ ] ;
85
+
86
+ if ( polyfilledBuiltins . globals ) {
87
+ [ ...polyfilledBuiltins . globals ] . forEach ( ( [ , value ] ) => {
88
+ initialOptions . inject . push ( value ) ;
89
+ } ) ;
81
90
}
82
91
83
- // TODO these polyfill module cannot import anything, is that ok?
84
92
/**
85
93
*
86
94
* @param args
@@ -90,12 +98,16 @@ export function NodeModulesPolyfillPlugin(options = {}) {
90
98
const argsPath = args . path . replace ( / ^ n o d e : / , '' ) ;
91
99
const isCommonjs = args . namespace . endsWith ( 'commonjs' ) ;
92
100
93
- const resolved = polyfilledBuiltins . get ( removeEndingSlash ( argsPath ) ) ;
101
+ const resolved = polyfilledBuiltins . libs . get (
102
+ removeEndingSlash ( argsPath ) ,
103
+ ) ;
94
104
const contents = await (
95
105
await fs . promises . readFile ( resolved )
96
106
) . toString ( ) ;
97
107
const resolveDir = path . dirname ( resolved ) ;
98
108
109
+ const modifiedContents = contents . replace ( / n o d e : / g, '' ) ;
110
+
99
111
if ( isCommonjs ) {
100
112
return {
101
113
loader : 'js' ,
@@ -107,7 +119,7 @@ export function NodeModulesPolyfillPlugin(options = {}) {
107
119
}
108
120
return {
109
121
loader : 'js' ,
110
- contents,
122
+ contents : modifiedContents ,
111
123
resolveDir,
112
124
} ;
113
125
} catch ( e ) {
@@ -118,8 +130,10 @@ export function NodeModulesPolyfillPlugin(options = {}) {
118
130
} ;
119
131
}
120
132
}
133
+
121
134
onLoad ( { filter : / .* / , namespace } , loader ) ;
122
135
onLoad ( { filter : / .* / , namespace : commonjsNamespace } , loader ) ;
136
+
123
137
const filter = new RegExp (
124
138
[
125
139
...polyfilledBuiltinsNames ,
@@ -129,26 +143,37 @@ export function NodeModulesPolyfillPlugin(options = {}) {
129
143
. join ( '|' ) , // TODO builtins could end with slash, keep in mind in regex
130
144
) ;
131
145
146
+ /**
147
+ *
148
+ * @param args
149
+ */
132
150
async function resolver ( args ) {
133
151
const argsPath = args . path . replace ( / ^ n o d e : / , '' ) ;
134
152
const ignoreRequire = args . namespace === commonjsNamespace ;
153
+ const isCommonjs = ! ignoreRequire && args . kind === 'require-call' ;
135
154
136
- if ( ! polyfilledBuiltins . has ( argsPath ) ) {
155
+ if ( ! polyfilledBuiltins . libs . has ( argsPath ) ) {
137
156
return ;
138
157
}
139
158
140
- const isCommonjs = ! ignoreRequire && args . kind === 'require-call' ;
159
+ if (
160
+ initialOptions . external &&
161
+ initialOptions . external . includes ( args . path )
162
+ ) {
163
+ return ;
164
+ }
141
165
166
+ // eslint-disable-next-line consistent-return
142
167
return {
143
168
namespace : isCommonjs ? commonjsNamespace : namespace ,
144
169
path : argsPath ,
145
170
} ;
146
171
}
147
172
onResolve ( { filter } , resolver ) ;
173
+
148
174
// onResolve({ filter: /.*/, namespace }, resolver)
149
175
} ,
150
176
} ;
151
177
}
152
178
153
179
export default NodeModulesPolyfillPlugin ;
154
- /* eslint-enable */
0 commit comments