【问题标题】:Repository not found when building with webpack使用 webpack 构建时找不到存储库
【发布时间】:2019-04-08 01:27:13
【问题描述】:

我会尽量把这个问题说清楚。

我正在使用 NodeJS 开发后端,它使用 TypeORM 与 SQL Server 数据库进行交互。我使用 webpack4 建立了一个构建系统,在那里我将 TypeORM 实体构建为 commonjs 模块(我的入口点与数据库中的实体一样多)。我有两个单独的 webpack 配置,一个用于主文件,另一个用于 TypeORM 实体。

构建过程很顺利,没有警告没有错误,但是当我运行主文件 TypeORM 给我 RepositoryNotFoundError: No repository for "Foo" was found. looks like this entity is not registred in the default connection.

我在使用 Typescipt 编译器时没有这个问题。

非常感谢您的帮助。

webpack.config.js:

const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CleanBuildDir = require('clean-webpack-plugin');

function localExternals(context, request, callback) {
  if (/\.\//.test(request)) {
    return callback(null, `commonjs ${request}`);
  }
  return callback();
}

const entitesConfig = {
  target: 'node',
  mode: 'development',
  node: {
    __dirname: false,
    __filename: false,
  },
  entry: () => {
    const files = fs.readdirSync(path.resolve(__dirname, 'src/entities'));
    const entries = {};
    files.forEach(file => {
      entries[file.split('.')[0]] = path.resolve(__dirname, 'src/entities', file);
    });
    return entries;
  },
  output: {
    path: path.resolve(__dirname, 'dist/entities'),
    filename: '[name].js',
    libraryTarget: 'commonjs',
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  externals: [nodeExternals(), localExternals],
  module: {
    rules: [
      {
        test: /\.(t|j)s$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
    ],
  },
  plugins: [new CleanBuildDir()],
};

const mainConfig = {
  target: 'node',
  mode: 'development',
  node: {
    __dirname: false,
    __filename: false,
  },
  entry: path.resolve(__dirname, 'src/main.ts'),
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  externals: [nodeExternals(), localExternals],
  module: {
    rules: [
      {
        test: /\.(t|j)s$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
    ],
  },
  plugins: [new CleanBuildDir()],
};

module.exports = [entitesConfig, mainConfig];

ormconfig.js:

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

const entitiesFiles = fs
  .readdirSync(path.resolve(__dirname, 'dist/entities'))
  .map(file => path.resolve(__dirname, 'dist/entities', file));

module.exports = {
  name: 'default',
  type: 'mssql',
  host: '127.0.0.1',
  port: 1433,
  username: '*****',
  password: '*****',
  database: 'Foo',
  schema: 'dbo',
  synchronize: false,
  entities: entitiesFiles,
};

import { createConnection } from 'typeorm';
import { Foo } from './entities/Foo';
import { performance } from 'perf_hooks';

function selection(table: string, columns: string[]): string[] {
  return columns.map(col => `${table}.${col}`);
}

async function job(server: string) {
  const connection = await createConnection();
  const start = performance.now();
  const foos = await connection
    .getRepository(Foo)
    .createQueryBuilder('foo')
    .leftJoinAndSelect('affaire.bar', 'bar')
    .orderBy('affaire.Code_Affaire', 'DESC')
    .getMany();
  console.log(`Done in ${performance.now() - start} ms`);
  console.log(foos);
}

job('ANY').catch(err => console.log(`Error in job : ${err}`));

babel.config.js:

const presets = ['@babel/typescript', ['@babel/env', { useBuiltIns: 'usage', targets: { node: 'current' } }]];
const plugins = [
  ['@babel/proposal-decorators', { decoratorsBeforeExport: true }],
  '@babel/proposal-class-properties',
  '@babel/proposal-object-rest-spread',
];

module.exports = { presets, plugins };

【问题讨论】:

    标签: typescript babeljs webpack-4 typeorm


    【解决方案1】:

    我设法通过将@babel/proposal-decorators 的选项从decoratorsBeforeExport: true 更改为legacy: true 并将loose: true 添加到@babel/proposal-class-properties 插件来使其工作。

    新的 babel.config.js:

    const presets = ['@babel/typescript', ['@babel/env', { useBuiltIns: 'usage', targets: { node: 'current' } }]];
    const plugins = [
      ['@babel/proposal-decorators', { legacy: true }],
      ['@babel/proposal-class-properties', { loose: true }],
      '@babel/proposal-object-rest-spread',
    ];
    
    module.exports = { presets, plugins };

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2018-02-23
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      相关资源
      最近更新 更多