inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.3k stars 718 forks source link

SWC compatibility #1403

Open GuillaumeCisco opened 2 years ago

GuillaumeCisco commented 2 years ago

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

Stack trace

HeadFox commented 2 years ago

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

GuillaumeCisco commented 2 years ago

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

HeadFox commented 2 years ago

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

leonardssh commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

Normally yes. With also reflect-metadata installed

sannajammeh commented 2 years ago

@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 commented 2 years ago

@GuillaumeCisco can you close this Pr ? :)

jiby-aurum commented 2 years ago

have used inversify with swc and it works fine for me

ko22009 commented 1 year ago

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

jantoine1 commented 1 year ago

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