1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var Registry = require('./');
|
5 | var relativeRequire = require('process-relative-require');
|
6 | var debug = require('debug')('ember-cli:preprocessors');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function setupRegistryForEachAddon(registry, parent) {
|
18 | parent.initializeAddons();
|
19 | var addons = parent.addons || (parent.project && parent.project.addons);
|
20 |
|
21 | if (!addons) {
|
22 | return;
|
23 | }
|
24 |
|
25 | addons.forEach(function(addon) {
|
26 | if (addon.setupPreprocessorRegistry) {
|
27 | addon.setupPreprocessorRegistry('parent', registry);
|
28 | }
|
29 | });
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | module.exports.setupRegistry = function(appOrAddon) {
|
42 | var registry = appOrAddon.registry;
|
43 | if (appOrAddon.setupPreprocessorRegistry) {
|
44 | appOrAddon.setupPreprocessorRegistry('self', registry);
|
45 | }
|
46 | setupRegistryForEachAddon(registry, appOrAddon);
|
47 |
|
48 | addLegacyPreprocessors(registry);
|
49 | };
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | module.exports.defaultRegistry = function(app) {
|
60 | var registry = new Registry(app.dependencies(), app);
|
61 |
|
62 | return registry;
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function addLegacyPreprocessors(registry) {
|
74 | registry.add('minify-css', 'broccoli-csso', null);
|
75 |
|
76 | registry.add('template', 'broccoli-emblem-compiler', ['embl', 'emblem']);
|
77 | registry.add('template', 'broccoli-ember-hbs-template-compiler', ['hbs', 'handlebars']);
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | module.exports.isType = function(file, type, registryOwner) {
|
98 | var extension = path.extname(file).replace('.', '');
|
99 |
|
100 | if (extension === type) { return true; }
|
101 |
|
102 | if (registryOwner.registry.extensionsForType(type).indexOf(extension) > -1) {
|
103 | return true;
|
104 | }
|
105 | };
|
106 |
|
107 | module.exports.preprocessMinifyCss = function(tree, options) {
|
108 | var plugins = options.registry.load('minify-css');
|
109 |
|
110 | if (plugins.length === 0) {
|
111 | var CleanCSS = require('broccoli-clean-css');
|
112 | return new CleanCSS(tree, options);
|
113 | } else if (plugins.length > 1) {
|
114 | throw new Error('You cannot use more than one minify-css plugin at once.');
|
115 | }
|
116 |
|
117 | var plugin = plugins[0];
|
118 |
|
119 | try {
|
120 | return relativeRequire(plugin.name).call(null, tree, options);
|
121 | } catch (error) {
|
122 | if (error && error.code === 'MODULE_NOT_FOUND') {
|
123 | return processPlugins(plugins, arguments);
|
124 | }
|
125 |
|
126 | throw error;
|
127 | }
|
128 | };
|
129 |
|
130 | module.exports.preprocessCss = function(tree, inputPath, outputPath, options) {
|
131 | var plugins = options.registry.load('css');
|
132 |
|
133 | if (plugins.length === 0) {
|
134 | var Funnel = require('broccoli-funnel');
|
135 |
|
136 | return new Funnel(tree, {
|
137 | srcDir: inputPath,
|
138 |
|
139 | getDestinationPath: function(relativePath) {
|
140 | if (options.outputPaths) {
|
141 |
|
142 |
|
143 | var path = relativePath.replace(/\.css$/, '');
|
144 |
|
145 |
|
146 | if (options.outputPaths[path]) {
|
147 | return options.outputPaths[path];
|
148 | }
|
149 | }
|
150 |
|
151 | return outputPath + '/' + relativePath;
|
152 | }
|
153 | });
|
154 | }
|
155 |
|
156 | return processPlugins(plugins, arguments);
|
157 | };
|
158 |
|
159 | module.exports.preprocessTemplates = function(/* tree */) {
|
160 | var options = arguments[arguments.length - 1];
|
161 | var plugins = options.registry.load('template');
|
162 |
|
163 | debug('plugins found for templates: %s', plugins.map(function(p) { return p.name; }));
|
164 |
|
165 | if (plugins.length === 0) {
|
166 | throw new Error('Missing template processor');
|
167 | }
|
168 |
|
169 | return processPlugins(plugins, arguments);
|
170 | };
|
171 |
|
172 | module.exports.preprocessJs = function(/* tree, inputPath, outputPath, options */) {
|
173 | var options = arguments[arguments.length - 1];
|
174 | var plugins = options.registry.load('js');
|
175 | var tree = arguments[0];
|
176 |
|
177 | if (plugins.length === 0) { return tree; }
|
178 |
|
179 | return processPlugins(plugins, arguments);
|
180 | };
|
181 |
|
182 | function processPlugins(plugins, args) {
|
183 | args = Array.prototype.slice.call(args);
|
184 | var tree = args.shift();
|
185 |
|
186 | plugins.forEach(function(plugin) {
|
187 | debug('processing %s', plugin.name);
|
188 | tree = plugin.toTree.apply(plugin, [tree].concat(args));
|
189 | });
|
190 |
|
191 | return tree;
|
192 | }
|