I want to start using ES6, and I want to use grunt to manage my files. This is my project structure so far:
Gruntfile.js
package.json
dist/
src/
index.es6
And this is what index.es6
looks like:
import MapGL from 'react-map-gl';
const data = [];
const viewport = new Viewport();
These packages are all defined in package.json
and installed.
How do I turn this ES6 file into ES5 JavaScript? Right I'm able to turn it into JavaScript of a sort, but it's not transforming the require
statements at all.
This is my current Gruntfile:
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
options: {
separator: ';'
},
dist: {
src: ['src/**/*.js'],
dest: 'dist/<%= pkg.name %>.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */
'
},
dist: {
files: {
'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
}
}
},
jshint: {
files: ['src/index.js', 'test/index.js'],
options: {
reporterOutput: '',
esnext: true,
globals: {
console: true,
module: true,
document: true
}
}
},
babel: {
files: {
expand: true,
src: ['src/*.es6'],
ext: '-compiled.js'
},
options: {
sourceMap: true,
presets: ['babel-preset-es2015']
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['babel', 'jshint', 'concat']
}
});
grunt.registerTask('default', ['babel', 'jshint', 'concat', 'uglify']);
};
Running grunt
produces the following files:
Gruntfile.js
package.json
dist/
myproject.js
src/
index.es6
index-compiled.js
index-compiled.map
But myproject.js
contains the line var _reactMapGl = require('react-map-gl');
which fails in the browser.
See Question&Answers more detail:
os