Skip to content
Merged
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
20 changes: 20 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint import/order: off */
import assert from 'node:assert'
import { spawnSync } from 'node:child_process'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

import { RepositoryState } from '@napi-rs/simple-git'
import { bar as subBar } from '@subdirectory/bar.mjs'
Expand Down Expand Up @@ -102,3 +104,21 @@ await test('postgres should work', async () => {
await test('resolve conditions', () => {
assert.equal(name, 'from-dev')
})

// `with { type: 'text' }` is supported since Node.js 26.5, behind --experimental-import-text
const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(Number)
const supportsTextImports = nodeMajor > 26 || (nodeMajor === 26 && nodeMinor >= 5)

await test('text import attributes should pass through to the default loader', { skip: !supportsTextImports }, () => {
const { status, stderr } = spawnSync(
process.execPath,
[
'--experimental-import-text',
'--import=@swc-node/register/esm-register',
fileURLToPath(new URL('./text-import/index.ts', import.meta.url)),
],
{ env: process.env },
)

assert.equal(status, 0, stderr?.toString())
})
1 change: 1 addition & 0 deletions packages/integrate-module/src/text-import/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text import works
5 changes: 5 additions & 0 deletions packages/integrate-module/src/text-import/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assert from 'node:assert'

import data from './data.txt' with { type: 'text' }

assert.equal(data, 'text import works\n')
4 changes: 4 additions & 0 deletions packages/integrate-module/src/text-import/txt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.txt' {
const text: string
export default text
}
7 changes: 7 additions & 0 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ export const load: LoadHook = async (url, context, nextLoad) => {
return nextLoad(url, context)
}

// import attributes are handled by the default loader,
// e.g. `with { type: 'text' }` since Node.js 26.5 (behind --experimental-import-text)
if (context.importAttributes?.type) {
debug('skip load: import attributes', url)
return nextLoad(url, context)
}

const { source, format: resolvedFormat } = await nextLoad(url, context)

if (!source) {
Expand Down