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 globby = require("globby");
|
8 | var sysUtils = require("systemjs-builder/lib/utils.js");
|
9 | var utils = require("./utils");
|
10 | var Builder = require("systemjs-builder");
|
11 | function bundle(cfg) {
|
12 | var baseURL = path.resolve(cfg.baseURL);
|
13 | var builder = new Builder(cfg.baseURL, cfg.configPath);
|
14 | builder.config(cfg.builderCfg);
|
15 | var output = generateOutput(baseURL, cfg.includes, builder);
|
16 | var outputFileName = getOutputFileName(baseURL, cfg.bundleName, output, (cfg.options && cfg.options.rev));
|
17 | if (fs.existsSync(outputFileName)) {
|
18 | if (!cfg.force) {
|
19 | throw new Error("A bundle named '" + outputFileName + "' already exists. Use the --force option to overwrite it.");
|
20 | }
|
21 | fs.unlinkSync(outputFileName);
|
22 | }
|
23 | fs.writeFileSync(outputFileName, output);
|
24 | if (cfg.options && cfg.options.inject) {
|
25 | injectLink(outputFileName, baseURL, cfg.options.inject);
|
26 | }
|
27 | return Promise.resolve();
|
28 | }
|
29 | exports.bundle = bundle;
|
30 | function generateOutput(baseURL, includes, builder) {
|
31 | var templates = [];
|
32 | globby
|
33 | .sync(includes, {
|
34 | cwd: baseURL.replace(/\\/g, '/')
|
35 | })
|
36 | .forEach(function (file) {
|
37 | if (file !== '.') {
|
38 | file = path.resolve(baseURL, file);
|
39 | var content = fs.readFileSync(file, {
|
40 | encoding: 'utf8'
|
41 | });
|
42 | var $ = cheerio.load(content);
|
43 | var name_1 = getCanonicalName(builder, file, 'view').replace(/!view$/g, '');
|
44 | $('template').attr('id', name_1);
|
45 | var template = $.html('template');
|
46 | templates.push(template);
|
47 | }
|
48 | });
|
49 | return templates.join('\n');
|
50 | }
|
51 | exports.generateOutput = generateOutput;
|
52 | function getOutputFileName(baseURL, bundleName, output, rev) {
|
53 | var outFileName = utils.getOutFileName(output, bundleName + '.html', rev);
|
54 | return path.resolve(baseURL, outFileName);
|
55 | }
|
56 | exports.getOutputFileName = getOutputFileName;
|
57 | function injectLink(outfile, baseURL, inject) {
|
58 | var bundleFile = path.resolve(baseURL, path.relative(baseURL, outfile));
|
59 | var indexFile = path.resolve(baseURL, inject.indexFile);
|
60 | var destFile = path.resolve(baseURL, inject.destFile);
|
61 | var relPath = path.relative(path.dirname(indexFile), path.dirname(bundleFile)).replace(/\\/g, '/');
|
62 | var link = createLink(bundleFile, relPath);
|
63 | addLink(link, indexFile, destFile);
|
64 | }
|
65 | function addLink(link, indexFile, destFile) {
|
66 | var content = fs.readFileSync(indexFile, {
|
67 | encoding: 'utf8'
|
68 | });
|
69 | var $ = cheerio.load(content);
|
70 | if ($('link[aurelia-view-bundle][href="' + link + '"]').length === 0) {
|
71 | $('head').append('<link aurelia-view-bundle rel="import" href="' + link + '">');
|
72 | }
|
73 | fs.writeFileSync(destFile, $.html());
|
74 | }
|
75 | function createLink(bundleFile, relPath) {
|
76 | if (!relPath.startsWith('.')) {
|
77 | return relPath ? './' + relPath + '/' + path.basename(bundleFile) : './' + path.basename(bundleFile);
|
78 | }
|
79 | else {
|
80 | return relPath + '/' + path.basename(bundleFile);
|
81 | }
|
82 | }
|
83 | function getCanonicalName(builder, file, pluginName) {
|
84 | return builder.getCanonicalName(sysUtils.toFileURL(file) + '!' + pluginName);
|
85 | }
|
86 |
|
\ | No newline at end of file |