css-module-builder
Advanced tools
Comparing version 1.0.0-beta.5 to 1.0.0-beta.6
81
index.js
'use strict'; | ||
/** | ||
* This is a factory that creates a css modules that are: | ||
* 1. compatible with webpack's extract-text-webpack-plugin | ||
* 2. compatible with webpack's style-loader | ||
* 3. maintains the same interface when used outside of webpack. | ||
* | ||
* Because I tried to aim for these 3 things, there are a few questionable decisions that I've made | ||
* (from the perspective of writing clean code). | ||
* | ||
* Gist: | ||
* The resulting module is an array of arrays, (the null can probably be removed, was just added because | ||
* this is how webpack-contrib/css-loader represents css-modules): | ||
* | ||
* [ [module-id, css, null] ] | ||
* | ||
* This array also has all of the variables defined in the css (be it, unique class mappings or | ||
* @value definitions) attached at the top level. This means that everything is in the same level. | ||
* | ||
* var module = builder.initialize(/* ommited *\/) | ||
* module[0] // => entrypoint module (represented as an array) | ||
* module.localVar // => local value | ||
* module.locals.localVar // => local value | ||
* | ||
* This is not ideal because it makes for a really messy module. | ||
* | ||
*/ | ||
var stringify = JSON.stringify; | ||
@@ -8,9 +35,11 @@ var isArray = Array.isArray; | ||
var push = Array.prototype.push; | ||
var isEnumerable = Object.prototype.propertyIsEnumerable; | ||
var CSSModuleStaticProperties = { | ||
toString: { value: toString }, | ||
importEach: { value: importEach }, | ||
__is_css_module__: { value: true }, | ||
defineLocals: { value: defineLocals }, | ||
get: { value: strictGetLocal }, | ||
__is_css_module__: { value: true } | ||
importEach: { value: importEach }, | ||
locals: { get: createLocals }, | ||
toString: { value: toString } | ||
}; | ||
@@ -23,3 +52,2 @@ | ||
Object.defineProperties(module, { | ||
locals: { value: {} }, | ||
__imported_modules__: { value: {} } | ||
@@ -34,13 +62,31 @@ }); | ||
function getLocal(module, localName) { | ||
var values; | ||
if (typeof module.locals === 'object') { | ||
values = module.locals[localName]; | ||
} else { | ||
values = module[localName]; | ||
return module.locals[localName] || ''; | ||
} | ||
return (values || ''); | ||
if (module[localName] && isALocalDefinition(module, localName)) { | ||
return module[localName]; | ||
} | ||
return ''; | ||
} | ||
/** | ||
* Ensures it is an enumerable property that is not an array element | ||
*/ | ||
function isALocalDefinition(module, local) { | ||
return isEnumerable.call(module, local) && !/^\d+$/.test(local) | ||
} | ||
function createLocals() { | ||
var locals = {}; | ||
for (var local in this) { | ||
if (isALocalDefinition(this, local)) | ||
locals[local] = this[local]; | ||
} | ||
return locals; | ||
} | ||
/* Shared counter to guarantee unique ids for nonCSSModule imports */ | ||
@@ -123,12 +169,15 @@ var nonCSSModuleImportId = 0; | ||
eachLocalKV(localDefinitions, function (key, value) { | ||
this.locals[key] = value; | ||
this[key] = value; | ||
}, this); | ||
} | ||
function strictGetLocal(local) { | ||
if (this.locals[local] === undefined) { | ||
throw new Error(stringify(local) + ' is not defined in ' + stringify(this.locals, null, 2)); | ||
function strictGetLocal(localName) { | ||
var local = getLocal(this, localName); | ||
if (local) { | ||
return local; | ||
} else { | ||
throw new Error(stringify(localName) + ' is not defined in ' + stringify(this.locals, null, 2)); | ||
} | ||
return this.locals[local]; | ||
} | ||
@@ -135,0 +184,0 @@ |
{ | ||
"name": "css-module-builder", | ||
"version": "1.0.0-beta.5", | ||
"version": "1.0.0-beta.6", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 0" | ||
"test": "mocha test/index.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"mocha": "^3.2.0" | ||
} | ||
} |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
6734
3
208
2
1