Skip to content

Commit 2cea66c

Browse files
authored
fix module resolution (#1080)
1 parent 66c4dae commit 2cea66c

File tree

15 files changed

+58
-36
lines changed

15 files changed

+58
-36
lines changed

plugins/postcss-design-tokens/.tape.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ postcssTape(plugin)({
9494
message: "supports value parsing (G)",
9595
warnings: 2
9696
},
97+
'imported-double-slash': {
98+
message: "supports 'node_modules://'",
99+
},
97100
'examples/example': {
98101
message: 'minimal example',
99102
options: {},

plugins/postcss-design-tokens/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes to PostCSS Design Tokens
22

3+
### Unreleased (minor)
4+
5+
- Add support for the shorter `node_modules:package` syntax
6+
- Fix node module resolution
7+
38
### 3.0.1
49

510
_July 24, 2023_

plugins/postcss-design-tokens/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ The `@design-tokens` rule is used to import design tokens from a JSON file into
321321
You can also import tokens from an `npm` package:
322322

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

328328
```

plugins/postcss-design-tokens/dist/index.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/postcss-design-tokens/dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ The `@design-tokens` rule is used to import design tokens from a JSON file into
190190
You can also import tokens from an `npm` package:
191191

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

197197
```

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { DEFAULT_CONDITION } from '../constants';
77
import module from 'module';
88
import type { Helpers, Root } from 'postcss';
99

10-
const require = module.createRequire(import.meta.url);
11-
1210
function parseImport(statement: string): { filePath: string, format: string, conditions: Array<string> } {
1311
const importAST = valueParser(statement);
1412

@@ -48,18 +46,21 @@ export async function tokensFromImport(root: Root, postcssHelpers: Helpers, buil
4846
}
4947

5048
let resolvedPath = '';
51-
if (filePath.startsWith('node_modules://')) {
52-
try {
53-
resolvedPath = require.resolve(filePath.slice(15), {
54-
paths: [
55-
path.dirname(sourceFilePath),
56-
],
57-
});
58-
} catch (e) {
59-
throw new Error(`Failed to read ${filePath} with error ${(e as Error).message}`);
49+
50+
try {
51+
if (filePath.startsWith('node_modules://')) {
52+
const require = module.createRequire(path.dirname(sourceFilePath));
53+
54+
resolvedPath = require.resolve(filePath.slice(15));
55+
} else if (filePath.startsWith('node_modules:')) {
56+
const require = module.createRequire(path.dirname(sourceFilePath));
57+
58+
resolvedPath = require.resolve(filePath.slice(13));
59+
} else {
60+
resolvedPath = path.resolve(path.dirname(sourceFilePath), filePath);
6061
}
61-
} else {
62-
resolvedPath = path.resolve(path.dirname(sourceFilePath), filePath);
62+
} catch (e) {
63+
throw new Error(`Failed to read ${filePath} with error ${(e as Error).message}`);
6364
}
6465

6566
if (alreadyImported.has(resolvedPath)) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@design-tokens url(node_modules://style-dictionary-design-tokens-example/style-dictionary.tokens.json) format('style-dictionary3');
2+
3+
.foo {
4+
color: design-token('color.font.base');
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: #111111;
3+
}
Lines changed: 1 addition & 1 deletion
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');

0 commit comments

Comments
 (0)