【问题标题】:TypeScript into ES6将 TypeScript 导入 ES6
【发布时间】:2017-01-07 20:29:21
【问题描述】:

伙计们。我在将 TypeScript 转换为 JavaScript 时遇到了一些小问题。

我有这个代码

import { someMethod } from 'someModule'

function *someGeneratorFunction(object : someObject) {
    someMethod();
}

当我尝试将其转换为 ES5 时,我遇到了一个问题:ES5 标准不支持生成器函数。而import 正在转换为require

但是当我将此代码转换为 ES6 时,import 出现问题。 Bcoz' 它不会转换为require。它仍然像import

但稳定节点不支持import 构造。在这种情况下我能做什么?

我需要使用这个方案:TypeScript->ES6->ES5(通过 Babel)吗?还是有别的方法?

【问题讨论】:

  • “ES5 标准不支持生成器函数。” 这到底是什么意思?如果您的意思是 ES5 中不存在生成器,那是正确的,但我想 typescript 将生成器转换为在 ES5 中工作的东西。
  • @FelixKling TypeScript 编译器按原样转换生成器。当我使用 --target ES5 标志进行编译时,我遇到了一个异常:生成器仅在针对 EcmaScript 2015 或更高版本时可用。

标签: typescript compilation ecmascript-6


【解决方案1】:

在这种情况下我能做什么?

你可以告诉 TypeScript 你想使用哪个模块系统。来自documentation

要编译,我们必须在命令行上指定一个模块目标。对于 Node.js,使用--module commonjs;对于require.js,使用--module amd

tsc --module commonjs Test.ts

【讨论】:

    【解决方案2】:

    我通常使用这种配置..

    用于转换我的 tsconfig.json 的打字稿:

    {
      "compilerOptions": {
        "outDir": "app_build/",
        "target": "es6", // <-- TARGETING ES6
        "module": "commonjs",
        "moduleResolution": "node",
        "lib": [
          "es5",
          "dom"
        ],
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
      },
      "compileOnSave": true,
      "exclude": [
        "node_modules",
        "app_build/js",
        "typings/main",
        "typings/main.d.ts"
      ]
    }
    

    我的 Webpack 开发文件(webpack.dev.js):

    var ExtractTextPlugin = require("extract-text-webpack-plugin");
    var webpack = require("webpack");
    var HtmlWebpackPlugin = require("html-webpack-plugin");
    var CleanWebpackPlugin = require('clean-webpack-plugin');
    var path = require('path');
    
    module.exports = {
        entry: {
            "polyfills": "./polyfills.ts",
            "vendor": "./vendor.ts",
            "app": "./app/main.ts",
    
        },
        resolve: {
            extensions: ['', '.ts', '.js', '.json', '.css', '.scss', '.html']
        },
        output: {
            path: "./app_build",
            filename: "js/[name]-[hash:8].bundle.js"
        },
        devtool: 'source-map',
        module: {
            loaders: [
                {
                    loader: "babel-loader",
    
                    // Skip any files outside of your project's `src` directory
    
                    exclude: [
                      path.resolve(__dirname, "node_modules")
                    ],
                    // Only run `.js` and `.jsx` files through Babel
                    test: /\.js/,
    
                    // Options to configure babel with
                    query: {
                        plugins: ['transform-runtime', 'babel-plugin-transform-async-to-generator'],
                        presets: ['es2015', 'stage-0'], //<-- BABEL TRANSPILE
                    }
                },
                {
                    test: /\.ts$/,
                    loader: "ts"
                },
                {
                    test: /\.html$/,
                    loader: "html"
                },
                //{
                //    test: /\.(png|jpg|gif|ico|woff|woff2|ttf|svg|eot)$/,
                //    loader: "file?name=assets/[name]-[hash:6].[ext]",
                //},
                {
                    test: /\.(png|jpg|gif|ico)$/,
                    //include:  path.resolve(__dirname, "assets/img"),
                    loader: 'file?name=/assets/img/[name]-[hash:6].[ext]'
                },
                {
                    test: /\.(woff|woff2|eot|ttf|svg)$/,
                  //  exclude: /node_modules/,
                    loader: 'file?name=/assets/fonts/[name].[ext]'
                },
                // Load css files which are required in vendor.ts
                {
                    test: /\.css$/,
                    loader: "style-loader!css-loader"
                },
                {
                    test: /\.scss$/,
                    loader: ExtractTextPlugin.extract('css!sass')
                },
            ]
        },
        plugins: [
            new ExtractTextPlugin("css/[name]-[hash:8].bundle.css", { allChunks: true }),
            new webpack.optimize.CommonsChunkPlugin({
                name: ["app", "vendor", "polyfills"]
            }),
            new CleanWebpackPlugin(
                [
                    "./app_build/js/",
                    "./app_build/css/",
                    "./app_build/assets/",
                    "./app_build/index.html"
                ]
            ),
            // inject in index.html
            new HtmlWebpackPlugin({
                template: "./index.html",
                inject: "body",
                //minifyJS: true,
                //minifyCSS: true,
            }),
            new webpack.ProvidePlugin({
                jQuery: 'jquery',
                $: 'jquery',
                jquery: 'jquery'
            })
        ],
        devServer: {
            //contentBase: path.resolve(__dirname, "app_build/"),
            historyApiFallback: true,
            stats: "minimal"
        }
    };
    

    我将它用于 Angular 2 项目.. 希望它对你有所帮助

    【讨论】:

    • 我从这个配置中看到你使用 Babel 将 ES6 转换为 ES5。可能这是唯一的选择。
    • 是的,我这样做是因为当我使用 typescript 时,我想使用所有 es6(一些 es7)功能,然后我使用 webpack 和 babel 来转换 ES5 中 typescript 的 js 输出。如果你想要你也可以尝试将 tsconfig.json 中的 "target": "es6", 更改为 --> "target": "es5" ..但是你失去了一些不错的功能..这只是我的想法 :-)
    • 是的,我写过它。当目标是 ES5 时,没有生成器支持。但是使用 ES6 目标它按原样编译 import 构造。所以我需要使用这个方案:TypeScript into ES6。 ES6 到 ES5(通过 Babel)
    猜你喜欢
    • 2018-11-17
    • 2015-08-13
    • 2017-03-11
    • 2018-03-08
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2020-08-09
    相关资源
    最近更新 更多