Skip to content

custom Import attributes with esm hooks #51128

Description

@npenin

Version

20.10

Platform

Linux akala 6.2.16-19-pve #1 SMP PREEMPT_DYNAMIC PMX 6.2.16-19 (2023-10-24T12:07Z) x86_64 x86_64 x86_64 GNU/Linux

Subsystem

esm hooks

What steps will reproduce the bug?

Setup an ESM hook that handles a specific import attribute that relies on the builtin esm loader

import fs from 'fs/promise';
import { fileURLToPath } from 'url';
export async function load(url, context, nextLoad){
    
if(context.importAttributes.type!=='x')
return nextLoad(url, context)

    const source = await fs.readFile(fileURLToPath, 'utf8')

        return ({
            format: 'module',
            shortCircuit: true,
            source: source.replace(/import .*['"][^'"]+['"]$/g, m=>m+'with {type:'x'}')
        })
}

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

Either have a customAttribute hook to handle this validation or disable the validation when a custom hook is registered

What do you see instead?

TypeError [ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED]: Import attribute type "x" is unsupported
at handleInvalidType (node:internal/modules/esm/assert:106:11)
at validateAttributes (node:internal/modules/esm/assert:78:14)
at defaultLoad (node:internal/modules/esm/load:150:3)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async Hooks.load (node:internal/modules/esm/hooks:448:20)
at async handleMessage (node:internal/modules/esm/worker:196:18) {
code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED'
}

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loaders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions