-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
111 lines (91 loc) · 2.87 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict';
/* eslint-env node */
const path = require('path');
const BroccoliFilter = require('broccoli-persistent-filter');
const md5Hex = require('md5-hex');
const { transformImports, createImportWarning } = require('./lib/utils');
const {
transformOctaneImports,
hasOctaneImports,
} = require('./lib/octane-utils');
let usingStylesImport = false;
try {
usingStylesImport = !!require.resolve('ember-template-styles-import');
} catch (e) {
// noop
}
class TemplateImportProcessor extends BroccoliFilter {
constructor(inputNode, options = {}) {
if (!options.hasOwnProperty('persist')) {
options.persist = true;
}
super(inputNode, {
annotation: options.annotation,
persist: options.persist,
});
this.options = options;
this._console = this.options.console || console;
this.extensions = ['hbs', 'handlebars'];
this.targetExtension = 'hbs';
}
baseDir() {
return __dirname;
}
cacheKeyProcessString(string, relativePath) {
return md5Hex([string, relativePath]);
}
processString(contents, relativePath) {
if (hasOctaneImports(contents)) {
contents = transformOctaneImports(contents, relativePath);
}
const { imports, rewrittenContents } = transformImports(
contents,
relativePath,
this.options.root,
usingStylesImport
);
let header = imports
.map(({ importPath, localName, isLocalNameValid }) => {
const warn = createImportWarning(
relativePath,
localName,
isLocalNameValid,
this._console
);
let componentName = `(component '${importPath}')`;
return `${warn}{{#let ${componentName} as |${localName}|}}`;
})
.join('');
let footer = imports.map(() => `{{/let}}`).join('');
let result = header + rewrittenContents + footer;
return result;
}
}
module.exports = {
name: require('./package').name,
setupPreprocessorRegistry(type, registry) {
// this is called before init, so, we need to check podModulePrefix later (in toTree)
let componentsRoot = null;
const projectConfig = this.project.config();
const podModulePrefix = projectConfig.podModulePrefix;
// by default `ember g component foo-bar --pod`
// will create app/components/foo-bar/{component.js,template.hbs}
// so, we can handle this case and just fallback to 'app/components'
if (podModulePrefix === undefined) {
componentsRoot = path.join(this.project.root, 'app', 'components');
} else {
componentsRoot = path.join(this.project.root, podModulePrefix);
}
registry.add('template', {
name: 'ember-template-component-import',
ext: 'hbs',
toTree: (tree) => {
tree = new TemplateImportProcessor(tree, { root: componentsRoot });
return tree;
},
});
if (type === 'parent') {
this.parentRegistry = registry;
}
},
};