Skip to content

Commit e19de04

Browse files
committed
fix: add prepackage step to rewrite imports
1 parent c41b3ce commit e19de04

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev": "svelte-kit dev",
77
"package": "svelte-kit package",
8+
"postpackage": "node scripts/postpackage",
89
"prebuild": "rm -rf build",
910
"build": "svelte-kit build",
1011
"postbuild": "touch docs/.nojekyll",

scripts/postpackage.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { promises as fs } from 'fs';
2+
import path from 'path';
3+
4+
const PACKAGE_FOLDER = process.env.PACKAGE_FOLDER || 'package';
5+
const TARGETS = ['components', 'config', 'src', 'stores'];
6+
const BASE_DIR = path.join(process.cwd(), PACKAGE_FOLDER);
7+
8+
const getFolder = async (folder, depth = 0) => {
9+
const contents = await fs.readdir(path.join(process.cwd(), PACKAGE_FOLDER, folder), {
10+
withFileTypes: true
11+
});
12+
13+
return {
14+
depth,
15+
folders: await Promise.all(
16+
contents
17+
.filter((f) => f.isDirectory())
18+
.map((dir) => getFolder(`${folder}/${dir.name}`, depth + 1))
19+
),
20+
files: contents.filter((f) => !f.isDirectory()).map(({ name }) => `${folder}/${name}`)
21+
};
22+
};
23+
24+
const transformFolder = async ({ folders, files, depth }) => {
25+
await Promise.all(
26+
files.map(async (f) => {
27+
const filepath = path.join(BASE_DIR, f);
28+
const contents = await fs.readFile(filepath, 'utf-8');
29+
const base = depth ? [...Array(depth + 1).fill('..')].join('/') : './';
30+
const updated = contents.replace(/from '\$lib(.*)'/g, `from '${base}$1'`);
31+
await Promise.all(
32+
[
33+
updated === contents ? null : fs.writeFile(filepath, updated),
34+
...folders.map(transformFolder)
35+
].filter(Boolean)
36+
);
37+
})
38+
);
39+
};
40+
41+
Promise.all(TARGETS.map((t) => getFolder(t).then(transformFolder)));

0 commit comments

Comments
 (0)