Webpack - 您如何要求捆绑包中的可选依赖项(saslprep) [英] Webpack - How do you require an optional dependency in bundle (saslprep)

查看:43
本文介绍了Webpack - 您如何要求捆绑包中的可选依赖项(saslprep)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在部署过程中使用 webpack 将多个后端脚本捆绑到一个文件中.

I am using webpack to bundle a number of back-end scripts into a single file during my deployment process.

当连接到 MongoDB 数据库时,有一个可选的依赖项,如果不包含它会抛出警告.

When connecting to the MongoDB database there is an optional dependency, which throws a warning if it is not included.

Warning: no saslprep library specified. Passwords will not be sanitized

在我的开发环境中,通过安装可选的依赖项很容易解决这个错误.

In my development environment this error is easily resolved by installing the optional dependency.

npm install saslprep --save

但是,当与 webpack 捆绑时,不包含可选依赖项,并且警告在生产部署中仍然存在.我可以很容易地找到原因,mongodb 库要求将此作为可选依赖项:

However when bundling with webpack the optional dependency is not included and the warning persists in the production deployment. I can trace the cause of this easily enough, the mongodb library is requiring this as an optional dependency:

let saslprep;
try {
  saslprep = require('saslprep');
} catch (e) {
  // don't do anything;
}

我尝试使用填充、外部、插件来遵循 webpack 文档,坦率地说,我对解决此问题的正确方法感到非常迷茫.这是我当前的 webpack.config 文件(试图将其作为插件).

I have tried following the webpack documentation using shimming, externals, plugins and frankly am quite lost as to the correct approach to resolve this issue. Here is my current webpack.config file (attempting to require this as a plugin).

const path = require('path');
const webpack = require('webpack');

module.exports = {
    entry: './src/api/index.ts',
    target: 'node',
    mode: 'production',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.tsx', '.ts', '.json']
    },
    output: {
    filename: 'api.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new webpack.IgnorePlugin(/fsevents/),
        new webpack.IgnorePlugin(/blessed/),
        new webpack.ProvidePlugin({
            saslprep: path.resolve(__dirname, "node_modules/saslprep/index.js")
        })
    ],
};

提前致谢.

推荐答案

感谢 Brendan 将我引向正确的方向.最终在这里找到了答案:http://www.matthiassommer.it/软件架构/webpack-node-modules/

Thanks to Brendan for steering me in the right direction. Ultimately the answer was found here: http://www.matthiassommer.it/software-architecture/webpack-node-modules/

关键信息是:

Webpack 的编译器将 require() 调用转换为它自己的 webpack_require().在运行时,它会查看其内部模块注册表.

Webpack’s compiler converts the require() call to its own webpack_require(). At runtime it looks into its internal module registry.

按照其中概述的步骤,解决方案变为:

Following the steps outlined therein the resolution becomes:

const path = require('path');
const webpack = require('webpack');

module.exports = {
    entry: './src/api/index.ts',
    target: 'node',
    mode: 'production',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.tsx', '.ts', '.json'],
    },
    output: {
        filename: 'api.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new webpack.IgnorePlugin(/fsevents/),
        new webpack.IgnorePlugin(/blessed/),
    ],
    externals: {
        "saslprep": "require('saslprep')"
    }
};

请注意,在我的测试中,导入外部对象时似乎需要saslprep"周围的引号.

这篇关于Webpack - 您如何要求捆绑包中的可选依赖项(saslprep)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆