typescript - 使用 webpack 构建时找不到存储库
问题描述
我会尽量把这个问题说清楚。
我正在使用 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 };
解决方案
@babel/proposal-decorators
我设法通过更改from decoratorsBeforeExport: true
to的选项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 };
推荐阅读
- javascript - 在选择时验证 Wordpress 媒体附件字段
- javascript - 如何解决此错误 TypeError: _components_firebase_Firebase__WEBPACK_IMPORTED_MODULE_2__.default.auth is not a function
- sharepoint-online - Sharepoint Online REST 奇怪错误(400)-“值不能为空。参数名称:键”
- python - 给定两列相同的熊猫,在行中找到相似的元素以创建新列
- ios - 英语字符串集与支持 UIStoryboard 的本地化相结合
- python - 根据 Pandas 行中的列名创建变量
- vuejs2 - 数组更改不会重新渲染模板
- excel - 从命名范围偏移创建范围
- arrays - 如何让 Arrayformula 在自动填充时查找 Arrayformula?
- if-statement - 如何根据条件选择应用哪个装饰器?