BuckleScript

BuckleScript

  • Docs
  • Try
  • API
  • Community
  • Blog
  • Languages icon中文
    • 日本語
    • English
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • 繁體中文
    • 帮助我们翻译
  • GitHub

›Interop

Intro

  • What & Why
  • Installation
  • New Project
  • Concepts Overview

Interop

  • Overview
  • Cheatsheet
  • Embed Raw JavaScript
  • Common Data Types
  • Intro to External
  • Bind to Global Values
  • Null, Undefined & Option
  • Object
  • Object 2
  • Class
  • Function
  • Import & Export
  • Regular Expression
  • Exceptions
  • JSON
  • Fast Pipe
  • Generate Converters & Helpers
  • Better Data Structures Printing (Debug Mode)
  • NodeJS Special Variables
  • Miscellaneous
  • Browser Support & Polyfills

Build System

  • Overview
  • Configuration
  • Automatic Interface Generation
  • Interop with JS Build System
  • Performance
  • Advanced

Standard Library

  • Overview

Advanced

  • Conditional Compilation
  • Extended Compiler Options
  • Use Existing OCaml Libraries
  • Difference from Native OCaml
  • Compiler Architecture & Principles
  • Comparison to Js_of_ocaml
Translate

Overview

Interop 是 interoperability (互操作)的缩写,在BuckleScript中,Interop表示与javascript进行通信。 BuckleScript的核心目标之一就是尽可能平滑的与跨堆栈javascript进行交互。

实际开发中,一个常规的代码库,很难在短短几天之内,从一种技术转换成另一种技术的实现,这个过程可能会花费几个月甚至几年。 对于一个互操作性没有充分仔细的被设计的语言, 如果还在犹豫是否应该引入到代码库中的时候,转换代价在初期将成为这个语言的价值的短板,并最终放弃这个语言。

一个精心设计的互操作性的语言应该是简单,高效,本地的和非指令性的,允许开发者使用熟悉的概念去使用这个语言,并且应该是足够自信的去推荐给对这个技术不是很了解的上级管理者,并且当用于团队的项目之后应该是高产的。

申明

The above section might sound a bit buzzword-y; here is how we've designed BuckleScript's interop, from the lowest part of the stack to the highest.

变量

大部分变量的名称能被编译成了干净的js名称, hello编译成了hello,但有些可能会编译成比如__$INTERNAL$hello$1, 干净的名称(没有被混淆过的)能够更好的调试。 我们见过一个团队中,有对BuckleScript和Reason不是那么熟悉的成员需要紧急修复bug的场景,直接在BuckScript文件中写一段原始的js代码。 邻人欣慰的是BuckleScript设计了这样的"最后逃生窗口",实在没招的情况下这种方式也是支持的。

数据结构

BuckleScript(简称BS)的主要数据结构完全映射到了JS的数据结构,例如,BS 中的string 就是js中的string,BS 中的 array 就是 js中的 array,下面就是BS的代码:

let messages = [| "hello"; "world"; "how"; "are"; "you" |]
let messages = [|"hello", "world", "how", "are", "you"|];

Roughly compiles to the JS code:

var messages = ["hello", "world", "how", "are", "you"]

There is zero mental and performance overhead while using such value. Naturally, the value on the BuckleScript side is automatically typed to be an array of strings.

This behavior doesn't hold for complex data structures; the dedicated sections for each offer more info.

函数

In most cases, you can directly call a JS function from BS, and vice-versa! These calls are free of performance overhead in most cases.

模块/文件

Every let declarations in a BS file is exported by default and usable from JS. For the other way around, you can declare in a BS file what JS module you want to use inside BS. We can both output and consume CommonJS, ES6 and AMD modules.

The generated output is clean enough that it could be passed as (slightly badly indented) hand-written JavaScript code. Try a few snippets in our playground!

构建系统

BS comes with a lightning-fast (fastest?) build system. It starts up in a few milliseconds and shuts down as fast. Incremental compilation is two digits of milliseconds. This allows the build system to be inserted invisibly into your whole JS build pipeline without embarrassing it. Unless your JS build pipeline is already embarrassingly slow. That's ok.

1 BS file compiles to 1 JS file. The build can be configured to generate JS files alongside or outside your ml/re source files. This means you don't have to ask the infra team's help in trying out BuckleScript at your company; simply generate the JS files and check them in. From the perspective of the rest of the compilation pipeline, it's as if you've written these JS files by hand. This is how Messenger successfully introduced Reason into the codebase.

包管理

We use NPM and Yarn. Since the generated output is clean enough, you can publish them at NPM prepublishOnly time and remove all trace of BuckleScript beforehand. The consumer wouldn't even know you wrote in BS, not JS! Check your node_modules right now; you might have been using some transitive BS code without knowing!

结论

Hopefully you can see from the previous overview that we've poured lots of thoughts into simplicity, familiarity and performance in the interop architecture. The next few sections describe each of these points in detail.

← 上一页下一页 →
  • 申明
    • 变量
    • 数据结构
    • 函数
    • 模块/文件
    • 构建系统
    • 包管理
  • 结论