-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Can't bundle html file along using file loader #621
Comments
Actually I just realized that esbuild doesn't serve the user's html file, instead you have to fetch the bundle from the serve api using a separate html file. I think this is pretty inconvenient since you would have two almost identical html files, one for development fetching the bundle from the serve api and one for production that imports the actual bundle. |
I just ran into this myself, your html file will be ignored unless you use its path. For example:
That will result in the file being copied. I was about to open a ticket about this to see if maybe there's a way to bypass that with a plugin. |
Ah that error message makes sense to me now. Thanks for clarifying. Now that I think about it, maybe a better solution would be to instead use a shell command before building, like |
Even if I already have an HTML file in the public folder, I don't see it when I use the serve API. I only see JS files. Did you guys find a way? |
The idea is that the serve API serves the bundled files, not your html file. Then, in your html file you import from the URL of the serve API. So if it is running on As I said in a previous comment, I think the serve API should serve your html file too because currently you need to use different file paths for dev and prod. I ended up just using nginx to serve everything and watch-exec to execute esbuild everytime I save a file. |
Oh, I see. It's possible to do something for the path, but you still need to serve the HTML somehow. 😕 |
@AntoineBoulanger Here's a zero-dependency reverse-proxy server that wraps esbuild's serve in 50 lines: import esbuild from 'esbuild';
import http from 'http';
import fs from 'fs';
import path from 'path';
let __dirname = path.dirname(new URL(import.meta.url).pathname);
esbuild
.serve({ port: 3000 }, {
entryPoints: ['./index.tsx'],
format: 'esm',
bundle: true,
})
.catch(err => {
console.error(err);
process.exit(1);
});
let mime = {
'.html': 'text/html',
'.css': 'text/css',
};
let server = http.createServer((req, res) => {
let filename = req.url && req.url !== '/' ? req.url : 'index.html';
let filepath = path.join(__dirname, filename);
if (fs.existsSync(filepath)) {
let stream = fs.createReadStream(filepath);
stream.on('ready', () => {
let type = mime[path.parse(filepath).ext] || 'application/octet-stream';
console.log(`${req.method} ${req.url} => 200 ${type}`);
res.writeHead(200, { 'content-type': type });
stream.pipe(res);
});
stream.on('error', err => {
console.log(`${req.method} ${req.url} => 500 ${filepath} ${err.name}`);
res.writeHead(500, err.name);
res.end(JSON.stringify(err));
});
} else {
let reqProxy = http.request({ path: req.url, port: 3000 });
reqProxy.on('response', resProxy => {
let type = resProxy.headers['content-type'];
console.log(`${req.method} ${req.url} => ${resProxy.statusCode} ${type} via esbuild`);
res.writeHead(resProxy.statusCode, { 'content-type': type });
resProxy.pipe(res);
});
req.pipe(reqProxy);
}
});
server.listen(4000, err => {
if (err) throw err;
console.log('Served on http://localhost:4000');
}); Then I don't even have to worry about paths. My script tag points to "/index.js':
If you're serving a bunch of different content types then you'll have to add to |
Looks like with v0.8.45 we were gifted the |
Hello, some time has passed since the original question, and the only simple answer seems to be:
I can't find a way to simply copy my
Has this been solved, or do I need to use Deno or Node to copy over |
In my experience when I've needed to copy entire folders I needed to write some Go / JS (essentially using Deno for your use case). This is how I went about it at the time but things may have changed: #982 (comment). Hopefully there are better approaches, this is simple what's worked for me. Once HTML becomes a first-class citizen in esbuild I don't think this will be an issue anymore. |
Now that plugins have Start and End callbacks, one thing you can do is maintain a list of files to copy:
There are some interesting advantages to this approach:
|
It might be worth noting here that the 0.17.0 commit mentions adding |
Thanks @cameronelliott! The |
I want esbuild to copy my index.html file along with the js bundle to the
public
folder so that I can use it with theserve
api. I'm getting this message:What am I doing wrong?
The text was updated successfully, but these errors were encountered: