stylus-loader
Advanced tools
Comparing version 1.1.1 to 1.2.0
35
index.js
@@ -10,2 +10,4 @@ var loaderUtils = require('loader-utils'); | ||
var globalImportsCache = {}; | ||
module.exports = function(source) { | ||
@@ -73,4 +75,35 @@ var self = this; | ||
var shouldCacheImports = stylusOptions.importsCache !== false; | ||
var importsCache; | ||
if (stylusOptions.importsCache !== false) { | ||
if (typeof stylusOptions.importsCache === 'object') { | ||
importsCache = stylusOptions.importsCache; | ||
} else { | ||
importsCache = globalImportsCache; | ||
} | ||
} | ||
// Use input file system's readFile if available. The normal webpack input | ||
// file system is cached with entries purged when they are detected to be | ||
// changed on disk by the watcher. | ||
var readFile; | ||
try { | ||
var inputFileSystem = this._compiler.inputFileSystem; | ||
readFile = inputFileSystem.readFile.bind(inputFileSystem); | ||
} catch (error) { | ||
readFile = fs.readFile; | ||
} | ||
var boundResolvers = PathCache.resolvers(options, this.resolve); | ||
PathCache.createFromFile(boundResolvers, {}, source, options.filename) | ||
PathCache | ||
.createFromFile({ | ||
resolvers: boundResolvers, | ||
readFile: readFile, | ||
}, { | ||
contexts: {}, | ||
sources: {}, | ||
imports: importsCache, | ||
}, source, options.filename) | ||
.then(function(importPathCache) { | ||
@@ -77,0 +110,0 @@ // CachedPathEvaluator will use this PathCache to find its dependencies. |
@@ -59,3 +59,9 @@ // Except for a block in #visitImport, everything here is an exact copy of the | ||
var str = fs.readFileSync(file, 'utf8'); | ||
var str; | ||
if (this.cache.sources && this.cache.sources[file]) { | ||
str = this.cache.sources[file]; | ||
} else { | ||
str = fs.readFileSync(file, 'utf8'); | ||
} | ||
if (literal && !this.resolveURL) return new nodes.Literal(str.replace(/\r\n?/g, '\n')); | ||
@@ -62,0 +68,0 @@ |
@@ -32,3 +32,12 @@ var Parser = require('stylus/lib/parser'); | ||
// Returns a list of paths that given source imports. | ||
function listImports(source) { | ||
function listImports(source, options) { | ||
// Store source -> imports work in a cache. The Parser is the most expensive | ||
// part of stylus and we can't use their cache without creating undesired side | ||
// effects later during the actual render. In single run builds this will | ||
// benefit repeated files imported like common styling. In multiple run builds | ||
// this will help stylus import trees when a dependency changes, the higher up | ||
// files won't need to be parsed again. | ||
var cache = options.cache; | ||
if (cache && cache[source]) { return cache[source]; } | ||
// Current idea here is to silence errors and let them rise in stylus's | ||
@@ -44,3 +53,8 @@ // renderer which has more handling so that the error message is more | ||
importVisitor.visit(ast); | ||
if (cache) { | ||
cache[source] = importVisitor.importPaths; | ||
} | ||
return importVisitor.importPaths; | ||
} |
@@ -20,4 +20,6 @@ var path = require('path'); | ||
// webpack's resolver. | ||
function PathCache(contexts) { | ||
function PathCache(contexts, sources, imports) { | ||
this.contexts = contexts; | ||
this.sources = sources; | ||
this.imports = imports; | ||
@@ -35,4 +37,4 @@ // Non relative paths are simpler and looked up in this as a fallback | ||
// Return a promise for a PathCache. | ||
PathCache.create = function(contexts) { | ||
return when(new PathCache(contexts)); | ||
PathCache.create = function(contexts, sources, imports) { | ||
return when(new PathCache(contexts, sources, imports)); | ||
}; | ||
@@ -165,12 +167,30 @@ PathCache.createFromFile = resolveFileDeep; | ||
// Load a file at fullPath, resolve all of it's imports and report for those. | ||
function resolveFileDeep(resolvers, contexts, source, fullPath) { | ||
function resolveFileDeep(helpers, parentCache, source, fullPath) { | ||
var resolvers = helpers.resolvers; | ||
var readFile = helpers.readFile; | ||
var contexts = parentCache.contexts; | ||
var sources = parentCache.sources; | ||
contexts = contexts || {}; | ||
var nestResolve = resolveFileDeep.bind(null, resolvers, contexts, null); | ||
var nestResolve = resolveFileDeep.bind(null, helpers, parentCache, null); | ||
var context = path.dirname(fullPath); | ||
readFile = whenNodefn.lift(readFile); | ||
return when | ||
.try(function() { | ||
if (typeof source === 'string') { return source; } | ||
return readFile(fullPath, 'utf8'); | ||
.resolve(source || sources[fullPath] || readFile(fullPath)) | ||
// Cast the buffer from the cached input file system to a string. | ||
.then(String) | ||
// Store the source so that the evaluator doesn't need to touch the | ||
// file system. | ||
.then(function(_source) { | ||
sources[fullPath] = _source; | ||
return _source; | ||
}) | ||
.then(listImports) | ||
// Make sure the stylus functions/index.styl source is stored. | ||
.then(partial(ensureFunctionsSource, sources)) | ||
// List imports and use its cache. The source file is translated into a | ||
// list of imports. Where the source file came from isn't important for the | ||
// list. The where is added by resolveMany with the context and resolvers. | ||
.then(partialRight(listImports, { cache: parentCache.imports })) | ||
.then(resolveMany.bind(null, resolvers, context)) | ||
@@ -188,4 +208,40 @@ .then(function(newPaths) { | ||
}) | ||
.yield(contexts) | ||
.then(PathCache.create); | ||
.then(function() { | ||
return PathCache.create(contexts, sources, parentCache.imports); | ||
}); | ||
} | ||
var functionsPath = path.join( | ||
__dirname, '../node_modules/stylus/lib/functions/index.styl' | ||
); | ||
var functionsSource = readFile(functionsPath) | ||
.then(String); | ||
function ensureFunctionsSource(sources, source) { | ||
if (!sources[functionsPath]) { | ||
return functionsSource | ||
.then(function(functionsSource) { | ||
sources[functionsPath] = functionsSource; | ||
}) | ||
// Pass through the source given to this function. | ||
.yield(source); | ||
} | ||
// Pass through the source given to this function. | ||
return source; | ||
} | ||
var slice = Array.prototype.slice.call.bind(Array.prototype.slice); | ||
function partial(fn) { | ||
var args = slice(arguments, 1); | ||
return function() { | ||
return fn.apply(this, args.concat(slice(arguments))); | ||
}; | ||
} | ||
function partialRight(fn) { | ||
var args = slice(arguments, 1); | ||
return function() { | ||
return fn.apply(this, slice(arguments).concat(args)); | ||
}; | ||
} |
{ | ||
"name": "stylus-loader", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Stylus loader for webpack", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"lib/" | ||
], | ||
"scripts": { | ||
"test": "webpack-dev-server --config test/webpack.config.js" | ||
"test": "testem ci", | ||
"test-dev": "testem -l firefox" | ||
}, | ||
@@ -19,3 +24,3 @@ "author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)", | ||
"dependencies": { | ||
"loader-utils": "~0.2.6", | ||
"loader-utils": "^0.2.9", | ||
"stylus": "^0.49.3", | ||
@@ -25,8 +30,13 @@ "when": "~3.6.x" | ||
"devDependencies": { | ||
"css-loader": "~0.9.1", | ||
"benchmark": "^1.0.0", | ||
"css-loader": "^0.14.0", | ||
"mocha": "~2.1.0", | ||
"mocha-loader": "~0.7.1", | ||
"nib": "^1.0.4", | ||
"node-libs-browser": "^0.5.2", | ||
"raw-loader": "~0.5.1", | ||
"should": "~4.6.1", | ||
"style-loader": "^0.12.2", | ||
"testem": "^0.8.3", | ||
"webpack": "^1.9.8", | ||
"webpack-dev-server": "~1.7.0" | ||
@@ -33,0 +43,0 @@ }, |
# stylus-loader | ||
A [stylus](http://learnboost.github.io/stylus/) loader for [webpack](https://github.com/webpack/webpack). | ||
[![build status](https://secure.travis-ci.org/shama/stylus-loader.svg)](https://travis-ci.org/shama/stylus-loader) | ||
[![NPM version](https://badge.fury.io/js/stylus-loader.svg)](https://badge.fury.io/js/stylus-loader) | ||
## Usage | ||
@@ -5,0 +8,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100
27225
12
8
597
Updatedloader-utils@^0.2.9