Skip to content

Commit 64af0d7

Browse files
feat: add swc
1 parent a1fda3c commit 64af0d7

9 files changed

Lines changed: 137 additions & 11 deletions

File tree

.swcrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": true,
6+
"decorators": true
7+
},
8+
"transform": {
9+
"react": {
10+
"runtime": "automatic",
11+
"pragma": "React.createElement",
12+
"pragmaFrag": "React.Fragment",
13+
"throwIfNamespace": true,
14+
"useBuiltins": true
15+
}
16+
}
17+
},
18+
"sourceMaps": true
19+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@babel/preset-react": "^7.17.12",
6464
"@babel/preset-typescript": "^7.17.12",
6565
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
66+
"@swc/core": "^1.2.203",
6667
"@types/compression": "^1.7.2",
6768
"@types/cookie-parser": "^1.4.3",
6869
"@types/cors": "^2.8.12",
@@ -111,6 +112,7 @@
111112
"sass": "^1.52.3",
112113
"sass-loader": "^13.0.0",
113114
"style-loader": "^3.3.1",
115+
"swc-loader": "^0.2.3",
114116
"thread-loader": "^3.0.4",
115117
"typescript": "^4.7.3",
116118
"webpack-bundle-analyzer": "^4.5.0",

readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,3 @@ yarn run start
7575
## react router 6
7676

7777
## jest
78-
79-
## TODO support swc-loader new SSR from React18

script/dynamic.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class DynamicRouter {
1616
let indexPath = 0;
1717
let dynamicPath = 0;
1818
let fallbackPath = 0;
19-
const chunkPrePath = prePath.replaceAll("/", "-");
2019
fs.promises
2120
.readdir(dirName, { withFileTypes: true })
2221
.then((files) =>
@@ -32,7 +31,6 @@ class DynamicRouter {
3231
dynamicPath++;
3332
const [, params] = Array.from(dynamicPathReg.exec(fileName));
3433
config.path = `${prePath}:${params}`;
35-
config.chunkName = `${chunkPrePath}-dynamic_${params}`.toLowerCase();
3634
} else {
3735
throw new Error(`file router dynamicPath duplicate`);
3836
}
@@ -41,21 +39,18 @@ class DynamicRouter {
4139
if (indexPath === 0) {
4240
indexPath++;
4341
config.path = `${prePath}`;
44-
config.chunkName = `${chunkPrePath}-index`.toLowerCase();
4542
} else {
4643
throw new Error("file router default path duplicate");
4744
}
4845
} else if (fileName === "404") {
4946
if (prePath === "/") {
5047
fallbackPath++;
5148
config.path = `${prePath}*`;
52-
config.chunkName = `${chunkPrePath}-${fileName}`.toLowerCase();
5349
} else {
5450
throw new Error(`can not add 404 page on the ${prePath}`);
5551
}
5652
} else {
5753
config.path = `${prePath}${fileName}`;
58-
config.chunkName = `${chunkPrePath}-${fileName}`.toLowerCase();
5954
}
6055
config.componentPath = `${prePath.slice(1)}${fileName}`;
6156
// 文件名字重复

src/router/dynamicRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/* do not editor this template */
33
import type { DynamicRouteConfig } from "types/router";
44

5-
export const dynamicRouteConfig: DynamicRouteConfig[] = [{"path":"/ChakraUi","chunkName":"--chakraui","componentPath":"ChakraUi"},{"path":"/Great","chunkName":"--great","componentPath":"Great"},{"path":"/Home","chunkName":"--home","componentPath":"Home"},{"path":"/I18n","chunkName":"--i18n","componentPath":"I18n"},{"path":"/Tcc","chunkName":"--tcc","componentPath":"Tcc"},{"path":"/Tdd","chunkName":"--tdd","componentPath":"Tdd"},{"path":"/","chunkName":"--index","componentPath":"index"},{"path":"/Foo/:id","chunkName":"-foo--dynamic_id","componentPath":"Foo/:id"},{"path":"/Foo/Gff","chunkName":"-foo--gff","componentPath":"Foo/Gff"},{"path":"/*","chunkName":"--404","componentPath":"404"}];
5+
export const dynamicRouteConfig: DynamicRouteConfig[] = [{"path":"/ChakraUi","componentPath":"ChakraUi"},{"path":"/Great","componentPath":"Great"},{"path":"/Home","componentPath":"Home"},{"path":"/I18n","componentPath":"I18n"},{"path":"/Tcc","componentPath":"Tcc"},{"path":"/Tdd","componentPath":"Tdd"},{"path":"/","componentPath":"index"},{"path":"/Foo/:id","componentPath":"Foo/:id"},{"path":"/Foo/Gff","componentPath":"Foo/Gff"},{"path":"/*","componentPath":"404"}];

src/types/router/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ export interface TransformType {
1313

1414
export interface DynamicRouteConfig {
1515
path: string;
16-
chunkName: string;
1716
componentPath?: string;
1817
}

webpack/optimization.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 压缩css
22
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
33

4-
const optimizationConfig = ({ env, isDev = true, isMiddleWareDevelop }) => {
4+
const optimizationConfig = ({ env, isDev = true }) => {
55
if (env === "client") {
66
if (!isDev) {
77
return {

webpack/rules.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const cssRules = (env, isDev = true) => {
1818
};
1919
};
2020

21+
const jsRulesWithSWC = (env, isDev = true) => {
22+
return {
23+
test: /\.[jt]sx?$/,
24+
exclude: /node_modules/,
25+
use: {
26+
loader: "swc-loader",
27+
},
28+
};
29+
};
30+
2131
const jsRules = (env, isDev = true) => {
2232
const workerPool = {
2333
workers: 3,
@@ -60,6 +70,16 @@ const jsRules = (env, isDev = true) => {
6070

6171
const cssModuleRules = (env, isDev = true) => {
6272
// css module
73+
74+
// https://github.com/webpack-contrib/thread-loader/issues/135
75+
// const workerPoolSass = {
76+
// workers: 3,
77+
// workerParallelJobs: 2,
78+
// poolTimeout: isDev ? Infinity : 2000,
79+
// };
80+
81+
// threadLoader.warmup(workerPoolSass, ["sass-loader", "css-loader"]);
82+
6383
return {
6484
test: /\.module\.s?css$/,
6585
use: [
@@ -70,6 +90,10 @@ const cssModuleRules = (env, isDev = true) => {
7090
: {
7191
loader: MiniCssExtractPlugin.loader,
7292
}),
93+
// {
94+
// loader: require.resolve("thread-loader"),
95+
// options: workerPoolSass,
96+
// },
7397
// 启用js中import css为对象,启用css module以及生成的类名
7498
{
7599
loader: "css-loader",
@@ -107,6 +131,6 @@ const resourceRules = (env, isDev = true) => {
107131
};
108132
};
109133

110-
const rulesConfig = ({ env, isDev }) => [cssRules(env, isDev), jsRules(env, isDev), cssModuleRules(env, isDev), resourceRules(env, isDev)];
134+
const rulesConfig = ({ env, isDev }) => [cssRules(env, isDev), jsRulesWithSWC(env, isDev), cssModuleRules(env, isDev), resourceRules(env, isDev)];
111135

112136
exports.rulesConfig = rulesConfig;

yarn.lock

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,90 @@
22802280
dependencies:
22812281
"@sinonjs/commons" "^1.7.0"
22822282

2283+
"@swc/core-android-arm-eabi@1.2.203":
2284+
version "1.2.203"
2285+
resolved "https://registry.npmmirror.com/@swc/core-android-arm-eabi/-/core-android-arm-eabi-1.2.203.tgz#4bf3208e767c1235f3c3143e9743c1e9e923d342"
2286+
integrity sha512-maKYooa0+h66Y/t81lJblimJYWAON1onMwczxe+uQs1FkcnGa/ixhnmRDXIM0wpivMu93EIq3teKR43nr2K/Yg==
2287+
2288+
"@swc/core-android-arm64@1.2.203":
2289+
version "1.2.203"
2290+
resolved "https://registry.npmmirror.com/@swc/core-android-arm64/-/core-android-arm64-1.2.203.tgz#3fc703e3b9eebcc51e705672f3901f008b273750"
2291+
integrity sha512-Zg57EuQa06cTNk2enort0/djXyEaYI0ectydZLPv4oj0ubjLGTZMDkuxPaYWSs9eHT1A6Ge8bwQCA7t/GLYGGA==
2292+
2293+
"@swc/core-darwin-arm64@1.2.203":
2294+
version "1.2.203"
2295+
resolved "https://registry.npmmirror.com/@swc/core-darwin-arm64/-/core-darwin-arm64-1.2.203.tgz#c583b7d08fe9e12cc0631268cf12d980ad3bee72"
2296+
integrity sha512-BVwIAhkMz58V6I+xLsVMeOKSORe8iaYnCHUZbgI0NfAqvUYBUqmwzt+Fww44wv3Ibxb4my1zk7BG02d7Ku94+A==
2297+
2298+
"@swc/core-darwin-x64@1.2.203":
2299+
version "1.2.203"
2300+
resolved "https://registry.npmmirror.com/@swc/core-darwin-x64/-/core-darwin-x64-1.2.203.tgz#55b5746566c6f9b4fa5648b5cb5e4f5d49553326"
2301+
integrity sha512-Z9gwtHwv3jEntjVANYmhzVvIVkgbkWAsLGP2UBez2D8CgScx+5Gnb0C5qT4nwX0Q+YD42rdHp7M551ZqVOo2FQ==
2302+
2303+
"@swc/core-freebsd-x64@1.2.203":
2304+
version "1.2.203"
2305+
resolved "https://registry.npmmirror.com/@swc/core-freebsd-x64/-/core-freebsd-x64-1.2.203.tgz#62d96cb70a62b95035cf76ea9945925979be65db"
2306+
integrity sha512-9aCC80BvU+IGqrmyY2r/3NRveOQg9BSCT+6N4esBKMLlTaDmuARSBON1TXjUF7HPUqzNB4ahri9HIx52wImXqQ==
2307+
2308+
"@swc/core-linux-arm-gnueabihf@1.2.203":
2309+
version "1.2.203"
2310+
resolved "https://registry.npmmirror.com/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.2.203.tgz#02900d2ed7e599270bf90b5e768ff5cc5a0ecbf3"
2311+
integrity sha512-SoeXRqawk5aufUArS1s58prCAT24+p3lITh5Jv4dYk2PwGZpOHC7ADcVKq/55XayTxSafwXD9jObNTJzQ6moqw==
2312+
2313+
"@swc/core-linux-arm64-gnu@1.2.203":
2314+
version "1.2.203"
2315+
resolved "https://registry.npmmirror.com/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.2.203.tgz#5900a538fd2020d24c54922d80ab74985161b76a"
2316+
integrity sha512-bF8t8fd8MSx6qWgi1mYlyj1XYPWeGtGRVei1C1AcyXzcD34H0H37D6z2YBXfQrMhFED/s0oCPB2qvPh0j1jbjw==
2317+
2318+
"@swc/core-linux-arm64-musl@1.2.203":
2319+
version "1.2.203"
2320+
resolved "https://registry.npmmirror.com/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.2.203.tgz#a2ef6f54ade66fb2fa65dba4e833cc81ce3241fb"
2321+
integrity sha512-lFfPFgbEGhxsgL3PWRp4exzIlI3MuJWFFkiYqKMeDdHSUOdhtcQUCGw9D6Iat/1mCNxuTrDxQOBQBUhc9g6DoA==
2322+
2323+
"@swc/core-linux-x64-gnu@1.2.203":
2324+
version "1.2.203"
2325+
resolved "https://registry.npmmirror.com/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.2.203.tgz#3b8bb47cde0f70f2c2e0e2d36b89b506b6b63b70"
2326+
integrity sha512-46ykzctv5W4PxeRE/brZyxWRSfdhJllCFUySRubhMLCuhs6VLtZzmWBefxPHTUDpBzmhX8kcaiKwwY2tqV0A9g==
2327+
2328+
"@swc/core-linux-x64-musl@1.2.203":
2329+
version "1.2.203"
2330+
resolved "https://registry.npmmirror.com/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.2.203.tgz#976bc9878db05dab974a235df0a9f9e3f8417540"
2331+
integrity sha512-LXPlxppioO9d1kpqu8qJiLvyDYJmXO7vcbmtOuM3nCPQPdVDii7sx4JtbunOMs/sY2ilFUfF7f6oNf2RkRPu1Q==
2332+
2333+
"@swc/core-win32-arm64-msvc@1.2.203":
2334+
version "1.2.203"
2335+
resolved "https://registry.npmmirror.com/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.2.203.tgz#533c45c6dedd81a84821c83296c6ccd8dd006633"
2336+
integrity sha512-De9btHHbi6nTKSMaujAdpvM40XaEH1dTkKPK0H4JX+6WZYhOFYl0silvd6CIFewdnkKLdSVvTnfPubV+c0S8eA==
2337+
2338+
"@swc/core-win32-ia32-msvc@1.2.203":
2339+
version "1.2.203"
2340+
resolved "https://registry.npmmirror.com/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.2.203.tgz#a0723d7f7957619a86a1df8797742db77c65f787"
2341+
integrity sha512-YwGOD22qbDZ+ByiPnLqQdbGVE8k61R/mx3bZOpQnK0hkg/W5ysUBOYwr9aflLcNMRJuKxzVrCmSGBHMJN5AjfA==
2342+
2343+
"@swc/core-win32-x64-msvc@1.2.203":
2344+
version "1.2.203"
2345+
resolved "https://registry.npmmirror.com/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.2.203.tgz#e80fae36bc4f98b9679cc4bf697f8ae253973281"
2346+
integrity sha512-LAlXKK7rl+sLAgyXxuzCkaYQdoG797O/sRFC6eMyb4/eDtSctmVSCQl5xefuH+cofuZCTSk4OgzqmdJ2Ue/Jmw==
2347+
2348+
"@swc/core@^1.2.203":
2349+
version "1.2.203"
2350+
resolved "https://registry.npmmirror.com/@swc/core/-/core-1.2.203.tgz#a082fb77ce3653b02551ca7af3c7258c07b8f57f"
2351+
integrity sha512-GZXeITqg3YuXFPaSMYk3g9h9j+pIc5sjt4jS5VvFHk8wXUfk/tvP5GwOPmEyXmVJkvEDJPXLip6lqfeKlvNceA==
2352+
optionalDependencies:
2353+
"@swc/core-android-arm-eabi" "1.2.203"
2354+
"@swc/core-android-arm64" "1.2.203"
2355+
"@swc/core-darwin-arm64" "1.2.203"
2356+
"@swc/core-darwin-x64" "1.2.203"
2357+
"@swc/core-freebsd-x64" "1.2.203"
2358+
"@swc/core-linux-arm-gnueabihf" "1.2.203"
2359+
"@swc/core-linux-arm64-gnu" "1.2.203"
2360+
"@swc/core-linux-arm64-musl" "1.2.203"
2361+
"@swc/core-linux-x64-gnu" "1.2.203"
2362+
"@swc/core-linux-x64-musl" "1.2.203"
2363+
"@swc/core-win32-arm64-msvc" "1.2.203"
2364+
"@swc/core-win32-ia32-msvc" "1.2.203"
2365+
"@swc/core-win32-x64-msvc" "1.2.203"
2366+
22832367
"@szmarczak/http-timer@^1.1.2":
22842368
version "1.1.2"
22852369
resolved "https://registry.npmmirror.com/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421"
@@ -8234,6 +8318,11 @@ svgo@^2.7.0:
82348318
picocolors "^1.0.0"
82358319
stable "^0.1.8"
82368320

8321+
swc-loader@^0.2.3:
8322+
version "0.2.3"
8323+
resolved "https://registry.npmmirror.com/swc-loader/-/swc-loader-0.2.3.tgz#6792f1c2e4c9ae9bf9b933b3e010210e270c186d"
8324+
integrity sha512-D1p6XXURfSPleZZA/Lipb3A8pZ17fP4NObZvFCDjK/OKljroqDpPmsBdTraWhVBqUNpcWBQY1imWdoPScRlQ7A==
8325+
82378326
tapable@^2.0.0, tapable@^2.1.1, tapable@^2.2.0, tapable@^2.2.1:
82388327
version "2.2.1"
82398328
resolved "https://registry.npmmirror.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"

0 commit comments

Comments
 (0)