Skip to content

Commit

Permalink
CoffeeScript now supports ES2015 modules (#7818)
Browse files Browse the repository at this point in the history
* Update CoffeeScript to 1.11.0, which supports modules; update check for whether to pass CoffeeScript’s output to Babel to look not just for backticks, but also for `import`, `export` and `function*`, the current ESNext features output by CoffeeScript

* Add tests for imported modules using CoffeeScript’s new native import statement

* Test that CoffeeScript native export statements work, by importing something exported by such a statement

* Improve regex

* Optimize regex
  • Loading branch information
GeoffreyBooth authored and benjamn committed Sep 28, 2016
1 parent 2bc5b4b commit 12472cf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions packages/coffeescript/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Package.registerBuildPlugin({
use: ['caching-compiler', 'ecmascript'],
sources: ['plugin/compile-coffeescript.js'],
npmDependencies: {
"coffee-script": "1.10.0",
"coffee-script": "1.11.0",
"source-map": "0.5.6"
}
});
Expand All @@ -18,10 +18,10 @@ Package.onUse(function (api) {
api.use('babel-compiler');

// Because the CoffeeScript plugin now calls
// BabelCompiler.prototype.processOneFileForTarget for any raw
// JavaScript enclosed by backticks, it must provide the same runtime
// environment that the 'ecmascript' package provides. The following
// api.imply calls should match those in ../ecmascript/package.js,
// BabelCompiler.prototype.processOneFileForTarget for any ES2015+
// JavaScript or JavaScript enclosed by backticks, it must provide the
// same runtime environment that the 'ecmascript' package provides.
// The following api.imply calls should match those in ../ecmascript/package.js,
// except that coffeescript does not api.imply('modules').
api.imply('ecmascript-runtime');
api.imply('babel-runtime');
Expand All @@ -37,6 +37,7 @@ Package.onTest(function (api) {
'tests/coffeescript_test_setup.js',
'tests/coffeescript_tests.coffee',
'tests/coffeescript_strict_tests.coffee',
'tests/coffeescript_module.coffee',
'tests/es2015_module.js',
'tests/litcoffeescript_tests.litcoffee',
'tests/litcoffeescript_tests.coffee.md',
Expand Down
5 changes: 3 additions & 2 deletions packages/coffeescript/plugin/compile-coffeescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export class CoffeeCompiler extends CachingCompiler {

let sourceMap = JSON.parse(output.v3SourceMap);

if (source.indexOf('`') !== -1) {
// If source contains backticks, pass the coffee output through babel-compiler
if (source.match(/`|\b(?:import|export|yield)\b/)) {
// If source contains backticks or features that output as ES2015+,
// pass the coffee output through babel-compiler
const doubleRoastedCoffee =
this.babelCompiler.processOneFileForTarget(inputFile, output.js);

Expand Down
1 change: 1 addition & 0 deletions packages/coffeescript/tests/coffeescript_module.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export testingForNativeImportedModule123456789 = true
22 changes: 18 additions & 4 deletions packages/coffeescript/tests/coffeescript_tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ Tinytest.add "coffeescript - compile", (test) -> test.isTrue true


# import/export statements must be top-level
`import { Meteor as testingForImportedSymbol123456789 } from "meteor/meteor";`
Tinytest.add "coffeescript - import external package via backticks", (test) ->
test.isTrue testingForImportedSymbol123456789?
`import { Meteor as testingForBacktickedImportedSymbol } from "meteor/meteor";`
Tinytest.add "coffeescript - import external package via backticked import statement", (test) ->
test.isTrue testingForBacktickedImportedSymbol?

`import { testingForImportedModule987654321 } from "./es2015_module.js";`
Tinytest.add "coffeescript - import local module via backticks", (test) ->
Tinytest.add "coffeescript - import local module via backticked import statement", (test) ->
test.isTrue testingForImportedModule987654321?


import { Meteor as testingForNativeImportedSymbol } from "meteor/meteor"
Tinytest.add "coffeescript - import external package via native import statement", (test) ->
test.isTrue testingForNativeImportedSymbol?

import { testingForImportedModule123456789 } from "./es2015_module.js";
Tinytest.add "coffeescript - import local module via native import statement", (test) ->
test.isTrue testingForImportedModule123456789?


import { testingForNativeImportedModule123456789 } from "./coffeescript_module.coffee";
Tinytest.add "coffeescript - import local module exported by a CoffeeScript native export statement, via native import statement", (test) ->
test.isTrue testingForNativeImportedModule123456789?
1 change: 1 addition & 0 deletions packages/coffeescript/tests/es2015_module.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const testingForImportedModule123456789 = true;
export const testingForImportedModule987654321 = true;

0 comments on commit 12472cf

Please sign in to comment.