简体   繁体   English

了解 react-scripts 如何开始工作

[英]Understanding how react-scripts start work

So i was moving my code to esbuild to fasten my builds.所以我将我的代码移动到 esbuild 以固定我的构建。

When I do so, my website looks weird (when I run using esbuild).当我这样做时,我的网站看起来很奇怪(当我使用 esbuild 运行时)。

I was comparing the diff between index.html of the webpack and esbuild and noticed that webpack one have this我正在比较 webpack 和 esbuild 的 index.html 和 esbuild 之间的差异,并注意到 webpack 有这个

  <script src="/static/js/bundle.js"></script><script src="/static/js/vendors~main.chunk.js"></script><script src="/static/js/main.chunk.js"></script><script src="/static/js/bundle.js"></script><script src="/static/js/vendors~main.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>

Which is missing in esbuild. esbuild 中缺少哪个。

This is my esbuild这是我的 esbuild

// build.js
const esbuild = require('esbuild');
const inlineImage = require('esbuild-plugin-inline-image');
const { sassPlugin } = require('esbuild-sass-plugin');
const svgrPlugin = require('esbuild-plugin-svgr');

esbuild
  .serve(
    {
      servedir: 'public',
      port: 3000,
    },
    {
      entryPoints: ['./src/index.tsx'],
      outfile: './public/assets/app.js',
      minify: true,
      bundle: true,
      define: {
        'process.env.NODE_ENV': '"dev"',
        'process.env.REACT_APP_BACKEND_URL': '"https://something.xyz.ai/"',
        'process.env.REACT_APP_BACKEND_WSS': '"wss://something.xyz.ai/ws/"',
        'process.env.REACT_APP_BACKEND_URL_STAGE': '"https://stage-new.something.xyz.ai/"',
        'process.env.REACT_APP_HELP_AND_SUPPORT_URL': '"https://docs.something.xyz.ai/"',
      },
      loader: {
        '.js': 'jsx',
        '.ts': 'tsx',
      },
      plugins: [svgrPlugin({ exportType: 'named' }), inlineImage(), sassPlugin()],
    },
  )
  .catch(() => process.exit(1));

I am curious, how does react put /static/js/bundle.js and other scripts and how can I make esbuild do the same?我很好奇,如何 react put /static/js/bundle.js和其他脚本,我怎样才能让 esbuild 做同样的事情?

The scripts present in webpack output are due to code splitting . webpack output 中出现的脚本是由于代码拆分

You can enable code splitting in esbuild by adding splitting: true to the config esbuild.serve(..., { splitting: true, ... }) or --splitting in case of command line.您可以在 esbuild 中启用代码拆分,方法是在配置esbuild.serve(..., { splitting: true, ... }) splitting: true添加 split: true 或在命令行中--splitting

One caveat though;不过有一个警告; currently esbuild does not support code splitting fully:目前 esbuild 不完全支持代码拆分:

Code splitting is still a work in progress.代码拆分仍在进行中。 It currently only works with the esm output format.它目前仅适用于 esm output 格式。 There is also a known ordering issue with import statements across code splitting chunks.跨代码拆分块的导入语句还有一个已知的排序问题。 You can follow the tracking issue for updates about this feature.您可以关注跟踪问题以获取有关此功能的更新。


When I do so, my website looks weird (when I run using esbuild).当我这样做时,我的网站看起来很奇怪(当我使用 esbuild 运行时)。

Code splitting is a performance optimisation and as such rather unlikely cause of issues with altered functioning of the app.代码拆分是一种性能优化,因此不太可能导致应用程序功能改变的问题。 More likely cause is that some assets are not loaded properly.更可能的原因是某些资产未正确加载。 Depending on the complexity of previous webpack setup switching to esbuild may require performing additional steps to match webpack setup.根据之前 webpack 设置的复杂性,切换到 esbuild 可能需要执行额外的步骤来匹配 webpack 设置。 The more non-standard setup the harder it gets.非标准设置越多,就越难。

Specifically so-called loaders ( esbuild docs | webpack docs ) may work differently for the same types of files.特别是所谓的加载程序( esbuild docs | webpack docs )对于相同类型的文件可能会以不同的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM