Skip to content

Commit 69445ac

Browse files
committed
feat: use synckit to support postcss-load-config v5
close #636
1 parent 325f62f commit 69445ac

File tree

7 files changed

+71
-36
lines changed

7 files changed

+71
-36
lines changed

.env-cmdrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ module.exports = {
1818
// eslint-disable-next-line no-process-env -- ignore
1919
process.env.NODE_OPTIONS || ''
2020
}`
21+
},
22+
synckit: {
23+
SYNCKIT_TIMEOUT: 1000,
24+
SYNCKIT_TS_RUNNER: 'esbuild-register'
2125
}
2226
};

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
"prerelease": "pnpm run clean && pnpm run build",
4848
"release": "changeset publish",
4949
"svelte-kit": "env-cmd -e sveltekit node node_modules/vite/bin/vite.js",
50-
"test": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
50+
"test": "env-cmd -e synckit pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
5151
"test:debug": "env-cmd -e debug pnpm run test",
5252
"test:update-fixtures": "env-cmd -e update-fixtures pnpm run test",
5353
"ts": "node -r esbuild-register",
5454
"typecov": "type-coverage",
55-
"update": "pnpm run ts ./tools/update.ts && pnpm run lint-fix:md",
55+
"update": "env-cmd -e synckit pnpm run ts ./tools/update.ts && pnpm run lint-fix:md",
5656
"version": "env-cmd -e version pnpm run update",
5757
"version:ci": "env-cmd -e version-ci pnpm run update && changeset version"
5858
},
@@ -73,11 +73,12 @@
7373
"esutils": "^2.0.3",
7474
"known-css-properties": "^0.29.0",
7575
"postcss": "^8.4.5",
76-
"postcss-load-config": "^3.1.4",
76+
"postcss-load-config": "^5.0.2",
7777
"postcss-safe-parser": "^6.0.0",
7878
"postcss-selector-parser": "^6.0.11",
7979
"semver": "^7.5.3",
80-
"svelte-eslint-parser": ">=0.34.0-next.4 <1.0.0"
80+
"svelte-eslint-parser": ">=0.34.0-next.4 <1.0.0",
81+
"synckit": "^0.9.0"
8182
},
8283
"devDependencies": {
8384
"@1stg/browserslist-config": "^2.0.0",
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import type { AST } from 'svelte-eslint-parser';
2-
import postcss from 'postcss';
3-
import postcssLoadConfig from 'postcss-load-config';
2+
import { createSyncFn } from 'synckit';
3+
44
import type { RuleContext } from '../../../types';
5-
import type { TransformResult } from './types';
65
import { getCwd, getFilename } from '../../../utils/compat';
6+
import type { TransformResult } from './types';
7+
8+
const postcssProcess = createSyncFn(require.resolve('./postcss.worker')) as (options: {
9+
cwd: string;
10+
filename: string;
11+
code: string;
12+
configFilePath?: unknown;
13+
}) => {
14+
output: string;
15+
mappings: string;
16+
};
717

818
/**
919
* Transform with postcss
@@ -24,33 +34,24 @@ export function transform(
2434
inputRange = [node.startTag.range[1], node.range[1]];
2535
}
2636
const code = text.slice(...inputRange);
27-
2837
const filename = `${getFilename(context)}.css`;
38+
2939
try {
3040
const configFilePath = postcssConfig?.configFilePath;
3141

32-
const config = postcssLoadConfig.sync(
33-
{
34-
cwd: getCwd(context),
35-
from: filename
36-
},
37-
typeof configFilePath === 'string' ? configFilePath : undefined
38-
);
39-
40-
const result = postcss(config.plugins).process(code, {
41-
...config.options,
42-
map: {
43-
inline: false
44-
}
42+
const result = postcssProcess({
43+
cwd: getCwd(context),
44+
filename,
45+
code,
46+
configFilePath
4547
});
4648

4749
return {
4850
inputRange,
49-
output: result.content,
50-
mappings: result.map.toJSON().mappings
51+
...result
5152
};
5253
} catch (_e) {
53-
// console.log(e)
54+
console.error(_e);
5455
return null;
5556
}
5657
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import postcss from 'postcss';
2+
import postcssLoadConfig from 'postcss-load-config';
3+
import { runAsWorker } from 'synckit';
4+
5+
runAsWorker(
6+
async ({
7+
cwd,
8+
filename,
9+
code,
10+
configFilePath
11+
}: {
12+
cwd: string;
13+
filename: string;
14+
code: string;
15+
configFilePath?: unknown;
16+
}): Promise<{
17+
output: string;
18+
mappings: string;
19+
}> => {
20+
const config = await postcssLoadConfig(
21+
{
22+
cwd,
23+
from: filename
24+
},
25+
typeof configFilePath === 'string' ? configFilePath : undefined
26+
);
27+
28+
const result = await postcss(config.plugins).process(code, {
29+
...config.options,
30+
map: {
31+
inline: false
32+
}
33+
});
34+
35+
return {
36+
output: result.content,
37+
mappings: result.map.toJSON().mappings
38+
};
39+
}
40+
);

src/types-for-node.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export type ASTNodeWithParent =
1717
| AST.SvelteProgram;
1818

1919
export type ASTNodeListener = {
20-
AccessorProperty?: (node: TSESTree.AccessorProperty & ASTNodeWithParent) => void;
2120
ArrayExpression?: (node: TSESTree.ArrayExpression & ASTNodeWithParent) => void;
2221
ArrayPattern?: (node: TSESTree.ArrayPattern & ASTNodeWithParent) => void;
2322
ArrowFunctionExpression?: (node: TSESTree.ArrowFunctionExpression & ASTNodeWithParent) => void;
@@ -105,9 +104,6 @@ export type ASTNodeListener = {
105104
WhileStatement?: (node: TSESTree.WhileStatement & ASTNodeWithParent) => void;
106105
WithStatement?: (node: TSESTree.WithStatement & ASTNodeWithParent) => void;
107106
YieldExpression?: (node: TSESTree.YieldExpression & ASTNodeWithParent) => void;
108-
TSAbstractAccessorProperty?: (
109-
node: TSESTree.TSAbstractAccessorProperty & ASTNodeWithParent
110-
) => void;
111107
TSAbstractKeyword?: (node: TSESTree.TSAbstractKeyword & ASTNodeWithParent) => void;
112108
TSAbstractMethodDefinition?: (
113109
node: TSESTree.TSAbstractMethodDefinition & ASTNodeWithParent
@@ -183,7 +179,6 @@ export type ASTNodeListener = {
183179
TSQualifiedName?: (node: TSESTree.TSQualifiedName & ASTNodeWithParent) => void;
184180
TSReadonlyKeyword?: (node: TSESTree.TSReadonlyKeyword & ASTNodeWithParent) => void;
185181
TSRestType?: (node: TSESTree.TSRestType & ASTNodeWithParent) => void;
186-
TSSatisfiesExpression?: (node: TSESTree.TSSatisfiesExpression & ASTNodeWithParent) => void;
187182
TSStaticKeyword?: (node: TSESTree.TSStaticKeyword & ASTNodeWithParent) => void;
188183
TSStringKeyword?: (node: TSESTree.TSStringKeyword & ASTNodeWithParent) => void;
189184
TSSymbolKeyword?: (node: TSESTree.TSSymbolKeyword & ASTNodeWithParent) => void;
@@ -245,7 +240,6 @@ export type ASTNodeListener = {
245240
};
246241

247242
export type ESNodeListener = {
248-
AccessorProperty?: (node: TSESTree.AccessorProperty & ASTNodeWithParent) => void;
249243
ArrayExpression?: (node: TSESTree.ArrayExpression & ASTNodeWithParent) => void;
250244
ArrayPattern?: (node: TSESTree.ArrayPattern & ASTNodeWithParent) => void;
251245
ArrowFunctionExpression?: (node: TSESTree.ArrowFunctionExpression & ASTNodeWithParent) => void;
@@ -323,9 +317,6 @@ export type TSNodeListener = {
323317
Decorator?: (node: TSESTree.Decorator & ASTNodeWithParent) => void;
324318
ImportAttribute?: (node: TSESTree.ImportAttribute & ASTNodeWithParent) => void;
325319
StaticBlock?: (node: TSESTree.StaticBlock & ASTNodeWithParent) => void;
326-
TSAbstractAccessorProperty?: (
327-
node: TSESTree.TSAbstractAccessorProperty & ASTNodeWithParent
328-
) => void;
329320
TSAbstractKeyword?: (node: TSESTree.TSAbstractKeyword & ASTNodeWithParent) => void;
330321
TSAbstractMethodDefinition?: (
331322
node: TSESTree.TSAbstractMethodDefinition & ASTNodeWithParent
@@ -401,7 +392,6 @@ export type TSNodeListener = {
401392
TSQualifiedName?: (node: TSESTree.TSQualifiedName & ASTNodeWithParent) => void;
402393
TSReadonlyKeyword?: (node: TSESTree.TSReadonlyKeyword & ASTNodeWithParent) => void;
403394
TSRestType?: (node: TSESTree.TSRestType & ASTNodeWithParent) => void;
404-
TSSatisfiesExpression?: (node: TSESTree.TSSatisfiesExpression & ASTNodeWithParent) => void;
405395
TSStaticKeyword?: (node: TSESTree.TSStaticKeyword & ASTNodeWithParent) => void;
406396
TSStringKeyword?: (node: TSESTree.TSStringKeyword & ASTNodeWithParent) => void;
407397
TSSymbolKeyword?: (node: TSESTree.TSSymbolKeyword & ASTNodeWithParent) => void;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"target": "es2020",
44
"module": "commonjs",
5-
"moduleResolution": "Node16",
5+
"moduleResolution": "Node10",
66
"lib": ["es2020", "dom"],
77
"allowJs": true,
88
"checkJs": true,

typings/estree/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export type Program = TSESTree.Program;
1212
export type Expression = TSESTree.Expression;
1313
export type Statement = TSESTree.Statement;
1414
export type Pattern = TSESTree.DestructuringPattern;
15-
export type AccessorProperty = TSESTree.AccessorProperty;
1615
export type ArrayExpression = TSESTree.ArrayExpression;
1716
export type ArrayPattern = TSESTree.ArrayPattern;
1817
export type ArrowFunctionExpression = TSESTree.ArrowFunctionExpression;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy