Overriding Webpack 4 Entry Point

给你一囗甜甜゛ 提交于 2021-01-29 10:19:47

问题


Here's the directory of my project:

   -node_modules
   -src
       -client
          -js
          -styles
          -views
          -index.js
          -webpack.config.js
       -server
   -.babelrc
   -package
   -package-lock
   -README.md
   -webpack.dev
   -webpack.prod

Here's the content of my webpack.config.js

const path = require('path')
const webpack = require('webpack')

module.exports = {
    mode: 'none',
    //entry: path.resolve(__dirname) + './src/client/index.js',

    module: {
        rules: [
            {
                test: '/\.js$./',
                exclude: /node_modules/,
                loader: "babel-loader"
            }
        ]
    }
}

I want to override the default entry point inside webpack.config.js to point to the index.js file in the same level.

By default, if I have the index.js file inside the src directory, without specifying the entry point, it works as expected.

I have tried changing the entry points like this:

entry: path.resolve(__dirname, './src/client/index.js'),

entry: path.resolve(__dirname, 'src') + 'client/index.js',

entry: path.resolve(__dirname, './src/index.js'),

entry: path.resolve(__dirname) + '/src/client/index.js',

entry: path.resolve(__dirname) + './src/client/index.js'

I'm using a windows 10 Operating System.


回答1:


module.exports = {
  entry: './index.js'
};

Ref: https://webpack.js.org/concepts/entry-points/


Alternatively, you can also use

module.exports = {
  entry: require.resolve('./index')
};

Ref: https://nodejs.org/api/modules.html#modules_require_resolve_request_options




回答2:


What solved my problem was to move the webpack.config.js file to the root folder and then configured the entry point like this:

module.exports = {
    ...    
    entry: './src/client/index.js',
    ...
}


来源:https://stackoverflow.com/questions/58915153/overriding-webpack-4-entry-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!