Skip to content
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

Add support for ES6 Modules #15

Merged
merged 2 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ define([], function() {
});
```

Transformed to **ES6** (`defineModule('es6')`):

```javascript
export default {
start: function() {},
end: function() {},
version: "1.0"
};
```

Transformed to **Hybrid** (`defineModule('hybrid')`):

```javascript
Expand Down Expand Up @@ -93,6 +103,7 @@ The desired output type. One of the following:
* `commonjs` - Produce CommonJS modules
* `node` - Produce Node modules (alias for `commonjs`)
* `amd` - Produce AMD modules
* `es6` - Produce ES6 modules
* `hybrid` - Produce hybrid modules that can be used in most environments
* `plain` - Return an unmolested function definition

Expand All @@ -103,7 +114,7 @@ Type: `Object`
Default: `{}`

An object containing dependencies that should be imported for this module. This option is only
supported for `commonjs`, `node`, `amd`, and `hybrid` modules. For other systems, you will have
supported for `commonjs`, `node`, `amd`, `es6` and `hybrid` modules. For other systems, you will have
to manage the dependency loading in another way.

The property name in the object should be the value of the variable that the
Expand All @@ -129,6 +140,12 @@ define(['library'], function(Library) {
});
```

**ES6**

```javascript
import Library from "library";
```

**Hybrid**

```javascript
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ function makeHybrid(moduleContents, filePath, opts) {
'})(function(' + defines.join(',') + ') { return ' + moduleContents + '; });';
}

function makeEs6(moduleContents, filePath, opts) {
var requires = _.map(opts.require, function(key, value) {
if (key !== null) {
return 'import ' + value + ' from ' + JSON.stringify(key) + ';';
}
});
return requires.join('') + 'export default ' + moduleContents + ';';
}

function makePlain(moduleContents, filePath, opts) {
// moduleObject;
return moduleContents + ';';
Expand Down Expand Up @@ -95,6 +104,7 @@ module.exports = function(type, options) {
else if (type === 'commonjs' || type === 'node') { contents = makeCommonJS(contents, file.path, opts); }
else if (type === 'hybrid') { contents = makeHybrid(contents, file.path, opts); }
else if (type === 'plain') { contents = makePlain(contents, file.path, opts); }
else if (type === 'es6') { contents = makeEs6(contents, file.path, opts); }
else {
throw new Error('Unsupported module type for gulp-define-module: ' + type);
}
Expand Down
4 changes: 4 additions & 0 deletions test/expected/basic_es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function() {
// this is a module definition file that includes this single module.
this.property = "some property";
};
4 changes: 4 additions & 0 deletions test/expected/basic_es6_advanced.js.regex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import\s([^\s]*)\sfrom\s\"([^\s)]*)\"\;export default Application\.Library\.TEMPLATES\['prefix\.basic'\] = function\(\) {
\/\/ this is a module definition file that includes this single module\.
this\.property = "some property";
}\;
4 changes: 4 additions & 0 deletions test/expected/basic_es6_require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Library from "library";export default function() {
// this is a module definition file that includes this single module.
this.property = "some property";
};
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('gulp-define-module', function() {
it('makes anonymous AMD modules', basic('amd'));
it('makes named AMD modules', basic('amd', amdNamingOptions, 'named'));
it('makes CommonJS modules', basic('commonjs'));
it('makes ES6 modules', basic('es6'));
it('makes Node modules', basic('node'));
it('makes Hybrid modules', basic('hybrid'));
it('makes plain modules', basic('plain'));
Expand All @@ -77,6 +78,7 @@ describe('gulp-define-module', function() {
it('handles require for AMD', basic('amd', requireOptions, 'require'));
it('handles require for Node', basic('node', requireOptions, 'require'));
it('handles require for CommonJS', basic('commonjs', requireOptions, 'require'));
it('handles require for ES6', basic('es6', requireOptions, 'require'));
it('handles require for Hybrid', basic('hybrid', requireOptions, 'require'));
it('ignores require for plain', basic('plain', requireOptions));

Expand Down Expand Up @@ -180,6 +182,36 @@ describe('gulp-define-module', function() {

});

it('processes options for ES6 both through invocation and incoming file', function(done) {

var stream = defineModule('es6', {
wrapper: 'Application.Library.TEMPLATES[\'<%= name %>\'] = <%= contents %>',
context: function(context) {
return { name: context.prefix + '.' + context.name };
},
require: { Library: 'app-library', Vendor: null }
});

var file = fixtureFile('basic.js');
file.defineModuleOptions = {
wrapper: 'Library.TEMPLATES[\'<%= name %>\'] = <%= contents %>',
context: { prefix: 'prefix' },
require: { Library: 'app-library', Vendor: 'shared-vendor-library' }
};

stream.on('data', function(file) {
fileShouldMatchExpected(file, 'basic_es6_advanced.js', function(match) {
match[1].should.eql('Library');
match[2].should.eql('app-library');
done();
});
});

stream.write(file);
stream.end();

});

describe('wrapper context', function() {
it('defines name', basic('plain', {
wrapper: 'App["<%= name %>"] = <%= contents %>'
Expand Down