简体   繁体   English

如果我不需要 SSR,如何编译 JS 文件以在 WordPress 主题中使用 React 组件?

[英]How to compile the JS file to use a React component in a WordPress theme if I do not need SSR?

I have a WordPress theme and for a part of it I want to use a React component (I don't need SSR).我有一个 WordPress 主题,其中一部分我想使用 React 组件(我不需要 SSR)。 I have used create-react-app in the past but now I have this code:我过去曾使用过 create-react-app,但现在我有了以下代码:

<div id="react-nav-bar"></div>

<script src="https://unpkg.com/react@17/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js" crossorigin></script>

<script src="<?php echo get_template_directory_uri() ?>/react-nav-bar/build/static/js/runtime-main.39639aca.js"></script>

and it does not work.它不起作用。 The request to runtime-main.39639aca.js succeeds but it does not load my nav bar.runtime-main.39639aca.js的请求成功,但它没有加载我的导航栏。

The issue is that create-react-app produces more than a JS file, and I need just a JS file that I can put in the theme.问题是 create-react-app 生成的不仅仅是一个 JS 文件,我只需要一个可以放入主题中的 JS 文件。

I have tried some things with webpack without success.我用 webpack 尝试了一些事情但没有成功。 I searched on GitHub and there are some things, I do not know which one to try.我在 GitHub 上搜索了一些东西,我不知道该尝试哪一个。 I tried react-embedded and it threw me an error.我尝试了react-embedded ,但它给了我一个错误。 I also tried this but it does not work for me.我也试过这个,但它对我不起作用。

I also dislike the ES Lint warnings (I use VS Code).我也不喜欢 ES Lint 警告(我使用 VS Code)。

Update 1更新 1

This command does what I want, partially:此命令部分执行我想要的操作:

npx babel --watch react-nav-bar/src --out-dir react-nav-bar/dist --presets react-app/prod

This is how I've tried without WebPack, but the editor in VS Code is filled with red squiggles although it compiles well, and I cannot import npm modules.这就是我在没有 WebPack 的情况下尝试过的方法,但是 VS Code 中的编辑器虽然编译良好,但充满了红色曲线,并且我无法导入 npm 模块。 Is there a way to be able to import npm modules?有没有办法可以导入 npm 模块? Autocompletion makes me a lot more productive.自动补全让我更有效率。

I gave Rollup a try and it works beautifully.我试了一下Rollup ,效果很好。

I get some warnings in the DevTools console but it works.我在 DevTools 控制台中收到一些警告,但它可以工作。 With autocompletion and npm module includes.具有自动完成功能和 npm 模块包括。

Based on this gist I have these files:基于这个要点,我有这些文件:

src/index.tsx src/index.tsx

import * as React from "react";
import { render } from 'react-dom';

const App = () => {
  return <button onClick={() => alert('React from WordPress !')}>Cool!!!</button>;
};

window.addEventListener('DOMContentLoaded', () => {
  render(<App />, document.getElementById('react-nav-bar'));
});

package.json package.json

Just two additional scripts:只有两个额外的脚本:

"build": "rollup -c",
"watch": "rollup -cw"

The unusual dependencies when compared with the gist linked above:与上面链接的要点相比,不寻常的依赖关系:

"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"rollup-plugin-typescript": "^1.0.1",

rollup.config.js rollup.config.js

import typescript from 'rollup-plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
    input: './src/index.tsx',
    output: {
        file: './dist/bundle.js',
    },
    format: 'iife',
    plugins: [ resolve(), typescript(), commonjs() ],
};

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "module": "es6",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "outDir": "./dist",
    "preserveConstEnums": true,
    "target": "es5",
    "allowSyntheticDefaultImports": true
  },
  "exclude": [
    "node_modules"
  ]
}

For React to work with this I have the special line: "allowSyntheticDefaultImports": true .为了让 React 使用它,我有特殊的行: "allowSyntheticDefaultImports": true

../footer.php ../footer.php

<div id="react-nav-bar"></div>

<script>try { process.env.NODE_ENV } catch(e) { var process = { env: { NODE_ENV: 'production' } }; }</script>
<script src="<?php echo get_template_directory_uri() ?>/react-nav-bar/dist/bundle.js"></script>

This helped a lot: https://github.com/hybridsjs/hybrids/issues/27#issuecomment-452346986这有很大帮助: https://github.com/hybridsjs/hybrids/issues/27#issuecomment-452346986

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

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