Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
window.__ModuleLoader__.load({
id: "dsh-github",
id: "@perrylink/dsh-github",
factory: (require) => {
var module = { exports: {} };
var exports = module.exports;
Expand Down
5 changes: 4 additions & 1 deletion scripts/bundle-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'

const root = dirname(dirname(fileURLToPath(import.meta.url)))
// The registration id must be the package name: a client graph row is keyed by
// it, so any other id leaves the module unresolvable and the host never boots it.
const { name: pkgName } = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
const file = join(root, 'lib', 'client.js')
let text = readFileSync(file, 'utf8')
if (text.startsWith('window.__ModuleLoader__.load(')) process.exit(0)
Expand Down Expand Up @@ -37,7 +40,7 @@ text = text.replace(/\/\/# sourceMappingURL=.*$/gm, '')

const tail = exports.map(name => `\t\texports.${name} = ${name};`).join('\n')
const wrapped = `window.__ModuleLoader__.load({
\tid: "dsh-github",
\tid: ${JSON.stringify(pkgName)},
\tfactory: (require) => {
\t\tvar module = { exports: {} };
\t\tvar exports = module.exports;
Expand Down
8 changes: 7 additions & 1 deletion test/client-bundle.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'

const { name: pkgName } = JSON.parse(
readFileSync(new URL('../package.json', import.meta.url), 'utf8'),
) as { name: string }

describe('shipped browser bundle', () => {
it('is a classic-script loader factory, not bare ESM', () => {
const source = readFileSync(new URL('../lib/client.js', import.meta.url), 'utf8')
expect(source.startsWith('window.__ModuleLoader__.load({')).toBe(true)
expect(source).toContain('id: "dsh-github"')
// The host keys client graph rows by package name; any other id leaves the
// registration unresolvable and the host never imports it.
expect(source).toContain(`id: ${JSON.stringify(pkgName)}`)
expect(/^import\s/m.test(source)).toBe(false)
expect(/^export\s/m.test(source)).toBe(false)
})
Expand Down