-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
105 lines (88 loc) · 2.81 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
/* eslint-env node */
'use strict'
const path = require('path')
const merge = require('merge')
const mergeTrees = require('broccoli-merge-trees')
const PostcssFilter = require('broccoli-postcss')
const PostcssCompiler = require('broccoli-postcss-single')
function PostcssPlugin (addon) {
this.name = 'ember-cli-postcss'
this.addon = addon
this.ext = ['css', 'less', 'styl', 'scss', 'sass']
}
PostcssPlugin.prototype.toTree = function (tree, inputPath, outputPath, inputOptions) {
let inputTrees = [tree]
const defaultOptions = { enabled: true }
const options = merge.recursive(defaultOptions, this.addon._options.compile, inputOptions)
if (!options.enabled) {
return tree
}
if (options.includePaths) {
inputTrees = inputTrees.concat(options.includePaths)
}
const ext = options.extension || 'css'
const paths = options.outputPaths
const trees = Object.keys(paths).map((file) => {
const input = path.join(inputPath, `${file}.${ext}`)
const output = paths[file]
return new PostcssCompiler(inputTrees, input, output, options)
})
return mergeTrees(trees)
}
module.exports = {
name: 'ember-cli-postcss',
included (app) {
this._super.included.apply(this, arguments)
this._ensureThisImport()
const env = process.env.EMBER_ENV
const overrideBrowserslist = this.project.targets && this.project.targets.browsers
// Initialize options if none were passed
this._options = merge.recursive({}, {
compile: {
enabled: true,
overrideBrowserslist,
map: env !== 'development' ? false : {},
plugins: [],
inputFile: 'app.css',
outputFile: `${this.project.name()}.css`
},
filter: {
enabled: false,
overrideBrowserslist,
map: env !== 'development' ? false : {},
processTrees: ['css'],
plugins: []
}
}, this._getAddonOptions(app).postcssOptions)
},
_getAddonOptions (app) {
return (this.parent && this.parent.options) || (app && app.options) || {}
},
_ensureThisImport () {
if (!this.import) {
this._findHost = function findHostShim () {
let current = this
let app
do {
app = current.app || app
} while (current.parent.parent && (current = current.parent))
return app
}
this.import = function importShim (asset, options) {
const app = this._findHost()
app.import(asset, options)
}
}
},
postprocessTree (type, tree) {
const { enabled, processTrees } = this._options.filter
if (enabled && processTrees.includes(type)) {
tree = mergeTrees([tree, new PostcssFilter(tree, this._options.filter)], { overwrite: true })
}
return tree
},
setupPreprocessorRegistry (_type, registry) {
const addon = this
registry.add('css', new PostcssPlugin(addon))
}
}