在maven项目中,使用traceur或Babel(以前称为6to5)将ECMAScript6代码转译为ECMAScript5,是否已有首选方法?我已经在网络上搜索过了,但没有找到任何成功的方法。
在maven项目中,使用traceur或Babel(以前称为6to5)将ECMAScript6代码转译为ECMAScript5,是否已有首选方法?我已经在网络上搜索过了,但没有找到任何成功的方法。
该插件还允许您在测试阶段运行Karma和JSHint。
希望这有所帮助,
M
编辑:值得注意的是,插件进行的节点安装是局部项目的。因此无需全局安装节点。
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<nodeVersion>v4.6.0</nodeVersion>
</configuration>
<executions>
<execution>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
在成功完成Grunt转译步骤后,我遇到了将这些文件正确地打包到WAR文件中的问题。我尝试了将文件复制到目标爆炸式war文件夹的各种构建阶段和各种类似的方法,但都没有奏效。
解决方案最终是通过在maven-war-plugin部分的webResources部分包含转译后的文件来覆盖非转译的js文件。最终看起来像这样:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warName>Example-1.0-SNAPSHOT</warName>
<webResources>
<resource>
<directory>${project.build.directory}/dist</directory>
</resource>
</webResources>
</configuration>
这篇文章已经好几年没有更新了,提供的解决方案在干净安装后无法使用。经过一些尝试,我得出了以下解决方案:
如上所述,我使用了frontend-maven-plugin来安装Node、NPM、Grunt和Babel。
Node和NPM下载/安装Grunt和Babel。 Grunt调用Babel Babel进行转译。
Maven
为了启动这个过程,在pom.xml的构建部分添加以下内容。
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>initialize</phase>
</execution>
<execution>
<id>npm</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
<phase>initialize</phase>
</execution>
<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
<configuration>
<nodeVersion>v10.4.1</nodeVersion>
<npmVersion>6.1.0</npmVersion>
</configuration>
</plugin>
<!--- rest of your plugins follow -->
NPM
在项目的同级目录下创建一个 package.json 文件,告诉 Node/NPM 它需要安装哪些依赖到 node_modules 目录中。
{
"name": "npm-maven-base-project",
"version": "0.0.1",
"description": "Install Grunt and Babel to do some ES6 to ES5 transpiling",
"devDependencies": {
"@babel/core": "^7.0.0-beta.54",
"@babel/preset-env": "^7.0.0-beta.54",
"grunt-babel": "8.0.0-beta.0",
"grunt": "~0.4.5",
"grunt-cli": "1.2.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.5.0",
"load-grunt-tasks": "^3.5.2"
},
"main": "GruntFile.js",
"dependencies": {
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Grunt
接下来我们需要告诉Grunt需要做什么。在执行时,Grunt将调用Babel来转换您的代码。因此,在与pom.xml和package.json位于同一目录中创建一个'Gruntfile.js'文件。 module.exports = function(grunt) {
require('load-grunt-tasks')(grunt); // npm install --save-dev
// load-grunt-tasks
grunt.initConfig({
babel : {
options : {
sourceMap : false
},
dist : {
files : [{
'src/main/webapp/resources/scripts/destination.js' : 'src/main/webapp/resources/scripts/source.es6'
},{
'src/main/webapp/resources/scripts/destination.js' : 'src/main/webapp/resources/scripts/source.es6'
}]
}
}
});
grunt.registerTask('default', [ 'babel' ]);
};
{
"presets": ["@babel/preset-env"]
}
现在只需祈祷并运行...
mvn install
...然后您的构建应该能够运行并转译您的JS。