-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
65 lines (59 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* jshint node: true */
"use strict";
let path = require('path');
let uglify = require('uglify-js');
let DeployPluginBase = require('ember-cli-deploy-plugin');
let fs = require('fs');
let minimatch = require('minimatch');
module.exports = {
name: 'ember-cli-deploy-appshell',
included(app) {
this.app = app;
},
contentFor(type, config) {
if (type === 'head') {
let loader = fs.readFileSync(require.resolve('loader.js'), 'utf8');
let src = fs.readFileSync(path.join(__dirname, 'lib', 'bootloader.js'), 'utf8').replace(/MODULE_PREFIX/g, config.modulePrefix);
let bootloader = uglify.minify(loader + src, { fromString: true, mangle:true, compress: true}).code;
this.baseURL = config.baseURL;
return `<script type="text/javascript">${ bootloader }</script>`;
}
},
createDeployPlugin: function(options) {
let baseURL = () => this.baseURL;
let DeployPlugin = DeployPluginBase.extend({
name: options.name,
didBuild: function(context) {
let distDir = context.distDir;
let files = context.distFiles;
fs.writeFileSync(path.join(distDir, 'manifest.appcache'), this.renderManifest(files));
let indexHTML = fs.readFileSync(path.join(distDir, 'index.html'), 'utf8');
fs.writeFileSync(path.join(distDir, 'appshell.html'), indexHTML);
context.distFiles.push('appshell.html');
fs.writeFileSync(path.join(distDir, 'index.html'), indexHTML.replace(/<html>/i, `<html manifest=${baseURL()}manifest.appcache>`));
},
renderManifest: function(paths) {
let excludePattern = this.readConfig('excludePattern');
let prefixDomains = this.readConfig('prefixDomains');
let outputPaths = paths.filter(function(p){ return !minimatch(p, excludePattern); })
.map(function(p) {
let domain = Object.keys(prefixDomains).find(function(domain) {
return minimatch(p, prefixDomains[domain]);
});
if (domain) {
return domain + p;
} else {
return p;
}
});
outputPaths = outputPaths.concat(this.readConfig('externalDependencies'));
return `CACHE MANIFEST
# ${new Date()}
${ outputPaths.join("\n") }
NETWORK:
*`;
}
});
return new DeployPlugin();
}
};