Create-React-App 有一个非常有趣的设置。
我开始挖掘package.jsonnpm 脚本start
“开始”:“react脚本开始”
这
会将我带到他们的二进制文件中react-scripts,node_modules/.bin
我将在此处发布相关内容。
switch (script) {
  case 'build':
  case 'eject':
  case 'start':
  case 'test': {
    const result = spawn.sync(
      'node',
      [require.resolve('../scripts/' + script)].concat(args),
      { stdio: 'inherit' }
    );
所以这告诉我他们正在../scripts/文件夹中寻找脚本。
所以我转到 react-scripts npm module( node_modules/react-scripts) 并打开node_modules/react-scripts/scripts/start.js文件,因为我正在做npm start.
现在在这里我找到了我正在寻找的 webpack 配置。
他们特指node_modules/react-scripts/config/webpack.config.dev.js. 我会在这里发布相关的内容。
entry: [
  // Finally, this is your app's code:
  paths.appIndexJs,
],
plugins: [
  // Generates an `index.html` file with the <script> injected.
  new HtmlWebpackPlugin({
    inject: true,
    template: paths.appHtml,
  }),
所以引用的paths.appIndexJs文件就是 webpack config 中的入口文件。
他们正在使用HtmlWebpackPlugin在 path 加载 html paths.appHtml。
最后一块拼图是将其链接回您发布的文件。发布相关内容来自paths.js
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
module.exports = {
  ...
  appHtml: resolveApp('public/index.html'),
  appIndexJs: resolveApp('src/index.js'),
  ...
}
所以在你的应用程序目录中,
appHtml 是文件public/index.html
appIndexJs 是文件src/index.js
你的两个文件有问题。
哇!那真是一段旅程..:P
更新 1 - 截至react-scripts@3.x
下面的react-scripts二进制文件node_modules/.bin更改了如下逻辑。基本上做同样的事情。
if (['build', 'eject', 'start', 'test'].includes(script)) {
  const result = spawn.sync(
    'node',
    nodeArgs
      .concat(require.resolve('../scripts/' + script))
      .concat(args.slice(scriptIndex + 1)),
    { stdio: 'inherit' }
  );
dev 和 prod 的 webpack 配置已经合二为一。
const configFactory = require('../config/webpack.config');
HTMLWebpackPlugin 配置看起来像这样 - 这是因为他们必须有条件地在此之上添加生产配置
plugins: [
  // Generates an `index.html` file with the <script> injected.
  new HtmlWebpackPlugin(
    Object.assign(
      {},
      {
        inject: true,
        template: paths.appHtml,
      },
路径文件代码有一些更新
module.exports = {
  ...
  appHtml: resolveApp('public/index.html'),
  appIndexJs: resolveModule(resolveApp, 'src/index'),
  ...
};