diff --git a/.changeset/red-walls-allow.md b/.changeset/red-walls-allow.md new file mode 100644 index 0000000..5fdf1d5 --- /dev/null +++ b/.changeset/red-walls-allow.md @@ -0,0 +1,5 @@ +--- +'xstate-codegen': patch +--- + +Add support for babel macros used in machine code. diff --git a/packages/xstate-compiled/examples/withMacros.machine.ts b/packages/xstate-compiled/examples/withMacros.machine.ts new file mode 100644 index 0000000..247d5f3 --- /dev/null +++ b/packages/xstate-compiled/examples/withMacros.machine.ts @@ -0,0 +1,35 @@ +import env from 'penv.macro'; +import { Machine } from '@xstate/compiled'; + +type LightEvent = { type: 'TIMER' } | { type: 'POWER_OUTAGE' }; + +interface LightContext { + elapsed: number; +} + +const withMacros = Machine({ + initial: 'green', + context: { elapsed: 0 }, + on: { + POWER_OUTAGE: { + target: '.red', + }, + }, + states: { + green: { + on: { + TIMER: env({ development: 'red' }, 'yellow'), + }, + }, + yellow: { + on: { + TIMER: 'red', + }, + }, + red: { + on: { + TIMER: 'green', + }, + }, + }, +}); diff --git a/packages/xstate-compiled/package.json b/packages/xstate-compiled/package.json index 04827c5..980ca51 100644 --- a/packages/xstate-compiled/package.json +++ b/packages/xstate-compiled/package.json @@ -36,6 +36,7 @@ "@types/node": "^14.0.14", "@types/rimraf": "^3.0.0", "@xstate/react": "^0.8.1", + "penv.macro": "^0.4.0", "nodemon": "2.0.4", "typescript": "^3.9.7" }, diff --git a/packages/xstate-compiled/src/extractMachines.ts b/packages/xstate-compiled/src/extractMachines.ts index 44f618c..e988b75 100644 --- a/packages/xstate-compiled/src/extractMachines.ts +++ b/packages/xstate-compiled/src/extractMachines.ts @@ -257,6 +257,7 @@ export const extractMachines = async ( babelHelpers: 'bundled', extensions, plugins: [ + 'babel-plugin-macros', '@babel/plugin-transform-typescript', '@babel/plugin-proposal-optional-chaining', '@babel/plugin-proposal-nullish-coalescing-operator', diff --git a/yarn.lock b/yarn.lock index 1be2be8..1763eac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -221,7 +221,7 @@ dependencies: regenerator-runtime "^0.13.4" -"@babel/runtime@^7.7.2": +"@babel/runtime@^7.5.0", "@babel/runtime@^7.7.2": version "7.11.2" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736" integrity sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw== @@ -2998,6 +2998,13 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== +penv.macro@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/penv.macro/-/penv.macro-0.4.0.tgz#06e810a8234a07cb09f7a4c0819d8f88ec7d159a" + integrity sha512-rvqR3AreOAkii3ZPAiH+V4nNSk9W5bUAaT5+D9YK2dPzPmGZcFjuVyfBYmbkm979fR6gVoEfnvGMGTk4CFPskQ== + dependencies: + "@babel/runtime" "^7.5.0" + picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1, picomatch@^2.2.2: version "2.2.2" resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"