Skip to content

Commit

Permalink
fix: adapt to new node:test module in node 18 (#1069)
Browse files Browse the repository at this point in the history
* fix: adapt to new node:test module in node 18

* chore: add comment and use process.binding to replicate paperwork

* fix: replicate even more precise

* refactor: use precinct's mechanism for sub v18
  • Loading branch information
Skn0tt committed Apr 21, 2022
1 parent e5d1c7f commit 1ffe7c1
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/runtimes/node/bundlers/zisi/src_files.ts
@@ -1,8 +1,10 @@
/* eslint-disable max-lines */
import { dirname, basename, normalize } from 'path'
import * as process from 'process'

import { not as notJunk } from 'junk'
import precinct from 'precinct'
import semver from 'semver'

import { FeatureFlags } from '../../../../feature_flags.js'
import { nonNullable } from '../../../../utils/non_nullable.js'
Expand Down Expand Up @@ -87,6 +89,32 @@ const getDependencies = async function ({
}
}

const paperwork = async (path: string) => {
if (semver.lt(process.version, '18.0.0')) {
return await precinct.paperwork(path, { includeCore: false })
}

// for Node v18, we're temporarily using our own mechanism to filter out core dependencies, until
// https://github.com/dependents/node-precinct/pull/108 landed
const modules = await precinct.paperwork(path, { includeCore: true })
return modules.filter((moduleName) => {
if (moduleName.startsWith('node:')) {
return false
}

// only require("node:test") refers to the
// builtin, require("test") doesn't
if (moduleName === 'test') {
return true
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isNativeModule = moduleName in (process as any).binding('natives')

return !isNativeModule
})
}

const getFileDependencies = async function ({
featureFlags,
functionName,
Expand All @@ -111,9 +139,8 @@ const getFileDependencies = async function ({
state.localFiles.add(path)

const basedir = dirname(path)
const dependencies = featureFlags.parseWithEsbuild
? await listImports({ functionName, path })
: await precinct.paperwork(path, { includeCore: false })
const dependencies = featureFlags.parseWithEsbuild ? await listImports({ functionName, path }) : await paperwork(path)

const depsPaths = await Promise.all(
dependencies.filter(nonNullable).map((dependency) =>
getImportDependencies({
Expand Down

1 comment on commit 1ffe7c1

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 6.5s

largeDepsNft: 29.6s

largeDepsZisi: 1m 2.1s

Please sign in to comment.