Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 83f645c

Browse filesBrowse files
add shorthands to builder
1 parent 18d75a0 commit 83f645c
Copy full SHA for 83f645c

File tree

Expand file treeCollapse file tree

6 files changed

+298
-2
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+298
-2
lines changed

‎lib/builder.js

Copy file name to clipboardExpand all lines: lib/builder.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* Module dependencies.
44
*/
55

6-
var lookup = require('./lookup');
7-
var Build = require('./build');
86
var Batch = require('batch');
7+
var Build = require('./build');
8+
var lookup = require('./lookup');
9+
var shorthands = require('./shorthands');
910

1011
/**
1112
* Expose `Builder`
@@ -97,3 +98,9 @@ Builder.prototype.build = function(fn){
9798
});
9899
});
99100
};
101+
102+
/**
103+
* Mixin shorthands.
104+
*/
105+
106+
for (var key in shorthands) Builder.prototype[key] = shorthands[key];

‎lib/shorthands.js

Copy file name to clipboard
+126Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
var commonjs = require('./plugins/commonjs');
3+
var concat = require('./plugins/concat');
4+
var copy = require('./plugins/copy');
5+
var rewriteUrls = require('./plugins/rewrite-urls');
6+
var symlink = require('./plugins/symlink');
7+
8+
/**
9+
* Build all the native types with an `out` directory and optional `options`
10+
* then invoke `fn(err, build)`.
11+
*
12+
* @param {String} out
13+
* @param {Object} options (optional)
14+
* @property {Boolean} symlink
15+
* @property {String} rewriteUrls
16+
* @param {Function} fn
17+
*/
18+
19+
exports.all = function (out, options, fn) {
20+
if ('function' == typeof options) fn = options, options = null;
21+
options = options || {};
22+
23+
this
24+
.use(commonjs('json'))
25+
.use(commonjs('scripts'))
26+
.use(commonjs('templates'))
27+
.use(rewrite(options.rewriteUrls || ''))
28+
.use(concat('json'))
29+
.use(concat('scripts'))
30+
.use(concat('styles'))
31+
.use(concat('templates'))
32+
.use(copy('fonts', out, options))
33+
.use(copy('images', out, options))
34+
.build(fn);
35+
};
36+
37+
/**
38+
* Build scripts and invoke `fn(err, build)`.
39+
*
40+
* @param {Function} fn
41+
*/
42+
43+
exports.scripts = function (fn) {
44+
this
45+
.use(commonjs('scripts'))
46+
.use(concat('scripts'))
47+
.build(fn);
48+
};
49+
50+
/**
51+
* Build styles with `options` and invoke `fn(err, build)`.
52+
*
53+
* @param {Object} options (optional)
54+
* @property {String} rewriteUrls
55+
* @param {Function} fn
56+
*/
57+
58+
exports.styles = function (options, fn) {
59+
if ('function' == typeof options) fn = options, options = null;
60+
options = options || {};
61+
62+
this
63+
.use(rewriteUrls(options.rewriteUrls || ''))
64+
.use(concat('styles'))
65+
.build(fn);
66+
};
67+
68+
/**
69+
* Build templates and invoke `fn(err, build)`.
70+
*
71+
* @param {Function} fn
72+
*/
73+
74+
exports.templates = function (fn) {
75+
this
76+
.use(commonjs('templates'))
77+
.use(concat('templates'))
78+
.build(fn);
79+
};
80+
81+
/**
82+
* Build json and invoke `fn(err, build)`.
83+
*
84+
* @param {Function} fn
85+
*/
86+
87+
exports.json = function (fn) {
88+
this
89+
.use(commonjs('json'))
90+
.use(concat('json'))
91+
.build(fn);
92+
};
93+
94+
/**
95+
* Build images with `out` directory and `options` and invoke `fn(err, build)`.
96+
*
97+
* @param {String} out
98+
* @param {Object} options (optional)
99+
* @param {Function} fn
100+
*/
101+
102+
exports.images = function (out, options, fn) {
103+
if ('function' == typeof options) fn = options, options = null;
104+
options = options || {};
105+
106+
this
107+
.use(copy('images', out, options))
108+
.build(fn);
109+
};
110+
111+
/**
112+
* Build fonts with `out` directory and `options` and invoke `fn(err, build)`.
113+
*
114+
* @param {String} out
115+
* @param {Object} options (optional)
116+
* @param {Function} fn
117+
*/
118+
119+
exports.fonts = function (out, options, fn) {
120+
if ('function' == typeof options) fn = options, options = null;
121+
options = options || {};
122+
123+
this
124+
.use(copy('fonts', out, options))
125+
.build(fn);
126+
};

‎test/fixtures/fonts/component.json

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "fonts",
3+
"description": "a bunch of fonts",
4+
"styles": [
5+
"style.css"
6+
],
7+
"fonts": [
8+
"montserrat-regular.ttf"
9+
]
10+
}
28.3 KB
Binary file not shown.

‎test/fixtures/fonts/style.css

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@font-face {
2+
font-family: 'Montserrat';
3+
src: url('fonts/montserrat-regular.ttf') format('truetype');
4+
font-weight: normal;
5+
font-style: normal;
6+
}

‎test/shorthands.js

Copy file name to clipboard
+147Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
2+
var Builder = require('..');
3+
var exec = require('child_process').exec;
4+
var exists = require('fs').existsSync;
5+
var fs = require('fs');
6+
var read = require('fs').readFileSync;
7+
var vm = require('vm');
8+
9+
describe('shorthands', function(){
10+
afterEach(function(done){
11+
exec('rm -rf /tmp/build', done);
12+
});
13+
14+
describe('scripts', function(){
15+
it('should build scripts', function(done){
16+
var builder = new Builder('test/fixtures/hello');
17+
builder.path('..');
18+
builder.scripts(function(err, build){
19+
if (err) return done(err);
20+
var js = build.scripts.trim();
21+
var out = read('test/fixtures/hello-js.js', 'utf8').trim();
22+
js.should.eql(out);
23+
done();
24+
});
25+
});
26+
});
27+
28+
describe('json', function(){
29+
it('should build json', function(done){
30+
var builder = new Builder('test/fixtures/json');
31+
builder.path('..');
32+
builder.json(function(err, build){
33+
if (err) return done(err);
34+
var js = build.json.trim();
35+
var out = read('test/fixtures/json.js', 'utf8').trim();
36+
js.should.eql(out);
37+
done();
38+
});
39+
});
40+
});
41+
42+
describe('templates', function(){
43+
it('should build templates', function(done){
44+
var builder = new Builder('test/fixtures/template-strings');
45+
builder.path('..');
46+
builder.templates(function(err, build){
47+
if (err) return done(err);
48+
var js = build.templates.trim();
49+
var out = read('test/fixtures/template.js', 'utf8').trim();
50+
js.should.eql(out);
51+
done();
52+
});
53+
});
54+
});
55+
56+
describe('styles', function(){
57+
it('should build styles', function(done){
58+
var builder = new Builder('test/fixtures/assets-parent');
59+
builder.path('..');
60+
builder.styles(function(err, build){
61+
if (err) return done(err);
62+
build.styles.should.include('url("assets/images/logo.png")');
63+
build.styles.should.include('url("assets/images/maru.jpeg")');
64+
build.styles.should.include('url("assets/images/npm.png")');
65+
build.styles.should.include('url(http://example.com/images/manny.png)');
66+
build.styles.should.include('url(/public/images/foo.png)')
67+
build.styles.should.include('url(data:image/png;base64,PNG DATA HERE)');
68+
done();
69+
});
70+
});
71+
72+
it('should rewrite urls', function(done){
73+
var builder = new Builder('test/fixtures/assets-parent');
74+
builder.path('..');
75+
builder.styles({ rewriteUrls: 'build' }, function(err, build){
76+
if (err) return done(err);
77+
build.styles.should.include('url("build/assets/images/logo.png")');
78+
build.styles.should.include('url("build/assets/images/maru.jpeg")');
79+
build.styles.should.include('url("build/assets/images/npm.png")');
80+
build.styles.should.include('url(http://example.com/images/manny.png)');
81+
build.styles.should.include('url(/public/images/foo.png)')
82+
build.styles.should.include('url(data:image/png;base64,PNG DATA HERE)');
83+
done();
84+
});
85+
86+
});
87+
});
88+
89+
describe('images', function(){
90+
it('should build images', function(done){
91+
var builder = new Builder('test/fixtures/assets');
92+
builder.path('..');
93+
builder.images('/tmp/build', function(err){
94+
if (err) return done(err);
95+
exists('/tmp/build/assets/images/maru.jpeg').should.be.true;
96+
exists('/tmp/build/assets/images/logo.png').should.be.true;
97+
exists('/tmp/build/assets/images/npm.png').should.be.true;
98+
fs.lstat('/tmp/build/assets/images/npm.png', function(err, stats) {
99+
if (err) return done(err);
100+
stats.isSymbolicLink().should.be.false;
101+
done();
102+
});
103+
});
104+
});
105+
106+
it('should symlink when option is true', function(done){
107+
var builder = new Builder('test/fixtures/assets');
108+
builder.path('..');
109+
builder.images('/tmp/build', { symlink: true }, function(err){
110+
if (err) return done(err);
111+
fs.lstat('/tmp/build/assets/images/npm.png', function(err, stats) {
112+
if (err) return done(err);
113+
stats.isSymbolicLink().should.be.true;
114+
done();
115+
});
116+
});
117+
});
118+
});
119+
120+
describe('fonts', function(){
121+
it('should build fonts', function(done){
122+
var builder = new Builder('test/fixtures/fonts');
123+
builder.path('..');
124+
builder.fonts('/tmp/build', function(err){
125+
if (err) return done(err);
126+
fs.lstat('/tmp/build/fonts/montserrat-regular.ttf', function(err, stats) {
127+
if (err) return done(err);
128+
stats.isSymbolicLink().should.be.false;
129+
done();
130+
});
131+
});
132+
});
133+
134+
it('should symlink when option is true', function(done){
135+
var builder = new Builder('test/fixtures/fonts');
136+
builder.path('..');
137+
builder.fonts('/tmp/build', { symlink: true }, function(err){
138+
if (err) return done(err);
139+
fs.lstat('/tmp/build/fonts/montserrat-regular.ttf', function(err, stats) {
140+
if (err) return done(err);
141+
stats.isSymbolicLink().should.be.true;
142+
done();
143+
});
144+
});
145+
});
146+
});
147+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.