developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.06k stars 361 forks source link

umd output for `js` file not work in `<script />` tag #862

Closed littlee closed 3 years ago

littlee commented 3 years ago

./src/render.js

module.exports = {
  a: 1,
};

build log:

> microbundle build -i ./src/render.js --no-compress --no-sourcemap -f umd --name=MyLib

Build "MyLib" to dist:
        129 B: my-lib.umd.js.gz
        102 B: my-lib.umd.js.br

output file:

(function (factory) {
  typeof define === 'function' && define.amd ? define(factory) :
  factory();
}((function () {
  module.exports = {
    a: 1
  };

})));

the output file does not export MyLib to global

but when I change my input file to a .ts file, it works

./src/render.ts

export default { a: 1};
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  typeof define === 'function' && define.amd ? define(factory) :
  (global = global || self, global.MyLib = factory());
}(this, (function () {
  var render = {
    a: 1
  };

  return render;

})));
developit commented 3 years ago

Microbundle does not support CommonJS entry modules. Your JS version would work if you used ES Modules (this is the reason the TypeScript version worked):

export default {
  a: 1,
};
// or
export let a = 1;