UNPKG

2.15 kBJavaScriptView Raw
1const utils = require('./utils.js');
2const path = require('path');
3const fs = require('fs');
4
5const getPckg = (currPath = path.resolve(''))=>{
6 const pckg = path.join(currPath, 'package.json');
7 if(fs.existsSync(pckg)) return utils.require(pckg);
8 const info = path.parse(currPath);
9 if(info.root == info.dir) return {};
10 return getPckg(info.dir);
11};
12const pckg = getPckg();
13const packageOpts = pckg.vitreum || {};
14
15const defaultOpts = {
16 prod : undefined,
17 shared : ['./client'],
18 targets : [],
19 app : pckg.main || undefined,
20 dev : false,
21 embed : false,
22 static : false,
23 template : false,
24 babel : pckg.babel,
25 bundle : [],
26 packagesToTransform : [],
27 paths : {
28 build : './build',
29 code : 'bundle.js',
30 style : 'bundle.css',
31 render : 'render.js',
32 static : 'static.html',
33 libs : 'libs.js'
34 }
35}
36
37const validate = (opts)=>{
38 if(!opts.targets || !opts.targets.length) throw 'No build targets specified';
39 //if(!opts.app) throw `A app entry point has not been specified. Set 'main' in your package.json`;
40 if(!process.env.NODE_ENV) throw `NODE_ENV environment variable not set. If this is your development machine, we suggest setting it to 'local'`;
41
42 if(opts.packagesToTransform.length){
43 opts.bundle = opts.bundle.concat(opts.packagesToTransform
44 .map((packageName)=>`**/node_modules/${packageName}/**`));
45 }
46 return opts;
47}
48
49module.exports = (opts, targets)=>{
50 opts = Object.assign({}, defaultOpts, packageOpts, opts);
51 opts.paths = Object.assign({}, defaultOpts.paths, packageOpts.paths, opts.paths);
52 opts.targets = targets || opts.targets || packageOpts.targets || defaultOpts.targets;
53 opts.prod = (typeof opts.prod == 'undefined'
54 ? process.env.NODE_ENV.toLowerCase() == 'production'
55 : opts.prod
56 )
57
58 if(typeof opts.targets == 'string') opts.targets = [opts.targets];
59 opts.targets = opts.targets.map((target)=>path.resolve(process.cwd(), target));
60
61 if(typeof opts.template == 'string') opts.template = utils.require(opts.template);
62 if(!opts.template) opts.template = require('./templates/html.js');
63
64 if(opts.app) opts.app = path.resolve(process.cwd(), opts.app);
65 return validate(opts);
66}
\No newline at end of file