workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🚀 Feature Request: Make bundling and rules 2 different concepts

Open vicb opened this issue 1 year ago • 8 comments

  1. We have rules to upload different files:
rules = [
  { type = "CommonJS", globs = ["**/*.js"], fallthrough = true }
]
  1. By default wrangler bundles all the files together (using esbuild) that is rules from 1 above are ignore and all the imports are inlined.

As of today 1 and 2 are grouped around a single "Bundling" paradigm.

That is:

  • wrangler deploys will activate 2 and ignore 1
  • wrangler deploys --no-bundle will activate 1 and skip 1 (esbuild)

I believe 1 and 2 should be different concepts and not mutually exclusive.

For example you could have you framework composed of multiple files and then a directory containing the different pages of your application.

- app/
  - index.js
  - util.js
  - render.js
- pages/
  - homepage.js  
  - about.js

In that case it would be nice to bundle the app into a single file (2) and being able to require the page matching the url (1).

Updating ESBuild could solve some of the use case with wildcard imports.

workerd recently introduced createRequire. You can now write:

import { createRequire } from 'module';
const require = createRequire('/');
const msg = require('./hello.cjs').world();
console.log(`hello ${msg}`);

The code works fine with --no-bundle - as long as the hello.cjs has a matching rule in wrangler.toml. However the code will fail without --no-bundle because it can not resolve hello.cjs. If require is passed a variable, the code will fail to build because of the dynamic require.

That's why I believe we should be able to bundle the app/framework but still have module that you can require.

If both 1 and 2 can be used together, files matched by a rules must be marked as external so that esbuild does not try to inline them.

/cc @IgorMinar

vicb avatar Oct 10 '24 19:10 vicb