【问题标题】:Unexpected token `export` after webpack ts-loader for DropdownTreeviewModule用于 DropdownTreeviewModule 的 webpack ts-loader 后出现意外的令牌“导出”
【发布时间】:2017-09-26 15:24:57
【问题描述】:

我尝试将 anular2 treeview 模块包含到我的测试应用程序中,但收到错误意外令牌

'export * from '....//filepath.....' 在我的 bundle.js 中

我知道这是将 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"
  ]
}

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

告诉我,我在这里缺少什么?谢谢

【问题讨论】:

  • 通常当你导入一个你不应该的模块时会发生这种情况,当导入一些随机私有角度模块而不是从桶文件导入时我已经经历过这种情况(例如import {Something} from "angular/core/some/stupid/things"而不是import {Something} from "angular/core")。
  • @n00dl3:我已经按照确切的步骤,并尝试从主文件加载模块,但它似乎是节点模块本身的问题。所有js都在es6中。
  • Ow... 我也遇到过这种情况-使用 ng2-translate-,这是我在 webpack 配置中作为解决方法所做的:{ test: /ng2-translate(?:\/|\\).*\.js$/, loader: babel-loader", query: { "plugins": ["transform-es2015-modules-commonjs"] } } 请注意,我专门针对模块的 js 文件(他们不是ts,他们是js)。

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回答和时间(+upvote),您在您的上下文中是对的,但在我的情况下,问题在于导出的库本身,没有加载器可以纠正它。 [我已经尝试过你的解决方案:-)],所以我已经发布了我的解决方案作为我的答案。
    • 很高兴您找到了解决方案。 ;)
    猜你喜欢
    • 2021-06-22
    • 2017-03-05
    • 2016-11-11
    • 1970-01-01
    • 2017-12-10
    • 2016-08-01
    • 2018-11-24
    • 2021-05-24
    • 2018-03-04
    相关资源
    最近更新 更多