Skip to content
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

swc_bundler: support outputting an IIFE #1184

Closed
kitsonk opened this issue Oct 24, 2020 · 3 comments · Fixed by #1189
Closed

swc_bundler: support outputting an IIFE #1184

kitsonk opened this issue Oct 24, 2020 · 3 comments · Fixed by #1189
Assignees
Milestone

Comments

@kitsonk
Copy link
Contributor

kitsonk commented Oct 24, 2020

Describe the feature

Currently, swc_bundler can output an ES module, but it does support output as an IIFE.

For outputting the bundle, instead of exporting entries from the entry module in the bundle, swc_bundler would output an IIFE where the return value of the IIFE is the value of the exported module. For example if the entry module was:

a.js

export const a = "a";

The output of the bundle would be something like:

a.bundle.js

(function () {
  "use strict";
  var __exports = {};
  Object.defineProperty(__exports, "__esModule", {
    value: true
  });
  __exports.a = "a";
  return __exports;
})();

Babel plugin or link to the feature description

There are Babel plugins that in theory can do this, but IIFE output is one of the builtin output formats of Rollup (and other bundlers).

Additional context

In particular with Deno, we would like to have an option for Deno.bundle() to be able to output a single file ES module or an IIFE.

@kitsonk
Copy link
Contributor Author

kitsonk commented Oct 24, 2020

A couple of other considerations:

  • For emitting dynamic imports, assume that there is some sort of of import() global?

  • Support TLA by emitting an async IIFE, meaning the consumer would need to figure out how to wait for the promise resolution. For example, given the input of:

    b.js

    export const b = await Promise.resolve("b");

    The output would be something like:

    b.bundle.js

    (async function () {
      "use strict";
      var __exports = {};
      Object.defineProperty(__exports, "__esModule", {
        value: true
      });
      __exports.b = await Promise.resolve("b");
      return __exports;
    })();

@kdy1 kdy1 added this to the v1.2.37 milestone Oct 27, 2020
@kdy1 kdy1 mentioned this issue Oct 27, 2020
@kdy1 kdy1 self-assigned this Oct 27, 2020
kdy1 added a commit that referenced this issue Oct 28, 2020
swc_bundler:
 - Support emitting iife. (Closes #1184)

swc_ecma_parser:
 - Auto-detect script / module (Closes #1164)
 - lexer: Error recovery for strict mode and module mode.
 - More error recovery logic for strict mode violation.
 - Fix panic on invalid input. (Closes #1170)
@ClarisL
Copy link

ClarisL commented Jan 26, 2022

Hello,

I'm really interested in using the IIFE output from swc_bundler.
Happy to see this is now available !
Still I did not find any documentation regarding the setup part.

Do you have any piece of advice regarding how this can be setup ?
Thanks,

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 19, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants