Skip to content

Commit

Permalink
allowing for importer factory functions
Browse files Browse the repository at this point in the history
This will let the importer state be reset if scssify is called several times within the same process, e.g. by factor-bundle
  • Loading branch information
Nick Dresselhaus committed Jun 3, 2016
1 parent de17544 commit 2963d08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function scssify(file, content, config, stream, done) {
if (typeof sassOpts.importer === 'string') {
sassOpts.importer = require(path.resolve(sassOpts.importer))
}
else if (typeof sassOpts.importerFactory === 'string') {
let factory = require(path.resolve(sassOpts.importerFactory))
sassOpts.importer = factory()
}

if (options.autoInject === true) {
options.autoInject = merge({}, DEFAULT_CONFIG.autoInject)
Expand Down
19 changes: 19 additions & 0 deletions tests/custom-importers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('assert')
const integration = require('./util/integration')
const importSpy = require('./util/mock-importer').importedFiles
const factory = require('./util/mock-importer-factory')

before(function setup() {
importSpy.clear()
Expand All @@ -18,3 +19,21 @@ it('loads custom importer files', integration(__dirname + '/util/imports.scss',
assert.equal(files.next().value, 'vars', 'import spy was loaded')
done()
}))

it('loads importer from factory function every time it runs', function (done) {
// this mimics browserify + factor-bundle calling the transform several times in a single process
let file = __dirname + '/util/imports.scss'
let config = {
autoInject: false,
sass: {
importerFactory: 'tests/util/mock-importer-factory.js'
}
}
integration(file, config, function (context, done) {
integration(file, config, function (context, done) {
let count = factory.count
assert.equal(count, 2, 'factory not called expected number of times')
done()
})(done)
})(done)
});
9 changes: 9 additions & 0 deletions tests/util/mock-importer-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'
const mockImporter = require('./mock-importer')
module.exports = function () {
module.exports.count++;
return mockImporter
}

module.exports.count = 0;

0 comments on commit 2963d08

Please sign in to comment.