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

Next.js Support (SWC) #146

Closed
brunoalano opened this issue Jul 6, 2022 · 19 comments
Closed

Next.js Support (SWC) #146

brunoalano opened this issue Jul 6, 2022 · 19 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed wontfix This will not be worked on

Comments

@brunoalano
Copy link

Feature Request

A description of the problem you're trying to solve.
Actually SWC doesn't works with Next.js, since the project moved from Babel to SWC. Would be great to support this popular framework.

@samchon
Copy link
Owner

samchon commented Jul 6, 2022

It does not work on Babel either. Right now, I recommend you to generate special functions in another file and let nextjs to import that file.

About the swc tranpiler, I will study it, but may consume lots of time ToT

@samchon samchon self-assigned this Jul 6, 2022
@samchon samchon added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers wontfix This will not be worked on labels Jul 6, 2022
@samchon
Copy link
Owner

samchon commented Jul 6, 2022

Inquired to swc creator (@kdy1), and get answered like below:

SWC is not supporting TypeScript type system yet, therefore it is not possible now.

Therefore, I recommend you again, build typescript-json function call through tsc and import that in swc.

However, when swc starts supporting the TypeScript type system, I promise you that I will implement special transformer for the swc. It would be exciting even for me.

@samchon samchon closed this as completed Jul 8, 2022
@H4ad
Copy link

H4ad commented Oct 23, 2022

Hey @samchon, do you think it is possible now to add support in the current state of swc?

@samchon
Copy link
Owner

samchon commented Oct 23, 2022

@H4ad

Not possible.

Unfortunately, SWC author said he has no plan to support TypeScript type as an open source in his blog.

https://www.reddit.com/r/typescript/comments/y7yghy/typescript_type_checker_stc_will_not_be_open/

@H4ad
Copy link

H4ad commented Oct 23, 2022

I still don't understand the difference between supporting typescript type, from what I see in this library you implement transformers and looking at swc plugins and playing with AST it seems possible to implement (rewrite) this plugin for rust to add swc support.

Or am I confusing these two things?

@samchon
Copy link
Owner

samchon commented Oct 23, 2022

Without type system, how can I implement type checker? Not possible only with plugin API, but needs type system, too.

@H4ad
Copy link

H4ad commented Oct 23, 2022

Sorry to bother but I just want to understand, from what I could understand by searching the swc codebase, today swc has this file typescript.rs which represents the Typescript AST.

In addition, swc also parses the AST and returns the type information:

image

And reading some plugins written in rust for swc, like this one, it looks like it's possible to create what typescript-json does.

I ran the example in the README and the output of TSON.equals is:

const result2 = ((input, path = "$input") => {
    const $join = typescript_json_1.default.equals.join;
    const $io = [
        (input, path, exceptionable) => "string" === typeof input.name && "number" === typeof input.age && Object.entries(input).every(([key, value]) => {
            if (undefined === value)
                return true;
            if (["name", "age"].some(prop => key === prop))
                return true;
            return false;
        })
    ];
    return null !== input && ("object" === typeof input && null !== input && $io[0](input, path + "", true));
})(person); // -> false, do not allow

The library transforms the code and generates this function to validate equality.
So with these things it is theoretically possible to implement the same behavior, what exactly is the type system you need swc to implement this transformation?

Edit: After thinking for a few minutes, maybe now I managed to understand the problem, you can implement the library in rust, but you've lost the ability you have now as CLI, from what I see, you can't implement the library in typescript and leave the swc interpret, either you build in rust or you don't.

@samchon
Copy link
Owner

samchon commented Oct 24, 2022

@H4ad

I repeat that, not possible.

To make validation script, typescript-json must get type information from compiler, but SWC and Babel are not providing the type information. Without the type information, how can I make above validation script? SWC and Babel provides AST but type info is not in there. It's the reason why I can't support SWC and Babel yet.

As you're asking me repeatedly, I asked to the SWC author directly and below is the formal answer from him:

타입 정보 안 줘요 (no type information)

@H4ad
Copy link

H4ad commented Oct 24, 2022

@samchon

Now I understand, sorry to bother you and thanks for your time and patience, I hope one day the author decides to implement it, it would be great to have this kind of library implemented in such a fast compiler.

@samchon
Copy link
Owner

samchon commented Feb 19, 2023

Will support through #509

@ehaynes99
Copy link

FYI swc-project/swc#657 (comment)
It's a huge effort, though, so no idea on timeline.

@aspirisen
Copy link

I was able to run typia in nextjs via ts-loader, it is not ideal but at least something:

const nextConfig = {
  webpack: (config) => {
    config.module.rules.push({
      test: /\.typia\.(ts)x?$/, // I put typia code in separate files just not to slow down overall build performance
      loader: 'ts-loader',
      options: {
        transpileOnly: false, // Important: otherwise there will be no type information
        compilerOptions: {
          noEmit: false, // Important: otherwise build will fail because of void emit
        },
      },
    });

    return config;
  },
};

Here is stackblitz
Maybe it is worth to be mentioned in docs?

@petertflem
Copy link

@aspirisen Could you elaborate on how your example plays along with Next? I am not super familiar with webpack, but I guess this will "run?" .typia.ts files with the ts-loader. But how does that play along with Next's bundling etc, and what code goes on the server side and on the client side? Does Next.js take the output of this rule without problems as if it were done by Next's own setup, or is there some drawbacks?

@aspirisen
Copy link

@petertflem except that you have to use separate file for typia code there should be no other disadvantages

@petertflem
Copy link

@aspirisen Would it matter much if it compiled other code? I don't really plan to, other than for types and interfaces, but just out of interest. It wouldn't use SWC probably, but other than that?

@aspirisen
Copy link

@petertflem you can transpile other code via ts-loader, but it is much slower than SWC

@petertflem
Copy link

@aspirisen Thanks for the reply! 🙂

@mp3por
Copy link

mp3por commented Sep 3, 2023

@petertflem you can transpile other code via ts-loader, but it is much slower than SWC

Can you show how ? If we use ts-loader will we be able to use this library ?

@samchon
Copy link
Owner

samchon commented Sep 3, 2023

https://typia.io/docs/setup/#webpack

This be helpful?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

7 participants