amzn / style-dictionary

A build system for creating cross-platform styles.
https://styledictionary.com
Apache License 2.0
3.93k stars 558 forks source link

feat(typescript/combine-json): add .ts file processing if runtime sup… #1392

Open hsjobeki opened 1 week ago

hsjobeki commented 1 week ago

As described in https://github.com/amzn/style-dictionary/issues/1391 many runtimes implement support for executing .ts files without any need for transpilation.

It seems the only problem is that style-dictionary tries to parse .ts files with JSON5.parse rather than just importing the file like the regular .js files.

I've tested the changes with:

closes #1391

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

aws-amplify-us-west-1[bot] commented 1 week ago

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-1392.d16eby4ekpss5y.amplifyapp.com

hsjobeki commented 1 week ago

For reference here is the example log output for running node without --experimental-strip-types

Could not import TypeScript file: ./tokens/token.ts

Executing typescript files during runtime is only possible via
- 'node >= 22.6.0' with '--experimental-strip-types'

Alternatively by using 'deno' or 'bun'

If you are not able to satisfy the above requirements, consider transpiling the TypeScript file to plain JavaScript before running the Style Dictionary build process.

node:internal/modules/esm/get_format:218
  throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath);
        ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Failed to load or parse JSON or JS Object: Failed to load or parse JSON or JS Object: Unknown file extension ".ts" for ./tokens/token.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:218:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:244:36)
    at defaultLoad (node:internal/modules/esm/load:122:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:570:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:443:45)
    at async ModuleJob._link (node:internal/modules/esm/module_job:106:19) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
Node.js v22.9.0