InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

SWC compatibility

Open GuillaumeCisco opened this issue 3 years ago • 13 comments

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

GuillaumeCisco avatar Dec 08 '21 09:12 GuillaumeCisco

Just define https://swc.rs/docs/configuration/compilation#jsctransformdecoratormetadata in your .swcrc file

HeadFox avatar Dec 16 '21 15:12 HeadFox

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

GuillaumeCisco avatar Dec 16 '21 16:12 GuillaumeCisco

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

HeadFox avatar Dec 16 '21 17:12 HeadFox

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

leonardssh avatar Feb 01 '22 02:02 leonardssh

@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 avatar Feb 10 '22 09:02 kristiegiles

@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

HeadFox avatar Feb 10 '22 09:02 HeadFox

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

kristiegiles avatar Feb 10 '22 10:02 kristiegiles

Normally yes. With also reflect-metadata installed

HeadFox avatar Feb 10 '22 10:02 HeadFox

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

sannajammeh avatar Feb 10 '22 12:02 sannajammeh

@GuillaumeCisco can you close this Pr ? :)

HeadFox avatar Feb 18 '22 15:02 HeadFox

have used inversify with swc and it works fine for me

jiby-aurum avatar Oct 05 '22 09:10 jiby-aurum

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

ko22009 avatar Jan 18 '23 05:01 ko22009

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

jantoine1 avatar Feb 08 '23 06:02 jantoine1