-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (62 loc) · 2.5 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
/* eslint-env node */
'use strict';
const metal = require('broccoli-metal');
const mergeTrees = require('broccoli-merge-trees');
const replace = require('broccoli-string-replace');
const stew = require('broccoli-stew');
const path = require('path');
module.exports = {
name: 'seamless-immutable',
importEmberStuff (tree) {
return metal(tree, (files) => {
var startIndex, endIndex, startDefine, endDefine;
Object.keys(files).forEach((key) => {
// tags obj as __defineNonEnumerable to avoid "object is not extensible" exception
startIndex = files[key].indexOf('addImmutabilityTag(obj)');
if (startIndex !== -1) {
endIndex = startIndex + 23;
var beforeIndex = files[key].slice(0, endIndex);
var afterIndex = files[key].slice(endIndex + 1, files[key].length - 1);
files[key] = `${beforeIndex}\n\naddPropertyTo(obj, '__defineNonEnumerable', function(){});\n\n${afterIndex}`;
}
// alter how seamless-immutable loads to play nice with ember-cli
startDefine = files[key].indexOf('define(function () {\n');
if (startDefine !== -1) {
endDefine = startDefine + 50;
var beforeDefine = files[key].slice(0, startDefine);
var afterDefine = files[key].slice(endDefine + 1, files[key].length);
files[key] = `define.amd=true;${beforeDefine}\n\nexports['default'] = Immutable;return Immutable;\n\n${afterDefine}\n\n`;
}
});
});
},
treeForAddon (tree) {
const seamlessPath = path.dirname(require.resolve('seamless-immutable/src/seamless-immutable.js'));
const theTree = stew.rename(seamlessPath, function(relativePath) {
return relativePath.replace('seamless-immutable.js', 'index.js');
});
const denodifiedSeamless = replace(theTree, {
files: [ '**/*.js' ],
patterns: [
{
match: /process\.env\.NODE_ENV/g,
replacement: JSON.stringify(process.env.EMBER_ENV)
}
]
});
let addon = this.addons.find(addon => addon.name === 'ember-cli-babel');
let seamlessTree = addon.transpileTree(denodifiedSeamless, {
'ember-cli-babel': {
compileModules: false
}
});
const seamlessTreeWithEmberMagic = this.importEmberStuff(seamlessTree);
if (!tree) {
return this._super.treeForAddon.call(this, seamlessTreeWithEmberMagic);
}
const trees = mergeTrees([seamlessTreeWithEmberMagic, tree], {
overwrite: true
});
return this._super.treeForAddon.call(this, trees);
}
};