在 DropdownTreeviewModule 的 webpack ts-loader 之后出现意外的标记“export”

Unexpected token `export` after webpack ts-loader for DropdownTreeviewModule

我尝试将 anular2 treeview 模块包含到我的测试应用程序中,但出现错误意外标记

'export * from '....//filepath.....' in my bundle.js

我知道这是将 typescript 文件转译为 js 的问题,但我只是将这个错误引入了这个特定的 treeview 模块。

我已经尝试了 ts-loaderbabel,但都没有用。

下面是我的webpack配置。

var WriteFilePlugin = require('write-file-webpack-plugin');
var path = require("path");

var APP_DIR = path.resolve(__dirname, "app");
var config = {
    entry: path.resolve(APP_DIR + "/main.ts"),
    output: {
        path: APP_DIR,
        publicPath: "/app/",
        filename: "bundle.js"
    },
    watch: true,
    resolve: {
        extensions: ['', '.webpack.js', ".web.js", ".ts", ".js", ".jsx"]
    },
    module: {
        loaders: [
            {
                test: /\.tsx?$/,
                loader:"babel!ts-loader",
                //loader:"ts-loader", // tried this also
                exclude: "node_modules"
            }
        ]
    }
}
module.exports = config;

和 tsconfig:

{
  "compilerOptions": {
    "target": "es6", //(also tried with "target:es5")
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "lib": [
      "es6",
      "dom"
    ]
  },
  "typeRoots": [
    "node_modules/@types"
  ],
  "types": [
    "core-js",
    "hammerjs",
    "lodash"
  ],
  "exclude": [
    "node_modules"
  ]
}

我所有其他导入和创建的模块都在工作,除了这个。

让我知道,我在这里缺少什么?谢谢

我在使用 ng2-translate 时遇到了同样的麻烦。

在我的 webpack 配置文件中,我将模块中的 js 文件作为目标,并使用 babel-loader 加载这些文件,应用 transform-es2015-modules-commonjs 插件。您需要更改 test 部分以匹配树视图模块路径:

{
  test: /ng2-translate(?:\/|\).*\.js$/,
  loader: "babel-loader",
  query: {
    "plugins": ["transform-es2015-modules-commonjs"]
  }
}

请注意,只有 js 个文件是目标文件,因为您并没有真正导入 ts 个文件,只是使用 ts 编译器的定义。

另请注意,我也进行了此更改,但我不记得具体原因,我猜它可能破坏了源映射:

{
  // basically this enable source-mapping for everything except this module
  test: /(?:(?!ng2-translate(?:\/|\).*))\.js$/,
  loaders: ["source-map-loader"]
},

我尝试了几个 es6 加载器来缓解转译问题,但实际上问题出在导出的库本身。所以下面 1 行解决了我的问题。

import {DropdownTreeviewModule} from 'ng2-dropdown-treeview/bundles/ng2-dropdown-treeview.umd';

而不是从 ng2-dropdown-treeview 导入模块,我必须从捆绑文件中导入它。