Open GuillaumeCisco opened 2 years ago
Just define https://swc.rs/docs/configuration/compilation#jsctransformdecoratormetadata in your .swcrc file
Thank you for your reply. We did use this configuration without success. Any ideas?
Can you give me more information ? Bundler used, config file, etc...
@GuillaumeCisco try this.
{
jsc: {
target: 'es2020',
parser: {
syntax: 'typescript',
dynamicImport: true,
decorators: true
},
transform: {
legacyDecorator: true,
decoratorMetadata: true
},
externalHelpers: true,
keepClassNames: true,
loose: true
}
}
@HeadFox for bundling, I recommend using rollup, along with the rollup-plugin-swc3 & rollup-plugin-tsconfig-paths
Some config:
import { swc } from 'rollup-plugin-swc3';
import { tsConfigPaths } from 'rollup-plugin-tsconfig-paths';
import { nodeResolve } from '@rollup/plugin-node-resolve';
// bypass errors thrown by inversify
const onwarn = (msg, warn) => {
if (!/Circular|The 'this' keyword/.test(msg)) {
warn(msg);
}
};
// ...
{
input: INPUT_FILE,
output: {
file: OUTPUT_FILE,
format: 'cjs'
},
plugins: [
tsConfigPaths({ tsConfigPath: PATH_TO_TSCONFIG }),
nodeResolve(),
swc({
tsconfig: PATH_TO_TSCONFIG,
minify: false, // set to true to minify
jsc: {
target: 'es2020',
parser: {
syntax: 'typescript',
dynamicImport: true,
decorators: true
},
transform: {
legacyDecorator: true,
decoratorMetadata: true
},
externalHelpers: true,
keepClassNames: true,
loose: true
}
})
],
onwarn
};
@GuillaumeCisco did you find a solution? I am having the same issue and the .swcrc
config @LeonardSSH suggested didn't work for me, either.
I am using Next.js 12 and am attempting to migrate from babel to swc. I get Attempted import error:
on everything defined with the @inject()
decorators when running next build
.
@kristiegiles Next12 disabled .swcrc load. But support for emitDecoratorMetadata will be enabled if specified in tsconfig in next Next.js release --> https://github.com/vercel/next.js/pull/32914
@kristiegiles Next12 disabled .swcrc load. But support for emitDecoratorMetadata will be enabled if specified in tsconfig in next Next.js release --> vercel/next.js#32914
Thanks! I also tried it without the .swcrc, and that didn't help. I am using Next 12.0.11-canary.11, and I do have "emitDecoratorMetadata": true
and experimentalDecorators": true
in my tsconfig. It should work with the latest canary version, right?
Normally yes. With also reflect-metadata installed
@kristiegiles Next12 disabled .swcrc load. But support for emitDecoratorMetadata will be enabled if specified in tsconfig in next Next.js release --> vercel/next.js#32914
Thanks! I also tried it without the .swcrc, and that didn't help. I am using Next 12.0.11-canary.11, and I do have
"emitDecoratorMetadata": true
andexperimentalDecorators": true
in my tsconfig. It should work with the latest canary version, right?
That's correct!
@GuillaumeCisco can you close this Pr ? :)
have used inversify with swc and it works fine for me
Problem about transpilation, you need set "target" es2015 or higher in your .swcrc config file.
The link provided in the first comment worked great. Thank you!!!
Is inversify compatible with SWC? From our tests, it looks like the
@inject(...)
decorators are not correctly transpiled.Expected Behavior
Works with swc
Current Behavior
@inject
decorators seems bypassed.Possible Solution
Steps to Reproduce (for bugs)
Context
Your Environment
Stack trace