Skip to content

Commit

Permalink
refactor: remove comments from bundle source code
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Jan 26, 2018
1 parent dda8b89 commit 28f603f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
78 changes: 48 additions & 30 deletions index.js
Expand Up @@ -2,7 +2,6 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

var path = require("path");

var loaderUtils = require("loader-utils");
Expand All @@ -16,63 +15,82 @@ module.exports.pitch = function (request) {
var options = loaderUtils.getOptions(this) || {};

validateOptions(require('./options.json'), options, 'Style Loader')

options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;

options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;

// need to use this variable, because function should be inlined
// if just store it in options, then after JSON.stringify
// function will be quoted and then in runtime will be just string
var insertInto;
if (typeof options.insertInto === "function") {
insertInto = options.insertInto.toString();
}
// we need to check if it string, or variable will be "undefined" and loader crash then
if (typeof options.insertInto === "string") {
insertInto = '"' + options.insertInto + '"';
}
// The variable is needed, because the function should be inlined.
// If is just stored it in options, JSON.stringify will quote
// the function and it would be just a string at runtime
var insertInto;

if (typeof options.insertInto === "function") {
insertInto = options.insertInto.toString();
}

// We need to check if it a string, or variable will be "undefined"
// and the loader crashes
if (typeof options.insertInto === "string") {
insertInto = '"' + options.insertInto + '"';
}

var hmrCode = [
"// Hot Module Replacement",
var hmr = [
// Hot Module Replacement,
"if(module.hot) {",
" // When the styles change, update the <style> tags",
// When the styles change, update the <style> tags
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
" var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
"",
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
"",
" var locals = (function(a, b) {",
" var key, idx = 0;",
"",
" for(key in a) {",
" if(!b || a[key] !== b[key]) return false;",
" idx++;",
" }",
"",
" for(key in b) idx--;",
"",
" return idx === 0;",
" }(content.locals, newContent.locals));",
" // This error is caught and not shown and causes a full reload.",
"",
// This error is caught and not shown and causes a full reload
" if(!locals) throw new Error('Aborting CSS HMR due to changed css-modules locals.');",
"",
" update(newContent);",
" });",
" // When the module is disposed, remove the <style> tags",
"",
// When the module is disposed, remove the <style> tags
" module.hot.dispose(function() { update(); });",
"}"
].join("\n");

return [
"// style-loader: Adds some css to the DOM by adding a <style> tag",
// Style Loader
// Adds CSS to the DOM by adding a <style> tag
"",
"// load the styles",
// Load styles
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
"",
"if(typeof content === 'string') content = [[module.id, content, '']];",
"// Prepare cssTransformation",
"",
// Transform styles",
"var transform;",
"var insertInto;",
options.transform ? "transform = require(" + loaderUtils.stringifyRequest(this, "!" + path.resolve(options.transform)) + ");" : "",
"insertInto = " + insertInto + ";" ,
"var options = " + JSON.stringify(options),
"options.transform = transform",
"options.insertInto = insertInto;",
"// add the styles to the DOM",
"var insertInto;",
"",
options.transform ? "transform = require(" + loaderUtils.stringifyRequest(this, "!" + path.resolve(options.transform)) + ");" : "",
"",
"var options = " + JSON.stringify(options),
"",
"options.transform = transform",
"options.insertInto = " + insertInto + ";",
"",
// Add styles to the DOM
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, options);",
"",
"if(content.locals) module.exports = content.locals;",
options.hmr ? hmrCode : ""
"",
options.hmr ? hmr : ""
].join("\n");
};
19 changes: 10 additions & 9 deletions url.js
Expand Up @@ -18,21 +18,22 @@ module.exports.pitch = function (request) {

options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;

var hmrCode = [
"// Hot Module Replacement",
var hmr = [
// Hot Module Replacement
"if(module.hot) {",
"\tmodule.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
"\t\tupdate(require(" + loaderUtils.stringifyRequest(this, "!!" + request) + "));",
"\t});",
"\tmodule.hot.dispose(function() { update(); });",
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
" update(require(" + loaderUtils.stringifyRequest(this, "!!" + request) + "));",
" });",
"",
" module.hot.dispose(function() { update(); });",
"}"
].join("\n");

return [
"// style-loader: Adds some reference to a css file to the DOM by adding a <link> tag",
// Adds some reference to a CSS file to the DOM by adding a <link> tag
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyleUrl.js")) + ")(",
"\trequire(" + loaderUtils.stringifyRequest(this, "!!" + request) + ")",
" require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ")",
", " + JSON.stringify(options) + ");",
options.hmr ? hmrCode : ""
options.hmr ? hmr : ""
].join("\n");
};
23 changes: 16 additions & 7 deletions useable.js
Expand Up @@ -18,21 +18,25 @@ module.exports.pitch = function (request) {

options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;

var hmrCode = [
"// Hot Module Replacement",
var hmr = [
// Hot Module Replacement
"if(module.hot) {",
" var lastRefs = module.hot.data && module.hot.data.refs || 0;",
"",
" if(lastRefs) {",
" exports.ref();",
" if(!content.locals) {",
" refs = lastRefs;",
" }",
" }",
"",
" if(!content.locals) {",
" module.hot.accept();",
" }",
"",
" module.hot.dispose(function(data) {",
" data.refs = content.locals ? 0 : refs;",
"",
" if(dispose) {",
" dispose();",
" }",
Expand All @@ -44,20 +48,25 @@ module.exports.pitch = function (request) {
"var refs = 0;",
"var dispose;",
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
"",
"if(typeof content === 'string') content = [[module.id, content, '']];",
// Export CSS Modules
"if(content.locals) exports.locals = content.locals;",
"",
"exports.use = exports.ref = function() {",
" if(!(refs++)) {",
" dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, " + JSON.stringify(options) + ");",
" }",
"",
" return exports;",
"};",
"",
"exports.unuse = exports.unref = function() {",
" if(refs > 0 && !(--refs)) {",
" dispose();",
" dispose = null;",
" }",
" if(refs > 0 && !(--refs)) {",
" dispose();",
" dispose = null;",
" }",
"};",
options.hmr ? hmrCode : ""
options.hmr ? hmr : ""
].join("\n");
};

0 comments on commit 28f603f

Please sign in to comment.