1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Promise = require("bluebird");
|
4 | var cheerio = require("cheerio");
|
5 | var fs = require("fs");
|
6 | var path = require("path");
|
7 | var config_serializer_1 = require("./config-serializer");
|
8 | var utils_1 = require("./utils");
|
9 | function unbundle(cfg) {
|
10 | var config = utils_1.ensureDefaults(cfg);
|
11 | utils_1.validateConfig(config);
|
12 | var tasks = [
|
13 | removeBundles(config),
|
14 | removeHtmlImportBundles(config)
|
15 | ];
|
16 | return Promise.all(tasks);
|
17 | }
|
18 | exports.unbundle = unbundle;
|
19 | function removeBundles(cfg) {
|
20 | var configPath = cfg.injectionConfigPath;
|
21 | var appCfg = config_serializer_1.getAppConfig(configPath);
|
22 | delete appCfg.bundles;
|
23 | delete appCfg.depCache;
|
24 | config_serializer_1.saveAppConfig(configPath, appCfg);
|
25 | return Promise.resolve();
|
26 | }
|
27 | function removeHtmlImportBundles(config) {
|
28 | var tasks = [];
|
29 | Object
|
30 | .keys(config.bundles)
|
31 | .forEach(function (key) {
|
32 | var cfg = config.bundles[key];
|
33 | if (cfg.htmlimport) {
|
34 | tasks.push(_removeHtmlImportBundle(utils_1.getHtmlImportBundleConfig(cfg, key, config)));
|
35 | }
|
36 | });
|
37 | return Promise.all(tasks);
|
38 | }
|
39 | function _removeHtmlImportBundle(cfg) {
|
40 | var inject = cfg.options.inject;
|
41 | var file = path.resolve(cfg.baseURL, inject.destFile);
|
42 | if (!fs.existsSync(file)) {
|
43 | return Promise.resolve();
|
44 | }
|
45 | return Promise
|
46 | .promisify(fs.readFile)(file, {
|
47 | encoding: 'utf8'
|
48 | })
|
49 | .then(function (content) {
|
50 | var $ = cheerio.load(content);
|
51 | return Promise.resolve($);
|
52 | })
|
53 | .then(function ($) {
|
54 | return removeLinkInjections($);
|
55 | })
|
56 | .then(function ($) {
|
57 | return Promise.promisify(fs.writeFile)(file, $.html());
|
58 | });
|
59 | }
|
60 | function removeLinkInjections($) {
|
61 | $('link[aurelia-view-bundle]').remove();
|
62 | return Promise.resolve($);
|
63 | }
|
64 |
|
\ | No newline at end of file |