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

esbuild_config macro #9

Open
alexeagle opened this issue Apr 19, 2022 · 9 comments
Open

esbuild_config macro #9

alexeagle opened this issue Apr 19, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@alexeagle
Copy link
Member

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

@danigar
Copy link

danigar commented Apr 19, 2022

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?

@alexeagle
Copy link
Member Author

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 aspect-build/rules_js#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
Copy link
Member Author

#11 is that example, however it's not working yet...

@danigar
Copy link

danigar commented Apr 20, 2022

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

@alexeagle
Copy link
Member Author

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.

@danigar
Copy link

danigar commented Apr 21, 2022

Sure. Happy to help giving feedback 👍

@alexeagle
Copy link
Member Author

The svg plugin is working now in #11

@cgrindel cgrindel added enhancement New feature or request prioritized labels Sep 27, 2022
@alexeagle
Copy link
Member Author

There is still a TODO for this

TODO: show how to write a config file that depends on plugins, similar to the esbuild_config macro in rules_nodejs.

@alexeagle
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: On Deck
Development

No branches or pull requests

4 participants