Skip to content

Commit 23d86d5

Browse files
committed
fix: use vite to create the bundles and externalize solid-js
1 parent 017fbd8 commit 23d86d5

4 files changed

Lines changed: 113 additions & 228 deletions

File tree

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,28 @@
3434
"@types/workerpool": "^6.1.1",
3535
"@typescript-eslint/eslint-plugin": "^5.50.0",
3636
"@typescript-eslint/parser": "^5.50.0",
37-
"@vitest/coverage-c8": "^0.28.3",
37+
"@vitest/coverage-c8": "^0.28.4",
3838
"arg": "^5.0.2",
3939
"chalk": "^5.2.0",
4040
"cheerio": "^1.0.0-rc.12",
4141
"dotenv": "^16.0.3",
42-
"esbuild": "^0.17.5",
43-
"esbuild-plugin-solid": "0.5.0",
4442
"eslint": "^8.33.0",
4543
"eslint-config-prettier": "^8.6.0",
4644
"fs-extra": "^11.1.0",
4745
"jsdom": "^21.1.0",
4846
"prettier": "2.8.3",
4947
"rgb-hex": "^4.0.0",
50-
"rollup": "^3.12.0",
48+
"rollup": "^3.13.0",
5149
"rollup-plugin-delete": "^2.0.0",
5250
"rollup-preset-solid": "^2.0.1",
5351
"solid-js": "1.6.10",
5452
"svgo": "3.0.2",
5553
"ts-node": "^10.9.1",
5654
"tslib": "^2.5.0",
5755
"typescript": "^4.9.5",
58-
"vite": "^4.0.4",
56+
"vite": "^4.1.1",
5957
"vite-plugin-solid": "2.5.0",
60-
"vitest": "0.28.3",
58+
"vitest": "0.28.4",
6159
"workerpool": "^6.3.1"
6260
},
6361
"engines": {

src/build/lib-files.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function getPackageExports(
3434
): PackageJSONExport {
3535
const exportsPayload = JSON.parse(`{
3636
"browser": "./${current.shortName}/index.js",
37-
"node": "./${current.shortName}/index.cjs"
37+
"node": "./${current.shortName}/index.ssr.js"
3838
}`);
3939

4040
return {
@@ -75,7 +75,7 @@ async function writeAssetsFiles() {
7575
const ignoredIcons = ["ImPagebreak"]; // due to the name conflict with ImPagebreak
7676

7777
const postBuildPool = pool("./src/build/post-build.js", {
78-
maxWorkers: Math.min(8, cpus - 1),
78+
maxWorkers: Math.min(4, cpus - 1),
7979
});
8080

8181
async function writeEachPack(pack: PackAttachedIcons) {
@@ -113,7 +113,7 @@ async function writeEachPack(pack: PackAttachedIcons) {
113113
);
114114
// add types for the bundles
115115
await fs.copyFile(bundlePath, `${packFolder}/index.d.ts`);
116-
await fs.copyFile(bundlePath, `${packFolder}/index.d.cts`);
116+
await fs.copyFile(bundlePath, `${packFolder}/index.ssr.d.ts`);
117117

118118
// replace the ts file with the web js bundle
119119
const postBuildWorker = await postBuildPool.proxy<{

src/build/post-build.ts

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,52 @@
1-
import { build, BuildOptions } from "esbuild";
2-
import { solidPlugin } from "esbuild-plugin-solid";
1+
import { dirname } from "path";
2+
import { build, InlineConfig } from "vite";
3+
import solidPlugin from "vite-plugin-solid";
34
import { worker } from "workerpool";
45

6+
function getOptions(filePath: string, server: boolean): InlineConfig {
7+
return {
8+
build: {
9+
lib: {
10+
entry: filePath,
11+
formats: ["es"],
12+
},
13+
outDir: dirname(filePath),
14+
minify: true,
15+
sourcemap: true,
16+
write: true,
17+
emptyOutDir: false,
18+
rollupOptions: {
19+
input: filePath,
20+
external: ["solid-js", "solid-js/web"],
21+
output: {
22+
entryFileNames: server ? "index.ssr.js" : "index.js",
23+
},
24+
},
25+
ssr: server,
26+
},
27+
plugins: [
28+
solidPlugin({
29+
babel: { compact: true },
30+
solid: { hydratable: !server, generate: server ? "ssr" : "dom" },
31+
ssr: server,
32+
dev: false,
33+
}),
34+
],
35+
};
36+
}
37+
538
/**
39+
* Bundle the icons with vite and vite-plugin-solid
40+
*
41+
* This builds two bundles, one for the client and one for the server.
642
*/
743
export async function postBuild(filePath: string) {
8-
// build the tsx file with esbuild
9-
const esbuildOptions: BuildOptions = {
10-
entryPoints: [filePath],
11-
bundle: true,
12-
minify: true,
13-
sourcemap: true,
14-
target: "es2021",
15-
write: true,
16-
};
44+
process.env.NODE_ENV = "production";
1745
await Promise.all([
18-
build({
19-
...esbuildOptions,
20-
format: "esm",
21-
outfile: filePath.replace(".ts", ".js"),
22-
plugins: [solidPlugin({ babel: { compact: true } })],
23-
}),
24-
build({
25-
...esbuildOptions,
26-
format: "cjs",
27-
outfile: filePath.replace(".ts", ".cjs"),
28-
plugins: [
29-
solidPlugin({
30-
babel: { compact: true },
31-
solid: { generate: "ssr", hydratable: true },
32-
}),
33-
],
34-
}),
46+
// Build the client bundle
47+
build(getOptions(filePath, false)),
48+
// Build the server bundle
49+
build(getOptions(filePath, true)),
3550
]);
3651
}
3752

0 commit comments

Comments
 (0)