人。我一直坚持将TypeScript转换为JavaScript的小问题。
我有这段代码
import { someMethod } from 'someModule'
function *someGeneratorFunction(object : someObject) {
someMethod();
}
当我尝试将其转换为ES5时,我遇到了一个问题:生成器功能不受ES5标准支持。 import
正在转换为require
但是当我将此代码转换为ES6时,我遇到了import
的问题。 Bcoz'它不转换为require
。它就像import
一样。
但稳定的Node并不支持import
构建。在这种情况下我能做些什么?
我需要使用此方案:TypeScript-> ES6-> ES5(通过Babel)?还是有另一种方法?
答案 0 :(得分:3)
在这种情况下我能做些什么?
您可以告诉TypeScript您要使用哪个模块系统。来自documentation:
要编译,我们必须在命令行上指定模块目标。对于Node.js,请使用
--module commonjs
;对于require.js,请使用--module amd
tsc --module commonjs Test.ts
答案 1 :(得分:1)
通常我会使用此配置..
for typescript转换我的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项目..希望它可以帮助你