style-dictionary icon indicating copy to clipboard operation
style-dictionary copied to clipboard

Source tokens and config in sub-directory/folder

Open fredboyle opened this issue 2 years ago • 6 comments

We have our Style Dictionary setup in a sub-directory of the root of the project. What's the best way to setup the config.json to ensure all paths are relative to the base Style Dictionary folder and not the root of the project?

I've tried various StyleDictionary.extend(...) variations in a custom build script but have only had luck with:

let StyleDictionaryExtended = StyleDictionary.extend(
  __dirname + "/../config.json"
);
StyleDictionaryExtended = StyleDictionary.extend({
  ...StyleDictionaryExtended,
  source: [__dirname + "/../tokens/**/*.json"],
});

Changing the above to the following didn't help either:

let StyleDictionaryExtended = StyleDictionary.extend(
  __dirname + "/../config.json"
);
StyleDictionaryExtended = StyleDictionary.extend({
  ...StyleDictionaryExtended,
  source: [__dirname + "/../tokens/**/*.json"],
  platforms: {
    css: { buildPath: __dirname + "/../build/css/" },
    scss: { buildPath: __dirname + "/../build/scss/" },
    json: { buildPath: __dirname + "/../build/json/" },
  },
});

Also changing the directories to more absolute values means the plain style-dictionary build command only functions from the root of our project.

Thanks for your help.

fredboyle avatar Apr 08 '22 22:04 fredboyle

Hey, I don't quite understand your use-case/what the goal is with this, would you mind elaborating on the use case further? Additionally, where were you running style-dictionary build from when not using absolute paths?

404tonynotfound avatar Apr 28 '22 21:04 404tonynotfound

@spirkolos Attached is a screenshot of the directory structure in our project. You'll notice that Style Dictionary is self contained within a subdirectory.

When we run our custom Style Dictionary build script, in src/style-dictionary/scripts/build-custom.js, from the root of our project the paths are not relative to the Style Dictionary config.json file's location but to the root of the project where we ran the script from.

So when we run our custom command of npm run build:tokens in the root of our project the paths end up being relative to the root location instead of the self contained Style Dictionary subdirectory.

Does that help?

Screen Shot 2022-04-28 at 14 35 23

fredboyle avatar Apr 28 '22 21:04 fredboyle

Can you provide your config.json?

chazzmoney avatar May 10 '22 21:05 chazzmoney

{
  "source": ["tokens/**/*.json"],
  "platforms": {
    "scss": {
      "transformGroup": "custom/scss",
      "prefix": "pds",
      "buildPath": "src/styles/global/",
      "files": [
        {
          "destination": "_variables.scss",
          "format": "scss/variables"
        }
      ]
    },
    "css": {
      "transformGroup": "custom/css",
      "prefix": "pds",
      "buildPath": "src/style-dictionary/_build/css/",
      "files": [
        {
          "destination": "design-tokens.css",
          "format": "css/variables"
        }
      ]
    },
    "json": {
      "transformGroup": "custom/css",
      "buildPath": "src/style-dictionary/_build/json/",
      "files": [
        {
          "destination": "design-tokens.json",
          "format": "json/nested"
        }
      ]
    }
  }
}

fredboyle avatar May 10 '22 22:05 fredboyle

Checking in for any help on this. Thanks.

fredboyle avatar Jun 10 '22 16:06 fredboyle

This doesn't work me either, I had to change my cwd to the location of the project.

Environment

Windows Node 18.12.1

Error given:

image

Directory structure:

image

Relative paths in config.json:

image

Package.json scripts to build:

image

Script that worked:

image

Conclusion

The problem is that all relative paths in config.json are resolved relative to the current working directory, rather than relative to the config.json itself. Not sure if that's intended behavior when working with the --config option to the build command.

Remaining problems

Now because I changed my cwd, if I want to include other base files, I need to use relative paths to get back up to node_modules:

config.json:

image

stephan-noel avatar Jul 22 '23 03:07 stephan-noel