Skip to content

Commit 83fe1f5

Browse files
Prepare package build scripts and setup
1 parent 2300673 commit 83fe1f5

File tree

6 files changed

+142
-5
lines changed

6 files changed

+142
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
index.cjs.js
64+
index.umd.js

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
regenerate.js

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
"name": "@sandstreamdev/std",
33
"version": "0.1.0",
44
"description": "",
5-
"main": "index.js",
6-
"module": "index.js",
75
"type": "module",
6+
"module": "index.js",
7+
"main": "index.cjs.js",
8+
"browser": "index.umd.js",
89
"sideEffects": false,
910
"scripts": {
10-
"prettier": "prettier \"**/*.{js,json,md}\"",
11+
"build": "npm run regenerate && rollup -c",
12+
"prepare": "npm run build",
13+
"prettier:check": "npm run prettier -- --check",
1114
"prettier:fix": "npm run prettier -- --write",
12-
"prettier:check": "npm run prettier -- --check"
15+
"prettier": "prettier \"**/*.{js,json,md}\"",
16+
"regenerate": "node --experimental-modules regenerate.js && npm run prettier:fix"
1317
},
1418
"repository": {
1519
"type": "git",
@@ -26,6 +30,7 @@
2630
},
2731
"homepage": "https://github.com/sandstreamdev/std#readme",
2832
"devDependencies": {
29-
"prettier": "^1.18.2"
33+
"prettier": "^1.18.2",
34+
"rollup": "^1.22.0"
3035
}
3136
}

regenerate.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { promises } from "fs";
2+
import path from "path";
3+
4+
const { readdir: readDirectoryAsync, writeFile: writeFileAsync } = promises;
5+
6+
const [, , cwd = process.cwd()] = process.argv;
7+
8+
const mapping = {
9+
function: "_function"
10+
};
11+
12+
const ignoredFiles = [
13+
".gitignore",
14+
"index.js",
15+
"LICENSE",
16+
"package-lock.json",
17+
"package.json",
18+
"README.md",
19+
"regenerate.js",
20+
"rollup.config.js"
21+
];
22+
23+
const ignoredDirectories = [".git", "node_modules"];
24+
25+
const identifier = name => mapping[name] || name;
26+
27+
const main = async cwd => {
28+
console.log(`Indexing files in ${cwd}...`);
29+
30+
const entries = await readDirectoryAsync(cwd, { withFileTypes: true });
31+
const files = entries
32+
.filter(x => x.isFile())
33+
.map(x => x.name)
34+
.filter(x => !ignoredFiles.includes(x));
35+
const directories = entries
36+
.filter(x => x.isDirectory())
37+
.map(x => x.name)
38+
.filter(x => !ignoredDirectories.includes(x));
39+
40+
for (const directory of directories) {
41+
await main(path.join(cwd, directory));
42+
}
43+
44+
const submodules = files.map(filePath => {
45+
const { base: fileName } = path.parse(filePath);
46+
const splitted = fileName.split(".");
47+
const id = identifier(splitted.slice(0, splitted.length - 1).join("_"));
48+
const extension = `.${splitted[splitted.length - 1]}`;
49+
50+
return [fileName, id, extension];
51+
});
52+
53+
const dependencies = [
54+
...submodules,
55+
...directories.map(x => [`${x}/index.js`, identifier(x), ""])
56+
];
57+
58+
const importDeclarations = dependencies
59+
.map(([fileName, id]) =>
60+
id !== "index" ? `import ${id} from './${fileName}'` : ""
61+
)
62+
.join("\r\n");
63+
64+
const exportDeclarationBody = dependencies
65+
.map(([, id]) => (id !== "index" ? id : ""))
66+
.join(", ");
67+
68+
const exportDeclaration = `export { ${exportDeclarationBody} }`;
69+
const defaultExport = `export default { ${exportDeclarationBody} }`;
70+
71+
console.log(`Indexed files in ${cwd}:`);
72+
73+
const moduleContents = [
74+
importDeclarations,
75+
exportDeclaration,
76+
defaultExport
77+
].join("\r\n\r\n");
78+
79+
console.log(moduleContents);
80+
81+
await writeFileAsync(path.join(cwd, "index.js"), moduleContents);
82+
};
83+
84+
main(cwd);

rollup.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
input: "index.js",
3+
output: [
4+
{
5+
exports: "named",
6+
file: "index.cjs.js",
7+
format: "cjs"
8+
},
9+
{
10+
file: "index.umd.js",
11+
format: "umd",
12+
name: "@sandstreamdev/std"
13+
}
14+
]
15+
};

0 commit comments

Comments
 (0)