-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
79 lines (67 loc) · 2.38 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
'use strict';
const ElmCompiler = require("./broccoli-elm");
const BroccoliMergeTrees = require("broccoli-merge-trees");
var defaultOptions = {
includePaths: []
};
class ElmPlugin {
constructor(options) {
this.name = "ember-elm";
this.ext = "elm";
this.options = Object.assign(
{},
defaultOptions,
options
);
}
toTree(tree, inputPath, outputPath) {
// Ignore elm outside of the main app, so we don't try to compile tests.
if (inputPath != "/") {
return tree;
}
var elmTargets = [tree];
if (this.options.includePaths.length > 0) {
elmTargets = this.options.includePaths;
}
var elmOptions = Object.assign({}, this.options);
delete elmOptions.includePaths;
// tree.destDir is undefined when tree is a BroccoliMergeTree.
// So we use outputPath instead, which seems to work.
const destDir = tree.destDir || outputPath;
const jsTree = tree;
const elmTree = new ElmCompiler(elmTargets, destDir, elmOptions);
return new BroccoliMergeTrees([jsTree, elmTree]);
}
}
/**
* @extends Addon
*
* See https://ember-cli.com/api/classes/Addon.html.
*/
module.exports = {
name: require('./package').name,
setupPreprocessorRegistry(type, registry) {
// No need to try to compile elm within the addon
if (type == "parent") {
let app = this.app || this;
let options = app.options || {};
let elmOptions = options.elm || {};
// We need to make sure ember-cli-babel runs after the elm compilation
// because it wraps the compilation output in the AMD module loader.
// Likely all preprocessors need to run after elm compilation , so we move them all to the back to the line.
// WARNING: in some projects ember-cli-babel gets added to the js registry
// by default, giving the impression this isn't needed. These lines are for projects that aren't so lucky.
let existingPreprocessors = registry.registeredForType('js');
// registeredForType returns the internal copy, that would get mutated
// by the lines below.
let preprocessorsBeforeElm = existingPreprocessors.slice(0);
preprocessorsBeforeElm.forEach(preprocessor =>
registry.remove('js', preprocessor)
);
registry.add("js", new ElmPlugin(elmOptions));
preprocessorsBeforeElm.forEach(preprocessor =>
registry.add('js', preprocessor)
);
}
}
};