@@ -3,17 +3,17 @@ import path from 'path';
3
3
import { walk } from 'estree-walker' ;
4
4
import MagicString from 'magic-string' ;
5
5
6
- import typescript from '@rollup/plugin-typescript ' ;
6
+ import inject from '@rollup/plugin-inject ' ;
7
7
import alias from '@rollup/plugin-alias' ;
8
8
import virtual from '@rollup/plugin-virtual' ;
9
9
import resolve from '@rollup/plugin-node-resolve' ;
10
+ import eslint from '@rbnlffl/rollup-plugin-eslint' ;
11
+ import typescript from '@rollup/plugin-typescript' ;
12
+ import sourcemaps from 'rollup-plugin-sourcemaps' ;
10
13
import commonjs from '@rollup/plugin-commonjs' ;
11
14
import json from '@rollup/plugin-json' ;
12
- import sourcemaps from 'rollup-plugin-sourcemaps' ;
13
- import eslint from '@rbnlffl/rollup-plugin-eslint' ;
14
15
15
16
// demo page specific imports
16
- import polyfillNode from 'rollup-plugin-polyfill-node' ;
17
17
import copy from 'rollup-plugin-copy' ;
18
18
import serve from 'rollup-plugin-serve' ;
19
19
import livereload from 'rollup-plugin-livereload' ;
@@ -126,35 +126,32 @@ export default (commandLineArgs) => {
126
126
} ) ) ,
127
127
preserveEntrySignatures : 'allow-extension' , // avoid rollup's additional facade chunk
128
128
plugins : [
129
- // First run plugins that map imports to the actual imported files, e.g. aliased imports or browser versions
130
- // of packages, such that subsequent plugins operate on the right files.
129
+ // First run plugins that map imports to the actual imported files, e.g. aliased and shimmed imports or
130
+ // browser versions of packages, such that subsequent plugins operate on the right files. Especially, we
131
+ // polyfill node builtins via aliased and virtual packages and later inject their node globals via the
132
+ // inject plugin.
131
133
alias ( {
132
134
entries : {
133
- // replace readable-stream imported by @ledgerhq/hw-app-btc/src/hashPublicKey > ripemd160 >
134
- // hash-base by stream which gets polyfilled by rollup-plugin-polyfill-node. Note that stream and
135
- // readable-stream are largely compatible and effectively the same code. However, the stream
136
- // polyfill used by rollup-plugin-polyfill-node is an older version which has less problems with
137
- // circular dependencies. The circular dependencies are currently being resolved in readable-stream
138
- // though and once merged (see https://github.com/nodejs/readable-stream/issues/348), this alias
139
- // should be removed or even turned around. Note that without the replacement, the stream polyfill
140
- // and readable-stream are both bundled, which is not desirable.
141
- 'readable-stream' : 'stream' ,
142
- // shim unnecessary axios for @ledgerhq /hw-transport-http
135
+ // Polyfill node's builtin stream module via readable-stream, which is essentially node's stream
136
+ // put into an npm package.
137
+ stream : 'readable-stream' ,
138
+ // Shim unnecessary axios for @ledgerhq /hw-transport-http.
143
139
axios : '../../../../src/lib/axios-shim.ts' ,
144
140
} ,
145
141
} ) ,
146
142
virtual ( {
147
- // don 't bundle unnecessary WebSocket polyfill
143
+ // Don 't bundle unnecessary WebSocket polyfill.
148
144
ws : 'export default {};' ,
145
+ // Polyfill node's global and process.env.NODE_ENV.
146
+ global : 'export default window;' ,
147
+ process : `export default { env: { NODE_ENV: ${ isProduction ? '"production"' : '"development"' } } };` ,
149
148
} ) ,
150
149
resolve ( {
151
150
browser : true , // use browser versions of packages if defined in their package.json
152
- preferBuiltins : false , // builtins are handled by polyfillNode
151
+ preferBuiltins : false , // process node builtins to use polyfill packages buffer, readable-stream, etc.
153
152
} ) ,
154
153
// Have eslint high up in the hierarchy to lint the original files.
155
154
eslint ( {
156
- // TODO remove once https://github.com/snowpackjs/rollup-plugin-polyfill-node/pull/3 is merged
157
- filterExclude : [ 'node_modules/**' , / ^ p o l y f i l l - n o d e : / ] , // ignore polyfill-node's virtual files
158
155
throwOnError : isProduction ,
159
156
} ) ,
160
157
// Check types and transpile ts to js. Note that ts does only transpile and not bundle imports.
@@ -167,22 +164,18 @@ export default (commandLineArgs) => {
167
164
rootDir : 'src' ,
168
165
noEmitOnError : isProduction ,
169
166
} ) ,
170
- // Read code including sourcemaps. Has to happen after ts as ts files should be loaded by typescript plugin
171
- // and the sourcemaps plugin can't parse ts files.
172
- sourcemaps ( {
173
- // TODO remove once https://github.com/snowpackjs/rollup-plugin-polyfill-node/pull/3 is merged
174
- exclude : [ / ^ p o l y f i l l - n o d e : / ] ,
175
- } ) ,
167
+ // Read code including sourcemaps. Has to happen after typescript as ts files should be loaded by typescript
168
+ // plugin and the sourcemaps plugin can't parse ts files.
169
+ sourcemaps ( ) ,
176
170
// Plugins for processing dependencies.
177
171
commonjs ( ) ,
178
172
json ( { // required for import of bitcoin-ops/index.json imported by bitcoinjs-lib
179
173
compact : true ,
180
174
} ) ,
181
- polyfillNode ( {
182
- include : [
183
- 'src/**/*' ,
184
- 'node_modules/**/*.js' ,
185
- ] ,
175
+ inject ( {
176
+ Buffer : [ 'buffer' , 'Buffer' ] , // add "import { Buffer } from 'buffer'" when node's Buffer global is used
177
+ global : 'global' , // add "import global from 'global'" when node's global variable 'global' is used
178
+ process : 'process' , // add "import process from 'process'" when node's global variable 'process' is used
186
179
} ) ,
187
180
// Last steps in output generation.
188
181
hoistDynamicImportDependencies ( ) ,
@@ -235,53 +228,53 @@ export default (commandLineArgs) => {
235
228
sourcemapPathTransform,
236
229
} ,
237
230
plugins : [
238
- // typescript needs the import as specified to find the .d.ts file but for actual import we need .es.js file
231
+ // First run plugins that map imports to the actual imported files, e.g. aliased and shimmed imports or
232
+ // browser versions of packages, such that subsequent plugins operate on the right files. Especially, we
233
+ // polyfill node builtins via aliased and virtual packages and later inject their node globals via the
234
+ // inject plugin.
239
235
alias ( {
240
236
entries : {
237
+ // typescript needs the imports as specified to find the .d.ts files but for actual import we need
238
+ // the .es.js files.
241
239
'../../dist/low-level-api/low-level-api' : '../low-level-api/low-level-api.es.js' ,
242
240
'../../dist/high-level-api/ledger-api' : '../high-level-api/ledger-api.es.js' ,
243
- // shim unnecessary axios for @ledgerhq /hw-transport-http
241
+ // Shim unnecessary axios for @ledgerhq /hw-transport-http.
244
242
axios : '../../../../src/lib/axios-shim.ts' ,
245
- // replace readable-stream imported by @ledgerhq/hw-app-btc/src/hashPublicKey > ripemd160 >
246
- // hash-base by stream which gets polyfilled by rollup-plugin-polyfill-node. Note that stream and
247
- // readable-stream are largely compatible and effectively the same code. However, the stream
248
- // polyfill used by rollup-plugin-polyfill-node is an older version which has less problems with
249
- // circular dependencies. The circular dependencies are currently being resolved in readable-stream
250
- // though and once merged (see https://github.com/nodejs/readable-stream/issues/348), this alias
251
- // should be removed or even turned around. Note that without the replacement, the stream polyfill
252
- // and readable-stream are both bundled, which is not desirable.
253
- 'readable-stream' : 'stream' ,
243
+ // Polyfill node's builtin stream module via readable-stream, which is essentially node's stream
244
+ // put into an npm package.
245
+ stream : 'readable-stream' ,
254
246
} ,
255
247
} ) ,
256
248
virtual ( {
257
- // don 't bundle unnecessary WebSocket polyfill
249
+ // Don 't bundle unnecessary WebSocket polyfill.
258
250
ws : 'export default {};' ,
251
+ // Polyfill node's global and process.env.NODE_ENV.
252
+ global : 'export default window;' ,
253
+ process : `export default { env: { NODE_ENV: ${ isProduction ? '"production"' : '"development"' } } };` ,
259
254
} ) ,
260
255
resolve ( {
261
256
browser : true , // use browser versions of packages if defined in their package.json
262
- preferBuiltins : false , // builtins are handled by polyfillNode
257
+ preferBuiltins : false , // process node builtins to use polyfill packages buffer, readable-stream, etc.
263
258
} ) ,
264
259
// Have eslint high up in the hierarchy to lint the original files.
265
260
eslint ( {
266
- // TODO remove once https://github.com/snowpackjs/rollup-plugin-polyfill-node/pull/3 is merged
267
- filterExclude : [ 'node_modules/**' , / ^ p o l y f i l l - n o d e : / ] , // ignore polyfill-node's virtual files
268
261
throwOnError : isProduction ,
269
262
} ) ,
263
+ // Check types and transpile ts to js. Note that ts does only transpile and not bundle imports.
270
264
typescript ( {
271
265
include : [ 'src/demo/**' , 'src/lib/**' ] ,
272
266
noEmitOnError : isProduction ,
273
267
} ) ,
274
- sourcemaps ( {
275
- // TODO remove once https://github.com/snowpackjs/rollup- plugin-polyfill-node/pull/3 is merged
276
- exclude : [ / ^ p o l y f i l l - n o d e : / ] ,
277
- } ) ,
268
+ // Read code including sourcemaps. Has to happen after typescript as ts files should be loaded by typescript
269
+ // plugin and the sourcemaps plugin can't parse ts files.
270
+ sourcemaps ( ) ,
271
+ // Plugins for processing dependencies.
278
272
commonjs ( ) ,
279
273
json ( ) , // required for import of secp256k1/lib/messages.json in secp256k1 imported by bitcoinjs-message
280
- polyfillNode ( {
281
- include : [
282
- 'src/**/*' ,
283
- 'node_modules/**/*.js' ,
284
- ] ,
274
+ inject ( {
275
+ Buffer : [ 'buffer' , 'Buffer' ] , // add "import { Buffer } from 'buffer'" when node's Buffer global is used
276
+ global : 'global' , // add "import global from 'global'" when node's global variable 'global' is used
277
+ process : 'process' , // add "import process from 'process'" when node's global variable 'process' is used
285
278
} ) ,
286
279
copy ( {
287
280
targets : [ {
0 commit comments