Added npm run dev (#1572)

This commit is contained in:
Andrea Giammarchi
2023-06-29 12:45:07 +02:00
committed by GitHub
parent 3a3cb7b11d
commit 7813c3f03f
7 changed files with 67 additions and 14 deletions

View File

@@ -8,5 +8,6 @@
"ecmaVersion": 12, "ecmaVersion": 12,
"sourceType": "module" "sourceType": "module"
}, },
"ignorePatterns": ["__template.js"],
"rules": {} "rules": {}
} }

View File

@@ -5,4 +5,5 @@ cjs/
!cjs/package.json !cjs/package.json
core.js core.js
esm/worker/xworker.js esm/worker/xworker.js
esm/worker/__template.js
types/ types/

View File

@@ -16,4 +16,6 @@ node.importmap
sw.js sw.js
tsconfig.json tsconfig.json
cjs/worker/_template.js cjs/worker/_template.js
cjs/worker/__template.js
esm/worker/_template.js esm/worker/_template.js
esm/worker/__template.js

View File

@@ -43,3 +43,7 @@ NO_MIN=1 npm run build
npm run server 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.

33
pyscript.core/dev.cjs Normal file
View File

@@ -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();

View File

@@ -8,6 +8,7 @@
"server": "npx static-handler --cors --coep --coop --corp .", "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", "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", "cjs": "ascjs --no-default esm cjs",
"dev": "node dev.cjs",
"rollup:core": "rollup --config rollup/core.config.js", "rollup:core": "rollup --config rollup/core.config.js",
"rollup:pyscript": "rollup --config rollup/pyscript.config.js", "rollup:pyscript": "rollup --config rollup/pyscript.config.js",
"rollup:xworker": "rollup --config rollup/xworker.config.js", "rollup:xworker": "rollup --config rollup/xworker.config.js",
@@ -32,6 +33,7 @@
"@rollup/plugin-terser": "^0.4.3", "@rollup/plugin-terser": "^0.4.3",
"ascjs": "^5.0.1", "ascjs": "^5.0.1",
"c8": "^8.0.0", "c8": "^8.0.0",
"chokidar": "^3.5.3",
"eslint": "^8.43.0", "eslint": "^8.43.0",
"linkedom": "^0.14.26", "linkedom": "^0.14.26",
"rollup": "^3.25.3", "rollup": "^3.25.3",
@@ -64,6 +66,6 @@
"coincident": "^0.8.3" "coincident": "^0.8.3"
}, },
"worker": { "worker": {
"blob": "sha256-CaHDEAEttvghrbLR/GVAofT+zQZyy0Ri9tkpVTNBbiE=" "blob": "sha256-eWNZbyS06lxxlUW/bkU7/fl/Levxxxfiv/+frsgl/fA="
} }
} }

View File

@@ -13,6 +13,12 @@ const PACKAGE_JSON = resolve(join(__dirname, "..", "package.json"));
for (const file of readdirSync(WORKERS_DIR)) { for (const file of readdirSync(WORKERS_DIR)) {
if (file.startsWith("__")) { if (file.startsWith("__")) {
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( const js = JSON.stringify(
readFileSync(join(WORKERS_DIR, file)).toString(), readFileSync(join(WORKERS_DIR, file)).toString(),
); );
@@ -20,7 +26,10 @@ for (const file of readdirSync(WORKERS_DIR)) {
hash.update(js); hash.update(js);
const json = require(PACKAGE_JSON); const json = require(PACKAGE_JSON);
json.worker = { blob: "sha256-" + hash.digest("base64") }; json.worker = { blob: "sha256-" + hash.digest("base64") };
writeFileSync(PACKAGE_JSON, JSON.stringify(json, null, " ") + "\n"); writeFileSync(
PACKAGE_JSON,
JSON.stringify(json, null, " ") + "\n",
);
writeFileSync( writeFileSync(
join(WORKERS_DIR, "xworker.js"), join(WORKERS_DIR, "xworker.js"),
`/* c8 ignore next */\nexport default () => new Worker(URL.createObjectURL(new Blob([${js}],{type:'application/javascript'})),{type:'module'});`, `/* c8 ignore next */\nexport default () => new Worker(URL.createObjectURL(new Blob([${js}],{type:'application/javascript'})),{type:'module'});`,
@@ -28,3 +37,4 @@ for (const file of readdirSync(WORKERS_DIR)) {
rmSync(join(WORKERS_DIR, file)); rmSync(join(WORKERS_DIR, file));
} }
} }
}