简体   繁体   English

将Node.js Web API与Webpack捆绑在一起

[英]Bundling node.js web API with webpack

I'm implementing a web API based on node.js. 我正在实现基于node.js的Web API。 API can interact with a database. API可以与数据库进行交互。 So I use node-postgres library for a data access layer. 因此,我将node-postgres库用于数据访问层。 Now I need to configure webpack in the right manner in order to bundle api in a sigle file. 现在,我需要以正确的方式配置webpack以便将api捆绑到单个文件中。 However I can't do that because of node-postgres dependency "pg-native". 但是由于node-postgres依赖“ pg-native”,我不能这样做。 I can only build api with this code in webpack.config.js: 我只能在webpack.config.js中使用此代码构建api:

externals: {
        'pg': 'commonjs pg'
}

But this solution force me to keep node_modules folder when I going to deploy the API. 但是,这种解决方案迫使我在部署API时保留node_modules文件夹。

Here is my webpack.config.js: 这是我的webpack.config.js:

var path = require('path');
var nodeNativeModules = {};

module.exports = function(environment) {
var entryCfg = '';
switch(environment){
    case 'development':
    entryCfg = { 'main_ts': './src/api/main.ts'};
    break;
}

var CONFIG = {
    entry: entryCfg,
    target: 'node',
    output: {
        path: path.join(__dirname, 'dist/'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.ts', '.js', '.json'],
    },
    externals: {
        'pg': 'commonjs pg'
    },
    module: {
        loaders: [{
            test: /\.json$/,
            loader: 'json-loader'
        }, {
            test: /\.ts$/,
            loaders: [
                'awesome-typescript-loader',
            ],
            exclude: [/\.(spec|e2e)\.ts$/]
        },]
    },
    devtool: 'source-map'
}
return CONFIG;
}

Is there a possible way to bundle node-postgres? 有没有可能捆绑node-postgres的方法? How to configure webpack to bundle native module dependencies? 如何配置webpack捆绑本机模块依赖项?

Since I don't use 'pg-native' my issue becomes a webpack configuration issue. 由于我不使用“ pg-native”,因此我的问题就变成了webpack配置问题。 Here is the answer that helps me https://github.com/serverless-heaven/serverless-webpack/issues/78 这是对我有帮助的答案https://github.com/serverless-heaven/serverless-webpack/issues/78

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM