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

How to use webpack_dev_server? #2098

Open
andy-zhou opened this issue Aug 5, 2020 · 9 comments
Open

How to use webpack_dev_server? #2098

andy-zhou opened this issue Aug 5, 2020 · 9 comments
Labels
help wanted need: discussion On the agenda for team meeting to determine next steps

Comments

@andy-zhou
Copy link

I found #1255, which seems to suggest that ibazel works with webpack_dev_server in examples/react_webpack. It's not clear how webpack_dev_server is involved in that example, however. Could you help explain?

@pward123
Copy link

I don't believe ibazel and webpack_dev_server work together. You just use webpack_dev_server. Here's how I'm using it.

load("@npm//http-server:index.bzl", "http_server")
load("@npm//webpack-cli:index.bzl", "webpack_cli")
load("@npm//webpack-dev-server:index.bzl", "webpack_dev_server")

package(default_visibility = ["//visibility:public"])

NPM_PACKAGES = [
    "@npm//@apollo/client",
    "@npm//@types/react",
    "@npm//graphql",
    "@npm//rc-slider",
    "@npm//react",
    "@npm//react-archer",
    "@npm//react-dom",
    "@npm//react-toastify",
    "@npm//styled-components",
    "@npm//subscriptions-transport-ws",
    "@npm//swiper",
]

MONOREPO_PACKAGES = [
]

filegroup(
    name = "srcs",
    srcs = glob(
        include = [
            "index.js",
            "src/**/*",
        ],
        exclude = [
            "**/*.spec.js",
            "node_modules/**/*",
        ],
    ),
)

# Note, on Windows you need `--enable_runfiles`
webpack_dev_server(
    name = "web-app",
    data = NPM_PACKAGES + MONOREPO_PACKAGES + [
        ":srcs",
        "webpack.config.js",
    ],
    templated_args = [
        "--config",
        "$(execpath webpack.config.js)",
        "--mode",
        "production",
    ],
)

webpack_cli(
    name = "bundle",
    output_dir = True,
    args = [
        "$(execpath index.js)",
        "--config",
        "$(execpath webpack.config.js)",
        "--mode",
        "production",
        "-o",
        "$(@D)/dsp.bundle.js",
    ],
    data = NPM_PACKAGES + MONOREPO_PACKAGES + [
        ":srcs",

        # For some reason I can't explain, the execpath items above
        # explicitly need to be included even though they're part of
        # the :srcs filegroup
        "index.js",
        "webpack.config.js",

        # dependencies specific to bundling
        "@npm//@babel/preset-env",
        "@npm//@babel/preset-react",
        "@npm//babel-loader",
        "@npm//css-loader",
        "@npm//copy-webpack-plugin",
        "@npm//html-webpack-plugin",
        "@npm//sass-loader",
        "@npm//style-loader",
    ],
)

# Production web server
http_server(
    name = "server.prod",
    data = ["//web-app:bundle"],
    templated_args = ["$$(rlocation $(rootpath //web-app:bundle))"],
)

@andy-zhou
Copy link
Author

Thanks for the reply!

Correct me if I'm wrong, but my impression is that without ibazel, bazel won't know about file changes and therefore webpack_dev_server won't handle live reloading.

If that's right, how do you support live reloading? Is the best method just ibazel and webpack_cli, ignoring the things that make webpack_dev_server nice?

@pward123
Copy link

In our case, we don't have any references to other targets in the workspace, only @npm// references. That may be why it's working for us without running ibazel.

I've always assumed that HMR would be pointless if you have ibazel launching webpack_dev_server because ibazel would restart it anytime files change. One workaround to try would be creating a target that had all of the monorepo references needed by the web-app and running an ibazel on that concurrently with webpack_dev_server

@andy-zhou
Copy link
Author

Interesting point about @npm// references working. Would be interesting to dig into why that works in the future.

Re: the second paragraph, adding ibazel_notify_changes to the tags attribute prevents the command from reloading, so I've been trying to get that to work. WIP, but I think it's blocked by bug I've linked, unfortunately.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!

@github-actions github-actions bot added the Can Close? We will close this in 30 days if there is no further activity label Oct 21, 2020
@alexeagle alexeagle added need: discussion On the agenda for team meeting to determine next steps and removed Can Close? We will close this in 30 days if there is no further activity labels Oct 21, 2020
@alexeagle
Copy link
Collaborator

@mrmeku has started a new effort for a dedicated webpack rule

@andy-zhou
Copy link
Author

andy-zhou commented Oct 21, 2020 via email

@mgenov
Copy link

mgenov commented Nov 28, 2020

@pward123 could you share and your webpack config?

What I see is that webpack-dev-server is started from root directory and is not able to find properly the entry point and source files. All examples in the repository are using root as starting point and the examples are working fine but this is not a case when you have multiple apps in separate folders each (in my case: few apps that needs to be migrated to use bazel-like way).

@Aghassi
Copy link
Contributor

Aghassi commented Jan 29, 2021

#2431 I've opened a PR that shows how to do this using webpack to invoke the server. It may not be 💯 the right way to do it, but it works. Happy to get input from other folks.

@mgenov My PR uses the --run-under flag for bazel to solve that problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted need: discussion On the agenda for team meeting to determine next steps
Projects
None yet
Development

No branches or pull requests

6 participants