Skip to content

promote the js_library rule to the public API #149

Closed

Description

@javatlacati

I looked through the definitions file and couldn't find any nodejs_library definition, does that means that I can only have exactly one global BUILD and have a monolithic building process?

Activity

  1. alexeagle commented on Mar 12, 2018

    @alexeagle
    Collaborator

    what would you expect such a nodejs_library rule to do?

    If you want to compile libraries in isolation, use a compile-to-JS rule like ts_library
    If you want to publish a package to npm, use npm_package

  2. an-kumar commented on Mar 20, 2018

    @an-kumar

    I think the point would be to have multiple nodejs packages that reference one another in the same repository. e.g:

    /path/to/first/package
    /path/to/second/package (dependent on first package)
    /path/to/some/binary (dependent on second package; transitively dependent on first package)

  3. alexeagle commented on Mar 23, 2018

    @alexeagle
    Collaborator

    @an-kumar yes, this is supported already

    For TypeScript, you have

    /path/to/first/package
      BUILD.bazel
        ts_library(name = "first", module_name = "first", srcs =["first.ts"])
    /path/to/second/package
      BUILD.bazel
        ts_library(name = "second" module_name = "second", srcs = ["second.ts"], deps = ["//path/to/first/package:first"])
      second.ts
        import * as f from 'first';
    /path/to/some/binary
      BUILD.bazel
        ts_library(name = "program", deps = ["//path/to/second/package:second"], srcs = ["program.ts"])
        nodejs_binary(name = "bin", deps = [":program"], ...)
      program.ts
        import * as s from 'second';
    

    If you don't use TypeScript, this is still supported, but not yet documented because we aren't sure we understand the use case (why use Bazel if you're not running any compilers?)

    https://github.com/bazelbuild/rules_nodejs/blob/befbd8961c18de98e254bea94db08ede78889f86/internal/js_library/js_library.bzl
    lets you assign the module_name just like above TS example

  4. Globegitter commented on Mar 25, 2018

    @Globegitter

    @alexeagle Our use case would be to have shared code between different rollup bundles. Both would then just define that common js_library as a dep. Or would you there just directly depend on the files?

    Edit: To be more precise, that particular use-case I am describing is in a monorepository structure, so it would be something like:

    /WORKSPACE
    /common # shared es6 modules
    /app1 # rollup_bundle depending on some libraries in common
    /app2 # rollup_bundle depending on some libraries in common
    ...
    

    Edit 2:
    Also something like a babel_library could be interesting to allow incremental babel transpilation.

  5. alexeagle commented on Apr 3, 2018

    @alexeagle
    Collaborator

    you can just use a plain filegroup rule if you want to be able to reference the same group of .js inputs in several different bundling rules.

    babel_library sounds like a great addition, want to try a PR for that?

  6. nimerritt commented on Apr 6, 2018

    @nimerritt
    Contributor

    @Globegitter we would also benefit from a babel rule as we are migrating a large service to TypeScript and it's nice to use Bazel to mix between the two as we move forward, especially given that the TypeScript rules don't take .js files.

  7. an-kumar commented on Apr 21, 2018

    @an-kumar

    @alexeagle well, similar to how in second.ts you can import from 'first', it would be nice if in a second.js you could require('first'). In this sense, the compilation is happening in the nodejs_binary rule, where the modules are being used -- creating a runnable nodejs binary with it's declared dependencies and transitive dependencies correctly in the node_modules path with the correct names.

    this doesn't seem doable with plain filegroups.

  8. an-kumar commented on Apr 21, 2018

    @an-kumar

    @alexeagle it also doesnt seem like nodejs_binary has a deps attribute, am i missing something about your ts example?

  9. Globegitter commented on Apr 25, 2018

    @Globegitter

    Yeah also the one thing about no nodejs_library because it does not compile - isn't it the same as python_library? Not sure if that is doing much either. But yeah happy to try filegroup for now and can hopefully get around to PRing a babel_library.

  10. alexeagle commented on Apr 27, 2018

    @alexeagle
    Collaborator

    @an-kumar nodejs_binary has no compile-time deps, only runtime deps. Bazel typically uses data[] for the latter, so that's where your .js files should end up to be require-able at runtime.

    Is there any change needed for this issue, or can I close it?

  11. prestonvanloon commented on May 11, 2018

    @prestonvanloon

    You can't use a filegroup as a dep of ts_library because it doesn't have a "JS" attribute.

    +1 to implement js_library.

    Also see bazelbuild/rules_typescript#201

  12. alexeagle commented on May 11, 2018

    @alexeagle
    Collaborator

    There actually is js_library already. It's undocumented because I'm not sure about all the use cases.
    https://github.com/bazelbuild/rules_typescript/blob/63bb1a33f5231c72d7d503454e986e10139ed2ff/BUILD.bazel#L64 is an example where we use it - not because it produces outputs that a filegroup wouldn't, but because we want a place to mark some extra info (in this case the AMD module name mapping, but it can also be useful for ESModule name mapping)

    For this issue, I still suspect the .js files can be a filegroup in the data of a ts_library - you wouldn't type-check your usage of the JS code from TypeScript, just need it to be present to require it at runtime.

    I'm still looking for a concrete use case that requires some change here...

  13. changed the title [-]no nodejs_library definition[/-] [+]promote the js_library rule to the public API[/+] on May 12, 2018
  14. Globegitter commented on May 15, 2018

    @Globegitter

    Now that I have had some time to evaluate things more and after the insights gainedin #200, I think what I would expect from bazel for our use-case is to provide a js_library which can create the AMD output as expected by ts_devserver (better even js_devserver in the future) then other rules could be easily written on top of that, such as babel_library or json_to_js_library producing the same amd output for dev and es6 module output for prod/rollup. Does that make sense?

  15. 18 remaining items

  16. pward123 commented on Feb 28, 2020

    @pward123

    This looks great!

    Would it be possible to support rollup_bundle so that @rollup/plugin-node-resolve automatically bundles monorepo dependencies?

  17. added this to the 2.0 milestone on Jun 9, 2020
  18. alexeagle commented on Jun 15, 2020

    @alexeagle
    Collaborator

    we ran into the issue that pkg_npm produces a TreeArtifact (output directory) which means it can't also supply individual file outputs to satisfy providers like DeclarationInfo. So it needs to have a non-directory output mode.

    This would be a non-breaking change so we'll put it off until 2.x after 2.0

  19. removed this from the 2.0 milestone on Jun 15, 2020
  20. added 6 commits that reference this issue on Sep 10, 2020
    ec5337e
    0c35e09
    58b043c
    a9e580c
    72e5ad4
    a4fb119
  21. added a commit that references this issue on Sep 10, 2020
    1e357fd
  22. added a commit that references this issue on Jan 15, 2021
    9671291
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions