Skip to content

Instantly share code, notes, and snippets.

@SleepWalker
Created July 23, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SleepWalker/a870a2b5e99a99403edf1fc7b17e6d4c to your computer and use it in GitHub Desktop.
Save SleepWalker/a870a2b5e99a99403edf1fc7b17e6d4c to your computer and use it in GitHub Desktop.
sass-loader: resolve @import as modules
var loaderUtils = require("loader-utils");
module.exports = function createImporter(options) {
return function(url, fileContext, done) {
if (options.test.test(url)) {
var request = loaderUtils.urlToRequest(url);
loaderContext.loadModule(request, function(err, source) {
if (err) return done(new Error(err));
done({
contents: loaderContext.exec(source)
});
});
} else {
done(false);
}
};
};
var loaderContext;
var Plugin = module.exports.Plugin = function() {};
Plugin.prototype.apply = function(compiler) {
compiler.plugin('compilation', function(compilation) {
compilation.plugin('normal-module-loader', setLoaderContext);
});
};
function setLoaderContext(instance) {
// inject loaderContext instance for importer function
loaderContext = instance;
}
{
"name": "webpack-utils",
"version": "1.0.0",
"description": "",
"keywords": [],
"author": "",
"dependencies": {
"loader-utils": "^0.2.12"
}
}
/**
* This is an example how to set up sass-loader to allow resolving import statements as webpack modules
*/
var iconfontImporter = require('./node-sass-iconfont-importer');
var webpackConfig = {
//...
plugins: [
new iconfontImporter.Plugin(),
//...
],
sassLoader: {
importer: iconfontImporter({
test: /\.font.(js|json)$/
})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment