Skip to content

Commit 6aefc0c

Browse files
IlyaSurmayvalorkin
authored andcommitted
feat(config): add config support (#17)
* feat(config): add config support * refactor(config): apply review notes, replace entryPoints with modules * feat(build): add es2015 bundles support * feat(config): add support for skipBundles and buildEs2015 options, fix bundle filenames with multiple entryPoints
1 parent 05f1963 commit 6aefc0c

File tree

7 files changed

+570
-29
lines changed

7 files changed

+570
-29
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
"node-sass-tilde-importer": "1.0.0",
6666
"read-pkg": "2.0.0",
6767
"reflect-metadata": "0.1.10",
68+
"rollup": "0.52.1",
69+
"rollup-plugin-commonjs": "8.2.6",
70+
"rollup-plugin-node-resolve": "3.0.0",
6871
"rxjs": "5.4.2",
6972
"semver": "5.3.0",
7073
"split": "1.0.0",

src/bin/ngm-cli.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const cli = meow(`
8585
alias: {
8686
m: 'message',
8787
p: 'project',
88+
c: 'config',
8889
w: 'watch',
8990
v: 'version',
9091
local: 'use-local-dependencies alias'
@@ -104,10 +105,10 @@ if (cli.input.length === 0) {
104105
}
105106

106107
// project flag is mandatory for now
107-
if (!cli.flags.project) {
108-
console.error('Please provide path to your projects source folder, `-p DIR` ');
109-
process.exit(1);
110-
}
108+
// if (!cli.flags.project) {
109+
// console.error('Please provide path to your projects source folder, `-p DIR` ');
110+
// process.exit(1);
111+
// }
111112

112113
import { main } from '../lib/ngm';
113114

src/commands/build.command.ts

Lines changed: 111 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import path = require('path');
55
const Listr = require('listr');
66
const cpy = require('cpy');
77
const del = require('del');
8+
const fs = require('fs');
89

910
import { findSubmodules, tasksWatch } from '../utils';
10-
import { build, bundleUmd, buildPkgs } from '../tasks';
11+
import { build, bundleUmd, buildPkgs, bundleEs2015 } from '../tasks';
1112
import { inlineResources } from '../helpers/inline-resources';
1213

13-
export function buildCommand({project, verbose, clean, local, main, watch, skipBundles}) {
14+
15+
export function buildCommand({project, verbose, clean, local, main, watch, skipBundles, buildEs2015}) {
1416
// 1. clean dist folders
1517
// 2.1 merge pkg json
1618
// todo: 2.2 validate pkg (main, module, types fields)
@@ -119,14 +121,28 @@ export function buildCommand({project, verbose, clean, local, main, watch, skipB
119121
task: () => new Listr(
120122
opts.map(opt => ({
121123
title: `Bundling ${opt.pkg.name}`,
122-
task: () => bundleUmd({
123-
main,
124-
src: opt.tmp,
125-
dist: opt.dist,
126-
name: opt.pkg.name,
127-
tsconfig: opt.tsconfig.path,
128-
minify: false
129-
})
124+
task: () => {
125+
if (Array.isArray(main) && main.length) {
126+
return Promise.all(main.map((entryPoint, i) => bundleUmd({
127+
main: entryPoint,
128+
src: opt.tmp,
129+
dist: opt.dist,
130+
name: i === 0 ? opt.pkg.name : entryPoint.replace('.ts', ''),
131+
tsconfig: opt.tsconfig.path,
132+
minify: false
133+
})))
134+
}
135+
136+
return bundleUmd({
137+
main,
138+
src: opt.tmp,
139+
dist: opt.dist,
140+
name: opt.pkg.name,
141+
tsconfig: opt.tsconfig.path,
142+
minify: false
143+
})
144+
},
145+
skip: () => watch || skipBundles
130146
}))
131147
),
132148
skip: () => watch || skipBundles
@@ -136,18 +152,65 @@ export function buildCommand({project, verbose, clean, local, main, watch, skipB
136152
task: () => new Listr(
137153
opts.map(opt => ({
138154
title: `Bundling ${opt.pkg.name}`,
139-
task: () => bundleUmd({
140-
main,
141-
src: opt.tmp,
142-
dist: opt.dist,
143-
name: opt.pkg.name,
144-
tsconfig: opt.tsconfig.path,
145-
minify: true
146-
})
155+
task: () => {
156+
if (Array.isArray(main) && main.length) {
157+
return Promise.all(main.map((entryPoint, i) => bundleUmd({
158+
main: entryPoint,
159+
src: opt.tmp,
160+
dist: opt.dist,
161+
name: i === 0 ? opt.pkg.name : entryPoint.replace('.ts', ''),
162+
tsconfig: opt.tsconfig.path,
163+
minify: true
164+
})))
165+
}
166+
167+
return bundleUmd({
168+
main,
169+
src: opt.tmp,
170+
dist: opt.dist,
171+
name: opt.pkg.name,
172+
tsconfig: opt.tsconfig.path,
173+
minify: true
174+
})
175+
},
176+
skip: () => watch || skipBundles
147177
}))
148178
),
149179
skip: () => watch || skipBundles
150180
},
181+
{
182+
title: 'Bundling es2015 version',
183+
task: () => new Listr(
184+
opts.map(opt => ({
185+
title: `Bundling ${opt.pkg.name}`,
186+
task: () => {
187+
const tsconfig = JSON.parse(fs.readFileSync(path.resolve(opt.tmp, 'tsconfig.json'), 'utf8'));
188+
tsconfig.compilerOptions.target = 'es2015';
189+
tsconfig.compilerOptions.module = 'es2015';
190+
tsconfig.compilerOptions.outDir = 'dist-es2015';
191+
fs.writeFileSync(path.resolve(opt.tmp, 'tsconfig.json'), JSON.stringify(tsconfig), 'utf8');
192+
193+
if (Array.isArray(main) && main.length) {
194+
return Promise.all(main.map((entryPoint, i) => bundleEs2015({
195+
input: entryPoint,
196+
dist: opt.dist,
197+
name: i === 0 ? opt.pkg.name : entryPoint.replace('.ts', ''),
198+
tmp: opt.tmp
199+
})))
200+
}
201+
202+
return bundleEs2015({
203+
input: main,
204+
dist: opt.dist,
205+
name: opt.pkg.name,
206+
tmp: opt.tmp
207+
});
208+
},
209+
skip: () => !buildEs2015 || watch || skipBundles
210+
}))
211+
),
212+
skip: () => !buildEs2015 || watch || skipBundles
213+
},
151214
{
152215
title: 'Clean .tmp folders',
153216
task: () => new Listr(
@@ -161,8 +224,34 @@ export function buildCommand({project, verbose, clean, local, main, watch, skipB
161224
}
162225

163226
export function buildTsRun(cli) {
164-
const {project, watch, verbose, clean, local, skipBundles} = cli.flags;
165-
let main = cli.flags.main || 'index.ts';
166-
return buildCommand({project, verbose, clean, local, main, watch, skipBundles})
167-
.then(tasks => tasksWatch({project, tasks, watch}));
227+
const config = cli.flags.config ? JSON.parse(fs.readFileSync(path.resolve(cli.flags.config), 'utf8')) : {};
228+
let {src, main, modules, watch, verbose, clean, local, skipBundles, buildEs2015} = config;
229+
const project = cli.flags.project || src;
230+
main = cli.flags.main || main || 'index.ts';
231+
verbose = cli.flags.verbose || verbose;
232+
watch = cli.flags.watch || watch;
233+
clean = cli.flags.clean || clean;
234+
skipBundles = cli.flags.skipBundles || skipBundles;
235+
buildEs2015 = cli.flags.buildEs2015 || buildEs2015;
236+
const modulePaths = modules ? modules.map(module => module.src) : [];
237+
238+
if (!project && !modulePaths) {
239+
console.error('Please provide path to your projects source folder, `-p DIR` or specify `src` or `modules` in config');
240+
process.exit(1);
241+
}
242+
243+
if (modulePaths.length) {
244+
const commands = modules.map((module: any) => {
245+
return {
246+
title: `Build ${module.src}`,
247+
task: () => buildCommand({project: module.src, verbose, clean, local, main: module.entryPoints, watch, skipBundles, buildEs2015})
248+
}
249+
});
250+
251+
const taskQueue = new Listr(commands, {renderer: verbose ? 'verbose' : 'default'});
252+
return tasksWatch({project: modulePaths, taskQueue, watch, paths: modulePaths});
253+
}
254+
255+
return buildCommand({project, verbose, clean, local, main, watch, skipBundles, buildEs2015})
256+
.then(taskQueue => tasksWatch({project, taskQueue, watch, paths: null}));
168257
}

0 commit comments

Comments
 (0)