UNPKG

2.17 kBJavaScriptView Raw
1var _ = require("lodash");
2var winston = require("winston");
3var stealTools = require("../../index");
4var clone = require("lodash/cloneDeep");
5var options = clone(require("./options"));
6var makeStealConfig = require("./make_steal_config");
7
8var pathsToOmit = [
9 "bundles-path",
10 "bundle-steal",
11 "watch",
12 "tree-shaking",
13 "no-tree-shaking"
14];
15
16var bundleOptions = _.assign(
17 {},
18 _.omit(options, pathsToOmit),
19 {
20 dest: {
21 alias: "d",
22 type: "string",
23 default: "",
24 describe: "Defaults to root folder, a directory to save the bundles"
25 },
26 filter: {
27 alias: "f",
28 default: "**",
29 type: "string",
30 describe: "Glob pattern to match modules to be included in the bundle"
31 },
32 deps: {
33 type: "boolean",
34 default: false,
35 describe: "Generates a development bundle of the dependencies in the node_modules folder"
36 },
37 dev: {
38 type: "boolean",
39 default: false,
40 describe: "Generates a development bundle like --deps but includes StealJS @config modules"
41 }
42 }
43);
44
45module.exports = {
46 command: "bundle",
47
48 describe: "Creates a custom bundle",
49
50 builder: bundleOptions,
51
52 handler: function (argv) {
53 var options = argv;
54 var config = makeStealConfig(argv);
55 var filter = getFilterPattern(options);
56
57 options.filter = filter ? filter : options.filter;
58
59 return stealTools.bundle(config, options)
60 .then(function() {
61 winston.info("\nBundle created successfully".green);
62 })
63 .catch(function(e) {
64 e = typeof e === "string" ? new Error(e) : e;
65
66 winston.error(e.message.red);
67 winston.error("\nBuild failed".red);
68
69 process.exit(1);
70 });
71 }
72};
73
74/**
75 * Returns the glob pattern(s) used to generate the bundle
76 * @param {{}} options The command options object
77 * @returns {String|Array} Glob pattern(s)
78 */
79function getFilterPattern(options) {
80 var pattern;
81
82 // if the --dev flag is passed, generate a bundle with the
83 // node_modules dependencies AND StealJS @config graph
84 if (options.dev) {
85 pattern = ["node_modules/**/*", "package.json"];
86 }
87 // make a bundle of dependencies in node_modules folder
88 // when the --deps flag is used
89 else if (options.deps) {
90 pattern = "node_modules/**/*";
91 }
92
93 return pattern;
94}