mattpocock / xstate-codegen

A codegen tool for 100% TS type-safety in XState
MIT License
245 stars 12 forks source link

JSX files cannot be imported #21

Open mattpocock opened 4 years ago

mattpocock commented 4 years ago
SyntaxError: C:\...\alert\index.tsx: Unexpected token, expected "," (53:20)

  51 |
  52 |   const list = alerts.map((alert, index) => (
> 53 |     <AlertComponent key={index} status={alert.status}>
     |                     ^
  54 |       ...
mattpocock commented 4 years ago

Suggested fix would be to add a babel JSX plugin here:

const build = await rollup({
  input: resolvedFilePath,
  external: (id) => !/^(\.|\/|\w:)/.test(id),
  plugins: [
    nodeResolvePlugin({
      extensions,
    }),
    babelPlugin({
      babelHelpers: 'bundled',
      extensions,
      plugins: [
        '@babel/plugin-transform-typescript',
        '@babel/plugin-proposal-optional-chaining',
        ...
      ],
    }),
  ],
});
rjdestigter commented 4 years ago

I tried with

[
    '@babel/plugin-transform-typescript', { isTSX: true }],
    '@babel/plugin-transform-react-jsx',
]

First without isTSX: true but that didn't make a difference. Setting isTSX to true seems to resolve the issue (I'm running into imported css now lol) but I'm not sure if that's preferable.

mattpocock commented 4 years ago

@Andarist's new system will be completely Typescript-based, meaning we won't have to worry about Babel configs and can drop a lot of the complexity in extractMachines.ts. The current workaround would probably to put your files with machines in files which don't export JSX files or CSS files. How painful is that for you?

rjdestigter commented 4 years ago

The current workaround would probably to put your files with machines in files which don't export JSX files or CSS files. How painful is that for you?

Not at all. I was able to refactor the the module being imported from and exclude the files that were using jsx/css.