/
index.js
92 lines (77 loc) · 2.48 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
/* eslint-env node */
'use strict';
const path = require('path');
const funnel = require('broccoli-funnel');
const stringReplace = require('broccoli-string-replace');
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
module.exports = {
name: 'ember-cli-showdown',
included() {
this._super.included.apply(this, arguments);
this.import('vendor/showdown.js', {
using: [{ transformation: 'amd', as: 'showdown' }]
});
},
findModulePath(basedir) {
try {
let resolve = require('resolve');
return path.dirname(resolve.sync('showdown', { basedir: basedir }));
} catch (_) {
try {
return path.dirname(require.resolve('showdown'));
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
this.ui.writeLine(
`ember-cli-showdown: showdown not installed, be sure you have showdown installed via npm/yarn.`
);
return;
}
throw e;
}
}
},
treeForVendor() {
let modulePath = this.findModulePath(this.project.root);
if (modulePath) {
let showdownTree = funnel(new UnwatchedDir(modulePath), {
include: ['showdown.js', 'showdown.js.map']
});
showdownTree = stringReplace(showdownTree, {
files: ['showdown.js'],
patterns: [
{
match: /\/\/# sourceMappingURL=showdown.js.map/g,
replacement: ''
}
]
});
let pkg = require(path.join(modulePath, '..', 'package.json'));
if (pkg) {
let version = pkg.version.split('.');
/* if version is >= 1.7.4, we do not need to remove 'typeof module !== 'undefined'' */
if (version.length >= 3) {
let major = parseInt(version[0], 10);
let minor = parseInt(version[1], 10);
let patch = parseInt(version[2], 10);
if (major >= 1 && minor >= 7 && patch >= 4) {
return showdownTree;
}
}
}
/*
* The stringReplace forces showdown's loader to use define.amd vs. commonjs/node.
* This allows us to use the vendored copy of showdown when the app is eval'd within node (fastboot).
* https://github.com/showdownjs/showdown/blob/5d2016c0c1fa2bd722e45b952f1e446c3c870d0f/src/loader.js#L4
*/
return stringReplace(showdownTree, {
files: ['showdown.js'],
patterns: [
{
match: /typeof module !== 'undefined'/g,
replacement: 'false'
}
]
});
}
}
};