【发布时间】: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