Skip to content

Commit 2f61b19

Browse files
authored
claycli compilation (#85)
* font compilation first pass * added logging and watch functionality * added media compilation * added multi-command build + watch * added style compilation * better error messaging for custom plugins * add styles to root command * template compilation * add sites back to media compilation * added alphabetical-based bucket * add template bundling * run media task before templates * testing script compilation first pass * testing more script compilation issues * client dependencies * model compilation, too large * client and model compilation, watching * underscore ids.json and registry.json * added kiln plugin compilation * compile legacy js * command-specific minification via env variables * fixed legacy script compilation * not working * instantiate legacy and controller modules on page load * compile kiln vue stuff with postcss * watch kiln * only recompile browserified things * use folder and file name for kiln plugin IDs * compile layout model.js and client.js * reorder subcommands in order of increasing complexity * auto-remove trailing slash from asset host * added docs * update code for highland 2.x, add test * update docs * updated docs and whatnot based on byron's feedback
1 parent d0f2caa commit 2f61b19

24 files changed

+15665
-3314
lines changed

README.md

+403-18
Large diffs are not rendered by default.

cli/cli-options.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,50 @@ module.exports = {
3434
requiresArg: true
3535
},
3636
publish: {
37-
alias: 'publish', // -p, --publish
37+
alias: 'publish', // -p, --publish (note: -p is also used when compiling css with plugins)
3838
describe: 'publish items when importing',
3939
type: 'boolean'
4040
},
4141
layout: {
42-
alias: 'layout', // -l, --layout
42+
alias: 'layout', // -l, --layout (note: -l is also used when compiling fonts)
4343
describe: 'export layout when exporting page(s)',
4444
type: 'boolean'
4545
},
4646
yaml: {
4747
alias: 'yaml', // -y, --yaml
4848
describe: 'parse bootstrap format',
4949
type: 'boolean'
50+
},
51+
// compilation options
52+
minify: {
53+
alias: 'minify', // -m, --minify
54+
describe: 'run through minification',
55+
type: 'boolean'
56+
},
57+
watch: {
58+
alias: 'watch', // -w, --watch
59+
describe: 'watch files and recompile on changes',
60+
type: 'boolean'
61+
},
62+
inlined: {
63+
alias: 'inlined', // -i, --inlined
64+
describe: 'compile base64 inlined fonts',
65+
type: 'boolean'
66+
},
67+
linked: {
68+
alias: 'linked', // -l, --linked (note: -l is also used when exporting layouts)
69+
describe: 'compile linked fonts',
70+
type: 'boolean',
71+
default: undefined // will be undefined if unset
72+
},
73+
plugins: {
74+
alias: 'plugins', // -p, --plugins (note: -p is also used when publishing imports)
75+
describe: 'space-separated list of postcss plugins',
76+
type: 'array'
77+
},
78+
globs: {
79+
alias: 'globs', // -g, --globs
80+
describe: 'globbing patterns of legacy js to compile to _global.js',
81+
type: 'array'
5082
}
5183
};

cli/compile/fonts.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const pluralize = require('pluralize'),
3+
compile = require('../../lib/cmd/compile'),
4+
options = require('../cli-options'),
5+
reporter = require('../../lib/reporters'),
6+
helpers = require('../../lib/compilation-helpers');
7+
8+
function builder(yargs) {
9+
return yargs
10+
.usage('Usage: $0 fonts')
11+
.example('$0 fonts', 'compile linked fonts')
12+
.example('$0 fonts --watch', 'compile and watch linked fonts')
13+
.example('$0 fonts --inlined --linked', 'compile inlined and linked fonts')
14+
.option('w', options.watch)
15+
.option('m', options.minify)
16+
.option('i', options.inlined)
17+
.option('l', options.linked)
18+
.option('r', options.reporter);
19+
}
20+
21+
function handler(argv) {
22+
const t1 = Date.now(),
23+
compiled = compile.fonts({
24+
watch: argv.watch,
25+
minify: argv.minify,
26+
inlined: argv.inlined,
27+
linked: argv.linked
28+
});
29+
30+
return compiled.build
31+
.map(reporter.logAction(argv.reporter, 'fonts'))
32+
.toArray((results) => {
33+
const t2 = Date.now();
34+
35+
reporter.logSummary(argv.reporter, 'fonts', (successes) => {
36+
let message = `Compiled ${pluralize('font', successes, true)} in ${helpers.time(t2, t1)}`;
37+
38+
if (compiled.watch) {
39+
message += '\nWatching for changes...';
40+
}
41+
return { success: true, message };
42+
})(results);
43+
44+
if (compiled.watch) {
45+
compiled.watch.on('raw', helpers.debouncedWatcher);
46+
}
47+
});
48+
}
49+
50+
module.exports = {
51+
command: 'fonts',
52+
describe: 'Compile fonts',
53+
builder,
54+
handler
55+
};

cli/compile/index.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
const h = require('highland'),
3+
_ = require('lodash'),
4+
chalk = require('chalk'),
5+
pluralize = require('pluralize'),
6+
compile = require('../../lib/cmd/compile'),
7+
options = require('../cli-options'),
8+
reporter = require('../../lib/reporters'),
9+
helpers = require('../../lib/compilation-helpers');
10+
11+
function builder(yargs) {
12+
return yargs
13+
.usage('Usage: $0 compile [asset type]')
14+
.command(require('./media'))
15+
.command(require('./fonts'))
16+
.command(require('./styles'))
17+
.command(require('./templates'))
18+
.command(require('./scripts'))
19+
.example('$0 compile', 'compile all assets')
20+
.example('$0 compile --watch', 'compile and watch all assets')
21+
.option('w', options.watch)
22+
.option('m', options.minify)
23+
// font-specific options
24+
.option('i', options.inlined)
25+
.option('l', options.linked)
26+
// style-specific options
27+
.option('p', options.plugins)
28+
// script-specific options
29+
.option('g', options.globs)
30+
// reporter option
31+
.option('r', options.reporter);
32+
}
33+
34+
function handler(argv) {
35+
const t1 = Date.now(),
36+
plugins = _.map(argv.plugins, (pluginName) => {
37+
try {
38+
return require(pluginName)();
39+
} catch (e) {
40+
console.error(`${chalk.red('Error: Cannot init plugin "' + pluginName + '"')}\n${chalk.grey(e.message)}`);
41+
}
42+
}),
43+
media = compile.media({ watch: argv.watch }); // run media task before others (specifically, templates)
44+
45+
return h(media.build).collect().toArray((mediaResults) => {
46+
const fonts = compile.fonts({
47+
watch: argv.watch,
48+
minify: argv.minify,
49+
inlined: argv.inlined,
50+
linked: argv.linked
51+
}),
52+
styles = compile.styles({
53+
watch: argv.watch,
54+
minify: argv.minify,
55+
plugins
56+
}),
57+
templates = compile.templates({
58+
watch: argv.watch,
59+
minify: argv.minify
60+
}),
61+
scripts = compile.scripts({
62+
watch: argv.watch,
63+
minify: argv.minify,
64+
globs: argv.globs
65+
}),
66+
tasks = [fonts, media, styles, templates, scripts],
67+
builders = _.map(tasks, (task) => task.build),
68+
watchers = _.map(tasks, (task) => task.watch).concat([media.watch]),
69+
isWatching = !!watchers[0];
70+
71+
return h([h.of(mediaResults)].concat(builders))
72+
.merge()
73+
.map(reporter.logAction(argv.reporter, 'compile'))
74+
.toArray((results) => {
75+
const t2 = Date.now();
76+
77+
reporter.logSummary(argv.reporter, 'compile', (successes) => {
78+
let message = `Compiled ${argv.minify ? 'and minified ' : '' }${pluralize('file', successes, true)} in ${helpers.time(t2, t1)}`;
79+
80+
if (isWatching) {
81+
message += '\nWatching for changes...';
82+
}
83+
return { success: true, message };
84+
})(results);
85+
86+
if (isWatching) {
87+
_.each(watchers, (watcher) => {
88+
watcher.on('raw', helpers.debouncedWatcher);
89+
});
90+
}
91+
});
92+
});
93+
}
94+
95+
module.exports = {
96+
command: 'compile [asset type]',
97+
describe: 'Compile fonts, media, styles, scripts, and templates',
98+
aliases: ['compiler', 'c'],
99+
builder,
100+
handler
101+
};

cli/compile/media.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const pluralize = require('pluralize'),
3+
compile = require('../../lib/cmd/compile'),
4+
options = require('../cli-options'),
5+
reporter = require('../../lib/reporters'),
6+
helpers = require('../../lib/compilation-helpers');
7+
8+
function builder(yargs) {
9+
return yargs
10+
.usage('Usage: $0 media')
11+
.example('$0 media', 'compile media files')
12+
.example('$0 media --watch', 'compile and watch media files')
13+
.option('w', options.watch)
14+
.option('r', options.reporter);
15+
}
16+
17+
function handler(argv) {
18+
const t1 = Date.now(),
19+
compiled = compile.media({ watch: argv.watch });
20+
21+
return compiled.build
22+
.map(reporter.logAction(argv.reporter, 'media'))
23+
.toArray((results) => {
24+
const t2 = Date.now();
25+
26+
reporter.logSummary(argv.reporter, 'media', (successes) => {
27+
let message = `Compiled ${pluralize('file', successes, true)} in ${helpers.time(t2, t1)}`;
28+
29+
if (compiled.watch) {
30+
message += '\nWatching for changes...';
31+
}
32+
return { success: true, message };
33+
})(results);
34+
35+
if (compiled.watch) {
36+
compiled.watch.on('raw', helpers.debouncedWatcher);
37+
}
38+
});
39+
}
40+
41+
module.exports = {
42+
command: 'media',
43+
describe: 'Compile component, layout, styleguide, and site media files',
44+
builder,
45+
handler
46+
};

cli/compile/scripts.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
const pluralize = require('pluralize'),
3+
compile = require('../../lib/cmd/compile'),
4+
options = require('../cli-options'),
5+
reporter = require('../../lib/reporters'),
6+
helpers = require('../../lib/compilation-helpers');
7+
8+
function builder(yargs) {
9+
return yargs
10+
.usage('Usage: $0 scripts')
11+
.example('$0 scripts', 'compile js files')
12+
.example('$0 scripts --watch', 'compile and watch js files')
13+
.option('w', options.watch)
14+
.option('m', options.minify)
15+
.option('g', options.globs)
16+
.option('r', options.reporter);
17+
}
18+
19+
function handler(argv) {
20+
const t1 = Date.now(),
21+
compiled = compile.scripts({
22+
watch: argv.watch,
23+
minify: argv.minify,
24+
globs: argv.globs
25+
});
26+
27+
return compiled.build
28+
.map(reporter.logAction(argv.reporter, 'scripts'))
29+
.toArray((results) => {
30+
const t2 = Date.now();
31+
32+
reporter.logSummary(argv.reporter, 'scripts', (successes) => {
33+
let message = `Compiled ${argv.minify ? 'and minified ' : '' }${pluralize('script', successes, true)} in ${helpers.time(t2, t1)}`;
34+
35+
if (compiled.watch) {
36+
message += '\nWatching for changes...';
37+
}
38+
return { success: true, message };
39+
})(results);
40+
41+
if (compiled.watch) {
42+
compiled.watch.on('raw', helpers.debouncedWatcher);
43+
}
44+
});
45+
}
46+
47+
module.exports = {
48+
command: 'scripts',
49+
describe: 'Compile scripts',
50+
builder,
51+
handler
52+
};

cli/compile/styles.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
const _ = require('lodash'),
3+
chalk = require('chalk'),
4+
pluralize = require('pluralize'),
5+
compile = require('../../lib/cmd/compile'),
6+
options = require('../cli-options'),
7+
reporter = require('../../lib/reporters'),
8+
helpers = require('../../lib/compilation-helpers');
9+
10+
function builder(yargs) {
11+
return yargs
12+
.usage('Usage: $0 styles')
13+
.example('$0 styles', 'compile css files with postcss')
14+
.example('$0 styles --watch', 'compile and watch css files')
15+
.option('w', options.watch)
16+
.option('m', options.minify)
17+
.option('p', options.plugins)
18+
.option('r', options.reporter);
19+
}
20+
21+
function handler(argv) {
22+
const t1 = Date.now(),
23+
plugins = _.map(argv.plugins, (pluginName) => {
24+
try {
25+
return require(pluginName)();
26+
} catch (e) {
27+
console.error(`${chalk.red('Error: Cannot init plugin "' + pluginName + '"')}\n${chalk.grey(e.message)}`);
28+
}
29+
}),
30+
compiled = compile.styles({
31+
watch: argv.watch,
32+
minify: argv.minify,
33+
plugins
34+
});
35+
36+
return compiled.build
37+
.map(reporter.logAction(argv.reporter, 'styles'))
38+
.toArray((results) => {
39+
const t2 = Date.now();
40+
41+
reporter.logSummary(argv.reporter, 'styles', (successes) => {
42+
let message = `Compiled ${argv.minify ? 'and minified ' : '' }${pluralize('css file', successes, true)} in ${helpers.time(t2, t1)}`;
43+
44+
if (compiled.watch) {
45+
message += '\nWatching for changes...';
46+
}
47+
return { success: true, message };
48+
})(results);
49+
50+
if (compiled.watch) {
51+
compiled.watch.on('raw', helpers.debouncedWatcher);
52+
}
53+
});
54+
}
55+
56+
module.exports = {
57+
command: 'styles',
58+
describe: 'Compile styles',
59+
builder,
60+
handler
61+
};

0 commit comments

Comments
 (0)