run webpack as a postinstall npm script

540 views Asked by At

I'm updating a private node_module that we install in projects via bitbucket. The package has some overrides that get bundled up via webpack and that bundle is used directly (i.e. outside of the build tools used for the project I'm installing the module for).

I would like to fire the module's build command as a postinstall script so that when we install or update the module in the main project the bundle is rebuilt.

In the module's package.json I have this:

"scripts": {
    "build": "webpack",
    "dev": "webpack --watch",
    "serve": "webpack-dev-server",
    "postinstall": "npm run build"
  },

and I have the following webpack.config.js file:

const path = require("path")

module.exports = {
  mode: "development",
  watch: false,
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  devServer: {
    contentBase: "dist",
  },
  module: {
    rules: [
      {
        test: /\.js/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
    ],
  },
}

And I've confirmed that firing npm run build works just fine in the modules codebase, but when I go to update the module in the main codebase I get errors:

webpack errors

Webpack is blowing up on the option chaining which I thought would be handled by the babel loader in the module's webpack config.

I double checked my module's package.json file and it definitely has babel's presets in the regular dependencies and not the dev dependencies (i.e. it gets installed as part of the module install).

dependencies list

Am I doing something wrong here? It seems like if the same build process works

1

There are 1 answers

0
Oliver Wolf On

Make sure your exports in the package.json points to the sources in the dist:

  "exports": {
    ".": "./dist/bundle.js"
  },

https://nodejs.org/api/packages.html#package-entry-points