UNPKG

2.38 kBJavaScriptView Raw
1/* eslint-disable prettier/prettier */
2'use strict';
3
4const path = require('path');
5const semver = require('semver');
6const funnel = require('broccoli-funnel');
7const stringReplace = require('broccoli-string-replace');
8const UnwatchedDir = require('broccoli-source').UnwatchedDir;
9
10module.exports = {
11 name: require('./package').name,
12
13 included() {
14 this._super.included.apply(this, arguments);
15
16 this.import('vendor/showdown.js', {
17 using: [{ transformation: 'amd', as: 'showdown' }]
18 });
19 },
20
21 findModulePath(basedir) {
22 try {
23 let resolve = require('resolve');
24
25 return path.dirname(resolve.sync('showdown', { basedir: basedir }));
26 } catch (_) {
27 try {
28 return path.dirname(require.resolve('showdown'));
29 } catch (e) {
30 if (e.code === 'MODULE_NOT_FOUND') {
31 this.ui.writeLine(
32 `ember-cli-showdown: showdown not installed, be sure you have showdown installed via npm/yarn.`
33 );
34 return;
35 }
36
37 throw e;
38 }
39 }
40 },
41
42 removeSourcemapAnnotation(node) {
43 return stringReplace(node, {
44 files: ['showdown.js'],
45 annotation: 'Remove sourcemap annotation (showdown)',
46 patterns: [
47 {
48 match: /\/\/# sourceMappingURL=showdown.js.map/g,
49 replacement: ''
50 }
51 ]
52 });
53 },
54
55 treeForVendor() {
56 let modulePath = this.findModulePath(this.project.root);
57
58 if (modulePath) {
59 let showdownTree = funnel(new UnwatchedDir(modulePath), {
60 include: ['showdown.js']
61 });
62
63 let pkg = require(path.join(modulePath, '..', 'package.json'));
64
65 if (pkg.version && semver.gt(pkg.version, '1.7.4')) {
66 return this.removeSourcemapAnnotation(showdownTree);
67 }
68
69 /*
70 * The stringReplace forces showdown's loader to use define.amd vs. commonjs/node.
71 * This allows us to use the vendored copy of showdown when the app is eval'd within node (fastboot).
72 * https://github.com/showdownjs/showdown/blob/5d2016c0c1fa2bd722e45b952f1e446c3c870d0f/src/loader.js#L4
73 */
74 return this.removeSourcemapAnnotation(
75 stringReplace(showdownTree, {
76 files: ['showdown.js'],
77 patterns: [
78 {
79 match: /typeof module !== 'undefined'/g,
80 replacement: 'false'
81 }
82 ]
83 })
84 );
85 }
86 }
87};