Skip to content
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

FIX: When using ESM, resolve Typescript imports using .js extension #345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luongjames8
Copy link

When using ESM imports and Typescript, the convention that the typescript team is pushing is to import typescript .ts files using .js file extension when using ESM modules in Node16 or NodeNext.

See this issue for more background: #TypeStrong/ts-loader/issues/111

However, older version of webpack (prior to v.5.74.0) could not resolve these typescript files with .js extension.

#Environment
ESM Modules, NodeNext

Observed Behaviour

ERROR in ../handler.ts 5:32-66
Module not found: Error: Can't resolve './balancesController.js' in '\sigma'
// balancesController file is named balancesController.ts

How to Fix

webpack bundled with serverless-bundle can resolve these filenames since webpack version v5.74.0, which gets included in the serverless-bundle repo. However, it requires some small modification to the webpack.config.js file, which is not exposed on serverless-bundle. Namely, we need to add an extension alias to module.exports.resolve

I have to add these lines in manually to my node_modules for serverless-bundle each time. Think will be relevant to have in the base configuration as more people may be using ESM modules.

Resolve TypeScript files imported using the .js extension when using ESM imports.
@jayair jayair added the enhancement New feature or request label Dec 8, 2022
@jayair
Copy link
Member

jayair commented Dec 8, 2022

Thanks for this. I think the tests are failing with this:

ValidationError: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
[681](https://github.com/AnomalyInnovations/serverless-bundle/actions/runs/3638996789/jobs/6167130454#step:6:682)
       - configuration.resolve has an unknown property 'extensionAlias'. These properties are valid:
[682](https://github.com/AnomalyInnovations/serverless-bundle/actions/runs/3638996789/jobs/6167130454#step:6:683)
         object { alias?, aliasFields?, byDependency?, cache?, cachePredicate?, cacheWithContext?, conditionNames?, descriptionFiles?, enforceExtension?, exportsFields?, extensions?, fallback?, fileSystem?, fullySpecified?, importsFields?, mainFields?, mainFiles?, modules?, plugins?, preferAbsolute?, preferRelative?, resolver?, restrictions?, roots?, symlinks?, unsafeCache?, useSyncFileSystemCalls? }ValidationError: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
[681](https://github.com/AnomalyInnovations/serverless-bundle/actions/runs/3638996789/jobs/6167130454#step:6:682)
       - configuration.resolve has an unknown property 'extensionAlias'. These properties are valid:
[682](https://github.com/AnomalyInnovations/serverless-bundle/actions/runs/3638996789/jobs/6167130454#step:6:683)
         object { alias?, aliasFields?, byDependency?, cache?, cachePredicate?, cacheWithContext?, conditionNames?, descriptionFiles?, enforceExtension?, exportsFields?, extensions?, fallback?, fileSystem?, fullySpecified?, importsFields?, mainFields?, mainFiles?, modules?, plugins?, preferAbsolute?, preferRelative?, resolver?, restrictions?, roots?, symlinks?, unsafeCache?, useSyncFileSystemCalls? }

@luongjames8
Copy link
Author

OK. Seems like resolve: needs to be create dynamically with some check based on the webpack version. Will do another iteration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants