wmo*_*ock 29 javascript ecmascript-6 babeljs
我很高兴现在通过Babeljs使用ECMAScript 6功能 - 特别是,我很乐意使用新模块功能开始使我的JavaScript代码更加模块化.
这是我到目前为止所写的内容:
// ECMAScript 6 code - lib.js
export const sqrt = Math.sqrt;
export function square (x) {
return x * x;
}
export function diag (x, y) {
return sqrt(square(x) + square(y));
}
// ECMAScript 6 code - main.js
import { square, diag } from 'lib';
console.log(square(11));
console.log(diag(4, 3));
Run Code Online (Sandbox Code Playgroud)
据我所知,我可以通过命令行上的babel将此代码从ES6转换为ES5:
babel lib.js > lib6to5.js
babel main.js > main6to5.js
Run Code Online (Sandbox Code Playgroud)
但是,在HTML中使用此代码需要做什么?
例如,这个index.html文件是什么样的:
<!-- index.html -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ECMAScript 6</title>
<!-- What goes here?
How do I include main6to5.js and lib6to5.js to make this work in the browser? -->
<script src="?????"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
谢谢
tre*_*ver 23
不使用模块: 如果您不使用模块(导入/导出),那么您只需将代码转换为ES5并将这些ES5文件包含在您的html中.例:
// ES6 - index.js
// arrow function
var result = [1, 2, 3].map(n => n * 2);
console.log(result);
// enhanced object literal
var project = "helloWorld";
var obj = {
// Shorthand for ‘project: project’
project,
// Methods
printProject() {
console.log(this.project);
},
[ "prop_" + (() => 42)() ]: 42
};
console.log(obj.printProject());
console.log(obj);
Run Code Online (Sandbox Code Playgroud)
透明到es5: babel index.js > es5.js
在index.html,include <script src="es5.js"></script>
will将在控制台中打印出以下内容:
[2,4,6]
helloWorld
{"project":"helloWorld","prop_42":42}
Run Code Online (Sandbox Code Playgroud)
使用模块:现在,如果您正在使用模块(在您的情况下使用lib.js和main.js),在将代码转换为ES5后,您还必须将它们(从AMD/CommonJS /模块捆绑到您的浏览器可以理解的代码).你可以用不同的生成系统,如这样做一饮而尽,的WebPack,browserify,等我将使用browserify在这里举例.
说我的文件夹结构如下所示:
es6
|- src
|- lib.js
|- main.js
|- compiled
|- index.html
Run Code Online (Sandbox Code Playgroud)
我运行babel将我的文件转换/src为/compiled文件夹:babel src --out-dir compiled.
现在我在编译的文件夹中有我的ES5代码.我在cmd行中安装browserify,然后将我的main.js(入口点)捆绑在我编译的文件夹中
~/es6 » npm install --global browserify
~/es6 » browserify ./compiled/main.js -o ./bundle.js
Run Code Online (Sandbox Code Playgroud)
现在我bundle.js看起来像这样:
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
exports.square = square;
exports.diag = diag;
var sqrt = exports.sqrt = Math.sqrt;
function square(x) {
return x * x;
}
function diag(x, y) {
return sqrt(square(x) + square(y));
}
Object.defineProperty(exports, "__esModule", {
value: true
});
},{}],2:[function(require,module,exports){
"use strict";
var _lib = require("./lib");
var square = _lib.square;
var diag = _lib.diag;
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5
},{"./lib":1}]},{},[2]);
Run Code Online (Sandbox Code Playgroud)
然后在index.html中:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ECMAScript 6</title>
<script src="./bundle.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后只需打开你的index.html,你的控制台应该给你以下内容:
121 bundle.js:27
5 bundle.js:28
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12084 次 |
| 最近记录: |