[英]How to transpile TypeScript to ES6 js for node server, then bundle es6.js for client?
Before migrating to TypeScript, I used to write isomorphic ReactJS code in ES6 and have webpack to handle the bundling for client side using webpack --watch
. 在迁移到TypeScript之前,我曾经在ES6中编写同构的ReactJS代码,并使用webpack --watch
使用webpack --watch
来处理客户端的捆绑。 But now with TypeScript in the mix, I'm looking for ways to transpile TypeScript code to ES6 first and then trigger webpack to bundle on file change. 但是现在有了TypeScript,我正在寻找方法,首先将TypeScript代码转换为ES6,然后触发webpack捆绑文件更改。
I use PhpStorm, and have tried this method with no success: 我使用了PhpStorm,并尝试了这种方法,但没有成功:
First I set up a TypeScript file watcher from PhpStorm to transpile ts scripts and save them in ./type_src/
, 首先,我从PhpStorm设置了TypeScript文件监视程序,以翻译ts脚本并将其保存在./type_src/
,
tsconfig.js tsconfig.js
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"moduleResolution": "node",
"removeComments": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"sourceMap": true,
"outDir": "./type_src/",
"jsx": "react",
"experimentalDecorators": true,
"noLib": false,
"declaration": false
},
"exclude": [
"node_modules",
"type_src"
]
}
then use webpack --watch
on /type_src
folders: 然后在/type_src
文件夹上使用webpack --watch
:
webpack.config: webpack.config:
let BUILD_DIR = path.resolve(__dirname, '../../site/js/build');
let APP_DIR = path.resolve(__dirname, 'type_src');
let common = {
output: {path: BUILD_DIR},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
include: APP_DIR,
query: {
presets: ['es2015', 'react']
}
}
]
},
resolve: {
alias: {
'react': path.join(__dirname, 'node_modules', 'react'),
'react-dom': path.join(__dirname, 'node_modules', 'react-dom')
},
extensions: ['','.js','jsx','.ts','.tsx'],
},
exclude: [
"node_modules",
"typings/index.d.ts"
]
};
The TypeScript file watcher is working, but I can't figure out why the webpack fails to trigger even though files in type_src
have changed by the watcher. TypeScript文件监视程序正在运行,但是我无法弄清为什么即使type_src
文件已被type_src
更改,但webpack仍无法触发。 Does anyone have suggestions for my situation? 有人对我的情况有建议吗?
If I were you, I would try to use only one tool. 如果我是你,我将尝试仅使用一种工具。
For example, if you prefer webpack, you can use ts-loader for processing typescript files. 例如,如果您更喜欢Webpack,则可以使用ts-loader处理打字稿文件。
Or, if you prefer typescript, you can generate single bundle file using outFile
in tsconfig.js
或者,如果您喜欢打字稿,则可以使用outFile
中的tsconfig.js
生成单个捆绑文件
{
"compilerOptions": {
....
"outFile": "bundle.js"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.