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

[Feature]: build stage 3 decorator with swc in modern.js module #5834

Open
yf-yang opened this issue Jun 15, 2024 · 2 comments
Open

[Feature]: build stage 3 decorator with swc in modern.js module #5834

yf-yang opened this issue Jun 15, 2024 · 2 comments
Assignees

Comments

@yf-yang
Copy link

yf-yang commented Jun 15, 2024

What problem does this feature solve?

I'd like to build a module which uses stage3 decorator. In modern.js app + rspack, I can easily configure swc to do so:

export default defineConfig<'rspack'>({
  plugins: [
    appTools({
      bundler: 'experimental-rspack',
    }),
  ],
  tools: {
    swc: {
      jsc: {
        transform: {
          decoratorVersion: '2022-03',
        },
      },
    },
  },
});

However, it seems modern.js module is using esbuild and there is no way to explicitly configure using swc or enable swc for this decorator transform.

Example file you can test with:

function loggedMethod<This, Args extends any[], Return>(
  target: (this: This, ...args: Args) => Return,
  context: ClassMethodDecoratorContext<
    This,
    (this: This, ...args: Args) => Return
  >,
) {
  const methodName = String(context.name);

  function replacementMethod(this: This, ...args: Args): Return {
    console.log(`LOG: Entering method '${methodName}'.`);
    const result = target.call(this, ...args);
    console.log(`LOG: Exiting method '${methodName}'.`);
    return result;
  }

  return replacementMethod;
}

export class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }

  @loggedMethod
  greet() {
    console.log(`Hello, my name is ${this.name}.`);
  }
}

What does the proposed API look like?

Maybe use SWC when "experimentalDecorator" of tsconfig is true?

Related: https://modernjs.dev/module-tools/en/guide/advance/in-depth-about-build.html#use-swc

@yf-yang
Copy link
Author

yf-yang commented Jun 16, 2024

Sorry, experimentalDecorator is stage2 decorator. Perhaps it should be explicitly configured.

@fi3ework
Copy link
Member

As per #5849. We're going to bump esbuild https://github.com/evanw/esbuild/blob/main/CHANGELOG.md#0210.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants