Replies: 4 comments
-
Right now, you can use the formats : |
Beta Was this translation helpful? Give feedback.
-
The code is about 9 lines, you can understand how it('import .ts in .js', () => {
expect(
crossImport('./foo.ts', { cwd: __dirname })
).toEqual({'bar': 'bar', 'foo': 'foo'})
}) |
Beta Was this translation helpful? Give feedback.
-
You can even import things with import tsNode from 'ts-node';
// This registers new ts compiler onto node itself which results in valid import of `.ts` files
tsNode.register();
const someFileModule = await import("some-file.ts") |
Beta Was this translation helpful? Give feedback.
-
Yeah but then you'll have an issue of whatever real-time config transpiler having a different typscript config than nextjs. |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
Related
next/lib/find-config
does not support ESM config files #34448find-config
#34451Describe the solution you'd like
As far as is generally known:
Recently, I encountered the same problem when implementing the compiler of Master CSS, so I created a plugin — cross-import that allows you to import ESM, Typescript modules in the CJS environment at runtime.
The principle is to compile, bundle and create virtual JavaScript modules at runtime through ESBuild, which is fast and formal because it is not hardcoded through
fs
.Also, cross-import is not asynchronous like ESM's Dynamic Import, it runs synchronously.
So now Next Team supports multi-format JavaScript modules in a CJS environment almost immediately:
By default
cross-import
is executed in CWD.Describe alternatives you've considered
I also created a package — techor to handle this kind of tech authoring.
I think this is quite possibly the only solution for different environment JavaScript modules!
Beta Was this translation helpful? Give feedback.
All reactions