forked from SassNinja/postcss-extract-media-query
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve subsequent plugins application & move into own module
in the course of this I've also deleted combine-media.js (legacy code that's not used anymore) and introduced a config option to provide more control
- Loading branch information
Showing
5 changed files
with
172 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const postcss = require('postcss'); | ||
const rootPath = require('app-root-path').path; | ||
|
||
class SubsequentPlugins { | ||
constructor() { | ||
this.config = {}; | ||
this.updateConfig(); | ||
} | ||
|
||
/** | ||
* (Re)init with current postcss config | ||
*/ | ||
_init() { | ||
this.allNames = this.config.plugins ? Object.keys(this.config.plugins) : []; | ||
this.subsequentNames = this.allNames.slice(this.allNames.indexOf('postcss-extract-media-query') + 1); | ||
this.subsequentPlugins = this.subsequentNames.map(name => ({ | ||
name, | ||
mod: this.config.pluginsSrc && this.config.pluginsSrc[name] || require(name), | ||
opts: this.config.plugins[name] | ||
})); | ||
} | ||
|
||
/** | ||
* Updates the postcss config by resolving file path | ||
* or by using the config file object | ||
* | ||
* @param {string|Object} file | ||
* @returns {Object} | ||
*/ | ||
updateConfig(file) { | ||
if (typeof file === 'object') { | ||
this.config = file; | ||
this._init(); | ||
return this.config; | ||
} | ||
if (typeof file === 'string' && !path.isAbsolute(file)) { | ||
file = path.join(rootPath, file); | ||
} | ||
const filePath = file || path.join(rootPath, 'postcss.config.js'); | ||
|
||
if (fs.existsSync(filePath)) { | ||
this.config = require(filePath); | ||
} | ||
this._init(); | ||
return this.config; | ||
} | ||
|
||
/** | ||
* Apply all subsequent plugins to the (extracted) css | ||
* | ||
* @param {string} css | ||
* @param {string} filePath | ||
* @returns {string} | ||
*/ | ||
applyPlugins(css, filePath) { | ||
const plugins = this.subsequentPlugins.map(plugin => plugin.mod(plugin.opts)); | ||
|
||
if (plugins.length) { | ||
return new Promise(resolve => { | ||
postcss(plugins) | ||
.process(css, { from: filePath, to: filePath }) | ||
.then(result => { | ||
resolve(result.css); | ||
}); | ||
}); | ||
} | ||
return Promise.resolve(css); | ||
} | ||
} | ||
|
||
module.exports = SubsequentPlugins; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters