用于DropdownTreeviewModule

时间:2017-04-28 13:27:48

标签: angular webpack ecmascript-6 webpack-dev-server typescript2.1

我试图在我的测试应用程序中加入anular2 treeview模块,但是获取错误意外令牌

  

'我在bundle.js中从'.... // filepath .....'导出*。

我知道这是将typescript文件转换为js的问题,但我只是将此错误传递给这个特定的树视图模块。

我已经尝试了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"
  ]
}

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

让我知道,我在这里失踪了什么?感谢

2 个答案:

答案 0 :(得分:1)

我使用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"]
},

答案 1 :(得分:1)

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

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

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