1 year ago

#384566

test-img

Md. A. Apu

Webpack build from sub directory

I've sub directories in the projects, each sub directory is like a project. I wanted to have separate webpack config file in each sub directory. Like following

├── package.json
├── subProject1
│   └── webpack.config.js      
└── subProject2
    └── webpack.config.js 

scripts in package.json

"scripts": {
  "build": "webpack",
  "build-p1": "webpack --config ./subProject1/webpack.config.js",
  "build-p2": "webpack --config ./subProject2/webpack.config.js" 
}

So my desired command will be npm run build-p1 or npm run build-p2; But seems like webpack is failing resolving relative modules in those sub directories.

Following is my webpack.config.js in sub directory.

const path = require('path');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');

const OUT_DIR = path.resolve(__dirname, 'dist');

module.exports = {
  mode: "development", // production
  context: __dirname,
  entry: path.join(__dirname, 'src/app.ts'),
  output: {
    filename: "bundle.js",
    path: OUT_DIR,
    publicPath: "dist" // not required for prod
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js']
  },
  plugins: [
    // clean dist fonder before build
    new CleanWebpackPlugin()
  ],
  
  devServer: {
    overlay: true,
    contentBase: OUT_DIR,
    watchContentBase: true,
    // publicPath: '/dist/',
    // open: true,
  },
};

With this I'm getting following error

Module not found: Error: Can't resolve './components/project-input.js' in '/[full-path-here]/subProject1/src'

That project-input.js is imported as ES6 module in src/app.ts

Can you spot which configuration I'm missing for this circumstances.

typescript

webpack

webpack-5

0 Answers

Your Answer

Accepted video resources