Skip to content

Commit fe2a590

Browse files
authored
design-tokens : implement require.resolve (#591)
* design-tokens : resolve node_modules * docs and changelog
1 parent c3611f4 commit fe2a590

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

plugins/postcss-design-tokens/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
### Unreleased
44

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

8+
npm package : `@your-org/your-tokens`
9+
710
```json
811
{
912
"font-size": 0.2
1013
}
1114
```
1215

1316
```css
17+
@design-tokens url(node_modules://@your-org/your-tokens/tokens.json) format('style-dictionary3');
18+
1419
.example {
1520
font-size: design-token('font-size' to vh);
1621

plugins/postcss-design-tokens/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ The `@design-tokens` rule is used to import design tokens from a JSON file into
282282
@design-tokens url('./tokens-dark-mode.json') format('style-dictionary3') when('dark');
283283
```
284284

285+
You can also import tokens from an `npm` pacakge:
286+
287+
```pcss
288+
@design-tokens url('node_modules://my-npm-package/tokens.json') format('style-dictionary3');
289+
@design-tokens url('node_modules://my-npm-package/tokens-dark-mode.json') format('style-dictionary3') when('dark');
290+
```
291+
285292
```
286293
@design-tokens [ <url> | <string> ]
287294
[ when(<theme-condition>*) ]?

plugins/postcss-design-tokens/docs/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ The `@design-tokens` rule is used to import design tokens from a JSON file into
185185
@design-tokens url('./tokens-dark-mode.json') format('style-dictionary3') when('dark');
186186
```
187187

188+
You can also import tokens from an `npm` pacakge:
189+
190+
```pcss
191+
@design-tokens url('node_modules://my-npm-package/tokens.json') format('style-dictionary3');
192+
@design-tokens url('node_modules://my-npm-package/tokens-dark-mode.json') format('style-dictionary3') when('dark');
193+
```
194+
188195
```
189196
@design-tokens [ <url> | <string> ]
190197
[ when(<theme-condition>*) ]?

plugins/postcss-design-tokens/src/data-formats/parse-import.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { extractStyleDictionaryTokens } from './style-dictionary/style-dictionar
44
import path from 'path';
55
import { promises as fsp } from 'fs';
66
import { DEFAULT_CONDITION } from '../constants';
7+
import module from 'module';
8+
9+
const require = module.createRequire(import.meta.url);
710

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

46-
const resolvedPath = path.resolve(path.dirname(sourceFilePath), filePath);
49+
let resolvedPath = '';
50+
if (filePath.startsWith('node_modules://')) {
51+
try {
52+
resolvedPath = require.resolve(filePath.slice(15), {
53+
paths: [
54+
path.dirname(sourceFilePath),
55+
],
56+
});
57+
} catch (e) {
58+
throw new Error(`Failed to read ${filePath} with error ${(e as Error).message}`);
59+
}
60+
} else {
61+
resolvedPath = path.resolve(path.dirname(sourceFilePath), filePath);
62+
}
63+
4764
if (alreadyImported.has(resolvedPath)) {
4865
return false;
4966
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@design-tokens url('../../../node_modules/style-dictionary-design-tokens-example/style-dictionary.tokens.json') format('style-dictionary3');
1+
@design-tokens url(node_modules://style-dictionary-design-tokens-example/style-dictionary.tokens.json) format('style-dictionary3');

rollup/configs/externals.js

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const externalsForCLI = [
5959
export const externalsForPlugin = [
6060
'assert',
6161
'fs',
62+
'module',
6263
'path',
6364
'url',
6465
'vm',

0 commit comments

Comments
 (0)