Last year, a new version of JavaScript was released with a lot of new goodies. One of them was that the syntax for importing and exporting modules was codified as the "only way" to handle JavaScript modules. Well, it's finally here. Another good thing is that it's organized in a way that you can statically analyze the entire module dependency tree. Pretty cool. Let's take a look at the syntax:
So, basically you can import the main value of a module ("default"), or a specific property from explicit exports, or a combination of both, or anything else. Correspondingly, you can export a value for the default module, or an object with multiple properties. You can also export properties one by one. ***Please refer to the style guide for style ;) ES7 also adds some small additions to this kind of syntax.
Nothing special, but when can we use them? We'll have to wait and see. Just like many ES6 syntax features, if you don't plan to support them now, you can use a tool called Babel to transpile them back to ES5. Once you are ready to support them, you can stop Babel from transpiling. Let's take a look at how this works. We'll implement this in Node.js and NPM. Try executing this file; src/letter_keys.js
src/arrow_keys.js
src/move.js
The assumption is that the main index.js file is used for the export of the internal module contents, and it assumes that these keys are exported from other files. This example is a bit convoluted, but that's okay. src/index.js
This can be used as part of a project that depends on this module. It should print the awsd key and the arrow object. Let's start with npm. Create the repo dir and initialize it:
This will take a while. As you might have guessed, babel-cli supports running Babel from the command line (6), and the babel-preset-stage-1 package provides the relevant ES7 module transpilation tools (at the time of writing). The -yes flag will cause npm to create a default package.json without asking the user. The -D flag is short for --save-dev, and will add the package to the devDependency entry in package.json. Now, add the preset parameters to the default babel configuration file: .babelrc
If this works, great! hello, future! However, at the time of writing, none of these examples will work in ES6, let alone Node.js. After these transpilation steps, they can be executed anyway. You should now have an almost empty package.json file that contains the three dev dependencies we added. Let's add a script to this package.json file to enable transpilation:
(Just add the "translate" line and the comma after the "test" line). This transpilation script is a compilation step. At the end of the article, you can find the final package.json file used in this article (official version). Now, all that’s left is to call npm to run the script to transpile and run our code.
Go for it! Now, as a bonus, we can use Jscrambler to "obfuscate" the code a bit. We can pass Babel transpiled code, so why not? Our (final) package.json file looks like this: package.json
Complete the configuration as usual (if you are using Node.js, you will need a pro account). Here are the files I used .jscramblerrc
Let's summarize it with a script. This script will translate the source files with Babel, output to the /build folder, then use Jscrambler to obfuscate the code, and then put the result into the /dist folder. The content in /dist can be run normally without using any ES7 features. run.sh
Make it ready:
Next, run:
You can go to the /dist folder to see the result. You will see that the result is far from the original file, this is because we protected it through Jscrambler, but the result is still runnable. This concludes the tutorial. Have fun using ES7! OneAPM helps you easily identify the performance bottleneck of Node.js applications, and analyzes layer by layer through powerful Trace records until the line-level problem code is identified. It displays the system response speed from the user's perspective and counts user usage by region and browser. |
<<: FastQuery: a framework for fast data operations
>>: What do you need to know to ensure mobile app quality?
After the underlying LeTV business develops, hard...
The emergence of mini programs has simplified the...
In 2020, Bilibili has successfully "broken o...
When people mention Huskies, they always associat...
Zhuan Zhuan and Xianyu are both C2C second-hand t...
Hello everyone, I am an ASO operations specialist...
Smartphones will remain the dominant consumer dev...
Shengtao E-commerce: 2022 anchor advanced trainin...
Recently, according to the official website of th...
Let me first tell you a real case we received: Xi...
Last Friday, NIO officially announced that the NI...
As a preliminary note, with the listing of Intern...
I recently started to select technology for the m...
Wuxi Guoda Machinery Equipment Co., Ltd. is a for...
Although many new projects are using RecyclerView...