伙计们。我在将TypeScript转换为JavaScript时遇到了一些小问题。
我有这个密码
import { someMethod } from 'someModule'
function *someGeneratorFunction(object : someObject) {
someMethod();
}当我试图将它转换为ES5时,我遇到了一个问题:生成器函数不受ES5标准的支持。import正在转换为require
但是,当我将这些代码转换为ES6时,import出现了问题。因为它正在转换而不是require。它就像import一样。
但是稳定节点不支持import的构建。在这种情况下我能做什么?
我需要使用这个方案:打字稿->ES6->ES5(通过Babel)?还是有别的方法?
发布于 2017-01-07 20:48:47
在这种情况下我能做什么?
您可以告诉TypeScript您想使用哪个模块系统。来自文档
要编译,我们必须在命令行上指定一个模块目标。对于Node.js,使用
--module commonjs;对于require.js,使用--module amdtsc --模块通用Test.ts
发布于 2017-01-07 20:34:58
通常我使用这种配置。
对于类型记录,我的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"
}
};我用它来做一个角度2的项目。希望它对你有帮助
https://stackoverflow.com/questions/41526196
复制相似问题