-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
124 lines (99 loc) · 3.64 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
112
113
114
115
116
117
118
119
120
121
122
123
124
var path = require('path');
var merge = require('merge');
var compileCompass = require('broccoli-compass-compiler');
var checker = require('ember-cli-version-checker');
var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
var glob = require('glob');
function CompassCompilerPlugin(optionsFn) {
this.name = 'ember-cli-compass-compiler';
this.ext = ['scss', 'sass'];
this.optionsFn = optionsFn;
}
CompassCompilerPlugin.prototype.toTree = function(tree, inputPath, outputPath, inputOptions) {
var removeLeadingSlash = function(string) {
return (string.length > 0 && string[0] === '/') ? string.slice(1) : string;
};
// Normalize paths
inputPath = removeLeadingSlash(inputPath);
outputPath = removeLeadingSlash(outputPath);
// Merge default options with supplied options
var defaultOptions = {
outputStyle: 'compressed',
compassCommand: 'compass',
importPath: [],
getCssDir: function(outputDir) {
return '"' + path.join(outputDir, outputPath) + '"';
}
};
var compassOptions = merge({}, defaultOptions, this.optionsFn(), inputOptions);
var outputPaths = compassOptions.outputPaths;
var trees = Object.keys(outputPaths).map(function(file) {
// Watch inputTrees and compassOptions.importPath directories
var inputTrees = [tree];
// Define getSassDir function if not overridden by user
if (!compassOptions.getSassDir) {
compassOptions.getSassDir = function(inputTrees, inputPaths) {
return inputPaths.reduce(function(pathFound, currentPath) {
// Return early if input path is already found
if (pathFound !== '') {
return pathFound;
}
// Filter pattern for .sass or .scss files
var pattern = path.join(currentPath, inputPath, file + '.s@(a|c)ss');
var result = glob.sync(pattern);
// Found the file we're looking for
if (result.length > 0) {
return path.dirname(result[0]);
}
return '';
}, '');
};
}
// Compile
var compassTree = compileCompass(inputTrees, compassOptions);
// Rename and move files
var extension = path.extname(outputPaths[file]); // compiled asset extension
var fileName = file + extension; // current file name
var outputFileName = path.basename(outputPaths[file], extension) + extension; // new name for asset
var outputDir = removeLeadingSlash(path.dirname(outputPaths[file])); // new directory for asset
var node = new Funnel(compassTree, {
srcDir: outputPath,
destDir: outputDir,
files: [fileName],
getDestinationPath: function(relativePath) {
if (relativePath === fileName) {
return outputFileName;
}
return relativePath;
}
});
return node;
});
return mergeTrees(trees);
};
module.exports = {
name: 'Ember CLI Compass Compiler',
shouldSetupRegistryInIncluded: function() {
return !checker.isAbove(this, '0.2.0');
},
setupPreprocessorRegistry: function(type, registry) {
registry.add('css', new CompassCompilerPlugin(this.compassOptions.bind(this)));
// prevent conflict with broccoli-sass if it's installed
if (registry.remove) registry.remove('css', 'broccoli-sass');
},
included: function(app) {
this.app = app;
this._super.included.apply(this, arguments);
if (this.shouldSetupRegistryInIncluded()) {
this.setupPreprocessorRegistry('parent', app.registry);
}
},
compassOptions: function () {
var app = this.app;
if (app && !app.options && app.app) {
app = app.app;
}
return (app && app.options && app.options.compassOptions) || {};
}
};