-
Notifications
You must be signed in to change notification settings - Fork 2k
Modules documentation #4309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modules documentation #4309
Changes from all commits
5b74433
54c189d
72f7dba
633dcb1
c162917
96b868a
50a112d
502be0a
1745601
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
"devDependencies": { | ||
"uglify-js": "~2.2", | ||
"jison": ">=0.2.0", | ||
"highlight.js": "~8.0.0", | ||
"highlight.js": "~9.6.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused. #3981 (comment):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
"underscore": "~1.5.2", | ||
"docco": "~0.6.2" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'local-file.coffee' | ||
import 'coffee-script' | ||
|
||
import _ from 'underscore' | ||
import * as underscore from 'underscore' | ||
|
||
import { now } from 'underscore' | ||
import { now as currentTimestamp } from 'underscore' | ||
import { first, last } from 'underscore' | ||
import utilityBelt, { each } from 'underscore' | ||
|
||
export default Math | ||
export square = (x) -> x * x | ||
export class Mathematics | ||
least: (x, y) -> if x < y then x else y | ||
|
||
export { sqrt } | ||
export { sqrt as squareRoot } | ||
export { Mathematics as default, sqrt as squareRoot } | ||
|
||
export * from 'underscore' | ||
export { max, min } from 'underscore' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
<a href="#comparisons">Chained Comparisons</a> | ||
<a href="#strings">String Interpolation, Block Strings, and Block Comments</a> | ||
<a href="#regexes">Block Regular Expressions</a> | ||
<a href="#modules">Modules</a> | ||
<a href="#cake">Cake, and Cakefiles</a> | ||
<a href="#source-maps">Source Maps</a> | ||
<a href="#scripts">"text/coffeescript" Script Tags</a> | ||
|
@@ -467,10 +468,11 @@ Expressions | |
</p> | ||
<p> | ||
If you'd like to create top-level variables for other scripts to use, | ||
attach them as properties on <b>window</b>, or on the <b>exports</b> | ||
object in CommonJS. The <b>existential operator</b> (covered below), gives you a | ||
reliable way to figure out where to add them; if you're targeting both | ||
CommonJS and the browser: <code>exports ? this</code> | ||
attach them as properties on <b>window</b>; attach them as properties on the | ||
<b>exports</b> object in CommonJS; or use an <a href="#modules"><code>export</code> | ||
statement</a>. If you’re targeting both CommonJS and the browser, the | ||
<b>existential operator</b> (covered below), gives you a | ||
reliable way to figure out where to add them: <code>exports ? this</code> | ||
</p> | ||
|
||
<p> | ||
|
@@ -937,6 +939,29 @@ Expressions | |
</p> | ||
<%= codeFor('heregexes') %> | ||
|
||
<p> | ||
<span id="modules" class="bookmark"></span> | ||
<b class="header">Modules</b> | ||
ES2015 modules are supported in CoffeeScript, with very similar <code>import</code> | ||
and <code>export</code> syntax: | ||
</p> | ||
<%= codeFor('modules') %> | ||
<p> | ||
Note that the CoffeeScript compiler <strong>does not resolve modules</strong>; writing an | ||
<code>import</code> or <code>export</code> statement in CoffeeScript will produce an | ||
<code>import</code> or <code>export</code> statement in the resulting output. | ||
It is your responsibility attach another transpiler, such as | ||
<a href="https://github.com/google/traceur-compiler">Traceur Compiler</a>, | ||
<a href="http://babeljs.io/">Babel</a> or | ||
<a href="https://github.com/rollup/rollup">Rollup</a>, to convert this ES2015 syntax into | ||
code that will work in your target runtimes. | ||
</p> | ||
<p> | ||
Also note that any file with an <code>import</code> or <code>export</code> statement will | ||
be output without a <a href="#lexical-scope">top-level function safety wrapper</a>; | ||
in other words, importing or exporting modules will automatically trigger | ||
<a href="#usage">bare</a> mode for that file. This is because per the ES2015 spec, | ||
<code>import</code> or <code>export</code> statements must occur at the topmost scope. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you link to other parts of the documentation. Here's a quote from the
That mentions even CommonJS – should it perhaps mention import/export as well? Also, I'm having a hard time deciding where this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t understand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is what is hinted as:
# Browser globals only:
window.myExport = myExport
# CommonJS only:
exports.myExport = myExport
# Support either:
global = exports ? window
global.myExport = myExport
# `this` at the top level is the global object. Theoretically support more stuff:
global = exports ? this
global.myExport = myExport Not sure if we should go down this sub-quest of improving that paragraph of documentation “while at it,” or just leave it be and be happy with the addition of mentioning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There’s no convenient way to support all three. You either have to know you’re using modules or you’re targeting the browser/CommonJS. You can’t even do some kind of check as far as I’m aware, like “if |
||
|
||
<h2> | ||
<span id="cake" class="bookmark"></span> | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lydell how do you feel about this? How important is clear highlighting? I feel like we don’t want to wait until highlight.js updates its definition for CoffeeScript, and then we update the version of highlight.js we’re using.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to special-case here. However, I think we should use the power of regex instead of a
for
loop ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They’re actually not the same. We need to search for the space after the keyword, or else
as
matches withinclass="
. But if we replace including the space, we get<span class="reserved">as </span>
. Maybe this could be done with an even cleverer regex, but I’m not sure it’s worth the effort.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry, I didn’t see that space! This should do the trick: