From 9723971f953687993974467cf6f7e3b059ce17a2 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 9 Jan 2016 20:27:46 +0300 Subject: [PATCH 1/2] Add custom load support --- index.js | 20 ++++++++------------ lib/load-content.js | 12 ++++++++++++ test/custom-load.js | 18 ++++++++++++++++++ test/fixtures/custom-load.css | 1 + test/fixtures/custom-load.expected.css | 1 + 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 lib/load-content.js create mode 100644 test/custom-load.js create mode 100644 test/fixtures/custom-load.css create mode 100644 test/fixtures/custom-load.expected.css diff --git a/index.js b/index.js index 9abf8688..cb9577c0 100755 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ -var fs = require("fs") var path = require("path") var assign = require("object-assign") var postcss = require("postcss") var joinMedia = require("./lib/join-media") var resolveId = require("./lib/resolve-id") +var loadContent = require("./lib/load-content") var parseStatements = require("./lib/parse-statements") function AtImport(options) { @@ -11,7 +11,7 @@ function AtImport(options) { root: process.cwd(), path: [], skipDuplicates: true, - encoding: "utf8", + load: loadContent, }, options) options.root = path.resolve(options.root) @@ -281,14 +281,8 @@ function loadImportContent( state.importedFiles[filename][media] = true } - return new Promise(function(resolve, reject) { - fs.readFile(filename, options.encoding, function(err, data) { - if (err) { - return reject(err) - } - resolve(data) - }) - }).then(function(content) { + return Promise.resolve(options.load(filename, options)) + .then(function(content) { if (typeof options.transform === "function") { content = options.transform(content, filename) } @@ -333,8 +327,10 @@ function loadImportContent( state, media, processor - ).then(function(statements) { - return processor.process(newStyles).then(function(newResult) { + ) + .then(function(statements) { + return processor.process(newStyles) + .then(function(newResult) { result.messages = result.messages.concat(newResult.messages) return statements diff --git a/lib/load-content.js b/lib/load-content.js new file mode 100644 index 00000000..d2853e57 --- /dev/null +++ b/lib/load-content.js @@ -0,0 +1,12 @@ +var fs = require("fs") + +module.exports = function(filename) { + return new Promise(function(resolve, reject) { + fs.readFile(filename, "utf-8", function(err, data) { + if (err) { + return reject(err) + } + resolve(data) + }) + }) +} diff --git a/test/custom-load.js b/test/custom-load.js new file mode 100644 index 00000000..676ea0bf --- /dev/null +++ b/test/custom-load.js @@ -0,0 +1,18 @@ +import test from "ava" +import compareFixtures from "./lib/compare-fixtures" + +test("should accept content", t => { + return compareFixtures(t, "custom-load", { + load: () => { + return "custom-content {}" + }, + }) +}) + +test("should accept promised content", t => { + return compareFixtures(t, "custom-load", { + load: () => { + return Promise.resolve("custom-content {}") + }, + }) +}) diff --git a/test/fixtures/custom-load.css b/test/fixtures/custom-load.css new file mode 100644 index 00000000..0a2e522a --- /dev/null +++ b/test/fixtures/custom-load.css @@ -0,0 +1 @@ +@import "foo" diff --git a/test/fixtures/custom-load.expected.css b/test/fixtures/custom-load.expected.css new file mode 100644 index 00000000..e36a838c --- /dev/null +++ b/test/fixtures/custom-load.expected.css @@ -0,0 +1 @@ +custom-content {} From d798ffac391bd4a135acd73520a1bc115f4d7222 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 9 Jan 2016 20:41:17 +0300 Subject: [PATCH 2/2] Add documentation and changelog --- CHANGELOG.md | 14 ++++++++++++++ README.md | 15 ++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0a70a11..ab09961e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,20 @@ See [resolve option](https://github.com/postcss/postcss-import#resolve) for more ([#130](https://github.com/postcss/postcss-import/pull/130)) - Changed: glob resolver do not add `moduleDirectories` and parse all uri as glob patterns ([#131](https://github.com/postcss/postcss-import/pull/131)) +- Added: support custom `load` option +([#144](https://github.com/postcss/postcss-import/pull/144)) +- Removed: `encoding` option. +([#144](https://github.com/postcss/postcss-import/pull/144)) + +Encoding can be specified in custom `load` option + +```js +postcssImport({ + load: function(filename) { + return fs.readFileSync(filename, "utf-8") + } +}) +``` # 7.1.3 - 2015-11-05 diff --git a/README.md b/README.md index 79aa46a5..364ac314 100755 --- a/README.md +++ b/README.md @@ -118,13 +118,6 @@ Default: `undefined` An array of plugins to be applied on each imported file. -#### `encoding` - -Type: `String` -Default: `utf8` - -Use if your CSS is encoded in anything other than UTF-8. - #### `onImport` Type: `Function` @@ -149,6 +142,14 @@ You can overwrite the default path resolving way by setting this option. This function gets `(id, basedir, importOptions)` arguments and returns full path, array of paths or promise resolving paths. You can use [resolve](https://github.com/substack/node-resolve) for that. +#### `load` + +Type: `Function` +Default: null + +You can overwrite the default loading way by setting this option. +This function gets `(filename, importOptions)` arguments and returns content or promised content. + #### `skipDuplicates` Type: `Boolean`