I have a project that I need to use Bun to bundle up the TypeScript of the web page. I don't know if I should bundle the HTML pages or the TypeScript files.
Here are my bun.js
settings:
import html from 'bun-plugin-html';
await Bun.build({
entrypoints: ['./public/page1.html', './public/page2.html', './public/page3.html'],
sourcemap: "inline",
outdir: './public',
plugins: [
html()
],
});
The problem with the above is that the HTML pages are overwritten. In each page is the script:
<script type="module" src="index.ts">
When I run bun run start
that web page changes that line to point to the JavaScript file:
<script type="module" src="index.js">
I'd like to keep everything in the same directory. I could rename the public directory to source and then export to public
but then, what I'm trying to do is bundle the TypeScript files themselves and I could leave the HTML pages alone if that makes sense.
So could I point to the TypeScript files in those HTML pages and then created the bundled JavaScript files? Does that make sense?
Here is my bun.js
settings:
// point to typescript files and leave the html pages out of it
await Bun.build({
entrypoints: ['./public/page1.ts', './public/page2.ts', './public/page3.ts'],
sourcemap: "inline",
outdir: './public',
});
Is that possible?
Here's what I found for building all Typescript or Typescript in HTML files in a directory
bun add -d bun-plugin-html
if you want to bundle typescript in html pagesbun install
(or npm install)bun bun.build.js
to build and bundle the typescript (See contents of bun.build.js file)Your typescript classes will be compiled into a javascript file that bundles all of your typescript. (See https://bun.sh/)
bun.build.js:
// note: you could also pass in one or all of the HTML files
// but if you do that the HTML file might be over written
// to fix that, set an output directory in the options
import html from 'bun-plugin-html';
import { Glob } from "bun";
// read in all the typescript files in the /public directory
// (or the directory where your typescript files are located)
// and compile them into builded javascript files in the same directory
// using the same name as the typescript file but with a js extension
// your HTML pages should reference the javascript file
// unless you are compiling the HTML files
const sourceDirectory = "./public/";
const glob = new Glob('*.ts');
var entrypoints = [...glob.scanSync(sourceDirectory)];
entrypoints = entrypoints.map((x) => sourceDirectory + x);
console.log("Compiling " + entrypoints.length + " typescript files...");
const results = await Bun.build({
entrypoints: entrypoints,
publicPath: "",
sourcemap: "inline",
outdir: './',
plugins: [
html()
],
});
if (results.success==false) {
console.error("Build failed");
for (const message of results.logs) {
console.error(message);
}
}
else {
console.log("Compiled " + results.outputs.length + " javascript files...");
}