Skip to content

Commit

Permalink
Support package.json#addon (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Oct 29, 2024
1 parent cd7efb4 commit 2d4f7ab
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { lookupPackageScope } = require('bare-module-resolve')
const { lookupPrebuildsScope } = require('bare-addon-resolve')
const lex = require('bare-module-lexer')
const resolve = require('./lib/resolve')
const errors = require('./lib/errors')
Expand Down Expand Up @@ -171,6 +172,10 @@ exports.package = function * (url, source, artifacts, visited, opts = {}) {
if (info) {
yield { dependency: { url, source, imports: {} } }

if (info.addon) {
yield { children: exports.prebuilds(url, artifacts, visited, opts) }
}

if (info.assets) {
yield { children: exports.assets(info.assets, url, artifacts, visited, opts) }
}
Expand Down Expand Up @@ -266,6 +271,28 @@ exports.imports = function * (parentURL, source, imports, artifacts, visited, op
return yielded
}

exports.prebuilds = function * (packageURL, artifacts, visited, opts = {}) {
const [prefix = null] = lookupPrebuildsScope(packageURL, opts)

if (prefix === null) return

let yielded = false

for (const url of yield { prefix }) {
const source = yield { module: url }

if (source !== null) {
addURL(artifacts.addons, url)

yield { children: exports.module(url, source, artifacts, visited, opts) }

yielded = true
}
}

return yielded
}

exports.assets = function * (patterns, parentURL, artifacts, visited, opts = {}) {
const matches = yield * exports.patternMatches(patterns, parentURL, [], opts)

Expand Down
89 changes: 89 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,95 @@ test('require + require.asset', (t) => {
])
})

test('package.json#addon', (t) => {
function readModule (url) {
if (url.href === 'file:///foo.js') {
return ''
}

if (url.href === 'file:///package.json') {
return '{ "name": "foo", "addon": true }'
}

if (url.href === 'file:///prebuilds/darwin-arm64/foo.bare') {
return '<native code>'
}

if (url.href === 'file:///prebuilds/linux-arm64/foo.bare') {
return '<native code>'
}

return null
}

function listPrefix (url) {
if (url.href === 'file:///prebuilds/darwin-arm64/') {
return [
new URL('file:///prebuilds/darwin-arm64/foo.bare')
]
}

if (url.href === 'file:///prebuilds/linux-arm64/') {
return [
new URL('file:///prebuilds/linux-arm64/foo.bare')
]
}

return []
}

{
const result = expand(traverse(new URL('file:///foo.js'), { host: 'darwin-arm64' }, readModule, listPrefix))

t.alike(result.values, [
{
url: new URL('file:///foo.js'),
source: '',
imports: {
'#package': 'file:///package.json'
}
},
{
url: new URL('file:///package.json'),
source: '{ "name": "foo", "addon": true }',
imports: {}
},
{
url: new URL('file:///prebuilds/darwin-arm64/foo.bare'),
source: '<native code>',
imports: {
'#package': 'file:///package.json'
}
}
])
}
{
const result = expand(traverse(new URL('file:///foo.js'), { host: 'linux-arm64' }, readModule, listPrefix))

t.alike(result.values, [
{
url: new URL('file:///foo.js'),
source: '',
imports: {
'#package': 'file:///package.json'
}
},
{
url: new URL('file:///package.json'),
source: '{ "name": "foo", "addon": true }',
imports: {}
},
{
url: new URL('file:///prebuilds/linux-arm64/foo.bare'),
source: '<native code>',
imports: {
'#package': 'file:///package.json'
}
}
])
}
})

test('package.json#assets', (t) => {
function readModule (url) {
if (url.href === 'file:///foo.js') {
Expand Down

0 comments on commit 2d4f7ab

Please sign in to comment.