rules_esbuild icon indicating copy to clipboard operation
rules_esbuild copied to clipboard

esbuild_config macro

Open alexeagle opened this issue 3 years ago • 9 comments

Yeah I was going to comment that we should include that macro here https://github.com/bazelbuild/rules_nodejs/blob/stable/packages/esbuild/test/plugins/BUILD.bazel#L21-L37

alexeagle avatar Apr 19 '22 14:04 alexeagle

Hi @alexeagle. I'm trying to early adopt these esbuild rules and I'm facing an issue with not found dependencies (esbuild plugins).

To emulate the config from the previous esbuild rules I copied the config macro:

def esbuild_config(name, config_file, srcs = [], deps = [], **kwargs):
    """Macro for an esbuild configuration file and its assoicated dependencies
    Args:
        name: Unique name for this rule
        config_file: The configuration file / entrypoint
        srcs: List of source files referenced by the configuration
        deps: List of dependencies required for this configuration
        **kwargs: Any other common attributes
    """

    js_library(
        name = name,
        srcs = [config_file],
        **kwargs
    )

    js_library(
        name = "%s_deps" % name,
        srcs = srcs,
        deps = deps,
        **kwargs
    )

Reference it in the application BUILD file:

esbuild_config(
    name = "esbuild_config",
    config_file = "esbuild.config.mjs",
    deps = [
        "@npm//@types/node",
        "@npm//esbuild-plugin-alias",
        "@npm//esbuild-plugin-svgr",
    ],
)

esbuild(
    name = "bundle",
    config = ":esbuild_config",
    entry_point = "src/main.jsx",
    splitting = True,
    deps = [ ":webapp" ],
)

And esbuild bundle crashes with:

Error while loading configuration 
Cannot find package 'esbuild-plugin-alias' imported from /private/...../esbuild.config.mjs

This setup worked well with thre previous esbuild rules.. Am I missing something?

danigar avatar Apr 19 '22 15:04 danigar

Hi @danigar that's exciting to see such bleeding edge usage already :)

Your plugin usage there depends on the "dynamic linker" in rules_nodejs, so that your call to esbuild will get those extra npm packages inserted into the binary. rules_esbuild depends only on our new re-implementation in aspect-build/rules_js and that usecase is coming soon in https://github.com/aspect-build/rules_js/pull/31

In the meantime if you'd like to make this work, then you just need to make a custom esbuild binary that has all your plugins "statically linked", which is to say, in the data of the nodejs_binary:

nodejs_binary(
    name = "launcher",
    entry_point = "@aspect_rules_esbuild//esbuild/private:launcher.js",
    data = [
        "@npm_esbuild-{version}",
        # ... more here
    ],
)

and then pass that to the launcher attribute of the esbuild rule.

I'll add an example to the repo so it's more clear, and also gives us the spot to update when rules_js is ready.

alexeagle avatar Apr 20 '22 14:04 alexeagle

https://github.com/aspect-build/rules_esbuild/pull/11 is that example, however it's not working yet...

alexeagle avatar Apr 20 '22 14:04 alexeagle

Thank you for the clarification, Alex. I will keep an eye on this and use esbuild from rules_nodejs in the meantime.

danigar avatar Apr 20 '22 17:04 danigar

To be clear, we are reviewing the rules_js fixes right now so it's quite possible we'll have #11 merged this week. If you're okay staying on bleeding edge, we're happy to have a bit of good feedback from adoption.

alexeagle avatar Apr 20 '22 17:04 alexeagle

Sure. Happy to help giving feedback 👍

danigar avatar Apr 21 '22 07:04 danigar

The svg plugin is working now in https://github.com/aspect-build/rules_esbuild/pull/11

alexeagle avatar Apr 21 '22 17:04 alexeagle

There is still a TODO for this https://github.com/aspect-build/rules_esbuild/blob/c12ccaeb0d97c3959250e7f81685c6296342b5c3/esbuild/private/esbuild.bzl#L163

alexeagle avatar Mar 27 '23 16:03 alexeagle

I can't tell if this is actually still needed, or if writing a config file like /examples/plugin shows is easy enough that we can just drop the esbuild_config macro

alexeagle avatar Mar 27 '23 16:03 alexeagle