javascript - 更改/添加 index.js 的 create-react-app 入口点或位置

标签 javascript reactjs webpack plugins config

我正在尝试使用 create-react-app 创建一个简单的网络应用程序。我已将页面和组件移动到标记为 pages 的 src 下的单独目录中和 components .我想将 index.js 的入口点从 src/index.js 更改/添加到 src/pages/index.js。另一种选择是将 index.js 保留在 src/并从页面和组件目录导入。有人可以帮我找到如何更改入口点路径吗?

我似乎找不到默认 html-webpack-plugin 配置文件的位置。

这是我希望的结构:

(in src)
...
pages/
   - App.js
   - Home.js

components/
   - Filter.js

serviceWorker.js

最佳答案

我使用 react-rewired 来实现这样的目标:
https://github.com/timarney/react-app-rewired/issues/189
在我的 config-overrides.js

console.log("@@@@@@@@@@@@@@@@@@@@@@@");
console.log("@ using react-rewired @");
console.log("@@@@@@@@@@@@@@@@@@@@@@@");

module.exports = {
  // The Webpack config to use when compiling your react app for development or production.
  webpack: function(config, env) {
    // ...add your webpack config
    console.log("@@@@@@@@@@@");
    console.log("@ webpack @");
    console.log("@@@@@@@@@@@");
    console.log({config, env});

    if (env === 'development') {
      config.entry = config.entry.replace(new RegExp("index.ts$"), 'runApp.tsx');
      console.log("entry point changed:", config.entry);
    }

    return config;
  },
}
我需要在不同的文件上运行,但仅在运行开发服务器时
上面的链接有完整的解决方案

关于javascript - 更改/添加 index.js 的 create-react-app 入口点或位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55494682/

相关文章:

javascript - 如何减少 GridStack 网格元素的最小高度和重量?

javascript - 在类 React ES6 中调用静态函数

Webpack -p 慢速构建 "[BABEL]"注意 : The code generator has deoptimised the styling of "file.jsx" as it exceeds the max of "500KB"

javascript - 将 webpack 与 jQuery 结合使用时出错

javascript - $.get json数组复制

javascript - 刷新时有时无法加载页面

android - 单词上的点击事件 react native

angular - 无法在 Angular 构造函数或 OnInit 中放置断点

javascript - Cypress - 如何正确等待导入的 JS 函数的结果

javascript - 无法更改 Material-UI OutlinedInput 的边框颜色