1 | import * as _ from 'lodash';
|
2 | import * as revPath from 'rev-path';
|
3 | import * as revHash from 'rev-hash';
|
4 | import * as fs from 'fs';
|
5 | import * as path from 'path';
|
6 | import {Config, BundleConfig, ConfigBody, ConfigHeader} from './models';
|
7 |
|
8 | export function getOutFileName(source: string, fileName: string, rev: boolean) {
|
9 | return rev ? revPath(fileName, revHash(new Buffer(source, 'utf-8'))) : fileName;
|
10 | }
|
11 |
|
12 | export function validateConfig(config: Config) {
|
13 | if (!fs.existsSync(config.baseURL)) {
|
14 | throw new Error(
|
15 | `Path '${path.resolve(config.baseURL)}' does not exist. Please provide a valid 'baseURL' in your bundle configuration.`);
|
16 | }
|
17 | let configPaths: string[] = [];
|
18 | let configPath = config.configPath;
|
19 |
|
20 | if (typeof configPath === 'string') {
|
21 | configPaths.push(configPath);
|
22 | } else {
|
23 | configPath.forEach(p => configPaths.push(p));
|
24 | }
|
25 |
|
26 | configPaths.forEach(p => {
|
27 | if (!fs.existsSync(p)) {
|
28 | throw new Error(
|
29 | `File '${path.resolve(p)}' was not found! Please provide a valid 'config.js' file for use during bundling.`);
|
30 | }
|
31 | });
|
32 | }
|
33 |
|
34 | export function getHTMLMinOpts(opts: any) {
|
35 | return _.defaultsDeep(opts, {
|
36 | caseSensitive: true,
|
37 | collapseBooleanAttributes: true,
|
38 | collapseWhitespace: true,
|
39 | conservativeCollapse: true,
|
40 | removeCDATASectionsFromCDATA: true,
|
41 | removeComments: true,
|
42 | removeCommentsFromCDATA: true,
|
43 | removeEmptyAttributes: true,
|
44 | removeRedundantAttributes: false,
|
45 | removeScriptTypeAttributes: true,
|
46 | removeStyleLinkTypeAttributes: true,
|
47 | ignoreCustomFragments: [/\${[\s\S]*?}/],
|
48 | useShortDoctype: true,
|
49 | minifyCSS: true,
|
50 | minifyJS: true
|
51 | });
|
52 | }
|
53 |
|
54 | export function getCSSMinOpts(opts: any) {
|
55 | return _.defaultsDeep(opts, {
|
56 | advanced: true,
|
57 | agressiveMerging: true,
|
58 | mediaMerging: true,
|
59 | restructuring: true,
|
60 | shorthandCompacting: true,
|
61 | });
|
62 | }
|
63 |
|
64 | export function getBundleConfig(bundleCfg: ConfigBody, bundleName: string, config: Config) {
|
65 | return _.defaultsDeep<ConfigBody, BundleConfig>(bundleCfg, {
|
66 | baseURL: config.baseURL,
|
67 | builderCfg: config.builderCfg,
|
68 | bundleName: bundleName,
|
69 | configPath: config.configPath,
|
70 | excludes: [],
|
71 | includes: [],
|
72 | outputPath: config.outputPath,
|
73 | injectionConfigPath: config.injectionConfigPath,
|
74 | force: config.force,
|
75 | options: {
|
76 | depCache: false,
|
77 | cssminopts: {},
|
78 | htmlminopts: {},
|
79 | inject: true,
|
80 | minify: false,
|
81 | rev: false,
|
82 | },
|
83 | });
|
84 | }
|
85 |
|
86 | export function getHtmlImportBundleConfig(bundleCfg: ConfigBody, bundleName: string, config: ConfigHeader) {
|
87 | let cfg = _.defaultsDeep<ConfigBody, BundleConfig>(bundleCfg, {
|
88 | htmlimport: true,
|
89 | includes: '*.html',
|
90 | bundleName: bundleName,
|
91 | options: {
|
92 | inject: false
|
93 | },
|
94 | force: config.force,
|
95 | baseURL: config.baseURL,
|
96 | configPath: config.configPath,
|
97 | builderCfg: config.builderCfg
|
98 | });
|
99 |
|
100 | if (!cfg.options.inject) {
|
101 | return cfg;
|
102 | }
|
103 |
|
104 | if (typeof cfg.options.inject === 'boolean') {
|
105 | cfg.options.inject = {
|
106 | indexFile: 'index.html',
|
107 | destFile: 'index.html'
|
108 | };
|
109 | } else {
|
110 | cfg.options.inject.indexFile = cfg.options.inject.indexFile || 'index.html';
|
111 | cfg.options.inject.destFile = cfg.options.inject.destFile || 'index.html';
|
112 | }
|
113 |
|
114 | return cfg;
|
115 | }
|
116 |
|
117 | export function ensureDefaults(config: Config) {
|
118 | return _.defaults<Config>(config, {
|
119 | baseURL: '.',
|
120 | builderCfg: {},
|
121 | bundles: {},
|
122 | configPath: './config.js',
|
123 | force: false,
|
124 | injectionConfigPath: getDefaultInjectionConfigFilePath(config.configPath),
|
125 | });
|
126 | }
|
127 |
|
128 | function getDefaultInjectionConfigFilePath(configPath: string|string[]) {
|
129 | if (typeof configPath === 'string') {
|
130 | return configPath;
|
131 | }
|
132 |
|
133 | if (Array.isArray(configPath)) {
|
134 | return configPath[0];
|
135 | }
|
136 | throw new Error(
|
137 | 'No bundle injection config file path provided. Set `injectionConfigPath` property in the bundle config.');
|
138 | }
|