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

+3
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

+5
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

+2-2
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

+1-1
Large diffs are not rendered by default.

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

+1-1
Large diffs are not rendered by default.

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

+2-2
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

+14-13
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)) {
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: #111111;
3+
}
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');

plugins/postcss-global-data/.tape.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ postcssTape(plugin)({
2020
plugins: [
2121
plugin({
2222
files: [
23-
'node_modules://open-props/media.min.css',
23+
'node_modules:open-props/media.min.css',
2424
'node_modules://open-props/open-props.min.css',
2525
]
2626
}),

plugins/postcss-global-data/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes to PostCSS global-data
22

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

510
_July 24, 2023_
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"use strict";var e=require("path"),r=require("fs");const t=require("module").createRequire("undefined"==typeof document?require("url").pathToFileURL(__filename).href:document.currentScript&&document.currentScript.src||new URL("index.cjs",document.baseURI).href);function parseImport(s,n,o,a){var c;let i="";if(o.startsWith("node_modules://"))try{i=t.resolve(o.slice(15),{paths:[e.dirname(o)]})}catch(e){throw new Error(`Failed to read ${o} with error ${e.message}`)}else i=e.resolve(o);if(a.has(i))return!1;a.add(i),n.result.messages.push({type:"dependency",plugin:"postcss-global-data",file:i,parent:null==(c=s.source)||null==(c=c.input)?void 0:c.file});const u=r.readFileSync(i,"utf8");return n.postcss.parse(u,{from:i})}const creator=e=>{const r=Object.assign({files:[]},e);return{postcssPlugin:"postcss-global-data",prepare(){let e=new Set,t=new Set;return{Once:(s,n)=>{r.files.forEach((r=>{if(e.has(r))return;const o=parseImport(s,n,r,e);o&&o.each((e=>{s.append(e),t.add(e)}))}))},OnceExit:()=>{t.forEach((e=>{e.remove()})),t=new Set,e=new Set}}}}};creator.postcss=!0,module.exports=creator;
1+
"use strict";var e=require("path"),s=require("fs"),r=require("module");function parseImport(t,o,a,n){var c;let l="";try{if(a.startsWith("node_modules://")){l=r.createRequire(process.cwd()).resolve(a.slice(15))}else if(a.startsWith("node_modules:")){l=r.createRequire(process.cwd()).resolve(a.slice(13))}else l=e.resolve(a)}catch(e){throw new Error(`Failed to read ${a} with error ${e.message}`)}if(n.has(l))return!1;n.add(l),o.result.messages.push({type:"dependency",plugin:"postcss-global-data",file:l,parent:null==(c=t.source)||null==(c=c.input)?void 0:c.file});const i=s.readFileSync(l,"utf8");return o.postcss.parse(i,{from:l})}const creator=e=>{const s=Object.assign({files:[]},e);return{postcssPlugin:"postcss-global-data",prepare(){let e=new Set,r=new Set;return{Once:(t,o)=>{s.files.forEach((s=>{if(e.has(s))return;const a=parseImport(t,o,s,e);a&&a.each((e=>{t.append(e),r.add(e)}))}))},OnceExit:()=>{r.forEach((e=>{e.remove()})),r=new Set,e=new Set}}}}};creator.postcss=!0,module.exports=creator;
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import e from"path";import t from"fs";import r from"module";const s=r.createRequire(import.meta.url);function parseImport(r,o,a,n){var l;let p="";if(a.startsWith("node_modules://"))try{p=s.resolve(a.slice(15),{paths:[e.dirname(a)]})}catch(e){throw new Error(`Failed to read ${a} with error ${e.message}`)}else p=e.resolve(a);if(n.has(p))return!1;n.add(p),o.result.messages.push({type:"dependency",plugin:"postcss-global-data",file:p,parent:null==(l=r.source)||null==(l=l.input)?void 0:l.file});const i=t.readFileSync(p,"utf8");return o.postcss.parse(i,{from:p})}const creator=e=>{const t=Object.assign({files:[]},e);return{postcssPlugin:"postcss-global-data",prepare(){let e=new Set,r=new Set;return{Once:(s,o)=>{t.files.forEach((t=>{if(e.has(t))return;const a=parseImport(s,o,t,e);a&&a.each((e=>{s.append(e),r.add(e)}))}))},OnceExit:()=>{r.forEach((e=>{e.remove()})),r=new Set,e=new Set}}}}};creator.postcss=!0;export{creator as default};
1+
import e from"path";import s from"fs";import r from"module";function parseImport(t,o,a,n){var l;let c="";try{if(a.startsWith("node_modules://")){c=r.createRequire(process.cwd()).resolve(a.slice(15))}else if(a.startsWith("node_modules:")){c=r.createRequire(process.cwd()).resolve(a.slice(13))}else c=e.resolve(a)}catch(e){throw new Error(`Failed to read ${a} with error ${e.message}`)}if(n.has(c))return!1;n.add(c),o.result.messages.push({type:"dependency",plugin:"postcss-global-data",file:c,parent:null==(l=t.source)||null==(l=l.input)?void 0:l.file});const i=s.readFileSync(c,"utf8");return o.postcss.parse(i,{from:c})}const creator=e=>{const s=Object.assign({files:[]},e);return{postcssPlugin:"postcss-global-data",prepare(){let e=new Set,r=new Set;return{Once:(t,o)=>{s.files.forEach((s=>{if(e.has(s))return;const a=parseImport(t,o,s,e);a&&a.each((e=>{t.append(e),r.add(e)}))}))},OnceExit:()=>{r.forEach((e=>{e.remove()})),r=new Set,e=new Set}}}}};creator.postcss=!0;export{creator as default};

plugins/postcss-global-data/src/parse-import.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ import fs from 'fs';
33
import type { Helpers, Root } from 'postcss';
44
import module from 'module';
55

6-
const require = module.createRequire(import.meta.url);
7-
86
export function parseImport(root: Root, postcssHelpers: Helpers, filePath: string, alreadyImported: Set<string>) {
97
let resolvedPath = '';
108

11-
if (filePath.startsWith('node_modules://')) {
12-
try {
13-
resolvedPath = require.resolve(filePath.slice(15), {
14-
paths: [
15-
path.dirname(filePath),
16-
],
17-
});
18-
} catch (e) {
19-
throw new Error(`Failed to read ${filePath} with error ${(e as Error).message}`);
9+
try {
10+
if (filePath.startsWith('node_modules://')) {
11+
const require = module.createRequire(process.cwd());
12+
13+
resolvedPath = require.resolve(filePath.slice(15));
14+
} else if (filePath.startsWith('node_modules:')) {
15+
const require = module.createRequire(process.cwd());
16+
17+
resolvedPath = require.resolve(filePath.slice(13));
18+
} else {
19+
resolvedPath = path.resolve(filePath);
2020
}
21-
} else {
22-
resolvedPath = path.resolve(filePath);
21+
} catch (e) {
22+
throw new Error(`Failed to read ${filePath} with error ${(e as Error).message}`);
2323
}
2424

2525
if (alreadyImported.has(resolvedPath)) {

0 commit comments

Comments
 (0)