-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📦 Add
kdjs
usage and declarative worker build
- Loading branch information
1 parent
a483136
commit 78a2de5
Showing
8 changed files
with
209 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env bun | ||
import yaml from "js-yaml"; | ||
import { readFile, readdir } from "node:fs/promises"; | ||
import { uploadWorker } from "./cloudflare/targets"; | ||
import { compileWorker } from "./js/targets"; | ||
|
||
/** @define {string} */ | ||
const ROOT_PATH = "../../.."; | ||
|
||
/** | ||
* @param {string} dirName | ||
* @return {!Promise<!Object>} | ||
*/ | ||
const readBuildRecipe = (dirName) => readdir(dirName) | ||
.then((/** !Array<string> */ dir) => { | ||
for (const file of dir) { | ||
if (!file.startsWith(".") && file.endsWith(".yaml")) | ||
return readFile(`${dirName}/${file}`) | ||
.then((content) => yaml.load(content)); | ||
} | ||
return Promise.reject(); | ||
}); | ||
|
||
/** | ||
* @param {string} dirName | ||
* @param {!Object} env | ||
*/ | ||
const buildCrate = (dirName, env) => import(`${ROOT_PATH}/${dirName}/build.js`) | ||
.then((buildFile) => buildFile.default && buildFile.default.build | ||
? buildFile.default.build(env) | ||
: Promise.reject()) | ||
.catch(() => readBuildRecipe(dirName) | ||
.then((recipe) => { | ||
if (recipe.worker) | ||
compileWorker(dirName, recipe.worker, env); | ||
}) | ||
); | ||
|
||
/**s | ||
* @param {string} dirName | ||
* @param {!Object} env | ||
*/ | ||
const deployCrate = (dirName, env) => import(`${dirName}/build.js`) | ||
.then( | ||
(buildFile) => buildFile.default.deploy(env), | ||
() => readBuildRecipe(dirName) | ||
.then((recipe) => { | ||
if (recipe.worker) | ||
return compileWorker(dirName, recipe.worker) | ||
.then((code) => uploadWorker(env.cloudflare.auth, recipe.worker.name, code)) | ||
}) | ||
); | ||
|
||
/** @dict */ | ||
const env = yaml.load(await readFile(".gizli.yaml", "utf8").catch(() => "")); | ||
/** @const {string} */ | ||
const crate = process.argv[2] || "."; | ||
/** @const {boolean} */ | ||
const isDeploy = process.argv[3] == "deploy"; | ||
|
||
isDeploy | ||
? deployCrate(crate, env) | ||
: buildCrate(crate, env); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { compile } from "../../kdjs/compile"; | ||
import { selectDefines, ensureDotJs } from "./util"; | ||
|
||
/** | ||
* @param {string} dirName | ||
* @param {{ | ||
* entry: string, | ||
* defines: !Array<string|({ | ||
* module: string, | ||
* value: string | ||
* })> | ||
* }} config | ||
* @param {!Object=} env | ||
* @return {!Promise<string>} | ||
*/ | ||
const compileWorker = async (dirName, config, env) => { | ||
/** @const {string} */ | ||
const entry = ensureDotJs(`${dirName}/${config.entry}`); | ||
const compileParams = { | ||
entry, | ||
output: `build/${entry}`, | ||
}; | ||
if (config.defines) | ||
compileParams.define = selectDefines(entry.slice(0, -3), config.defines, env); | ||
return compile(compileParams); | ||
} | ||
|
||
export { | ||
compileWorker | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,52 @@ | ||
# KimlikDAO js compiler | ||
# `kdjs`: KimlikDAO JavaScript compiler | ||
|
||
Primarily wraps Google Closure Compiler (GCC) and UglifyJS but adds additional functionality | ||
to GCC such as ability to compile es6 modules. | ||
`kdjs` is a javascript compiler with advanced optimization and minification capabilities. | ||
It primarily builds upon the Google Closure Compiler (GCC) however it has many custom optimization | ||
passes and a more powerful module system. | ||
|
||
For instance, `kdjs` it is able to generate es6 modules with `export`s and unlinked `import`s, which | ||
is not possible with GCC. | ||
|
||
`kdjs` expects your code to be fully annotated using the Google Closure Compiler type annotations. | ||
Just like in GCC, your type annotations can be either in a `.js` file or an externs file, which in `kdjs` | ||
has to end with the extension `.d.js`. | ||
|
||
You can run `kdjs` directly | ||
|
||
```shell | ||
bun kdjs/kdjs.js entry.js | ||
``` | ||
|
||
`kdjs` will automatically crawl all the imported files from the entry.js and include the externs files for libraries that it recognizes. | ||
or install it system wide | ||
|
||
```shell | ||
cd kdjs | ||
npm i -g . | ||
``` | ||
|
||
When you run `kdjs` with a supplied entry file like so | ||
|
||
```shell | ||
$ kdjs entry.js | ||
``` | ||
|
||
it will automatically crawl all the imported files from the entry.js and | ||
include the externs files for libraries that it recognizes. | ||
|
||
```shell | ||
$ kdjs | ||
|
||
kdjs 0.0.1 | ||
|
||
Usage: kdjs entry.js [parameters] | ||
|
||
Parameters: | ||
--output (-o) : The name of the output file | ||
--print : Print the compiled code to stdout | ||
--strict : Report unknown type | ||
--loose : Don't perform strictTypeCheck | ||
--nologs : Strip all console.log() calls | ||
--define : Values for @define annotated variables | ||
--isolateDir : Directory name to write the isolated and preprocessed input files | ||
--emit_shebang : Whether to place bun shebang sequence at the beginning of the output | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters