Ad

Unable To Copy Bundle.js Of A React Project To Spring Project Using Webpack

- 1 answer

I have a React project within a Spring project. I want to have a debug environment, where React files are watched, and are compiled & copied to the Spring project (different directory). I am using Webpack 4.8.

Currently I have to run npm run build:dev every time I make changes to the React code, correct bundle.js is created and is moved to the correct location. But running it every time is a hassle. I've tried appending the script with --watch, and although it does create a new build, it isn't copied to the final location.

My package.json script

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node_modules\\.bin\\webpack-dev-server --config .\\webpack.dev.js",
    "dev": "webpack --config .\\webpack.dev.js",
    "build": "webpack --config .\\webpack.prod.js",
    "copy:prod": "copy .\\dist\\bundle.js ..\\xxx\\src\\main\\resources\\static\\ /Y",
    "copy:dev": "copy .\\dist\\bundle.js ..\\xxx\\build\\resources\\main\\static\\ /Y",
    "build:dev": "npm run dev && npm run copy:dev",
    "build:prod": "npm run build && npm run copy"
  },

For anyone interested, this is my webpack.dev.js

const path = require("path");
const { resolve } = require("path")

const config = {
  mode: "development",
  entry: "./src/index.js",
  stats: {
    colors: true
  },
  devtool: "inline-source-map",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      }
    ]
  }
};

 module.exports = config;

Ideally, I'd like any changes to the React code to trigger a build, and copy the build to a different location. The current situation is that I am able to get the correct build using --watch but it's not being copied to the specified location.

Ad

Answer

Okay, so I used a library called copy-webpack-plugin. And the updated webpack.dev.js file now looks like

const path = require("path");
const { resolve } = require("path");
const CopyPlugin = require('copy-webpack-plugin');

const config = {
  mode: "development",
  entry: "./src/index.js",
  stats: {
    colors: true
  },
  devtool: "inline-source-map",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      }
    ]
  },
  plugins: [
    new CopyPlugin([
        { from: './dist/bundle.js', to: '../../xxx/build/resources/main/static/', force: true},
        { from: './dist/bundle.js.map', to: '../../xxx/build/resources/main/static/', force: true}
    ], { copyUnmodified: true })
  ]
};

 module.exports = config;

And updated my npm script

"dev": "webpack --config .\\webpack.dev.js --watch",

Now when I run npm run dev I have the updated bundle.js copied in the desired location, and webpack also watches my React files.

Ad
source: stackoverflow.com
Ad