栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

React JS index.js文件如何联系index.html以获取ID引用?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

React JS index.js文件如何联系index.html以获取ID引用?

Create-React-App
有一个非常有趣的设置。

我开始挖掘

package.json
npm脚本
start

“ start”:“反应脚本开始”

这将带我进入他们的二进制文件

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 pre:  paths.appIndexJs,],plugins: [  // Generates an `index.html` file with the <script> injected.  new HtmlWebpackPlugin({    inject: true,    template: paths.appHtml,  }),

因此,所引用的

paths.appIndexJs
文件是webpack配置中的入口文件。

他们正在使用HtmlWebpackPlugin在路径上加载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'),  ...};


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636512.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号