在使用我发布的包时出现错误“TextEncoder 不是构造函数”

问题描述 投票:0回答:1

我用 ReactJs、TypeScript 和 rollup 编写了一个包。

这是我的 rollup.config

import resolve  from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import external from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer'
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

export default {
    input: 'src/index.ts',
    output: [
        {
            file: 'dist/index.js',
            format: 'esm',
            sourcemap: true,
        },
    ],
    plugins: [
        external(),
        resolve(),
        typescript({ tsconfig: './tsconfig.json' }),
        postcss({
            extensions: ['.scss'],
            extract: false,
            modules: {
                generateScopedName: '[name]__[local]___[hash:base64:5]',
            },
            use: [
                ['sass', {
                    includePaths: ['node_modules', 'src'],
                }],
            ],
            plugins: [
                autoprefixer(),
            ],
        }),
        terser(),
        commonjs({
            include: 'node_modules/**',
        }),
        json(),
    ],
}

这也是我的该包的 package.json

{...
  "devDependencies": {
    "@apollo/client": "^3.10.8",
    "@rollup/plugin-commonjs": "^26.0.1",
    "@rollup/plugin-node-resolve": "^15.2.3",
    "@rollup/plugin-json": "^6.1.0",
    "@rollup/plugin-replace": "^5.0.7",
    "@rollup/plugin-terser": "^0.4.4",
    "@rollup/plugin-typescript": "^11.1.6",
    "@types/node": "^20.14.11",
    "@types/react": "^18.3.3",
    "autoprefixer": "^10.4.19",
    "axios": "^1.7.2",
    "graphql": "^16.9.0",
    "graphql-request": "^7.1.0",
    "json-to-graphql-query": "^2.2.5",
    "postcss": "^8.4.39",
    "postcss-modules": "^6.0.0",
    "react-cookie": "^7.2.0",
    "rollup": "^4.18.1",
    "rollup-plugin-dts": "^6.1.1",
    "rollup-plugin-peer-deps-external": "^2.2.4",
    "rollup-plugin-postcss": "^4.0.2",
    "rollup-plugin-sass": "^1.13.1",
    "sass": "^1.77.6",
    "tslib": "^2.6.3",
    "typescript": "^5.5.3"
  },
  "dependencies": {
    "react": ">=18.0.0",
    "react-dom": ">=18.0.0"
  },
  "peerDependencies": {
    "react": ">=18.0.0",
    "react-dom": ">=18.0.0"
  }
}

当我在另一个具有 webpack.config 和 typescript 的 React 应用程序中安装包时,运行它时出现错误

未处理的运行时错误 类型错误:util__WEBPACK_IMPORTED_MODULE_2__.TextEncoder 不是构造函数

但我不在我的代码中使用“TextEncoder”,它是在我的构建文件中生成的。但我无法找出问题并解决它。请注意,我在构建时没有任何错误,并且我成功发布了我的包。我也尝试过快速文本编码,但没有帮助,所以我删除了

reactjs typescript rollup
1个回答
0
投票

你找到解决办法了吗? 我也遇到同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.