Skip to content

design-tokens : implement require.resolve #591

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

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
5 changes: 5 additions & 0 deletions plugins/postcss-design-tokens/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

### Unreleased

- Added support for design token file imports from npm packages.
- Added support for arbitrary unit assignment to unit-less design tokens.

npm package : `@your-org/your-tokens`

```json
{
"font-size": 0.2
}
```

```css
@design-tokens url(node_modules://@your-org/your-tokens/tokens.json) format('style-dictionary3');

.example {
font-size: design-token('font-size' to vh);

Expand Down
7 changes: 7 additions & 0 deletions plugins/postcss-design-tokens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ The `@design-tokens` rule is used to import design tokens from a JSON file into
@design-tokens url('./tokens-dark-mode.json') format('style-dictionary3') when('dark');
```

You can also import tokens from an `npm` pacakge:

```pcss
@design-tokens url('node_modules://my-npm-package/tokens.json') format('style-dictionary3');
@design-tokens url('node_modules://my-npm-package/tokens-dark-mode.json') format('style-dictionary3') when('dark');
```

```
@design-tokens [ <url> | <string> ]
[ when(<theme-condition>*) ]?
Expand Down
7 changes: 7 additions & 0 deletions plugins/postcss-design-tokens/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ The `@design-tokens` rule is used to import design tokens from a JSON file into
@design-tokens url('./tokens-dark-mode.json') format('style-dictionary3') when('dark');
```

You can also import tokens from an `npm` pacakge:

```pcss
@design-tokens url('node_modules://my-npm-package/tokens.json') format('style-dictionary3');
@design-tokens url('node_modules://my-npm-package/tokens-dark-mode.json') format('style-dictionary3') when('dark');
```

```
@design-tokens [ <url> | <string> ]
[ when(<theme-condition>*) ]?
Expand Down
19 changes: 18 additions & 1 deletion plugins/postcss-design-tokens/src/data-formats/parse-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { extractStyleDictionaryTokens } from './style-dictionary/style-dictionar
import path from 'path';
import { promises as fsp } from 'fs';
import { DEFAULT_CONDITION } from '../constants';
import module from 'module';

const require = module.createRequire(import.meta.url);

function parseImport(statement: string): { filePath: string, format: string, conditions: Array<string> } {
const importAST = valueParser(statement);
Expand Down Expand Up @@ -43,7 +46,21 @@ export async function tokensFromImport(buildIs: Array<string>, sourceFilePath: s
return false;
}

const resolvedPath = path.resolve(path.dirname(sourceFilePath), filePath);
let resolvedPath = '';
if (filePath.startsWith('node_modules://')) {
try {
resolvedPath = require.resolve(filePath.slice(15), {
paths: [
path.dirname(sourceFilePath),
],
});
} catch (e) {
throw new Error(`Failed to read ${filePath} with error ${(e as Error).message}`);
}
} else {
resolvedPath = path.resolve(path.dirname(sourceFilePath), filePath);
}

if (alreadyImported.has(resolvedPath)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/postcss-design-tokens/test/imported.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@design-tokens url('../../../node_modules/style-dictionary-design-tokens-example/style-dictionary.tokens.json') format('style-dictionary3');
@design-tokens url(node_modules://style-dictionary-design-tokens-example/style-dictionary.tokens.json) format('style-dictionary3');
1 change: 1 addition & 0 deletions rollup/configs/externals.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const externalsForCLI = [
export const externalsForPlugin = [
'assert',
'fs',
'module',
'path',
'url',
'vm',
Expand Down