【问题标题】:StencilJS error with React output target - You may need an additional loader to handle the result of these loadersReact 输出目标的 StencilJS 错误 - 您可能需要额外的加载器来处理这些加载器的结果
【发布时间】:2021-12-29 14:32:24
【问题描述】:

我正在 Stencil 中为自定义设计系统创建一些基本元素。我创建了一些基本组件,它们可以作为自定义元素单独工作,但在用作 React 组件时会抛出错误。

我通过 Stencil 通过在 stencil.config.ts 中包含 @stencil/react-output-target 生成了 React 组件。

reactOutputTarget({
      componentCorePackage: '@sr-design-system/simple-stencil-demo',
      proxiesFile: './react/src/components/index.ts',
      includeImportCustomElements: true
    }),

然后我将所有组件(自定义元素和 React)上传到一个私有 npm 包,并将它们安装在一个单独的项目中。自定义元素似乎工作正常,但使用 React 元素时出现以下错误。

ERROR in ./node_modules/@sr-design-system/simple-stencil-demo/react/src/index.ts 6:12
Module parse failed: Unexpected token (6:12)
File was processed with these loaders:
 * ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| import { createReactComponent } from './react-component-lib';
|
> import type { JSX } from '@sr-design-system/simple-stencil-demo/';
|
| import { defineCustomElement as defineSrText } from '@sr-design-system/simple-stencil-demo/dist/components/sr-text';
 @ ./src/App.jsx 7:0-73
 @ ./src/index.jsx 7:0-24 12:33-36

webpack 5.65.0 compiled with 1 error and 1 warning in 63 ms

我已经被这个问题困扰了好几天了。知道解决方案是什么吗?

===tsconfig.json===

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "allowUnreachableCode": false,
    "declaration": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["dom", "es2017"],
    "module": "es2015",
    "moduleResolution": "node",
    "pretty": true,
    "removeComments": false,
    "strictPropertyInitialization": false,
    "target": "es2017",
    "baseUrl": ".",
    "paths": {
      "@srds/react": ["./react"]
    },
    "jsx": "react",
    "jsxFactory": "h"
  },
  "include": ["src"]
}

===stencil.config.ts===

import { Config } from '@stencil/core';
import { reactOutputTarget } from '@stencil/react-output-target';

export const config: Config = {
  namespace: 'simple-stencil-demo',
  bundles: [{ components: ['sr-text'] }, { components: ['text-demo'] }],
  outputTargets: [
    reactOutputTarget({
      componentCorePackage: '@sr-design-system/simple-stencil-demo',
      proxiesFile: './react/src/components/index.ts',
      includeImportCustomElements: true,
    }),
    {
      type: 'dist',
      esmLoaderPath: './loader',
    },
    {
      type: 'dist-custom-elements',
    },
    {
      type: 'docs-readme',
    },
    {
      type: 'www',
      serviceWorker: null, // disable service workers
    },
  ],
  buildEs5: 'prod',
};

【问题讨论】:

  • 您找到问题了吗?
  • 还没有。我创建了一个新项目,将所有 Web 组件移到那里并从头开始执行该过程。我终于让它工作了。我将追溯我的步骤,将它们写下来并与 Stencil 文档进行比较,看看是否有任何差异/缺失的步骤。

标签: reactjs typescript webpack stenciljs


【解决方案1】:

我发现了问题所在。出于某种原因,我每次运行npm run build 时都没有为我生成dist 文件夹。

有时会生成,有时则不会。我相信这是由于我的组件代码中的一些错误,它默默地失败了。所以现在我每次构建库时都会检查 dist 文件夹。

在我最后的工作尝试中,我按照 Stencil 团队在其documentation 中的建议采用了 monorepo 方法。

以下是我为具有 React 输出的基本 Stencil 库所采取的所有步骤:

  • 创建一个单一仓库
  • 创建模板库
    • 使用npx stencil generate生成组件
    • 将 package.json 中的名称更新为 MY_LIBRARY
    • npm i @stencil/react-output-target
    • 将 React Wrapper 函数添加到 stencil.config.ts
    react({
       componentCorePackage: 'MY_LIBRARY',
       proxiesFile: '../MY_REACT_LIBRARY/src/components/stencil-generated/index.ts',
       includeDefineCustomElements: true,
     }),
    
  • 回到 monorepo 的根目录
  • 创建一个反应库
  • 在模板库中
    • 运行npm run build
    • 检查 dist 文件夹是否包含所有子文件夹(cjs、collection、components、esm、types、web-components、index.cjs.js、index.js)
    • 运行 npm link 以生成全局符号链接
  • 在反应库中
    • 运行npm link MY_LIBRARY
    • 运行npm i
    • 运行npm run build(不确定是否需要此步骤,因为它没有记录,但我还是这样做了)
    • 运行npm link
  • 回到 monorepo 的根目录
  • 创建一个 React 演示
    • npx create-react-app MY_REACT_DEMO --template typescript
    • npm link MY_REACT_LIBRARY
    • 从库中导入一个组件并在App.tsx中使用它
    • npm run start

当我确认一切正常后,我添加了一个基本的 lerna.jsonconfig 用于 npm 包管理。使用这个配置,Lerna 会自动为我们的包处理 symver。

{
  "version": "independent",
  "npmClient": "npm",
  "command": {
    "publish": {
      "allowBranch": ["master", "react-generation"],
      "ignoreChanges": ["*.md", "build.js", "config.json"],
      "message": "(auto) Lerna publish",
      "registry": "URL_TO_MY_PACKAGE_REGISTRY"
    },
    "bootstrap": {
      "ignore": "component-*",
      "npmClientArgs": ["--no-package-lock"]
    }
  },
  "packages": ["MY_LIBRARY", "MY_REACT_LIBRARY"]
}

配置 Lerna 后,我按照他们的发布向导使用命令 npx lerna publish 进行发布。

当它发布后,可以使用npm i MY_REACT_LIBRARY 将包安装到任何 React 项目中,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2020-09-12
    • 1970-01-01
    • 2022-11-06
    • 2020-12-04
    • 2021-08-05
    • 2022-11-10
    • 1970-01-01
    相关资源
    最近更新 更多