Skip to content

Commit 8c8ce97

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

File tree

5 files changed

+67
-25
lines changed

5 files changed

+67
-25
lines changed

.env-cmdrc.js

+5-1
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-
}
21+
},
22+
test: {
23+
SYNCKIT_TIMEOUT: 1000,
24+
SYNCKIT_TS_RUNNER: 'esbuild-register',
25+
},
2226
};

package.json

+5-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",
@@ -168,6 +169,7 @@
168169
"svelte": "^5.0.0-next.33",
169170
"svelte-adapter-ghpages": "0.2.2",
170171
"svelte-i18n": "^4.0.0",
172+
"ts-node": "^10.9.2",
171173
"tslib": "^2.5.0",
172174
"type-coverage": "^2.22.0",
173175
"typescript": "~5.1.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,35 @@
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+
}: { cwd: string; filename: string; code: string; configFilePath?: unknown; }): Promise<{
12+
output: string
13+
mappings: string
14+
}> => {
15+
const config = await postcssLoadConfig(
16+
{
17+
cwd,
18+
from: filename
19+
},
20+
typeof configFilePath === 'string' ? configFilePath : undefined
21+
);
22+
23+
const result = await postcss(config.plugins).process(code, {
24+
...config.options,
25+
map: {
26+
inline: false
27+
}
28+
});
29+
30+
return {
31+
output: result.content,
32+
mappings: result.map.toJSON().mappings,
33+
}
34+
}
35+
);

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)