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

Fix(variables-scss): Export in JS barrel file #DS-1503 #1687

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
'!.*.js',
'packages/analytics',
'packages/common',
'packages/design-tokens',
'packages/web-react',
'packages/web',
'packages/form-validations',
Expand Down
57 changes: 29 additions & 28 deletions exporters/variables-scss/generated/exporter.cjs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export const grids = {

const mockedRootThemeFile = fs.readFileSync(path.join(__dirname, '../__fixtures__/mockedRootThemeFile.scss'), 'utf-8');

const mockedRootThemeJsFile = `import * as themeLight from './themes/theme-light';
import * as themeLightInverted from './themes/theme-light-inverted';\n
const mockedRootThemeJsFile = `import * as themeLight from './theme-light';
import * as themeLightInverted from './theme-light-inverted';\n
// The first theme is the default theme, as the left column in the Figma table.
export const themes = {
themeLight: {
colors: themeLight.colors
colors: themeLight.colors,
},
themeLightInverted: {
colors: themeLightInverted.colors
colors: themeLightInverted.colors,
},
};
`;
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('fileGenerator', () => {
{ path: './js/global/', fileName: 'index.ts', content: barrelJsFile },
// Root barrel files
{ path: './scss/', fileName: '@global.scss', content: "@forward 'global';\n" },
{ path: './js/', fileName: '@global.ts', content: "export * from './global';\n" },
{ path: './js/', fileName: 'index.ts', content: "export * from './global';\nexport * from './themes';\n" },
// Themes files
{ path: './scss/themes/theme-light/', fileName: '_colors.scss', content: emptyFile },
{ path: './js/themes/theme-light/', fileName: 'colors.ts', content: emptyFile },
Expand All @@ -110,7 +110,7 @@ describe('fileGenerator', () => {
{ path: './js/themes/theme-light-inverted/', fileName: 'index.ts', content: barrelJsColorFile },
// Themes root barrel files
{ path: './scss/', fileName: '@themes.scss', content: mockedRootThemeFile },
{ path: './js/', fileName: '@themes.ts', content: mockedRootThemeJsFile },
{ path: './js/themes', fileName: 'index.ts', content: mockedRootThemeJsFile },
]);
});
});
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('fileGenerator', () => {

expect(content).toBe(
// eslint-disable-next-line prettier/prettier, quotes -- special characters in the string
'themeLight: {\ncolors: themeLight.colors\n},\nthemeLightInverted: {\ncolors: themeLightInverted.colors\n},',
'themeLight: {\ncolors: themeLight.colors,\n},\nthemeLightInverted: {\ncolors: themeLightInverted.colors,\n},',
);
});
});
Expand All @@ -219,7 +219,7 @@ describe('fileGenerator', () => {
const content = generateRootThemesFileImports(themes as TokenTheme[], true);

expect(content).toBe(
"import * as themeLight from './themes/theme-light';\nimport * as themeLightInverted from './themes/theme-light-inverted';",
"import * as themeLight from './theme-light';\nimport * as themeLightInverted from './theme-light-inverted';",
);
});
});
Expand All @@ -228,7 +228,7 @@ describe('fileGenerator', () => {
it('should generate js import statement', () => {
const content = jsImportStatement('theme-light');

expect(content).toBe("import * as themeLight from './themes/theme-light';");
expect(content).toBe("import * as themeLight from './theme-light';");
});
});

Expand Down
14 changes: 9 additions & 5 deletions exporters/variables-scss/src/generators/fileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const generateBarrelFile = (files: { fileName: string; content: string }[
.join('\n')}\n`;
};
export const jsImportStatement = (name: string) => {
return `import * as ${toCamelCase(name)} from './${THEMES_DIRECTORY}/${name}';`;
return `import * as ${toCamelCase(name)} from './${name}';`;
};

export const scssImportStatement = (name: string) => `@use '${THEMES_DIRECTORY}/${name}';`;
Expand All @@ -56,7 +56,7 @@ export const generateRootThemesFileContent = (themes: TokenTheme[], hasJsOutput:
return themes
.map((theme) => {
return hasJsOutput
? `${toCamelCase(theme.name)}: {\n${COLOR_KEY}: ${toCamelCase(theme.name)}.${COLOR_KEY}\n},`
? `${toCamelCase(theme.name)}: {\n${COLOR_KEY}: ${toCamelCase(theme.name)}.${COLOR_KEY},\n},`
: `${theme.name}: (\n${COLOR_KEY}: ${theme.name}.$${COLOR_KEY},\n),`;
})
.join('\n');
Expand Down Expand Up @@ -115,8 +115,8 @@ export const generateOutputFilesByThemes = async (
});
outputFiles.push({
path: `./${JS_DIRECTORY}/`,
fileName: '@global.ts',
content: `export * from './${GLOBAL_DIRECTORY}';\n`,
fileName: 'index.ts',
content: `export * from './${GLOBAL_DIRECTORY}';\nexport * from './${THEMES_DIRECTORY}';\n`,
});

// Compute themed tokens for all themes in parallel
Expand Down Expand Up @@ -162,7 +162,11 @@ export const generateOutputFilesByThemes = async (
const rootThemesFileContent = generateThemesRootFile(themes);
const rootTsThemesFileContent = generateThemesRootFile(themes, true);
outputFiles.push({ path: `./${SCSS_DIRECTORY}/`, fileName: '@themes.scss', content: rootThemesFileContent });
outputFiles.push({ path: `./${JS_DIRECTORY}/`, fileName: '@themes.ts', content: rootTsThemesFileContent });
outputFiles.push({
path: `./${JS_DIRECTORY}/${THEMES_DIRECTORY}`,
fileName: 'index.ts',
content: rootTsThemesFileContent,
});

return outputFiles;
};
18 changes: 18 additions & 0 deletions packages/design-tokens/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# .eslintignore

node_modules

# NOTE:
# The following directives are only relevant when linting the whole
# project directory, ie. running `eslint .` ⚠️

# If you compile JavaScript into some output folder, exclude it here
dist
build

# Highly recommended to re-include JavaScript dotfiles to lint them
# (This will cause .eslintrc.js to be linted by ESLint 🤘)
!.*.js

# Some tools use this pattern for their configuration files. Lint them!
!*.config.js
12 changes: 12 additions & 0 deletions packages/design-tokens/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
extends: ['../../.eslintrc', '@lmc-eu/eslint-config-typescript', '@lmc-eu/eslint-config-jest'],

parserOptions: {
ecmaVersion: 'latest',
project: './tsconfig.eslint.json',
},

rules: {
'prettier/prettier': 'off',
},
};
8 changes: 7 additions & 1 deletion packages/design-tokens/.stylelintrc.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import stylelintConfig from 'stylelint-config-spirit';

export default stylelintConfig;
export default {
...stylelintConfig,

rules: {
'prettier/prettier': null,
},
};
4 changes: 3 additions & 1 deletion packages/design-tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"build:js": "vite build",
"build:scss": "shx mkdir -p scss && shx cp -r src/scss/generated/* src/scss/@tokens.scss scss/",
"clean": "rimraf esm cjs umd scss types",
"lint": "stylelint ./src/**/*.scss",
"lint": "npm-run-all --serial lint:scripts lint:styles",
"lint:scripts": "eslint ./src/**/*.ts",
"lint:styles": "stylelint ./src/**/*.scss",
"test": "yarn lint",
"types": "tsc"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/design-tokens/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["./", "./.eslintrc.cjs"]
}
Loading