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

error : StyleDictionary.extend is not a function #1346

Closed VijaytaWJ closed 1 month ago

VijaytaWJ commented 2 months ago

Hello All, I am trying to use StyleDictionary as a module, the following is the code.

import StyleDictionary from 'style-dictionary';
// Extend Style Dictionary
const StyleDictionaryExtended = StyleDictionary.extend({
    source: ['tokens/**/*.json'],
    platforms: {
        dart: {
            transformGroup: 'dart',
            buildPath: 'build/dart/',
            files: [
                {
                    destination: 'tokens.dart',
                    format: 'dart/class',
                    className: 'RIBUIT',
                },
            ],
        },
    },
});
StyleDictionaryExtended.buildAllPlatforms();

however i get error as "TypeError: StyleDictionary.extend is not a function " , i checked the path, it is referring to the correct StyleDictonary.js and it does have a extend method, have read many tutorials to double check the configuration but not sure what the issue is

Style dictionary version 4.1.2 and node js version 20.17.0

jorenbroekema commented 1 month ago

https://styledictionary.com/version-4/migration/ you'll need to adjust your code for v4, StyleDictionary.extend is no longer the method for initialising a dictionary, you need to use:

import StyleDictionary from 'style-dictionary';

// initialise Style Dictionary
const sd = new StyleDictionary({
    source: ['tokens/**/*.json'],
    platforms: {
        dart: {
            transformGroup: 'dart',
            buildPath: 'build/dart/',
            files: [
                {
                    destination: 'tokens.dart',
                    format: 'dart/class',
                    options: {
                      className: 'RIBUIT',
                    },
                },
            ],
        },
    },
});
await sd.buildAllPlatforms();
VijaytaWJ commented 1 month ago

thanks Jorenbrokekema, i fixed that by using new StyleDictionary constructor. However i now get error while registering transformer, the error is get is Error: transform must be a function

StyleDictionary.registerTransform({ name: 'name', type: 'name', transformer: (token, _, options) => { return token.name; } });

jorenbroekema commented 1 month ago

transformer was changed to transform, I would advise going through the migration docs entirely and maybe run the codemods that are available since recently to automatically try to migrate your code from v3 to v4

VijaytaWJ commented 1 month ago

Thanks for suggestions, changing it to transform worked