diff --git a/pyscript.core/.eslintrc.json b/pyscript.core/.eslintrc.json index 6d5c3787..66c656ce 100644 --- a/pyscript.core/.eslintrc.json +++ b/pyscript.core/.eslintrc.json @@ -8,5 +8,6 @@ "ecmaVersion": 12, "sourceType": "module" }, + "ignorePatterns": ["__template.js"], "rules": {} } diff --git a/pyscript.core/.gitignore b/pyscript.core/.gitignore index 3150ddba..1b2dc421 100644 --- a/pyscript.core/.gitignore +++ b/pyscript.core/.gitignore @@ -5,4 +5,5 @@ cjs/ !cjs/package.json core.js esm/worker/xworker.js +esm/worker/__template.js types/ diff --git a/pyscript.core/.npmignore b/pyscript.core/.npmignore index 686fd10a..c5cc42aa 100644 --- a/pyscript.core/.npmignore +++ b/pyscript.core/.npmignore @@ -16,4 +16,6 @@ node.importmap sw.js tsconfig.json cjs/worker/_template.js +cjs/worker/__template.js esm/worker/_template.js +esm/worker/__template.js diff --git a/pyscript.core/README.md b/pyscript.core/README.md index 88fdc748..a69279fe 100644 --- a/pyscript.core/README.md +++ b/pyscript.core/README.md @@ -43,3 +43,7 @@ NO_MIN=1 npm run build npm run server ``` + +### Dev Build + +Beside spinning the _localhost_ server via `npm run server`, the `npm run dev` will watch changes in the `./esm` folder and it will build automatically non optimized artifacts out of the box. diff --git a/pyscript.core/dev.cjs b/pyscript.core/dev.cjs new file mode 100644 index 00000000..04fd1919 --- /dev/null +++ b/pyscript.core/dev.cjs @@ -0,0 +1,33 @@ +let queue = Promise.resolve(); + +const { exec } = require("node:child_process"); + +const build = (fileName) => { + if (fileName) console.log(fileName, "changed"); + else console.log("building without optimizations"); + queue = queue.then( + () => + new Promise((resolve, reject) => { + exec( + "npm run rollup:xworker && npm run rollup:core && npm run rollup:pyscript", + { cwd: __dirname, env: { ...process.env, NO_MIN: true } }, + (error) => { + if (error) reject(error); + else + resolve( + console.log(fileName || "", "build completed"), + ); + }, + ); + }), + ); +}; + +const options = { + ignored: /\/(?:__template|interpreters|xworker)\.[mc]?js$/, + persistent: true, +}; + +require("chokidar").watch("./esm", options).on("change", build); + +build(); diff --git a/pyscript.core/package.json b/pyscript.core/package.json index af8241bc..6b7107ca 100644 --- a/pyscript.core/package.json +++ b/pyscript.core/package.json @@ -8,6 +8,7 @@ "server": "npx static-handler --cors --coep --coop --corp .", "build": "npm run rollup:xworker && npm run rollup:core && npm run rollup:pyscript && eslint esm/ && npm run ts && npm run cjs && npm run test", "cjs": "ascjs --no-default esm cjs", + "dev": "node dev.cjs", "rollup:core": "rollup --config rollup/core.config.js", "rollup:pyscript": "rollup --config rollup/pyscript.config.js", "rollup:xworker": "rollup --config rollup/xworker.config.js", @@ -32,6 +33,7 @@ "@rollup/plugin-terser": "^0.4.3", "ascjs": "^5.0.1", "c8": "^8.0.0", + "chokidar": "^3.5.3", "eslint": "^8.43.0", "linkedom": "^0.14.26", "rollup": "^3.25.3", @@ -64,6 +66,6 @@ "coincident": "^0.8.3" }, "worker": { - "blob": "sha256-CaHDEAEttvghrbLR/GVAofT+zQZyy0Ri9tkpVTNBbiE=" + "blob": "sha256-eWNZbyS06lxxlUW/bkU7/fl/Levxxxfiv/+frsgl/fA=" } } diff --git a/pyscript.core/rollup/build_xworker.cjs b/pyscript.core/rollup/build_xworker.cjs index 646da781..b48a2cbd 100644 --- a/pyscript.core/rollup/build_xworker.cjs +++ b/pyscript.core/rollup/build_xworker.cjs @@ -13,18 +13,28 @@ const PACKAGE_JSON = resolve(join(__dirname, "..", "package.json")); for (const file of readdirSync(WORKERS_DIR)) { if (file.startsWith("__")) { - const js = JSON.stringify( - readFileSync(join(WORKERS_DIR, file)).toString(), - ); - const hash = createHash("sha256"); - hash.update(js); - const json = require(PACKAGE_JSON); - json.worker = { blob: "sha256-" + hash.digest("base64") }; - writeFileSync(PACKAGE_JSON, JSON.stringify(json, null, " ") + "\n"); - writeFileSync( - join(WORKERS_DIR, "xworker.js"), - `/* c8 ignore next */\nexport default () => new Worker(URL.createObjectURL(new Blob([${js}],{type:'application/javascript'})),{type:'module'});`, - ); - rmSync(join(WORKERS_DIR, file)); + if (process.env.NO_MIN) { + writeFileSync( + join(WORKERS_DIR, "xworker.js"), + `/* c8 ignore next */\nexport default () => new Worker('/esm/worker/__template.js',{type:'module'});`, + ); + } else { + const js = JSON.stringify( + readFileSync(join(WORKERS_DIR, file)).toString(), + ); + const hash = createHash("sha256"); + hash.update(js); + const json = require(PACKAGE_JSON); + json.worker = { blob: "sha256-" + hash.digest("base64") }; + writeFileSync( + PACKAGE_JSON, + JSON.stringify(json, null, " ") + "\n", + ); + writeFileSync( + join(WORKERS_DIR, "xworker.js"), + `/* c8 ignore next */\nexport default () => new Worker(URL.createObjectURL(new Blob([${js}],{type:'application/javascript'})),{type:'module'});`, + ); + rmSync(join(WORKERS_DIR, file)); + } } }