This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
116 lines (93 loc) · 3.54 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
const path = require('path');
const MergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const ElementBundler = require('./lib/bundler');
const ElementWriter = require('./lib/writer');
const Config = require('./lib/config');
const { scrapeDeps } = require('./lib/scraper');
const extractDeps = require('./lib/extractor');
const fs = require('fs-extra');
module.exports = {
name: require('./package').name,
isDevelopingAddon() {
return true;
},
included(appOrAddon) {
this._super.included.apply(this, arguments);
this._app = appOrAddon.app || appOrAddon;
this.options = new Config(this._app, this.ui);
this.importPolyfills();
},
importPolyfills() {
const { polyfillBundle } = this.options;
const webcomponentsjsPath = path.join(this._app.bowerDirectory, 'webcomponentsjs');
const webcomponentsjsPolyfill = path.join(webcomponentsjsPath, `webcomponents-${polyfillBundle}.js`);
const customElementsEs5Adapter = path.join(__dirname, 'polyfills/native-shim.js');
this._app.import(customElementsEs5Adapter, { options: 'prepend' });
this._app.import(webcomponentsjsPolyfill, { options: 'prepend' });
},
// insert polyfills and bundled elements
contentFor(type, config) {
if (type !== 'head') {
return null;
}
const headContents = [];
const { globalPolymerSettings } = this.options;
if (globalPolymerSettings) {
headContents.push(`<script>window.Polymer = ${JSON.stringify(globalPolymerSettings)};</script>`);
}
headContents.push(this.getBundleImport(config));
return headContents.join('\n');
},
getBundleImport(config) {
const { bundlerOutput, useRelativePath, lazyImport } = this.options;
const href = useRelativePath ? bundlerOutput : path.join(config.rootURL, bundlerOutput);
const rel = lazyImport ? 'lazy-import' : 'import';
const htmlImport = `<link rel="${rel}" href="${href}">`;
return htmlImport;
},
getImportPaths() {
const bowerPath = path.join(this.options.projectRoot, this.project.bowerDirectory);
const packages = scrapeDeps(this.project.bowerDependencies(), bowerPath, 'bower.json');
const notExcluded = (pkg) => !this.options.excludeElements.includes(pkg.name);
const importPaths = packages.filter(notExcluded).map((pkg) => pkg.elementPath);
const manualImportPaths = extractDeps(this.options.htmlImportsFile);
return importPaths.concat(manualImportPaths);
},
postprocessTree(type, tree) {
if (type !== 'all') {
return tree;
}
const importPaths = this.getImportPaths();
const bowerPath = path.join(this.options.projectRoot, this.project.bowerDirectory);
const elementsTree = new Funnel(new MergeTrees([bowerPath, ...this.options.elementPaths]));
const filepath = path.basename(this.options.htmlImportsFile);
const buildForProduction = Object.assign({}, this.options.buildForProduction, {
allImportsFile: this.options.allImportsFile,
tmpDestPath: this.options.tempPolymerBuildOutputPath
});
const writer = new ElementWriter(
elementsTree,
importPaths,
filepath,
buildForProduction
);
const bundler = new ElementBundler(writer, {
input: filepath,
output: this.options.bundlerOutput,
autoprefixer: this.options.autoprefixer,
buildForProduction
}, this.options.bundlerOptions);
return new MergeTrees([tree, bundler], {
overwrite: true,
annotation: 'Merge (ember-cli-polymer-bundler merge bundler with addon tree)'
});
},
postBuild() {
if (this.options.buildForProduction.enabled) {
fs.removeSync(this.options.tempPolymerBuildOutputPath);
fs.removeSync(this.options.allImportsFile);
}
}
};