1

I created a Typescript library for internal use and I want to configure my webpack-dev-serve to serve a build version of my library for users who will have to use it on local development.

Because currently, I have to run first npm run build and then npm run start. What I want to achieve is to remove the run build step and say to webpack-dev-serve, serve me the build version. I have tried several approaches and I didn't solve it, maybe I'm wrong.

package.json

{
  "name": "lib",
  "version": "0.0.0",
  "scripts": {
    "start": "webpack serve --mode=production",
    "build": "webpack --mode=production",
...
}

webpack.config.js

const path = require('path');

module.exports = (env = {}) => {
  return {
    devtool: env.WEBPACK_SERVE ? 'source-map' : undefined,
    devServer: {
      filename: 'main.js',
      port: 5050,
    },
    entry: './src/main.ts',
    module: {
      rules: [
        {
          exclude: /node_modules/,
          test: /\.tsx?$/,
          use: 'ts-loader',
        },
      ],
    },
    output: {
        filename: 'main.js',
        libraryTarget: 'system',
        path: path.join(__dirname, 'dist'),
      },
    resolve: {
      extensions: ['.js', '.ts'],
    },
  };
};

My approach:

  • I tried to use the flag --mode=production but the code is quite different and not works for me.
  • I included the historyApiFallbackinside the devServe, pointing to the path dist/main.js, but I still need to run first the npm run build

My goal is to call localhost:5050/main.js path and get the build version.

jrltt
  • 651
  • 10
  • 23

0 Answers0