-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(umd): Universal Module Definition builds (#78)
* updated package.json in platform/core with rc6 and alpha8-1 * added glob and rollup dependencies * updated typings with hammerjs so lib build doesnt fail * first draft of inline and rollup to umd files * updated finish-release script, updated RELEASE.md
- Loading branch information
1 parent
ab67c40
commit 23007cd
Showing
10 changed files
with
240 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
var gulp = require('gulp-help')(require('gulp')); | ||
var inlineResources = require('./inline-resources.js'); | ||
var config = require('../build.conf'); | ||
|
||
gulp.task('inline-resource-files', 'Inline resouces files into JS files', function() { | ||
inlineResources([config.paths.deployed]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
|
||
/** | ||
* Simple Promiseify function that takes a Node API and return a version that supports promises. | ||
* We use promises instead of synchronized functions to make the process less I/O bound and | ||
* faster. It also simplify the code. | ||
*/ | ||
function promiseify(fn) { | ||
return function() { | ||
const args = [].slice.call(arguments, 0); | ||
return new Promise((resolve, reject) => { | ||
fn.apply(this, args.concat([function (err, value) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(value); | ||
} | ||
}])); | ||
}); | ||
}; | ||
} | ||
|
||
const readFile = promiseify(fs.readFile); | ||
const writeFile = promiseify(fs.writeFile); | ||
|
||
function inlineResources(globs) { | ||
/** | ||
* For every argument, inline the templates and styles under it and write the new file. | ||
*/ | ||
for (let pattern of globs) { | ||
if (pattern.indexOf('*') < 0) { | ||
// Argument is a directory target, add glob patterns to include every files. | ||
pattern = path.join(pattern, '**', '*'); | ||
} | ||
|
||
const files = glob.sync(pattern, {}) | ||
.filter(name => /\.js$/.test(name)); // Matches only JavaScript files. | ||
|
||
// Generate all files content with inlined templates. | ||
files.forEach(filePath => { | ||
readFile(filePath, 'utf-8') | ||
.then(content => inlineTemplate(filePath, content)) | ||
.then(content => inlineStyle(filePath, content)) | ||
.then(content => removeModuleId(filePath, content)) | ||
.then(content => writeFile(filePath, content)) | ||
.catch(err => { | ||
console.error('An error occured: ', err); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
inlineResources(process.argv.slice(2)); | ||
} | ||
|
||
|
||
/** | ||
* Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and | ||
* replace with `template: ...` (with the content of the file included). | ||
* @param filePath {string} The path of the source file. | ||
* @param content {string} The source file's content. | ||
* @return {string} The content with all templates inlined. | ||
*/ | ||
function inlineTemplate(filePath, content) { | ||
return content.replace(/templateUrl:\s*'([^']+?\.html)'/g, function(m, templateUrl) { | ||
const templateFile = path.join(path.dirname(filePath), templateUrl); | ||
const templateContent = fs.readFileSync(templateFile, 'utf-8'); | ||
const shortenedTemplate = templateContent | ||
.replace(/([\n\r]\s*)+/gm, ' ') | ||
.replace(/"/g, '\\"'); | ||
return `template: "${shortenedTemplate}"`; | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and | ||
* replace with `styles: [...]` (with the content of the file included). | ||
* @param filePath {string} The path of the source file. | ||
* @param content {string} The source file's content. | ||
* @return {string} The content with all styles inlined. | ||
*/ | ||
function inlineStyle(filePath, content) { | ||
return content.replace(/styleUrls:\s*(\[[\s\S]*?\])/gm, function(m, styleUrls) { | ||
const urls = eval(styleUrls); | ||
return 'styles: [' | ||
+ urls.map(styleUrl => { | ||
const styleFile = path.join(path.dirname(filePath), styleUrl); | ||
const styleContent = fs.readFileSync(styleFile, 'utf-8'); | ||
const shortenedStyle = styleContent | ||
.replace(/([\n\r]\s*)+/gm, ' ') | ||
.replace(/"/g, '\\"'); | ||
return `"${shortenedStyle}"`; | ||
}) | ||
.join(',\n') | ||
+ ']'; | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Remove every mention of `moduleId: module.id`. | ||
* @param _ {string} The file path of the source file, currently ignored. | ||
* @param content {string} The source file's content. | ||
* @returns {string} The content with all moduleId: mentions removed. | ||
*/ | ||
function removeModuleId(_, content) { | ||
return content.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, ''); | ||
} | ||
|
||
|
||
module.exports = inlineResources; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
const gulp = require('gulp-help')(require('gulp')); | ||
const fs = require('fs'); | ||
const rollup = require('rollup').rollup; | ||
const commonjs = require('rollup-plugin-commonjs'); | ||
const nodeResolve = require('rollup-plugin-node-resolve'); | ||
const path = require('path'); | ||
const config = require('../build.conf'); | ||
|
||
function camelCase(str) { | ||
return str.replace(/-(\w)/g, (_, letter) => { | ||
return letter.toUpperCase(); | ||
}) | ||
} | ||
|
||
gulp.task('rollup-code', '', function() { | ||
const components = fs.readdirSync(config.paths.deployed) | ||
.filter(componentName => (fs.statSync(path.join(config.paths.deployed, componentName))).isDirectory()); | ||
|
||
const globals = { | ||
// Angular dependencies | ||
'@angular/core': 'ng.core', | ||
'@angular/common': 'ng.common', | ||
'@angular/forms': 'ng.forms', | ||
'@angular/http': 'ng.http', | ||
'@angular/router': 'ng.router', | ||
'@angular/platform-browser': 'ng.platformBrowser', | ||
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic', | ||
|
||
// Angular dependencies | ||
|
||
'@angular2-material/button': 'md.button', | ||
'@angular2-material/button-toggle': 'md.buttonToggle', | ||
'@angular2-material/card': 'md.card', | ||
'@angular2-material/checkbox': 'md.checkbox', | ||
'@angular2-material/core': 'md.core', | ||
'@angular2-material/grid-list': 'md.gridList', | ||
'@angular2-material/icon': 'md.icon', | ||
'@angular2-material/input': 'md.input', | ||
'@angular2-material/list': 'md.list', | ||
'@angular2-material/menu': 'md.menu', | ||
'@angular2-material/progress-bar': 'md.progressBar', | ||
'@angular2-material/progress-circle': 'md.progressCircle', | ||
'@angular2-material/radio': 'md.radio', | ||
'@angular2-material/sidenav': 'md.sidenav', | ||
'@angular2-material/slider': 'md.slider', | ||
'@angular2-material/slide-toggle': 'md.slideToggle', | ||
'@angular2-material/tabs': 'md.tabs', | ||
'@angular2-material/toolbar': 'md.toolbar', | ||
'@angular2-material/tooltip': 'md.tooltip', | ||
|
||
// Rxjs dependencies | ||
'rxjs/Subject': 'Rx', | ||
'rxjs/add/observable/forkJoin': 'Rx.Observable', | ||
'rxjs/add/observable/of': 'Rx.Observable', | ||
'rxjs/add/operator/toPromise': 'Rx.Observable.prototype', | ||
'rxjs/add/operator/map': 'Rx.Observable.prototype', | ||
'rxjs/add/operator/filter': 'Rx.Observable.prototype', | ||
'rxjs/add/operator/do': 'Rx.Observable.prototype', | ||
'rxjs/add/operator/share': 'Rx.Observable.prototype', | ||
'rxjs/add/operator/finally': 'Rx.Observable.prototype', | ||
'rxjs/add/operator/catch': 'Rx.Observable.prototype', | ||
'rxjs/Observable': 'Rx' | ||
}; | ||
components.forEach(name => { | ||
globals[`@covalent/${name}`] = `td.${camelCase(name)}` | ||
}); | ||
|
||
// Build all of them asynchronously. | ||
return components.reduce((previous, name) => { | ||
return previous | ||
.then(() => { | ||
return rollup({ | ||
entry: path.join(config.paths.deployed, name, 'index.js'), | ||
context: 'window', | ||
external: Object.keys(globals).concat(components.map(name => `@covalent/${name}`)), | ||
plugins: [ | ||
commonjs({ jsnext: true, main: true }), | ||
] | ||
}); | ||
}) | ||
.then((bundle) => { | ||
const result = bundle.generate({ | ||
moduleName: `td.${camelCase(name)}`, | ||
format: 'umd', | ||
exports: 'named', | ||
globals | ||
}); | ||
const outputPath = path.join(config.paths.deployed, name, `${name}.umd.js`); | ||
fs.writeFileSync( outputPath, result.code ); | ||
}); | ||
}, Promise.resolve()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,7 +439,6 @@ | |
code:before, | ||
code:after { | ||
letter-spacing: -0.2em; | ||
content: "\00a0"; | ||
} | ||
|
||
pre>code { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters