webpack 如何缩小node.js typescript后端项目?

igsr9ssn  于 11个月前  发布在  Webpack
关注(0)|答案(1)|浏览(101)

我的webpack.config.js:

const path = require('path');
var fs = require('fs')

var nodeModules = {};
fs.readdirSync('node_modules').filter(function (x) {return ['.bin'].indexOf(x) === -1;}).forEach(function (mod){nodeModules[mod]= 'commonjs ' + mod;
  });

module.exports = {
  mode: 'development',
  target: 'node',
  entry: './src/server,ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.node$/,
        loader: 'node-loader',
      },
    ],
  },
  externals: [nodeModules],
  devtool: 'source-map',

};

字符串
我的webpack.js.js文件有问题。当我运行npm run build时,它被配置为执行webpack,我遇到了与模块相关的错误。其中一个错误是:

ERROR in ./node_modules/sequelize/lib/dialects/postgres/hstore.js 2:15-35
Module not found: Error: Can't resolve 'pg-hstore' in 'C:\..\node_modules\sequelize\lib\dialects\postgres'
resolve 'pg-hstore' in 'C:\..\node_modules\sequelize\lib\dialects\postgres'
  Parsed request is a module

mutmk8jj

mutmk8jj1#

我通过编辑webpack.config.js解决了这个问题

var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var nodeExternals = require('webpack-node-externals');

module.exports = {
  mode: 'development',
  target: 'node',
  entry: './src/server.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.json'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@config': path.resolve(__dirname, 'src/config'),
      '@controllers': path.resolve(__dirname, 'src/controllers'),
      '@dtos': path.resolve(__dirname, 'src/dtos'),
      '@exceptions': path.resolve(__dirname, 'src/exceptions'),
      '@interfaces': path.resolve(__dirname, 'src/interfaces'),
      '@middlewares': path.resolve(__dirname, 'src/middlewares'),
      '@models': path.resolve(__dirname, 'src/models'),
      '@routes': path.resolve(__dirname, 'src/routes'),
      '@services': path.resolve(__dirname, 'src/services'),
      '@utils': path.resolve(__dirname, 'src/utils'),
      '@sentry/profiling-node': path.resolve(__dirname, 'node_modules/@sentry/profiling-node'),
    },
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.node$/,
        loader: 'node-loader',
      },
    ],
  },
  externals: [
    nodeExternals(),
  ],
  plugins: [new webpack.ContextReplacementPlugin(/Sequelize(\\|\/)/, path.resolve(__dirname, '../src'))],
  devtool: 'source-map',
};

字符串

相关问题