Skip to content

Added missing camelCase option for locals loader #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/compile-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var camelCase = require("lodash.camelcase");

module.exports = function compileExports(result, importItemMatcher, camelCaseKeys) {
if (!Object.keys(result.exports).length) {
return "";
}

var exportJs = Object.keys(result.exports).reduce(function(res, key) {
var valueAsString = JSON.stringify(result.exports[key]);
valueAsString = valueAsString.replace(result.importItemRegExpG, importItemMatcher);
res.push("\t" + JSON.stringify(key) + ": " + valueAsString);
if (camelCaseKeys) {
res.push("\t" + JSON.stringify(camelCase(key)) + ": " + valueAsString);
}
return res;
}, []).join(",\n");

return "{\n" + exportJs + "\n}";
};
20 changes: 5 additions & 15 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/
var path = require("path");
var loaderUtils = require("loader-utils");
var camelCase = require("lodash.camelcase");
var processCss = require("./processCss");
var getImportPrefix = require("./getImportPrefix");
var compileExports = require("./compile-exports");


module.exports = function(content, map) {
Expand Down Expand Up @@ -82,21 +82,11 @@ module.exports = function(content, map) {
return "\" + require(" + loaderUtils.stringifyRequest(this, urlRequest) + ") + \"";
}.bind(this));

var exportJs = "";
if(Object.keys(result.exports).length > 0) {
var boundImportItemMatcher = importItemMatcher.bind(this);
exportJs = Object.keys(result.exports).reduce(function(res, key) {
var valueAsString = JSON.stringify(result.exports[key]);
valueAsString = valueAsString.replace(result.importItemRegExpG, boundImportItemMatcher);
res.push("\t" + JSON.stringify(key) + ": " + valueAsString);
if (camelCaseKeys) {
res.push("\t" + JSON.stringify(camelCase(key)) + ": " + valueAsString);
}
return res;
}, []).join(",\n");
exportJs = "exports.locals = {\n" + exportJs + "\n};";
}

var exportJs = compileExports(result, importItemMatcher.bind(this), camelCaseKeys);
if (exportJs) {
exportJs = "exports.locals = " + exportJs;
}

var moduleJs;
if(query.sourceMap && result.map) {
Expand Down
14 changes: 6 additions & 8 deletions lib/localsLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
var loaderUtils = require("loader-utils");
var processCss = require("./processCss");
var getImportPrefix = require("./getImportPrefix");
var compileExports = require("./compile-exports");


module.exports = function(content) {
if(this.cacheable) this.cacheable();
var callback = this.async();
var query = loaderUtils.parseQuery(this.query);
var moduleMode = query.modules || query.module;
var camelCaseKeys = query.camelCase || query.camelcase;

processCss(content, null, {
mode: moduleMode ? "local" : "global",
Expand All @@ -33,16 +35,12 @@ module.exports = function(content) {
"[" + JSON.stringify(importItem.export) + "] + \"";
}

var exportJs = "";
if(Object.keys(result.exports).length > 0) {
exportJs = Object.keys(result.exports).map(function(key) {
var valueAsString = JSON.stringify(result.exports[key]);
valueAsString = valueAsString.replace(result.importItemRegExpG, importItemMatcher.bind(this));
return "\t" + JSON.stringify(key) + ": " + valueAsString;
}.bind(this)).join(",\n");
exportJs = "module.exports = {\n" + exportJs + "\n};";
var exportJs = compileExports(result, importItemMatcher.bind(this), camelCaseKeys);
if (exportJs) {
exportJs = "module.exports = " + exportJs;
}


callback(null, exportJs);
}.bind(this));
};