Skip to content

Commit

Permalink
Support conditional mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Oct 4, 2024
1 parent da6358d commit efc5cb2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ const Bundle = module.exports = exports = class Bundle {
return this
}

mount (root) {
mount (root, opts = {}) {
const mounted = new Bundle()

// Go through the private API properties as we're operating on already
// validated values.

if (this._main) mounted._main = mountPath(this._main, root)

mounted._imports = mountImportsMap(this._imports, root)
mounted._resolutions = mountResolutionsMap(this._resolutions, root)
mounted._imports = mountImportsMap(this._imports, root, null, opts)
mounted._resolutions = mountResolutionsMap(this._resolutions, root, opts)

for (const [key, file] of this._files) {
mounted._files.set(mountPath(key, root), file)
Expand Down Expand Up @@ -395,33 +395,33 @@ function mountPath (value, root) {
return value
}

function mountImportsMap (value, root) {
function mountImportsMap (value, root, conditionalRoot, opts) {
const { conditions = {} } = opts

const imports = {}

for (const entry of Object.entries(value)) {
imports[entry[0]] = mountImportsMapEntry(entry[1], root)
const condition = entry[0]

imports[condition] = mountImportsMapEntry(entry[1], root, conditionalRoot || conditions[condition], opts)
}

return imports
}

function mountImportsMapEntry (value, root) {
if (typeof value === 'string') return mountPath(value, root)
function mountImportsMapEntry (value, root, conditionalRoot, opts) {
const { conditions = {} } = opts

const imports = {}
if (typeof value === 'string') return mountPath(value, conditionalRoot || conditions.default || root)

for (const entry of Object.entries(value)) {
imports[entry[0]] = mountImportsMapEntry(entry[1], root)
}

return imports
return mountImportsMap(value, root, conditionalRoot, opts)
}

function mountResolutionsMap (value, root) {
function mountResolutionsMap (value, root, opts) {
const resolutions = {}

for (const entry of Object.entries(value)) {
resolutions[mountPath(entry[0], root)] = mountImportsMap(entry[1], root)
resolutions[mountPath(entry[0], root)] = mountImportsMap(entry[1], root, null, opts)
}

return resolutions
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ test('mount', (t) => {
])
})

test('mount, resolutions map', (t) => {
const bundle = new Bundle()

bundle
.write('/foo.js', 'foo')
.write('/bar.txt', 'bar', { asset: true })

bundle.resolutions = {
'/foo.js': {
bar: {
asset: '/bar.txt'
}
}
}

const mounted = bundle.mount(new URL('file:///dir/'), {
conditions: {
asset: new URL('file:///assets/')
}
})

t.alike(mounted.resolutions, {
'file:///dir/foo.js': {
bar: {
asset: 'file:///assets/bar.txt'
}
}
})
})

test('iterate', (t) => {
const bundle = new Bundle()

Expand Down

0 comments on commit efc5cb2

Please sign in to comment.