11ty / 11ty/eleventy-plugin-bundle

Bundling javascript modules

Open
#16 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
JavaScript
Stars
83
Forks
8
PR merge metrics
No merged PRs in 30d

Description

ab.webc:

<script type="module">
import { LitElement } from 'lit';
customElements.define('a-b', class extends LitElement {});
</script>

cd.webc:

<script type="module">
import { LitElement } from 'lit';
customElements.define('c-d', class extends LitElement {});
</script>

index.webc:

<script type="module" :src="getBundleFileUrl('js')" webc:keep></script>

Uncaught SyntaxError: redeclaration of import LitElement3k1axR86sr.js:3:9note: Previously declared at line 1, column 9

One way to fix this would be to add an async callback to the bundles option which provides an api to collect sources and write outputs, or to do so in the transforms option.

eleventyConfig.addPlugin(bundlerPlugin, {
  transforms: [
    async function(content, collect) {
          // this.type returns the bundle name.
      if (this.type === 'js' &&
          // proposal: this.attributes exposes the source element's attributes
          this.attributes.type === 'module') {
        collect(this, content);
        return false; // don't bundle, instead hold the item in memory
      }
    },
    async function(content) {
      if (this.type == bundlerPlugin.collection) { // a Symbol()
        const { build } = await import('esbuild');
        const result = await build({
          bundle: true,
          /* ... collected has source strings and metadata, etc */
          entryPoints: writeToTmpAndMakeEntryPointsConfig(collected),
        });
        const [{ path, contents }] = result.outputFiles;
        return {
           url: path,
           content: contents,
        }
      }
    }
  ],
});

above APIs are proposals for discussion only, salt to taste.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ab.webc, cd.webc, and index.webc examples and trace how the bundles and transforms options handle module scripts. Review the proposed collect callback and esbuild integration, then confirm the API design with maintainers. Done means module sources can be collected and bundled without duplicate import declarations, with documented behavior and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.