Skip to content

Commit 76933ab

Browse files
committed
rollup: replace rollup-plugin-polyfill-node by individual polyfills
Polyfill node builtins manually via individual npm packages and manual shims, to be able to update them manually to maintained versions. Notably, the buffer polyfill has been updated this way, for compatibility with the newest @ledgerhq/hw-app-btc, which uses readUint8() in BtcNew.signMessage without specifying an offset, which is not supported yet by the polyfill bundlded with rollup-plugin-polyfill-node. In order to properly handle the circular dependencies of the now updated readable-stream package, @rollup/plugin-commonjs had to be updated to at least version 22.0.0 to include rollup/plugins#1038 to fix rollup/rollup#1507. Additionally, rollup and @rollup/plugin-node-resolve had been updated to the minimum version compatible with the updated @rollup/plugin-commonjs.
1 parent 639d7dc commit 76933ab

File tree

3 files changed

+173
-96
lines changed

3 files changed

+173
-96
lines changed

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@
3737
"@ledgerhq/logs": "^6.11.0",
3838
"@rbnlffl/rollup-plugin-eslint": "^1.1.4",
3939
"@rollup/plugin-alias": "^3.1.2",
40-
"@rollup/plugin-commonjs": "^17.1.0",
40+
"@rollup/plugin-commonjs": "^22.0.0",
41+
"@rollup/plugin-inject": "^5.0.5",
4142
"@rollup/plugin-json": "^4.1.0",
42-
"@rollup/plugin-node-resolve": "^11.1.1",
43+
"@rollup/plugin-node-resolve": "^13.0.6",
4344
"@rollup/plugin-typescript": "^8.1.1",
4445
"@typescript-eslint/eslint-plugin": "^4.14.2",
4546
"@typescript-eslint/parser": "^4.14.2",
4647
"bitcoinjs-message": "^2.2.0",
4748
"eslint": "^7.19.0",
4849
"eslint-config-airbnb-typescript": "^12.3.1",
4950
"eslint-plugin-import": "^2.22.1",
50-
"rollup": "^2.38.5",
51+
"rollup": "^2.68.0",
5152
"rollup-plugin-copy": "^3.3.0",
5253
"rollup-plugin-livereload": "^2.0.0",
53-
"rollup-plugin-polyfill-node": "^0.5.0",
5454
"rollup-plugin-serve": "^1.1.0",
5555
"rollup-plugin-sourcemaps": "^0.6.3",
5656
"tslib": "^2.1.0",
@@ -69,6 +69,8 @@
6969
"@rollup/plugin-virtual": "^2.0.3",
7070
"@types/ledgerhq__hw-transport-u2f": "^4.21.4",
7171
"@types/w3c-web-usb": "^1.0.6",
72-
"bitcoinjs-lib": "^5.2.0"
72+
"bitcoinjs-lib": "^5.2.0",
73+
"buffer": "^6.0.3",
74+
"readable-stream": "^4.4.2"
7375
}
7476
}

rollup.config.js

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import path from 'path';
33
import { walk } from 'estree-walker';
44
import MagicString from 'magic-string';
55

6-
import typescript from '@rollup/plugin-typescript';
6+
import inject from '@rollup/plugin-inject';
77
import alias from '@rollup/plugin-alias';
88
import virtual from '@rollup/plugin-virtual';
99
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';
1013
import commonjs from '@rollup/plugin-commonjs';
1114
import json from '@rollup/plugin-json';
12-
import sourcemaps from 'rollup-plugin-sourcemaps';
13-
import eslint from '@rbnlffl/rollup-plugin-eslint';
1415

1516
// demo page specific imports
16-
import polyfillNode from 'rollup-plugin-polyfill-node';
1717
import copy from 'rollup-plugin-copy';
1818
import serve from 'rollup-plugin-serve';
1919
import livereload from 'rollup-plugin-livereload';
@@ -126,35 +126,32 @@ export default (commandLineArgs) => {
126126
})),
127127
preserveEntrySignatures: 'allow-extension', // avoid rollup's additional facade chunk
128128
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.
131133
alias({
132134
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.
143139
axios: '../../../../src/lib/axios-shim.ts',
144140
},
145141
}),
146142
virtual({
147-
// don't bundle unnecessary WebSocket polyfill
143+
// Don't bundle unnecessary WebSocket polyfill.
148144
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"'} } };`,
149148
}),
150149
resolve({
151150
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.
153152
}),
154153
// Have eslint high up in the hierarchy to lint the original files.
155154
eslint({
156-
// TODO remove once https://github.com/snowpackjs/rollup-plugin-polyfill-node/pull/3 is merged
157-
filterExclude: ['node_modules/**', /^polyfill-node:/], // ignore polyfill-node's virtual files
158155
throwOnError: isProduction,
159156
}),
160157
// Check types and transpile ts to js. Note that ts does only transpile and not bundle imports.
@@ -167,22 +164,18 @@ export default (commandLineArgs) => {
167164
rootDir: 'src',
168165
noEmitOnError: isProduction,
169166
}),
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: [/^polyfill-node:/],
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(),
176170
// Plugins for processing dependencies.
177171
commonjs(),
178172
json({ // required for import of bitcoin-ops/index.json imported by bitcoinjs-lib
179173
compact: true,
180174
}),
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
186179
}),
187180
// Last steps in output generation.
188181
hoistDynamicImportDependencies(),
@@ -235,53 +228,53 @@ export default (commandLineArgs) => {
235228
sourcemapPathTransform,
236229
},
237230
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.
239235
alias({
240236
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.
241239
'../../dist/low-level-api/low-level-api': '../low-level-api/low-level-api.es.js',
242240
'../../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.
244242
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',
254246
},
255247
}),
256248
virtual({
257-
// don't bundle unnecessary WebSocket polyfill
249+
// Don't bundle unnecessary WebSocket polyfill.
258250
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"'} } };`,
259254
}),
260255
resolve({
261256
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.
263258
}),
264259
// Have eslint high up in the hierarchy to lint the original files.
265260
eslint({
266-
// TODO remove once https://github.com/snowpackjs/rollup-plugin-polyfill-node/pull/3 is merged
267-
filterExclude: ['node_modules/**', /^polyfill-node:/], // ignore polyfill-node's virtual files
268261
throwOnError: isProduction,
269262
}),
263+
// Check types and transpile ts to js. Note that ts does only transpile and not bundle imports.
270264
typescript({
271265
include: ['src/demo/**', 'src/lib/**'],
272266
noEmitOnError: isProduction,
273267
}),
274-
sourcemaps({
275-
// TODO remove once https://github.com/snowpackjs/rollup-plugin-polyfill-node/pull/3 is merged
276-
exclude: [/^polyfill-node:/],
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.
278272
commonjs(),
279273
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
285278
}),
286279
copy({
287280
targets: [{

0 commit comments

Comments
 (0)