78 行
2.8 KiB
JavaScript
78 行
2.8 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { mkdtempSync, rmSync } from 'node:fs'
|
|
import { createRequire } from 'node:module'
|
|
import { tmpdir } from 'node:os'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
|
|
const require = createRequire(import.meta.url)
|
|
const { createModuleSerializer, withXuqmModuleConfig } = require('../metro')
|
|
|
|
function context(cacheFile, moduleId, moduleType, moduleIndex, reset = false) {
|
|
return {
|
|
XUQM_MODULE_CACHE_FILE: cacheFile,
|
|
XUQM_MODULE_ID: moduleId,
|
|
XUQM_MODULE_INDEX: String(moduleIndex),
|
|
XUQM_MODULE_TYPE: moduleType,
|
|
XUQM_RESET_MODULE_CACHE: reset ? 'true' : 'false',
|
|
}
|
|
}
|
|
|
|
test('Metro module ranges share startup/common and isolate every app or buz', () => {
|
|
const directory = mkdtempSync(path.join(tmpdir(), 'xuqm-metro-'))
|
|
const cacheFile = path.join(directory, 'ids.json')
|
|
try {
|
|
const startup = createModuleSerializer(context(cacheFile, 'startup', 'startup', 0, true))
|
|
const startupFactory = startup.createModuleIdFactory()
|
|
assert.equal(startupFactory('/runtime/prelude.js'), 0)
|
|
|
|
const common = createModuleSerializer(context(cacheFile, 'common', 'common', 1))
|
|
assert.equal(common.processModuleFilter({ path: '/runtime/prelude.js' }), false)
|
|
const commonFactory = common.createModuleIdFactory()
|
|
assert.equal(commonFactory('/runtime/prelude.js'), 0)
|
|
assert.equal(commonFactory('/shared/common.ts'), 1)
|
|
assert.equal(
|
|
common.processModuleFilter({ path: '/shared/common.ts' }),
|
|
true,
|
|
'common 本轮新分配的模块必须保留在当前 bundle 中',
|
|
)
|
|
|
|
const app = createModuleSerializer(context(cacheFile, 'app', 'app', 2))
|
|
const buz = createModuleSerializer(context(cacheFile, 'buz-a', 'buz', 3))
|
|
assert.equal(app.processModuleFilter({ path: '/shared/common.ts' }), false)
|
|
assert.equal(app.createModuleIdFactory()('/app/entry.ts'), 30_000_000)
|
|
assert.equal(buz.createModuleIdFactory()('/buz/entry.ts'), 40_000_000)
|
|
} finally {
|
|
rmSync(directory, { force: true, recursive: true })
|
|
}
|
|
})
|
|
|
|
test('Metro exposes the current project identity for safe debug reuse', () => {
|
|
const configured = withXuqmModuleConfig({})
|
|
const middleware = configured.server.enhanceMiddleware((_request, _response, next) => next(), {})
|
|
let contentType = ''
|
|
let responseBody = ''
|
|
let delegated = false
|
|
middleware(
|
|
{ url: '/xuqm/metro-info' },
|
|
{
|
|
setHeader(name, value) {
|
|
if (name === 'Content-Type') contentType = value
|
|
},
|
|
end(value) {
|
|
responseBody = value
|
|
},
|
|
},
|
|
() => {
|
|
delegated = true
|
|
},
|
|
)
|
|
|
|
assert.equal(delegated, false)
|
|
assert.equal(contentType, 'application/json; charset=utf-8')
|
|
assert.deepEqual(JSON.parse(responseBody), {
|
|
projectRoot: path.resolve(process.cwd()),
|
|
protocolVersion: 1,
|
|
})
|
|
})
|