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

SWC compatibility #1403

Open
GuillaumeCisco opened this issue Dec 8, 2021 · 13 comments
Open

SWC compatibility #1403

GuillaumeCisco opened this issue Dec 8, 2021 · 13 comments

Comments

@GuillaumeCisco
Copy link

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)

  1. compile you inversify app with swc

Context

Your Environment

  • Version used:
  • Environment name and version (e.g. Chrome 39, node.js 5.4):
  • Operating System and version (desktop or mobile):
  • Link to your project:

Stack trace

@HeadFox
Copy link

HeadFox commented Dec 16, 2021

@GuillaumeCisco
Copy link
Author

Thank you for your reply.
We did use this configuration without success.
Any ideas?

@HeadFox
Copy link

HeadFox commented Dec 16, 2021

Can you give me more information ? Bundler used, config file, etc...

@leonardssh
Copy link
Member

@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
};

@kristiegiles
Copy link

@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.

@HeadFox
Copy link

HeadFox commented Feb 10, 2022

@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

@kristiegiles
Copy link

@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?

@HeadFox
Copy link

HeadFox commented Feb 10, 2022

Normally yes. With also reflect-metadata installed

@sannajammeh
Copy link

@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?

That's correct!

@HeadFox
Copy link

HeadFox commented Feb 18, 2022

@GuillaumeCisco can you close this Pr ? :)

@jiby-aurum
Copy link

have used inversify with swc and it works fine for me

@ko22009
Copy link

ko22009 commented Jan 18, 2023

Problem about transpilation, you need set "target" es2015 or higher in your .swcrc config file.

@jantoine1
Copy link

The link provided in the first comment worked great. Thank you!!!

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

8 participants