TypeORM无法构建(Webpack +打字稿)

时间:2019-03-21 11:14:55

标签: typescript webpack typeorm

我正在尝试通过设置自定义webpack /打字稿将TypeORM添加到现有的Cordova / Ionic堆栈中。

当我尝试构建时,出现以下错误:  1.无法解析/ typeorm / browser-这与TypeORM请求的Webpack中的NormalModuleReplacementPlugin有关  2.从TypeORM导入d.ts文件时出现许多错误(我仅附加了两个作为参考):

ERROR in ./scripts/app/module.ts
Module not found: Error: Can't resolve './services/test-typeorm/browser' in 'C:\Users\gnesher\work\nortec\locusmap\LocusMap\scripts\app'
 @ ./scripts/app/module.ts 69:21-55
 @ ./scripts/app.ts
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./scripts/app.ts

ERROR in C:\Users\gnesher\work\nortec\locusmap\LocusMap\node_modules\typeorm\query-builder\QueryPartialEntity.d.ts
[tsl] ERROR in C:\Users\gnesher\work\nortec\locusmap\LocusMap\node_modules\typeorm\query-builder\QueryPartialEntity.d.ts(11,27)
      TS1005: ';' expected.

ERROR in C:\Users\gnesher\work\nortec\locusmap\LocusMap\node_modules\typeorm\query-builder\QueryPartialEntity.d.ts
[tsl] ERROR in C:\Users\gnesher\work\nortec\locusmap\LocusMap\node_modules\typeorm\query-builder\QueryPartialEntity.d.ts(11,47)
      TS1005: ';' expected.

我的Webpack配置看起来像这样:

const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const ModuleConcatPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
const webpack = require('webpack');

module.exports = {
  entry: "./scripts/app.ts",
  output: {
    path: path.resolve(__dirname),
    filename: "./www/scripts/appBundle.js",
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"]
  },
  devServer: {
    contentBase: './www'
  },
  optimization: {
    minimize: false
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: "/node_modules/",
        use: [
          {
            loader: "ts-loader",
            options: {
              configFile: "scripts/tsconfig.json"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './www/index.ejs'
    }),
    new ModuleConcatPlugin(),
    new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
      result.request = result.request.replace(/typeorm/, "typeorm/browser");
    })
  ],
  devtool: "source-map"
};

和我的tsconfig:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es5",
    "lib": [ "es5", "dom", "es6"]
  },
  "files": [
    // typings
    "typings/jquery/jquery.d.ts",
    "typings/angularjs/angular.d.ts",
    "typings/angular-ui-router/angular-ui-router.d.ts",
    "typings/angular-local-storage/angular-local-storage.d.ts",
    "typings/leaflet/geojson.d.ts",
    "typings/leaflet/leaflet.d.ts",
    "typings/moment/moment.d.ts",
    "typings/toastr/toastr.d.ts",
    "typings/cryptojs/cryptojs.d.ts",
    "typings/underscore/underscore.d.ts",
    "typings/ionic/ionic.d.ts",
    "typings/ngprogress/ngprogress.d.ts",
    "typings/d3/d3.d.ts",
    "typings/d3/nvd3.d.ts",
    "typings/bcryptjs/index.d.ts",

    // Cordova typings
    "typings/cordova/cordova.d.ts",
    "typings/cordova/plugins/Camera.d.ts",
    "typings/cordova/plugins/Device.d.ts",
    "typings/cordova/plugins/FileSystem.d.ts",
    "typings/cordova/plugins/FileTransfer.d.ts",
    "typings/cordova/plugins/imei.d.ts",
    "typings/cordova/plugins/Keyboard.d.ts",
    "typings/cordova/plugins/StatusBar.d.ts",
    "typings/cordova/plugins/NetworkInformation.d.ts",
    "typings/cordova/plugins/Media.d.ts",
    "typings/cordova/plugins/WebSQL.d.ts",
    "typings/cordova/ng-cordova/file.d.ts",
    "typings/cordova/plugins/ChromeSocketsTcp.d.ts",
    "typings/cordova/plugins/BleCentral.d.ts",
    "typings/cordova/plugins/PhotoLibrary.d.ts",
    "typings/cordova/plugins/BackgroundMode.d.ts",

    "app/controllers/charts-test.ts",
    "modules/project/modules/pressure-test/components/chart.ts",
    // Startup
    "app.ts"
  ]
}

我也将其报告为TypeORM Github存储库中的一个错误-但是积压了很多,而且它们不经常响应。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试使用以下示例webpack.config.ts生成构建:

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

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

module.exports = {
  entry: './app/server.ts',
  mode: 'development',
  output: {
    path: __dirname + '/dist',
    publicPath: '/',
    filename: 'server.js',
  },
  resolve: {
    // Add '.ts' and '.tsx' as a resolvable extension.
    extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js'],
  },

  module: {
    // loaders: [
    rules: [
      // All files with a '.ts' or '.tsx'
      // extension will be handled by 'ts-loader'
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
      },
    ],
  },
  target: 'node',
  externals: nodeModules,
};

谢谢。