mattpocock / xstate-codegen

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

using a class from another file in the initial context causes SyntaxError: Unexpected token #16

Closed redappleorigin closed 4 years ago

redappleorigin commented 4 years ago

I have an example branch here: https://github.com/redappleorigin/xstate-codegen/tree/imported-class-syntax-error

It is forked off of xstate-codegen@0.1.0-next.7

I added a new folder and file to examples 'models/example.model.ts' and modified fetchMachine.machine.ts so that it specifies an initialContext with one of the properties being new ExampleModel(). This causes a SyntaxError: Unexpected token 'export'

redappleorigin commented 4 years ago

The interface referencing the imported class is fine and does not throw an error. It seems that trying to instantiate my ExampleModel class anywhere in the machine file will cause that SyntaxError.

redappleorigin commented 4 years ago

I'm using node v14.2.0 on a windows 10 machine.

mattpocock commented 4 years ago

@redappleorigin I'm off on holiday today, so this fix might have to wait a few days. @Andarist would you know a reason why importing a class might mess with the extractMachines rollup config?

mattpocock commented 4 years ago

@redappleorigin Do you have a local babel config in the dir where you're running the codegen tool?

redappleorigin commented 4 years ago

@mattpocock no sir

Andarist commented 4 years ago

Not sure why this wouldn't be working - Rollup should handle your model file and transpile it down to CJS. I'm rewriting the extraction from the current Babel+Rollup+execute setup to a pure TS-based one, so I don't think it's worth looking into it now as it should just get solved soon-ish anyway.

redappleorigin commented 4 years ago

Did the example I provided have the same issue for you?

redappleorigin commented 4 years ago

Just curious if maybe it is my machine somehow.

rjdestigter commented 4 years ago

I'm running into this issue as well

redappleorigin commented 4 years ago

@Andarist I don't suppose you have a bleeding edge branch I could work off, would you?

mattpocock commented 4 years ago

@redappleorigin back from hols, so will have a look at your example tomorrow. Agree with @Andarist that this is odd.

rjdestigter commented 4 years ago

In my case it has to do with an import enum. As soon as I import it from another file rather than declare it in the same module where it is being used the SyntaxError: Unexpected token 'export' error shows up.

Edit: Removing enum usage from the imported file doesn't resolve the issue either though.

rjdestigter commented 4 years ago

It's because the configuration for external that is passed to rollup in extractMachines.ts isn't working correctly. It's marking files that start with a forward slash (/) as external when they're not.

(Relative paths are being converted to absolute paths and those absolute paths are marked as external)

mattpocock commented 4 years ago

@rjdestigter would this work?

// Tests for '.' or '/' at the start of a string
const internalRegex = /^(\.|\/)/;
external: (id) => !internalRegex.test(id),
redappleorigin commented 4 years ago

@mattpocock in my case I am on windows, so a slight change was required:


// Tests for '.', '/', or some drive letter followed immediately by a ':' at the start of a string
const internalRegex = /^(\.|\/|\w:)/.test(id);
external: (id) => !internalRegex.test(id),
mattpocock commented 4 years ago

Released under 0.1.0-next.8