plugins icon indicating copy to clipboard operation
plugins copied to clipboard

feat(relay): add esm support

Open XiNiHa opened this issue 3 years ago • 11 comments

XiNiHa avatar Jun 17 '22 12:06 XiNiHa

Mind elaborate what this PR exactly does? description / test cases?

kwonoj avatar Jun 19 '22 00:06 kwonoj

This PR lets users to use module: 'esm' option to emit top level import and its reference instead of require() calls. I'll add test cases this evening.

XiNiHa avatar Jun 19 '22 02:06 XiNiHa

Well, just realized it had no test code at all. It feels a bit hard to write all the test codes by myself. Can we continue the review with the current state? @kwonoj

XiNiHa avatar Jun 19 '22 13:06 XiNiHa

I'd be hesitant to review PR without having any proper description or test cases to explain what it does exactly.

kwonoj avatar Jun 19 '22 13:06 kwonoj

This PR lets users to use module: 'esm' option to emit top level import and its reference instead of require() calls.

I thought this is enough for describing what this PR does. Do you need any more details?

XiNiHa avatar Jun 19 '22 13:06 XiNiHa

This PR lets users to use module: 'esm' option to emit top level import and its reference instead of require() calls.

The reason I asked for test case is it is a snapshot to show pseudocode what it looks like for the input / outputs. Single line text doesn't seem to describe it precisely, while I can implicitly see behavior though. After all, having proper PR body serves as documentation for the future search context if we need to look back history, and for me it's hard to say current PR description / body fulfills those purpose.

kwonoj avatar Jun 19 '22 13:06 kwonoj

The current version of Relay plugin receives this input:

import { graphql } from 'relay'

const query = graphql`
  query MyQuery {
    foo { bar }
  }
`

and emits this:

import { graphql } from 'relay'

const query = require('./__generated__/MyQuery__graphql')

However, this is indeed ESM incompatible. Therefore this PR adds new module option to let users change the output to be ESM-compatible, like this:

import { graphql } from 'relay'
import MyQuery__graphql from './__generated__/MyQuery.graphql'

const query = MyQuery__graphql

XiNiHa avatar Jun 19 '22 14:06 XiNiHa

Thanks for the clarification. This helps a lot.

Random thought: what if we honor swcrc's module.type config instead of plugin specifies duplicated option for it again? say, like

//.swcrc
{
 "module": {
   // this controls whole transform to emit either cjs or preserve es6, plugin will honor this
  // ignore (or fallback) not yet supported types (amd, etcs) into cjs for now
   "type" : 'commonjs' | 'es6'
 }
}

This'll probably need upstream swc/core change though, as it needs to deliver swc config into plugin.

kwonoj avatar Jun 19 '22 14:06 kwonoj

That sounds very reasonable. What were the previous opinions from the team about exposing the whole swcrc to plugins? Can it be done without sacrificing anything serious? If there's nothing or tiny problems, I guess it'll be much better to go that way.

XiNiHa avatar Jun 19 '22 14:06 XiNiHa

What were the previous opinions from the team about exposing the whole swcrc to plugins?

It was not included as there was no concrete use case for the first phase of implementation.

Can it be done without sacrificing anything serious?

Plugin will need to deserialize swcrc in addition to plugin's config, which would be small overhead. I wouldn't expect it's critical serious.

kwonoj avatar Jun 19 '22 14:06 kwonoj

I'd then wait until that gets implemented.

XiNiHa avatar Jun 19 '22 14:06 XiNiHa

Any updates on this ? At present the plugin is not useable with typescript. seems like its very close no ?

mickdelaney avatar Feb 23 '23 21:02 mickdelaney

I think it's fine to use ESM imports unconditionally

kdy1 avatar Feb 24 '23 01:02 kdy1

That sounds reasonable. I'll look into it

XiNiHa avatar Feb 24 '23 06:02 XiNiHa

Done! Can you review the changes again @kwonoj

XiNiHa avatar Feb 24 '23 06:02 XiNiHa