Skip to content

Commit

Permalink
Web bundle concept
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomareVlad committed Jul 13, 2023
1 parent 3124f1f commit b1b6bef
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ test/cov_profile
coverage.lcov

# Build output
deno_cache/
out/
44 changes: 44 additions & 0 deletions bundling/bundle-web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {bundle} from "https://deno.land/x/[email protected]/mod.ts";
import {createCache} from "https://deno.land/x/[email protected]/mod.ts";

// Parse args
const [release, source = `https://deno.land/x/grammy@${release}/mod.ts`] = Deno.args;
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";
console.log(specifier);
}
return cache.load(specifier);
};

console.log(`Bundling version '${release}' from ${source} ...`);
// Bundle code
const {code: bundledCode} = await bundle(
"../src/mod.ts",
{
load,
compilerOptions: {
sourceMap: false,
inlineSources: false,
inlineSourceMap: false,
}
}
);

console.log("Emitting ...");
// Strip the huge inline source map which is somehow generated anyway
await Deno.writeTextFile(
"../out/web.mjs",
bundledCode.replace(/\/\/# sourceMappingURL=.*\n/, ""),
);
await Deno.writeTextFile(
"../out/web.d.ts",
'export * from "./mod";\n',
);

console.log("Done.");
5 changes: 4 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"dev": "deno fmt && deno lint && deno task test && deno task check",
"clean": "git clean -fX out test/cov_profile test/coverage coverage.lcov",
"coverage": "deno task clean && 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.'"
"report": "genhtml ./coverage.lcov --output-directory ./test/coverage/ && echo 'Point your browser to test/coverage/index.html to see the test coverage report.'",
"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",
},
"fmt": {
"indentWidth": 4,
"proseWrap": "preserve",
"exclude": [
"./deno_cache/",
"./node_modules/",
"./out/",
"./package-lock.json",
Expand All @@ -21,6 +23,7 @@
},
"lint": {
"exclude": [
"./deno_cache/",
"./node_modules/",
"./out/",
"./package-lock.json"
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
],
"main": "./out/mod.js",
"types": "./out/mod.d.ts",
"exports": {
".": {
"types": "./out/mod.d.ts",
"node": "./out/mod.js",
"browser": "./out/web.mjs",
"default": "./out/web.mjs"
},
"./web": {
"types": "./out/web.d.ts",
"default": "./out/web.mjs"
}
},
"keywords": [
"telegram",
"bot",
Expand Down
7 changes: 7 additions & 0 deletions src/deps.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {
delistify,
GLOBAL_CONSTRUCTOR_MAP,
listify,
} from "https://deno.land/x/[email protected]/mod.ts";
export * from "https://lib.deno.dev/x/grammy@v1/mod.ts";
export * from "https://lib.deno.dev/x/grammy@v1/types.ts";
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
"skipLibCheck": true,
"target": "es2019"
},
"include": ["src/"]
"include": [
"src/"
],
"exclude": [
"src/*.web.ts"
]
}

0 comments on commit b1b6bef

Please sign in to comment.