`与webpack捆绑后,无法读取未定义的``枚举''

发布于 2025-01-20 01:57:36 字数 2703 浏览 1 评论 0原文

我有一个 React 库,我想使用 Webpack 来构建它。该库是使用 Typescript 编写的。似乎一切正常,但由于某种原因枚举却不起作用。

当我将该库安装到我的 React 应用程序中时,我发现在浏览器控制台中无法读取枚举的属性。

我尝试查看输出的包,似乎枚举正在被编译到包中。我觉得我一定是某个地方配置错误。在我能够与 microbundle-crl 捆绑之前,我们正在从它转向 Webpack。

这是我的 webpack.config.js 文件:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const DtsBundleWebpack = require('dts-bundle-webpack');

module.exports = {
    mode: 'production',
    entry: './src/index.tsx',
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist'),
        clean: true,
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.json', 'jsx', '.js'],
    },
    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.(ts|tsx)$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader, //Extract css into files
                    'css-loader', // Turns css into commonjs
                ],
            },
            {
                test: /\.pcss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader', // Turns css into commonjs
                    'postcss-loader'
                ]
            },
        ],
    },
    plugins: [
        new MiniCssExtractPlugin({ filename: 'randomcompany-components.css' }),
        new DtsBundleWebpack({
            name: '@randomcompany/components',
            main: './dist/src/index.d.ts',
            out: '../index.d.ts',
        }),
    ],
};

我的 tsconfig.json 如下:

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "types": ["react", "react-scripts"],
    "moduleResolution": "node",
    "jsx": "react",
    "sourceMap": true,
    "declaration": true,
    "declarationDir": "dist",
    "esModuleInterop": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "isolatedModules": true,
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true
  },
  "include": ["src", "src/lang"],
  "exclude": ["node_modules", "dist", "src/stories"]
}

我的枚举采用以下格式:

export enum Test {
    TestOne = "Test One",
    TestTwo = "Test Two",
    TestThree = "Test Three"
};

export default Test;

I have a React Library that I want to make buildable using Webpack. The library is written using Typescript. It seems everything is working, but for some reason enums aren't.

When I install the library into my React App, I find Cannot read properties of undefined in the browser console for the enum.

I've tried looking in the outputted bundle and it does appear that the enum is being compiled into the bundle. I feel like I must have a misconfiguration somewhere. Before I was able to bundle with microbundle-crl which we're moving from in favour of Webpack.

Here is my webpack.config.js file:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const DtsBundleWebpack = require('dts-bundle-webpack');

module.exports = {
    mode: 'production',
    entry: './src/index.tsx',
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist'),
        clean: true,
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.json', 'jsx', '.js'],
    },
    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.(ts|tsx)$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader, //Extract css into files
                    'css-loader', // Turns css into commonjs
                ],
            },
            {
                test: /\.pcss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader', // Turns css into commonjs
                    'postcss-loader'
                ]
            },
        ],
    },
    plugins: [
        new MiniCssExtractPlugin({ filename: 'randomcompany-components.css' }),
        new DtsBundleWebpack({
            name: '@randomcompany/components',
            main: './dist/src/index.d.ts',
            out: '../index.d.ts',
        }),
    ],
};

My tsconfig.json is as follows:

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "types": ["react", "react-scripts"],
    "moduleResolution": "node",
    "jsx": "react",
    "sourceMap": true,
    "declaration": true,
    "declarationDir": "dist",
    "esModuleInterop": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "isolatedModules": true,
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true
  },
  "include": ["src", "src/lang"],
  "exclude": ["node_modules", "dist", "src/stories"]
}

My enum is in this format:

export enum Test {
    TestOne = "Test One",
    TestTwo = "Test Two",
    TestThree = "Test Three"
};

export default Test;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋千易 2025-01-27 01:57:36

您应该像以下内容一样写入它:

const Test = {
  TestOne: "Test One",
  TestTwo: "Test Two",
  TestThree: "Test Three",
};

export default Test;

但是我认为,使用enum使您的捆绑文件有些沉重,只需建议即可。使用简单的JavaScript对象。

You should write it like below:

const Test = {
  TestOne: "Test One",
  TestTwo: "Test Two",
  TestThree: "Test Three",
};

export default Test;

But in my opinion, using enum makes your bundle file a little bit heavy, just advise. use simple JavaScript objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文