You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
rollup_bundle should use write_amd_names_shim #137
Thanks for this package, it's coming along nicely. What are the plans for supporting commonjs style dependencies in rollup bundles? In rollup_bundle.bzl I see that there is currently an unused TMPL_additional_plugins, however I'm unsure of the planned format of this, given that in the rollup.config.js is simply expecting the contents to either be previously required, or a full require+call expression (e.g. require('myplugin')())?
In any case, the rollup-plugin-commonjs is common enough that it probably warrants inclusion in the base set of plugins by default (bazelResolve, nodeResolve, and commonjsResolve).
I've hacked in default support for commonjsResolve into my own fork, however it was done w/o any thought to integrating it with the rollup_bundle rule give it's only for my use case.
The commonjs plugin causes problems, for example it tries to load rxjs commonjs distro which doesn't tree-shake well. We'd need the right assertions in tests to prevent the commonjs plugin loading code that was built by bazel.
Can you share the use case? I assume you'd like to bundle some off-the-shelf JS library with your app?
"The commonjs plugin causes problems, for example it tries to load rxjs commonjs distro which doesn't tree-shake well."
IIUC that's actually the fault of the rxjs package.json file previously only declaring pkg.main to point to the root index.js file, which is a cjs module. Now that rxjs is properly using pkg.module to point to the _esm5 targets plus per subpackage package.json files this should be fixed and a non-issue for including rollup-commonjs.
It appears then that the real fix is for package maintainers to migrate towards explicit es6 module support. In the meanwhile, providing cjs support by default (or at least optionally) seems reasonable.
I think it would be great in general if it was possible to have an easy way to provide further rollup plugins. Just testing this out and we are also in the need of the json plugin, the babel plugin, etc.
But thanks @alexeagle for the pointer on how Angular does it currently. That should more than do for the time being.
@Globegitter someone has proposed a babel_library rule that would be similar to ts_library. What would you use the json plugin for? Remember under Bazel, we want to avoid Rollup expanding to become a build tool - it's not incremental and not idiomatic. Bazel should run all the different tools as different processes. Making build-optimizer into a Rollup plugin was a workaround for wrong layering in the Angular toolchain.
@gregmagolan merged a change to the rollup_bundle rule yesterday to error if there is an unresolved external module. Thinking more, I was probably wrong in my last comment - this should already work with the write_amd_names_shim. The idea here is that a combination of the UMD distro of the library (which exposes a global variable) plus a define call to give an AMD module name to that global variable, is sufficient to make this work in the bundler without any CJS loading.
Greg, we should discuss this issue, I'm not clear what we need to do here.
Activity
alexeagle commentedon Mar 5, 2018
Angular uses that undocumented
TMPL_additional_pluginsfromng_rollup_bundle: https://github.com/angular/angular/blob/master/packages/bazel/src/ng_rollup_bundle.bzl#L27The intent is that Bazel rules can extend
rollup_bundlethis way, but not Bazel BUILD files (at least not yet).The commonjs plugin causes problems, for example it tries to load rxjs commonjs distro which doesn't tree-shake well. We'd need the right assertions in tests to prevent the commonjs plugin loading code that was built by bazel.
Can you share the use case? I assume you'd like to bundle some off-the-shelf JS library with your app?
erickj commentedon Mar 5, 2018
"for example it tries to load rxjs commonjs distro which doesn't tree-shake well"
:/ i've noticed
"I assume you'd like to bundle some off-the-shelf JS library with your app"
exactly, specifically protobufjs at the moment. there are several other libs as well. Unfortunately cjs is just unavoidable ATM, practically speaking.
erickj commentedon Mar 10, 2018
One update re:
"The commonjs plugin causes problems, for example it tries to load rxjs commonjs distro which doesn't tree-shake well."
IIUC that's actually the fault of the rxjs package.json file previously only declaring
pkg.mainto point to the root index.js file, which is a cjs module. Now that rxjs is properly usingpkg.moduleto point to the _esm5 targets plus per subpackage package.json files this should be fixed and a non-issue for including rollup-commonjs.It appears then that the real fix is for package maintainers to migrate towards explicit es6 module support. In the meanwhile, providing cjs support by default (or at least optionally) seems reasonable.
[-]Rollup rule support for rollup-plugin-commonjs[/-][+]rollup_bundle support for commonjs-distributed libraries[/+]Globegitter commentedon Mar 17, 2018
I think it would be great in general if it was possible to have an easy way to provide further rollup plugins. Just testing this out and we are also in the need of the json plugin, the babel plugin, etc.
But thanks @alexeagle for the pointer on how Angular does it currently. That should more than do for the time being.
alexeagle commentedon May 12, 2018
@Globegitter someone has proposed a
babel_libraryrule that would be similar tots_library. What would you use the json plugin for? Remember under Bazel, we want to avoid Rollup expanding to become a build tool - it's not incremental and not idiomatic. Bazel should run all the different tools as different processes. Making build-optimizer into a Rollup plugin was a workaround for wrong layering in the Angular toolchain.Back to the primary thread here - I added an
amd_namesattribute injs_libraryhttps://github.com/bazelbuild/rules_nodejs/blob/master/internal/js_library/js_library.bzl#L62
that we use in
ts_devserverandts_web_testto load the runtime protobufjs support. I think we just need to wire this up the same way forrollup_bundle[-]rollup_bundle support for commonjs-distributed libraries[/-][+]rollup_bundle should use write_amd_names_shim[/+]Globegitter commentedon May 15, 2018
@alexeagle yeah you are right about that the json plugin is for a bazel non-idiomatic usage. This comment is also related #149 (comment)
alexeagle commentedon May 15, 2018
@gregmagolan merged a change to the
rollup_bundlerule yesterday to error if there is an unresolved external module. Thinking more, I was probably wrong in my last comment - this should already work with thewrite_amd_names_shim. The idea here is that a combination of the UMD distro of the library (which exposes a global variable) plus adefinecall to give an AMD module name to that global variable, is sufficient to make this work in the bundler without any CJS loading.Greg, we should discuss this issue, I'm not clear what we need to do here.
gregmagolan commentedon May 16, 2018
Here's what I ended up with to get the protobuf example in rules_typescript to bundle with rollup: https://github.com/gregmagolan/rules_typescript/blob/e771ee7f29868a3ab0e763afbf1b161d68606cdd/examples/protocol_buffers/BUILD.bazel#L64. Rollup treats the protobuf imports as globals and the protobuf code needs to be loaded outside of the bundle in a script tag. Will think about this some more about how to make it simpler but this is one way to do it.
The rest of the diff is here: https://github.com/bazelbuild/rules_typescript/pull/204/files#diff-3db4baf0fd0becb125c0e7860df3637eR64
alexeagle commentedon Nov 30, 2018
@gregmagolan can we close this one?
alexeagle commentedon Sep 27, 2019
I think this can be closed now that the new
rollup_bundlerule gives you control of the plugins