Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add bundling/build-cjs.ts #469

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions bundling/build-cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env -S deno run --no-prompt --allow-read=. --allow-write=out/
import {
Context,
deno2node,
emit,
ts,
} from "https://raw.githubusercontent.com/fromdeno/deno2node/70cddb305e3e65f51da4c34e7e8ebfc5f2085d38/src/mod.ts";

const compilerOptions: ts.CompilerOptions = {
"forceConsistentCasingInFileNames": true,
"newLine": ts.NewLineKind.LineFeed,
"noImplicitReturns": true,
"noUnusedParameters": true,
"rootDir": "src/",
"strict": true,
"declaration": true,
"module": ts.ModuleKind.CommonJS,
"outDir": "out/",
"skipLibCheck": true,
"target": ts.ScriptTarget.ES2019,
};

const ctx = new Context({ compilerOptions });
ctx.config = { shim: "src/shim.node.ts" };

console.time("Loading source files");
ctx.project.addSourceFilesAtPaths(Deno.args);
ctx.project.resolveSourceFileDependencies();
console.timeEnd("Loading source files");

await deno2node(ctx);

console.time("Emitting");
const diagnostics = await emit(ctx.project);
console.timeEnd("Emitting");

if (diagnostics.length !== 0) {
console.info(ctx.project.formatDiagnosticsWithColorAndContext(diagnostics));
console.info("TypeScript", ts.version);
console.info(`Found ${diagnostics.length} errors.`);
Deno.exit(1);
}
15 changes: 5 additions & 10 deletions bundling/bundle-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ const [release, source = `https://deno.land/x/grammy@${release}/mod.ts`] =
if (!release) throw new Error("No release specified!");

// Rewrite imports from .deno.ts to .web.ts
const cache = createCache();
const load = (specifier: string) => {
if (specifier.endsWith(".deno.ts")) {
const baseLength = specifier.length - ".deno.ts".length;
specifier = specifier.substring(0, baseLength) + ".web.ts";
}
return cache.load(specifier);
};
const cache = createCache({ root: "deno_cache/" });
const load = (specifier: string) =>
cache.load(specifier.replace(/\.deno\.ts$/, ".web.ts"));
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved

console.log(`Bundling version '${release}' from ${source} ...`);
// Bundle code
Expand All @@ -30,11 +25,11 @@ const { code: bundledCode } = await bundle(source, {
console.log("Emitting ...");
// Strip the huge inline source map which is somehow generated anyway
await Deno.writeTextFile(
"../out/web.mjs",
"out/web.mjs",
bundledCode.replace(/\/\/# sourceMappingURL=.*\n/, ""),
);
await Deno.writeTextFile(
"../out/web.d.ts",
"out/web.d.ts",
'export * from "./mod";\n',
);

Expand Down
10 changes: 4 additions & 6 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"lock": false,
"tasks": {
"check": "deno cache --check=all src/mod.ts",
"backport": "deno run --no-prompt --allow-read=. --allow-write=. https://deno.land/x/[email protected]/src/cli.ts tsconfig.json",
"contribs": "npx --yes all-contributors-cli@6",
"backport": "deno run --no-prompt --allow-read=. --allow-write=out/ bundling/build-cjs.ts src/mod.ts",
"test": "deno test --seed=123456 --parallel ./test/",
"dev": "deno fmt && deno lint && deno task test && deno task check",
"coverage": "rm -rf ./test/cov_profile && deno task test --coverage=./test/cov_profile && deno coverage --lcov --output=./coverage.lcov ./test/cov_profile",
"report": "genhtml ./coverage.lcov --output-directory ./test/coverage/ && echo 'Point your browser to test/coverage/index.html to see the test coverage report.'",
"bundle": "cd bundling && ./bundle-all.sh",
"bundle-web": "mkdir -p out deno_cache && cd bundling && DENO_DIR=$PWD/../deno_cache deno run --unstable --quiet --allow-net --allow-read --allow-env=DENO_DIR,XDG_CACHE_HOME,HOME,DENO_AUTH_TOKENS --allow-write=../out,$PWD/../deno_cache bundle-web.ts dev ../src/mod.ts"
"bundle-web": "mkdir -p out deno_cache && deno run --quiet --allow-net --allow-read --allow-env=DENO_AUTH_TOKENS --allow-write=out/,deno_cache/ bundling/bundle-web.ts dev src/mod.ts"
},
"fmt": {
"indentWidth": 4,
Expand All @@ -18,7 +18,6 @@
"./node_modules/",
"./out/",
"./package-lock.json",
"./bundling/bundles",
"./test/cov_profile"
]
},
Expand All @@ -27,8 +26,7 @@
"./deno_cache/",
"./node_modules/",
"./out/",
"./package-lock.json",
"./bundling/bundles"
"./package-lock.json"
]
}
}
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
"url": "https://github.com/grammyjs/grammY"
},
"scripts": {
"prepare": "npm run backport",
"backport": "deno2node tsconfig.json",
"contribs": "all-contributors"
"prepare": "deno task --quiet backport"
},
"dependencies": {
"@grammyjs/types": "3.1.2",
Expand All @@ -26,9 +24,7 @@
"devDependencies": {
"@types/debug": "^4.1.8",
"@types/node": "^12.20.55",
"@types/node-fetch": "2.6.2",
"all-contributors-cli": "6.24.0",
"deno2node": "^1.8.1"
"@types/node-fetch": "2.6.2"
},
"files": [
"out/"
Expand Down
26 changes: 0 additions & 26 deletions tsconfig.json

This file was deleted.