Skip to content

Commit

Permalink
Merge remote-tracking branch 'lookfirst/export2'
Browse files Browse the repository at this point in the history
Added support for ES2015 modules which is also needed for the typescript support.

Signed-off-by: Dominic Böttger <[email protected]>
  • Loading branch information
DominicBoettger committed Jan 26, 2016
2 parents e66316b + dacb592 commit 2885635
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ A custom Lodash template for generating the Javacript code. The template is call
* escapedContent: the escaped HTML content of the input file. Note: the HTML content is escaped for usage in a single quoted string.
* prettyEscapedContent: the readable, escaped HTML content of the input file.


Example

``` javascript
Expand All @@ -140,6 +141,28 @@ Type: `String`

The file extension of the generated files. Defaults to .js. Can be used to generate TypeScript files and create a gulp TypeScript - job to convert them. For a working example take a look at [angular-systemjs-typescript-boilerplate](https://github.com/INSPIRATIONlabs/angular-systemjs-typescript-boilerplate)

### options.export
Type: `String`

* `commonjs`: export the angular module using `module.exports =`
* `system`: export the angular module using `export default`

> Note this does not export anything with `declareModule` set to `true`.
Example

``` javascript
{
export: 'commonjs'
}
```

``` javascript
{
export: 'system'
}
```

## License

[MIT License](http://en.wikipedia.org/wiki/MIT_License)
Expand Down
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ var TEMPLATES = {

SINGLE_DECLARED_MODULE: "angular.module('<%= moduleName %>').run(['$templateCache', function($templateCache) {\n" +
" $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
"}]);\n"
"}]);\n",

COMMON_JS_EXPORTS: "module.exports = ",

SYSTEM_JS_EXPORTS: "export default "
};

/**
Expand Down Expand Up @@ -62,20 +66,31 @@ module.exports = function (options) {


function getTemplate() {
var template;

if (options && options.template) {
return options.template;
}
else if (options && options.moduleName) {
if (options.declareModule === false) {
return TEMPLATES.SINGLE_DECLARED_MODULE;
template = TEMPLATES.SINGLE_DECLARED_MODULE;
}
else {
return TEMPLATES.SINGLE_MODULE;
}
}
else {
return TEMPLATES.MODULE_PER_FILE;
template = TEMPLATES.MODULE_PER_FILE;
}

if (options && options.export === 'commonjs') {
template = TEMPLATES.COMMON_JS_EXPORTS + template;
}
else if (options && options.export === 'system') {
template = TEMPLATES.SYSTEM_JS_EXPORTS + template;
}

return template;
}

function getTemplateParams() {
Expand Down
36 changes: 36 additions & 0 deletions test/expected/exampleWithExportCommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = angular.module('fixtures/example.html', []).run(['$templateCache', function($templateCache) {
$templateCache.put('fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
}]);
36 changes: 36 additions & 0 deletions test/expected/exampleWithExportCommonModuleNameNoGenerate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = angular.module('myAwesomePartials').run(['$templateCache', function($templateCache) {
$templateCache.put('fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
}]);
36 changes: 36 additions & 0 deletions test/expected/exampleWithExportSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default angular.module('fixtures/example.html', []).run(['$templateCache', function($templateCache) {
$templateCache.put('fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
}]);
36 changes: 36 additions & 0 deletions test/expected/exampleWithExportSystemModuleNameNoGenerate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default angular.module('myAwesomePartials').run(['$templateCache', function($templateCache) {
$templateCache.put('fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
}]);
68 changes: 67 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,72 @@ describe("gulp-ng-html2js", function () {
testBufferedFile(null, expectedFile, done);
});

describe("when options.export is provided", function(){
it("commonjs", function(done){
var expectedFile = new gutil.File({
path: "test/expected/exampleWithExportCommon.js",
cwd: "test/",
base: "test/expected",
contents: fs.readFileSync("test/expected/exampleWithExportCommon.js")
});

var params = {
export: "commonjs"
};

testBufferedFile(params, expectedFile, done);
});

it("should use common, options.moduleName && options.declareModule when provided", function(done){
var expectedFile = new gutil.File({
path: "test/expected/exampleWithExportCommonModuleNameNoGenerate.js",
cwd: "test/",
base: "test/expected",
contents: fs.readFileSync("test/expected/exampleWithExportCommonModuleNameNoGenerate.js")
});

var params = {
moduleName: "myAwesomePartials",
declareModule: false,
export: "commonjs"
};

testBufferedFile(params, expectedFile, done);
});

it("system", function(done){
var expectedFile = new gutil.File({
path: "test/expected/exampleWithExportSystem.js",
cwd: "test/",
base: "test/expected",
contents: fs.readFileSync("test/expected/exampleWithExportSystem.js")
});

var params = {
export: "system"
};

testBufferedFile(params, expectedFile, done);
});

it("should use system, options.moduleName && options.declareModule when provided", function(done){
var expectedFile = new gutil.File({
path: "test/expected/exampleWithExportSystemModuleNameNoGenerate.js",
cwd: "test/",
base: "test/expected",
contents: fs.readFileSync("test/expected/exampleWithExportSystemModuleNameNoGenerate.js")
});

var params = {
moduleName: "myAwesomePartials",
declareModule: false,
export: "system"
};

testBufferedFile(params, expectedFile, done);
});
});

it("should use options.moduleName when provided", function (done) {
var expectedFile = new gutil.File({
path: "test/expected/exampleWithModuleName.js",
Expand Down Expand Up @@ -57,7 +123,7 @@ describe("gulp-ng-html2js", function () {

it("should use options.moduleName && options.declareModule when provided", function (done) {
var expectedFile = new gutil.File({
path: "test/expected/exampleWithModuleName.js",
path: "test/expected/exampleWithModuleNameNoGenerate.js",
cwd: "test/",
base: "test/expected",
contents: fs.readFileSync("test/expected/exampleWithModuleNameNoGenerate.js")
Expand Down

0 comments on commit 2885635

Please sign in to comment.