最新版的 node
支持最新版 ECMAScript
几乎全部特性,但有一个特性却一直到如今都尚未支持,那就是从 ES2015
开始定义的模块化机制。而如今咱们不少项目都是用 es6
的模块化规范来写代码的,包括 node
项目,因此,node
不能运行 es6
模块文件就会很不便。node
让 node
运行 es6
模块文件的方式有两种:git
es6
模块为 commonjs
模块hook
node
的 require
机制,直接让 node
的 require
加载 import/export
es6
模块为 commonjs
模块由于 node
支持几乎全部除 import/export
外的语法,因此咱们只须要将 import/export
转码成 require/exports
,而不须要转码其余语法。es6
好比下面的项目:github
- package.json - src/ - index.js - print.js - ...
# package.json { "main": "lib/index.js" # 由工具转码 src 目录下源文件到 lib 目录下 } # src/index.js import print from './print'; print('index'); export default print; # src/print.js export default str => { console.log('print: ' + str); };
由于 src
目录下的源文件都是 es6
模块化规范的,node
并不能直接运行,因此须要转码成 commonjs
规范的代码。npm
这个过程有两个方案:json
src
目录下的某个文件,而仅仅是以 src/index.js
为入口文件使用,能够把 src
目录下的文件打包成一个文件到 lib/index.js
:这种方式推荐使用工具 rollup src
目录下的文件,那就须要把 src
目录下的文件一对一的转码到 lib
目录下:这种方式推荐使用工具 gulp + babel src
目录下的文件打包成一个文件到 lib/index.js
相关文件:gulp
# rollup.config.js export default { input: 'src/index.js', output: { file: 'lib/index.js', format: 'cjs', }, }; # package.json { "scripts": { "build": "rollup -c" }, "devDependencies": { "rollup": "^0.66.4" } }
运行命令:babel
npm run build
结果:ide
# lib/index.js 'use strict'; var print = str => { console.log('print: ' + str); }; print('index'); module.exports = print;
src
目录下的文件一对一的转码到 lib
目录下相关文件:模块化
# build.js const gulp = require('gulp'); const babel = require('gulp-babel'); gulp.task('babel', () => gulp.src('src/**/*.js') .pipe(babel({ plugins: ['@babel/plugin-transform-modules-commonjs'] })) .pipe(gulp.dest('lib')) ); gulp.series('babel')(); # package.json { "scripts": { "build": "node build.js" }, "devDependencies": { "@babel/core": "^7.1.2", "@babel/plugin-transform-modules-commonjs": "^7.2.0", "gulp": "^4.0.0", "gulp-babel": "^8.0.0" } }
运行命令:
npm run build
结果:
# lib/index.js "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _print = _interopRequireDefault(require("./print")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } (0, _print.default)('index'); var _default = _print.default; exports.default = _default; # lib/print.js "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _default = str => { console.log('print: ' + str); }; exports.default = _default;
hook
node
的 require
机制,直接加载 import/export
这种机制通常是经过对 node
的 require
机制进行 hook
,劫持 require
抓取的源文件代码,把源代码转码成 commonjs
规范以后,再传送给 require
机制本来的代码流中。
pirates 之类的第三方 npm
包提供了这种添加 hook
的功能。
babel-register 即是使用这种方式达到 node
运行 es6
模块文件的目的的。
es6
模块文件示例目录:
- package.json - src/ - entry.js # 这里多了一个入口文件,专门用于注册 babel-register - index.js - print.js - ...
相关文件:
# package.json { "scripts": { "run": "node src/entry.js" }, "devDependencies": { "@babel/core": "^7.1.2", "@babel/plugin-transform-modules-commonjs": "^7.2.0", "@babel/register": "^7.0.0" } } # src/entry.js # 入口文件必须使用 commonjs 规范来写,由于尚未注册 hook require('@babel/register')({ plugins: ['@babel/plugin-transform-modules-commonjs'] }); require('./index'); # src/index.js import print from './print'; print('index'); # src/print.js export default str => { console.log('print: ' + str); };
运行:
npm run run
结果:
# 命令行打印 print: index
这种方式由于中间转码会有额外的性能损耗,因此不建议在生产环境下使用,只建议在开发模式下使用。
es6
模块文件babel-node 对 babel-register 进行了封装,提供了在命令行直接运行 es6
模块文件的便捷方式。
示例目录:
- package.json - src/ - index.js - print.js - ...
相关文件:
# package.json { "scripts": { "run": "babel-node src/index.js --plugins @babel/plugin-transform-modules-commonjs" }, "devDependencies": { "@babel/core": "^7.1.2", "@babel/node": "^7.2.0", "@babel/plugin-transform-modules-commonjs": "^7.2.0" } } # src/index.js import print from './print'; print('index'); # src/print.js export default str => { console.log('print: ' + str); };
运行:
npm run run
结果:
# 命令行打印 print: index
这种方式也不建议在生产环境下使用,只建议在开发模式下使用。
es6
就是指 ECMAScript 2015 es7
就是指 ECMAScript 2016 es8
就是指 ECMAScript 2017 es9
就是指 ECMAScript 2018 到写这篇文章为止,已发布了 ECMAScript 2018
。
更多博客,查看 https://github.com/senntyou/blogs
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)