Import scripts/build-client.mjs
This commit is contained in:
parent
2a7a068358
commit
636738377a
|
|
@ -0,0 +1,47 @@
|
|||
// Build lib/client.js (the file the host's client module loader fetches)
|
||||
// from client.js (the ESM source of truth).
|
||||
//
|
||||
// Why: the loader fetches a package's client as a CLASSIC script and expects
|
||||
// it to register a factory via window.__ModuleLoader__.load({ id, factory }).
|
||||
// A raw ESM file registers nothing (its `export` syntax never runs), which is
|
||||
// the v0.2.0 standalone-load failure. client.js stays ESM for the QQ-dsh
|
||||
// bundle's direct import and for the unit tests; this script mechanically
|
||||
// strips the export keywords and wraps the result in the registration IIFE.
|
||||
// It fails loudly if the source grows an export shape it cannot map.
|
||||
import { readFileSync, writeFileSync } from 'node:fs'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), '..')
|
||||
const source = readFileSync(join(root, 'client.js'), 'utf8')
|
||||
|
||||
const exported = []
|
||||
const body = source.replace(/^export (function|const) ([A-Za-z_$][\w$]*)/gm, (match, kind, name) => {
|
||||
exported.push(name)
|
||||
return kind + ' ' + name
|
||||
})
|
||||
if (/^export /m.test(body)) throw new Error('unmapped export syntax remains in client.js')
|
||||
for (const required of ['inject', 'apply']) {
|
||||
if (!exported.includes(required)) throw new Error('client.js no longer exports ' + required)
|
||||
}
|
||||
|
||||
const header = [
|
||||
"'use strict'",
|
||||
'/* Generated by scripts/build-client.mjs from client.js - do not edit by hand.',
|
||||
' The host client module loader fetches this file as a classic script and',
|
||||
' expects window.__ModuleLoader__.load({ id, factory }) registration. */',
|
||||
';(function () {',
|
||||
'',
|
||||
].join('\n')
|
||||
const footer = [
|
||||
'',
|
||||
' var plugin = { name: "dsh-better-chat", inject: inject, apply: apply }',
|
||||
' if (typeof window !== "undefined" && window.__ModuleLoader__ && typeof window.__ModuleLoader__.load === "function") {',
|
||||
' window.__ModuleLoader__.load({ id: "dsh-better-chat", factory: function () { return plugin } })',
|
||||
' }',
|
||||
'})()',
|
||||
'',
|
||||
].join('\n')
|
||||
|
||||
writeFileSync(join(root, 'lib', 'client.js'), header + body + footer)
|
||||
console.log('built lib/client.js from client.js; exports mapped:', exported.join(', '))
|
||||
Loading…
Reference in New Issue