This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
86 lines (73 loc) · 3.03 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
/* eslint-env node */
'use strict';
const path = require('path');
// broccoli plugins
const MergeTrees = require('broccoli-merge-trees');
const ElementBundler = require('./lib/bundler');
const ElementWriter = require('./lib/writer');
// internals
const Config = require('./lib/config');
const { scrapeDeps } = require('./lib/scraper');
const extractDeps = require('./lib/extractor');
module.exports = {
name: 'ember-polymer',
included(appOrAddon) {
this._super.included.apply(this, arguments);
// config
let app = appOrAddon.app || appOrAddon;
this.options = new Config(app, this.ui);
// import webcomponentsjs polyfill library
if (this.options.polyfillBundle && this.options.polyfillBundle !== 'none') {
app.import(`${app.bowerDirectory}/webcomponentsjs/webcomponents-${this.options.polyfillBundle}.js`);
}
},
// insert polymer and bundled elements
contentFor(type, config) {
if (type === 'head') {
let href = path.join(config.rootURL, this.options.bundlerOutput);
let content = `<link rel="import" href="${href}">`;
if (this.options.globalPolymerSettings) {
let settings = JSON.stringify(this.options.globalPolymerSettings);
content += `<script> window.Polymer = ${settings}; </script>`
}
return content;
}
},
postprocessTree(type, tree) {
if (type !== 'all') {
return tree;
}
// auto element import
let bowerPath = path.join(this.options.projectRoot,
this.project.bowerDirectory);
let bowerPackages = scrapeDeps(this.project.bowerDependencies(),
bowerPath, 'bower.json');
let npmPackages = scrapeDeps(this.project.dependencies(),
path.resolve('node_modules'), 'package.json');
let packages = bowerPackages.concat(npmPackages);
let exclude = pkg => !this.options.excludeElements.includes(pkg.name);
let elementPaths = packages.filter(exclude).map(pkg => pkg.elementPath);
// manual element import
let manualPackagePaths = extractDeps(this.options.htmlImportsFile);
elementPaths = elementPaths.concat(manualPackagePaths);
// check for duplicates
elementPaths.filter((ep, i, eps) => eps.includes(ep, i + 1)).forEach(ep => {
let relativePath = path.relative(this.options.projectRoot, ep);
this.ui.writeInfoLine(`The html import \`${relativePath}\` was already ` +
`automatically imported ✨ You can remove this ` +
`import. (ember-polymer)`);
});
// write and bundle
let filepath = path.basename(this.options.htmlImportsFile);
let writer = new ElementWriter(elementPaths, filepath);
let bundler = new ElementBundler(writer, {
input: filepath,
output: this.options.bundlerOutput
}, this.options.bundlerOptions);
// merge normal tree and our bundler tree
return new MergeTrees([ tree, bundler ], {
overwrite: true,
annotation: 'Merge (ember-polymer merge bundler with addon tree)'
});
}
};