【问题标题】:Webpack bundle.js changes not showing in a Spring application without restarting the server?在不重新启动服务器的情况下,Webpack bundle.js 更改不会显示在 Spring 应用程序中?
【发布时间】:2019-09-01 21:54:33
【问题描述】:

我按照本教程创建了一个 Spring-boot+ ReactJs 应用程序:https://spring.io/guides/tutorials/react-and-spring-data-rest/

应用程序以./mvnw spring-boot: run 启动,我有一个运行以下配置的 NPM 脚本"watch": "webpack --watch -d"

var path = require('path');

module.exports = {
    entry: './src/main/js/app.js',
    devtool: 'sourcemaps',
    cache: true,
    mode: 'development',
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/bundle.js'
    },
    module: {
        rules: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }]
            }
        ]
    }
};

app.js 按预期编译成bundle.js,但如果我在刷新页面时不使用./mvnw spring-boot:run 重新启动服务器,我将看不到更改。

我可能遗漏了一些东西,也许 spring-boot 没有使用我的 bundle.js 文件而是某种副本?

【问题讨论】:

    标签: reactjs spring-boot webpack


    【解决方案1】:

    我可以猜测嵌入式 tomcat 不会热重载静态文件。你可以试试这些Refreshing static content with Spring MVC and Boot

    但也将分享我的解决方案,即使用 webpack-dev-server 进行热重载和一般的 Web 开发。

    const path = require('path');
    ...
    devServer: {
        contentBase: path.join(__dirname, 'src'),
        publicPath: '/build',
        port: 8000,
        proxy: {
            "**": "http://localhost:8080"
        }
    },
    

    然后运行webpack-dev-server 命令来运行它。它会即时编译您的应用程序,您的更改会立即反映出来,一定要试一试。

    【讨论】:

    • 我尝试使用webpack-dev-server,它可以编译,但仍然没有反映更改。我还尝试了 spring 的热重载,它需要一个名为 spring-boot-devtools 的额外依赖项,据说它应该完全符合我的要求,但它仍然无法正常工作。如果我找到可行的解决方案,我会通知您。还是谢谢
    【解决方案2】:

    我今天遇到了同样的问题并解决了。

    关键是同时运行npm run watchmvn spring-boot:run

    确保你有

        "scripts": {
            "watch": "webpack --watch -d --output .//classes/static/built/bundle.js"
        }
    

    在您的package.json

        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-devtools</artifactId>
             <optional>true</optional>
        </dependency>   
    

    在你的pom.xml

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 2011-04-20
      • 2013-03-28
      • 1970-01-01
      相关资源
      最近更新 更多