通用的JavaScript

Meteor是一个用来创建JavaScript应用的全栈框架。这意味着Meteor应用与大部分应用不同,它包括运行在客户端、web浏览器及Cordova内核的移动应用的代码,运行在服务端、某个Node.js容器中的代码,或者同时运行在两者环境之间的通用代码。Meteor构建工具允许你轻松地指定JavaScript代码,包括任何被支持的UI模板、CSS规则以及静态资源,使其运行在使用一个结合ES2015importexport和Meteor构建工具默认文件加载顺序规则的环境中。

ES2015 模块

As of version 1.3, Meteor ships with full support for ES2015 modules. The ES2015 module standard is the replacement for CommonJS and AMD, which are commonly used JavaScript module format and loading systems. 在版本1.3中,Meteor提供了对于ES2015模块的全面支持。

In ES2015, you can make variables available outside a file using the export keyword. To use the variables somewhere else, you must import them using the path to the source. Files that export some variables are called "modules", because they represent a unit of reusable code. Explicitly importing the modules and packages you use helps you write your code in a modular way, avoiding the introduction of global symbols and "action at a distance".

Since this is a new feature introduced in Meteor 1.3, you will find a lot of code online that uses the older, more centralized conventions built around packages and apps declaring global symbols. This old system still works, so to opt-in to the new module system code must be placed inside the imports/ directory in your application. We expect a future release of Meteor will turn on modules by default for all code, because this is more aligned with how developers in the wider JavaScript community write their code.

You can read about the module system in detail in the modules package README. This package is automatically included in every new Meteor app as part of the ecmascript meta-package, so most apps won't need to do anything to start using modules right away.

Importing from packages

In Meteor, it is also simple and straightforward to use the import syntax to load npm packages on the client or server, and access the package's exported symbols as you would with any other module. You can also import from Atmosphere packages, but the import path must be prefixed with meteor/ to avoid conflict with the npm package namespace. For example, to import HTTP you could use import { HTTP } from 'meteor/http'.

`require`的使用

In Meteor, import statements compile to CommonJS require syntax. However, as a convention we encourage you to use import.

With that said, in some situations you may need to call out to require directly. One notable example is when requiring client or server-only code from a common file. As imports must be at the top-level scope, you may not place them within an if statement, so you'll need to write code like:

if (Meteor.isClient) {
  require('./client-only-file.js');
}

Note that dynamic calls to require() (where the name being required can change at runtime) cannot be analyzed correctly and may result in broken client bundles.

If you need to require from an ES2015 module with a default export, you can grab the export with require("package").default.

Another situation where you'll need to use require is in CoffeeScript files. As CS doesn't support the import syntax yet, you should use require:

{ FlowRouter } = require 'meteor/kadira:flow-router'
React = require 'react'

Exporting with CoffeeScript

When using CoffeeScript, not only the syntax to import variables is different, but also the export has to be done in a different way. Variables to be exported are put in the exports object:

exports.Lists = ListsCollection 'Lists'

results matching ""

    No results matching ""