覆盖 Webpack 4 入口点

Overriding Webpack 4 Entry Point

这是我的项目目录:

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

这是我的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"
            }
        ]
    }
}

我想覆盖 webpack.config.js 中的默认入口点以指向同一级别的 index.js 文件。

默认情况下,如果我在 src 目录中有 index.js 文件,而不指定入口点,它会按预期工作。

我试过像这样更改入口点:

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'

我使用的是 windows 10 操作系统。

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

参考:https://webpack.js.org/concepts/entry-points/


或者,您也可以使用

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

参考:https://nodejs.org/api/modules.html#modules_require_resolve_request_options

解决我问题的方法是将 webpack.config.js 文件移动到根文件夹,然后像这样配置入口点:

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