Created
July 23, 2016 12:29
-
-
Save SleepWalker/a870a2b5e99a99403edf1fc7b17e6d4c to your computer and use it in GitHub Desktop.
sass-loader: resolve @import as modules
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
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; | |
} |
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
{ | |
"name": "webpack-utils", | |
"version": "1.0.0", | |
"description": "", | |
"keywords": [], | |
"author": "", | |
"dependencies": { | |
"loader-utils": "^0.2.12" | |
} | |
} |
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 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