Skip to content

Feature Request: Support for ES Modules #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nickcox opened this issue May 11, 2021 · 2 comments
Closed

Feature Request: Support for ES Modules #22

nickcox opened this issue May 11, 2021 · 2 comments

Comments

@nickcox
Copy link

nickcox commented May 11, 2021

Afaict, there's no way to use an ES module based hander right now.

It would be awesome to have one less build step. Just wondering if this is on the roadmap?

@jimmywarting
Copy link

jimmywarting commented Nov 9, 2021

This code needs to be aware if the package.json has "type": "module" and if it is... then it needs to use dynamic import import(path) instead, this is allowed from node v12.20 and onwards from commonjs

/**
* Attempt to load the user's module.
* Attempts to directly resolve the module relative to the application root,
* then falls back to the more general require().
*/
function _tryRequire(appRoot: string, moduleRoot: string, module: string): any {
const lambdaStylePath = path.resolve(appRoot, moduleRoot, module);
if (_canLoadAsFile(lambdaStylePath)) {
return require(lambdaStylePath);
} else {
// Why not just require(module)?
// Because require() is relative to __dirname, not process.cwd()
const nodeStylePath = require.resolve(module, {
paths: [appRoot, moduleRoot],
});
return require(nodeStylePath);
}
}

but b/c you use typescript and downlevel everything to commonjs 👎 so it outputs something like __importStar(require('module'))) after you have compiled the source, then you need to use this to prevent typescript from rewriting import to require:

const _importDynamic = new Function('modulePath', 'return import(modulePath)')
_importDynamic(path)

(Imo i think you should ditch typescript, switch to ESM and not transpile anything - but this is just my opinion)

@krk
Copy link

krk commented Jun 23, 2023

This is fixed in #70.

@krk krk closed this as completed Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants