Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b23d36f

Browse files
committedJan 11, 2024
feat: use synckit to support postcss-load-config v5
close #636
1 parent 325f62f commit b23d36f

File tree

5 files changed

+70
-24
lines changed

5 files changed

+70
-24
lines changed
 

‎.env-cmdrc.js

+4
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+
test: {
23+
SYNCKIT_TIMEOUT: 1000,
24+
SYNCKIT_TS_RUNNER: 'esbuild-register'
2125
}
2226
};

‎package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
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 test 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",
@@ -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",
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
}
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+
);

‎tsconfig.json

+1-1
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,

0 commit comments

Comments
 (0)
Please sign in to comment.