With 6to5, let's write the modular development of ES6 today !, 6to5es6
Http://es6rocks.com/2014/10/es6-modules-today-with-6to5? Utm_source = javascriptweekly & utm_medium = email original link
I posted a photo on Twitter to express how happy I am. It is like a time machine that allows us to move to the future-ES6 era! Let me show you how to use 6to5 to implement the modularization of ES6 today.
With 6to5, We can practice the first step of ES6 modularization today.
If you do not know about the modular development of ES6, see JSModules. io. I also recommend you read @ jcemer's article A new syntax for modules in ES6, which involves A lot of cool things about JS modularization. He can instruct us to use 6to5. In general, 6to5 can try ES6 modular development in advance in an environment that supports ES5d. 6to5 has several advantages over other downgrade tools: * Readability: Your formatted code is retained as much as possible. * Scalability: supports a very large plug-in library and browser. * Adjustable: Because source map is supported, you can easily debug the compiled code. * High Efficiency: directly convert the code into code equivalent to ES without additional operations.
Write modules together
Let's try to write a module! Our application will not do anything except output logs. Its main purpose is to let you know how modularity works and how to make your existing environment use ES6 modular development. Basic directory structure:
├── Gruntfile.js├── package.json└── src ├── app.js ├── modules │ ├── bar.js │ ├── baz.js │ └── foo.js └── sample └── index.html
App. js is the main program and contains the modular directory we will store. below is the code of app. js:
import foo from "./modules/foo";import bar from "./modules/bar";console.log('From module foo >>> ', foo);console.log('From module bar >>> ', bar);
The above code is very simple. We imported the foo module and the bar module and printed them separately.
// foo.jslet foo = 'foo';export default foo;// bar.jslet bar = 'bar';export default bar;
In these modules, we only export two strings 'foo' and 'bar'. When we import these modules, our variables actually have data. Of course, why do we export objects, classes, functions, and so on? Now, you can imitate this example to write your own modules.
Build
As you already know, ES6 does not support your current browser and Node. js. There is only one way, that is, to use a downgrade converter to write ES6 modular code. As I mentioned earlier, I use 6to5, which can precisely achieve what we want. This task is running on Grunt, we use @ sindresorhus grunt-6to5
npm install grunt-cli -gnpm install grunt --save-devnpm install grunt-6to5 --save-dev
Our Gruntfile is similar to the following:
grunt.initConfig({ '6to5': { options: { modules: 'common' }, build: { files: [{ expand: true, cwd: 'src/', src: ['**/*.js'], dest: 'dist/', }], } }});
To test it in the browser, I made a copy task that just copies the sample/index.html file to our dist directory. The HTML file looks like this:
This is a simple and powerful configuration, and we have almost completed it. After you specify the source file and output file, this task can be run. The 'common' option aims to tell 6to5 that we will output the ES5CommonJS modular style. Of course, 6to5 also supports AMD. I wrote sample/index.html and asked him to test it in a browser environment. The HTML code is as follows:
<!doctype html>
Observe the above Code. We use the AMD RequireJS framework to load this JS. For this example, you need to change the above configuration file to modules: 'amd'
Run Everything is ready Dongfeng only owe, our code has been put in the es6-modules-today-with-6to5, you can clone yourself to play. Use npm install to install 6to5
Remember, the Grunt task will generate a target folder to store the converted code. Therefore, if you want to test the converted ES6 code using the CommonJS specification, You can execute the following command:
Node dist/app. js
Running Effect on Node. js If you use amdscripts, please upload index.html in the browser (for a bit, foreigners don't even know China's sea. js)
Result conclusion of browser execution Through this simple example, you learned how to write code simply using the ES6 modular style. 6to5 is a tool for gastrointestinal rods that allows you to experience the pleasure of ES6 Modularization in advance. Resource download es6-modules-today-with-6to5, welcome to submit some feedback
From http://www.hacke2.cn