我正在electron.js
使用React
。我正在使用JSX,因此需要使用它Babel
进行转换。许多教程都建议使用Webpack。
目前,我正在使用Webpack 4
。这是我的webpack.config.js
const path = require('path')
module.exports = {
entry: "./src/renderer.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "renderer.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
}
}
和我的 .babelrc
{
"presets": ["es2015", "stage-0", "react"]
}
我需要从这里开始,renderer.js
因为它包含了我的大部分代码和React
组件,结果是一个捆绑的js文件。
但是我要做的只是将我的所有jsx
文件转换为普通js
文件,就像将所有JSX文件src
根据文件dist
夹中的JS文件转换到其中一样,如果可用,在编辑文件时监视并转换。如何实现呢?
您可以简单地通过命令行运行babel:
babel --plugins transform-react-jsx-source script.js
文件:https://babeljs.io/docs/plugins/transform-react-jsx-source/
请注意页面上的三个“用法”。所有这些都将为您提供所需的东西,而它们都不使用
webpack
。该.babelrc
文件可以处理您所有的插件/转换,是推荐的用法。那你就跑babel
这是Material-UI的package.json中的示例:
"build:es2015": "cross-env NODE_ENV=production babel ./src --ignore *.spec.js --out-dir ./build",
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句