Skip to content

Commit 78cc913

Browse files
authored
fix(builtins): move to generated node builtins fixture (#157)
This generates the node builtins from runtime instead of using pulling them from the runtime. It will allow this to work in a browser setting with no resolve fallbacks needing to be added.
1 parent 728a77a commit 78cc913

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/builtin-modules.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["_http_agent","_http_client","_http_common","_http_incoming","_http_outgoing","_http_server","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_wrap","_stream_writable","_tls_common","_tls_wrap","assert","assert/strict","async_hooks","buffer","child_process","cluster","console","constants","crypto","dgram","diagnostics_channel","dns","dns/promises","domain","events","fs","fs/promises","http","http2","https","inspector","inspector/promises","module","net","os","path","path/posix","path/win32","perf_hooks","process","punycode","querystring","readline","readline/promises","repl","stream","stream/consumers","stream/promises","stream/web","string_decoder","sys","timers","timers/promises","tls","trace_events","tty","url","util","util/types","v8","vm","wasi","worker_threads","zlib","node:sea","node:sqlite","node:test","node:test/reporters"]

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const { builtinModules: builtins } = require('module')
2+
const builtins = require('./builtin-modules.json')
33

44
var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
55
var exclusionList = [

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@npmcli/template-oss": "4.28.1"
1212
},
1313
"scripts": {
14+
"builtin-fixture": "node -e \"console.log(JSON.stringify(require('node:module').builtinModules))\" > ./lib/builtin-modules.json",
1415
"cov:test": "TAP_FLAGS='--cov' npm run test:code",
1516
"test:code": "tap ${TAP_FLAGS:-'--'} test/*.js",
1617
"test:style": "standard",

test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,20 @@ test('validate-npm-package-name', function () {
173173
validForOldPackages: true,
174174
warnings: ['name can no longer contain capital letters'] })
175175
})
176+
/*
177+
* This test will start failing if a newer version of node is used that adds new builtins.
178+
* That means it's time to update the list of builtins that this package knows about by running "npm run builtin-fixture"
179+
*/
180+
test('node builtins', function () {
181+
const { builtinModules: nodeBuiltins } = require('node:module')
182+
for (const builtin of nodeBuiltins) {
183+
// some builtins are already invalid from other rules
184+
if (!builtin.startsWith('_') && !builtin.includes('/') && !builtin.includes(':')) {
185+
assert.deepStrictEqual(validate(builtin), {
186+
validForNewPackages: false,
187+
validForOldPackages: true,
188+
warnings: [`${builtin} is a core module name`],
189+
})
190+
}
191+
}
192+
})

0 commit comments

Comments
 (0)