Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(format): new built-in format for es6 module object #1380

Merged
merged 5 commits into from
Nov 7, 2024

Conversation

jackholden
Copy link
Contributor

Based on a conversation on Slack to create a PR

This PR adds a new format for generating ES6 module exports for the dictionary object. Also includes a minify flag so you can output just the values.

Usage:

Config (config.js):

export default {
  source: ["src/tokens/color.json"],
  platforms: {
    css: {
      transformGroup: "css",
      files: [
        {
          destination: "vars.css",
          format: "css/variables",
        },
      ],
    },
    es6Module: {
      transformGroup: "js",
      files: [
        {
          destination: "vars.js",
          format: "javascript/es6-module",
          options: {
            minify: true,
          },
        },
      ],
    },
  },
};

Input (color.json):

{
  "colors": {
    "$type": "color",
    "black": {
      "$value": "#000000"
    },
    "white": {
      "$value": "#ffffff"
    },
    "orange": {
      "100": {
        "$value": "#fffaf0"
      },
      "200": {
        "$value": "#feebc8"
      },
      "300": {
        "$value": "#fbd38d"
      },
      "400": {
        "$value": "#f6ad55"
      },
      "500": {
        "$value": "#ed8936"
      },
      "600": {
        "$value": "#dd6b20"
      },
      "700": {
        "$value": "#c05621"
      },
      "800": {
        "$value": "#9c4221"
      },
      "900": {
        "$value": "#7b341e"
      }
    }
  }
}

Output:

export default {
  "colors": {
    "black": "#000000",
    "white": "#ffffff",
    "orange": {
      "100": "#fffaf0",
      "200": "#feebc8",
      "300": "#fbd38d",
      "400": "#f6ad55",
      "500": "#ed8936",
      "600": "#dd6b20",
      "700": "#c05621",
      "800": "#9c4221",
      "900": "#7b341e"
    }
  },
};

Example usage:

import vars from '@your-namespace-package/tokens';

console.log(vars.colors.black); // #000000

@jackholden jackholden requested a review from a team as a code owner November 3, 2024 17:22
Copy link

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

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

jorenbroekema
jorenbroekema previously approved these changes Nov 4, 2024
Copy link
Collaborator

@jorenbroekema jorenbroekema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing, but not a big deal, looks good to me

lib/common/formats.js Outdated Show resolved Hide resolved
Co-authored-by: Joren Broekema <joren.broekema@gmail.com>
Copy link
Collaborator

@jorenbroekema jorenbroekema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a commit with changeset and renamed it to javascript/esm for brevity

@jorenbroekema jorenbroekema merged commit 0fcf229 into amzn:main Nov 7, 2024
5 checks passed
tkgroot pushed a commit to tkgroot/style-dictionary that referenced this pull request Dec 9, 2024
* feat(format): new built-in format for es6 module object

* feat(format): additional es6 module object test and snapshot

* style(format): improve readability with additional indentation

Co-authored-by: Joren Broekema <joren.broekema@gmail.com>

* chore: add changeset and rename to javascript/esm

---------

Co-authored-by: Joren Broekema <joren.broekema@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants