首页 > 解决方案 > 为什么我的 babel-polyfill 解析 node_modules 目录中的 ts 文件?

问题描述

我运行 a npm run dev,控制台打印下面的警告,这是我的基本 webpack 配置,我在条目中使用了“babel-polyfill”,并node_modules在我的 babel-loader 配置中排除了目录:

module.exports = {
    entry: {
        index: ['babel-polyfill', entry]
    },
    output: {
        path: dist
    },
    resolve: {
        extensions: ['.web.js', '.js', '.jsx', '.less', '.scss', '.css', '.json', '.png', '.jpg', '.jpeg'],
        alias: {
            '@': src,
            '@utils': path.resolve(src, 'utils'),
            '@common': path.resolve(src, 'common'),
        }
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                include: src,
                use: {
                    loader: 'babel-loader?cacheDirectory'
                }
            }, {
                test: /\.(eot|woff|svg|ttf|woff2|gif|appcache)(\?|$)/,
                exclude: /^node_modules$/,
                loader: 'url-loader?name=fonts/[name].[ext]',
                include: src
            }, {
                test: /\.(png|jpg)$/,
                // exclude: /^node_modules$/,
                loader: 'url-loader?limit=8192&name=images/[hash:8].[name].[ext]'

                // include: [SRC_PATH]
            }, { test: /\.(svg)$/i,
                loader: 'svg-sprite-loader',
                include: [
                    require.resolve('antd').replace(/warn\.js$/, '')
                // path.resolve(__dirname, 'images/'),  
                ]}
        ]
    },
    optimization: {
        runtimeChunk: { 
            name: 'runtime' 
        },
        splitChunks: {
            chunks: 'all',
            minSize: 30000, 
            maxSize: 200000, 
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 4,
            automaticNameDelimiter: '_',
            cacheGroups: {
                libs_antd: {
                    name: 'libs_antd',
                    test: path.resolve(root, 'node_modules', 'antd'),
                    chunks: 'async',
                    priority: -8,
                    reuseExistingChunk: true
                },
                verdors: {
                    name: 'verdors',
                    test: path.resolve(root, 'node_modules'),
                    chunks: 'initial',
                    priority: -10
                },
                node_commons: {
                    name: 'node_commons',
                    test: path.resolve(root, 'node_modules'),
                    chunks: 'async',
                    minChunks: 2,
                    priority: -10,
                    reuseExistingChunk: true
                }
            }
        },
        usedExports: true 
    },
    plugins
};

和警告信息:

WARNING in ./node_modules/_antd@3.23.4@antd/lib/locale/hu_HU.d.ts 1:8
Module parse failed: Unexpected token (1:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> declare const _default: {
|     locale: string;
|     Pagination: any;
 @ ./node_modules/antd/lib/locale lazy ^\.\/.*$ namespace object ./hu_HU.d.ts
 @ ./src/app/component/index.js
 @ ./src/app/model/container.js
 @ ./src/app/index.js
 @ ./src/router.js
 @ ./src/entry.js
 @ multi babel-polyfill ./src/entry

我认为不应该解析 node_modules 中的代码,我不知道为什么会出现警告?有任何想法吗?谢谢。

标签: javascriptreactjstypescriptwebpackbabeljs

解决方案


推荐阅读