css-modules-require-hook
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -8,4 +8,5 @@ 'use strict'; | ||
*/ | ||
module.exports = function attachHook(compile, extension, isException) { | ||
const existingHook = require.extensions[extension]; | ||
var existingHook = require.extensions[extension]; | ||
@@ -16,6 +17,6 @@ require.extensions[extension] = function cssModulesHook(m, filename) { | ||
} else { | ||
const tokens = compile(filename); | ||
var tokens = compile(filename); | ||
return m._compile(`module.exports = ${JSON.stringify(tokens)}`, filename); | ||
} | ||
}; | ||
}; | ||
}; |
171
lib/index.js
'use strict'; | ||
const {assign, identity, negate, camelCase: camelCaseFunc, mapKeys} = require('lodash'); | ||
const {dirname, relative, resolve} = require('path'); | ||
const {readFileSync} = require('fs'); | ||
const {transformTokens} = require('./transformTokens'); | ||
var _require = require('lodash'), | ||
assign = _require.assign, | ||
identity = _require.identity, | ||
negate = _require.negate; | ||
const attachHook = require('./attachHook'); | ||
const genericNames = require('generic-names'); | ||
const globToRegex = require('glob-to-regexp'); | ||
const validate = require('./validate'); | ||
var _require2 = require('path'), | ||
dirname = _require2.dirname, | ||
relative = _require2.relative, | ||
resolve = _require2.resolve; | ||
const postcss = require('postcss'); | ||
const Values = require('postcss-modules-values'); | ||
const LocalByDefault = require('postcss-modules-local-by-default'); | ||
const ExtractImports = require('postcss-modules-extract-imports'); | ||
const Scope = require('postcss-modules-scope'); | ||
const Parser = require('postcss-modules-parser'); | ||
var _require3 = require('fs'), | ||
readFileSync = _require3.readFileSync; | ||
const debugFetch = require('debug')('css-modules:fetch'); | ||
const debugSetup = require('debug')('css-modules:setup'); | ||
var _require4 = require('./transformTokens'), | ||
transformTokens = _require4.transformTokens; | ||
module.exports = function setupHook({ | ||
camelCase, | ||
devMode, | ||
extensions = '.css', | ||
ignore, | ||
preprocessCss = identity, | ||
processCss, | ||
processorOpts, | ||
append = [], | ||
prepend = [], | ||
createImportedName, | ||
generateScopedName, | ||
hashPrefix, | ||
mode, | ||
use, | ||
rootDir: context = process.cwd(), | ||
}) { | ||
var attachHook = require('./attachHook'); | ||
var genericNames = require('generic-names'); | ||
var globToRegex = require('glob-to-regexp'); | ||
var validate = require('./validate'); | ||
var postcss = require('postcss'); | ||
var Values = require('postcss-modules-values'); | ||
var LocalByDefault = require('postcss-modules-local-by-default'); | ||
var ExtractImports = require('postcss-modules-extract-imports'); | ||
var Scope = require('postcss-modules-scope'); | ||
var Parser = require('postcss-modules-parser'); | ||
var debugFetch = require('debug')('css-modules:fetch'); | ||
var debugSetup = require('debug')('css-modules:setup'); | ||
module.exports = function setupHook(_ref) { | ||
var camelCase = _ref.camelCase, | ||
devMode = _ref.devMode, | ||
_ref$extensions = _ref.extensions, | ||
extensions = _ref$extensions === undefined ? '.css' : _ref$extensions, | ||
ignore = _ref.ignore, | ||
_ref$preprocessCss = _ref.preprocessCss, | ||
preprocessCss = _ref$preprocessCss === undefined ? identity : _ref$preprocessCss, | ||
processCss = _ref.processCss, | ||
processorOpts = _ref.processorOpts, | ||
_ref$append = _ref.append, | ||
append = _ref$append === undefined ? [] : _ref$append, | ||
_ref$prepend = _ref.prepend, | ||
prepend = _ref$prepend === undefined ? [] : _ref$prepend, | ||
createImportedName = _ref.createImportedName, | ||
generateScopedName = _ref.generateScopedName, | ||
hashPrefix = _ref.hashPrefix, | ||
mode = _ref.mode, | ||
use = _ref.use, | ||
_ref$rootDir = _ref.rootDir, | ||
context = _ref$rootDir === undefined ? process.cwd() : _ref$rootDir; | ||
debugSetup(arguments[0]); | ||
validate(arguments[0]); | ||
const tokensByFile = {}; | ||
var tokensByFile = {}; | ||
// debug option is preferred NODE_ENV === 'development' | ||
const debugMode = typeof devMode !== 'undefined' | ||
? devMode | ||
: process.env.NODE_ENV === 'development'; | ||
var debugMode = typeof devMode !== 'undefined' ? devMode : process.env.NODE_ENV === 'development'; | ||
let scopedName; | ||
if (generateScopedName) { | ||
scopedName = typeof generateScopedName !== 'function' | ||
? genericNames(generateScopedName, {context, hashPrefix}) // for example '[name]__[local]___[hash:base64:5]' | ||
: generateScopedName; | ||
} else { | ||
var scopedName = void 0; | ||
if (generateScopedName) scopedName = typeof generateScopedName !== 'function' ? genericNames(generateScopedName, { context, hashPrefix }) // for example '[name]__[local]___[hash:base64:5]' | ||
: generateScopedName;else | ||
// small fallback | ||
scopedName = (local, filename) => { | ||
scopedName = function scopedName(local, filename) { | ||
return Scope.generateScopedName(local, relative(context, filename)); | ||
}; | ||
} | ||
const plugins = (use || [ | ||
...prepend, | ||
Values, | ||
mode | ||
? new LocalByDefault({mode}) | ||
: LocalByDefault, | ||
createImportedName | ||
? new ExtractImports({createImportedName}) | ||
: ExtractImports, | ||
new Scope({generateScopedName: scopedName}), | ||
...append, | ||
]).concat(new Parser({fetch})); // no pushing in order to avoid the possible mutations; | ||
var plugins = (use || [].concat(prepend, [Values, mode ? new LocalByDefault({ mode }) : LocalByDefault, createImportedName ? new ExtractImports({ createImportedName }) : ExtractImports, new Scope({ generateScopedName: scopedName })], append)).concat(new Parser({ fetch })); // no pushing in order to avoid the possible mutations; | ||
// https://github.com/postcss/postcss#options | ||
const runner = postcss(plugins); | ||
var runner = postcss(plugins); | ||
@@ -86,8 +85,6 @@ /** | ||
// getting absolute path to the processing file | ||
const filename = /[^\\/?%*:|"<>\.]/i.test(_to[0]) | ||
? require.resolve(_to) | ||
: resolve(dirname(from), _to); | ||
var filename = /[^\\/?%*:|"<>.]/i.test(_to[0]) ? require.resolve(_to) : resolve(dirname(from), _to); | ||
// checking cache | ||
let tokens = tokensByFile[filename]; | ||
var tokens = tokensByFile[filename]; | ||
if (tokens) { | ||
@@ -99,22 +96,20 @@ debugFetch(`${filename} → cache`); | ||
const source = preprocessCss(readFileSync(filename, 'utf8'), filename); | ||
var source = preprocessCss(readFileSync(filename, 'utf8'), filename); | ||
// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts | ||
const lazyResult = runner.process(source, assign({}, processorOpts, {from: filename})); | ||
var lazyResult = runner.process(source, assign({}, processorOpts, { from: filename })); | ||
// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings | ||
lazyResult.warnings().forEach(message => console.warn(message.text)); | ||
lazyResult.warnings().forEach(function (message) { | ||
return console.warn(message.text); | ||
}); | ||
tokens = lazyResult.root.tokens; | ||
if (!debugMode) { | ||
if (!debugMode) | ||
// updating cache | ||
tokensByFile[filename] = tokens; | ||
} else { | ||
tokensByFile[filename] = tokens;else | ||
// clearing cache in development mode | ||
delete require.cache[filename]; | ||
} | ||
if (processCss) { | ||
processCss(lazyResult.css, filename); | ||
} | ||
if (processCss) processCss(lazyResult.css, filename); | ||
@@ -125,9 +120,9 @@ debugFetch(`${filename} → fs`); | ||
return tokens; | ||
}; | ||
} | ||
const exts = toArray(extensions); | ||
const isException = buildExceptionChecker(ignore); | ||
var exts = toArray(extensions); | ||
var isException = buildExceptionChecker(ignore); | ||
const hook = filename => { | ||
const tokens = fetch(filename, filename); | ||
var hook = function hook(filename) { | ||
var tokens = fetch(filename, filename); | ||
return camelCase ? transformTokens(tokens, camelCase) : tokens; | ||
@@ -137,3 +132,5 @@ }; | ||
// @todo add possibility to specify particular config for each extension | ||
exts.forEach(extension => attachHook(hook, extension, isException)); | ||
exts.forEach(function (extension) { | ||
return attachHook(hook, extension, isException); | ||
}); | ||
}; | ||
@@ -146,5 +143,3 @@ | ||
function toArray(option) { | ||
return Array.isArray(option) | ||
? option | ||
: [option]; | ||
return Array.isArray(option) ? option : [option]; | ||
} | ||
@@ -157,11 +152,11 @@ | ||
function buildExceptionChecker(ignore) { | ||
if (ignore instanceof RegExp) { | ||
return filepath => ignore.test(filepath); | ||
} | ||
if (ignore instanceof RegExp) return function (filepath) { | ||
return ignore.test(filepath); | ||
}; | ||
if (typeof ignore === 'string') { | ||
return filepath => globToRegex(ignore).test(filepath); | ||
} | ||
if (typeof ignore === 'string') return function (filepath) { | ||
return globToRegex(ignore).test(filepath); | ||
}; | ||
return ignore || negate(identity); | ||
} | ||
} |
'use strict'; | ||
const {assign, camelCase, reduce} = require('lodash'); | ||
var _require = require('lodash'), | ||
assign = _require.assign, | ||
camelCase = _require.camelCase, | ||
reduce = _require.reduce; | ||
const camelizeKeys = (acc, value, key) => { | ||
const camelizedKey = camelCase(key); | ||
var camelizeKeys = function camelizeKeys(acc, value, key) { | ||
var camelizedKey = camelCase(key); | ||
if (camelizedKey !== key) acc[camelizedKey] = value; | ||
@@ -11,4 +14,4 @@ return acc; | ||
const camelizeDashedKeys = (acc, value, key) => { | ||
const camelizedKey = camelizeDashes(key); | ||
var camelizeDashedKeys = function camelizeDashedKeys(acc, value, key) { | ||
var camelizedKey = camelizeDashes(key); | ||
if (camelizedKey !== key) acc[camelizedKey] = value; | ||
@@ -18,13 +21,11 @@ return acc; | ||
const camelizeOnlyKeys = (acc, value, key) => { | ||
const camelizedKey = camelCase(key); | ||
if (camelizedKey !== key) acc[camelizedKey] = value | ||
else acc[key] = value; | ||
var camelizeOnlyKeys = function camelizeOnlyKeys(acc, value, key) { | ||
var camelizedKey = camelCase(key); | ||
if (camelizedKey !== key) acc[camelizedKey] = value;else acc[key] = value; | ||
return acc; | ||
}; | ||
const camelizeOnlyDashedKeys = (acc, value, key) => { | ||
const camelizedKey = camelizeDashes(key); | ||
if (camelizedKey !== key) acc[camelizedKey] = value | ||
else acc[key] = value; | ||
var camelizeOnlyDashedKeys = function camelizeOnlyDashedKeys(acc, value, key) { | ||
var camelizedKey = camelizeDashes(key); | ||
if (camelizedKey !== key) acc[camelizedKey] = value;else acc[key] = value; | ||
return acc; | ||
@@ -41,3 +42,5 @@ }; | ||
function camelizeDashes(str) { | ||
return str.replace(/-(\w)/g, (m, letter) => letter.toUpperCase()); | ||
return str.replace(/-(\w)/g, function (m, letter) { | ||
return letter.toUpperCase(); | ||
}); | ||
} | ||
@@ -52,16 +55,16 @@ | ||
switch (camelCase) { | ||
case true: | ||
return reduce(tokens, camelizeKeys, assign({}, tokens)); | ||
case true: | ||
return reduce(tokens, camelizeKeys, assign({}, tokens)); | ||
case 'dashes': | ||
return reduce(tokens, camelizeDashedKeys, assign({}, tokens)); | ||
case 'dashes': | ||
return reduce(tokens, camelizeDashedKeys, assign({}, tokens)); | ||
case 'dashesOnly': | ||
return reduce(tokens, camelizeOnlyDashedKeys, {}); | ||
case 'dashesOnly': | ||
return reduce(tokens, camelizeOnlyDashedKeys, {}); | ||
case 'only': | ||
return reduce(tokens, camelizeOnlyKeys, {}); | ||
case 'only': | ||
return reduce(tokens, camelizeOnlyKeys, {}); | ||
} | ||
return tokens; | ||
} | ||
} |
'use strict'; | ||
const { | ||
difference, | ||
forEach, | ||
isArray, | ||
isBoolean, | ||
isFunction, | ||
isPlainObject, | ||
isRegExp, | ||
isString, | ||
keys, | ||
} = require('lodash'); | ||
var _require = require('lodash'), | ||
difference = _require.difference, | ||
forEach = _require.forEach, | ||
isArray = _require.isArray, | ||
isBoolean = _require.isBoolean, | ||
isFunction = _require.isFunction, | ||
isPlainObject = _require.isPlainObject, | ||
isRegExp = _require.isRegExp, | ||
isString = _require.isString, | ||
keys = _require.keys; | ||
const rules = { | ||
var rules = { | ||
// hook | ||
camelCase: 'boolean|string', | ||
devMode: 'boolean', | ||
extensions: 'array|string', | ||
ignore: 'function|regex|string', | ||
preprocessCss: 'function', | ||
processCss: 'function', | ||
processorOpts: 'object', | ||
camelCase: 'boolean|string', | ||
devMode: 'boolean', | ||
extensions: 'array|string', | ||
ignore: 'function|regex|string', | ||
preprocessCss: 'function', | ||
processCss: 'function', | ||
processorOpts: 'object', | ||
// plugins | ||
append: 'array', | ||
prepend: 'array', | ||
use: 'array', | ||
append: 'array', | ||
prepend: 'array', | ||
use: 'array', | ||
createImportedName: 'function', | ||
generateScopedName: 'function|string', | ||
hashPrefix: 'string', | ||
mode: 'string', | ||
rootDir: 'string', | ||
hashPrefix: 'string', | ||
mode: 'string', | ||
rootDir: 'string' | ||
}; | ||
const tests = { | ||
array: isArray, | ||
boolean: isBoolean, | ||
var tests = { | ||
array: isArray, | ||
boolean: isBoolean, | ||
function: isFunction, | ||
object: isPlainObject, | ||
regex: isRegExp, | ||
string: isString, | ||
object: isPlainObject, | ||
regex: isRegExp, | ||
string: isString | ||
}; | ||
module.exports = function validate(options) { | ||
const unknownOptions = difference(keys(options), keys(rules)); | ||
if (unknownOptions.length) { | ||
throw new Error(`unknown arguments: ${unknownOptions.join(', ')}.`); | ||
} | ||
var unknownOptions = difference(keys(options), keys(rules)); | ||
if (unknownOptions.length) throw new Error(`unknown arguments: ${unknownOptions.join(', ')}.`); | ||
forEach(rules, (types, rule) => { | ||
if (typeof options[rule] === 'undefined') { | ||
return; | ||
} | ||
forEach(rules, function (types, rule) { | ||
if (typeof options[rule] === 'undefined') return; | ||
if (!types.split('|').some(type => tests[type](options[rule]))) { | ||
throw new TypeError(`should specify ${types} as ${rule}`); | ||
} | ||
if (!types.split('|').some(function (type) { | ||
return tests[type](options[rule]); | ||
})) throw new TypeError(`should specify ${types} as ${rule}`); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "css-modules-require-hook", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "A require hook to compile CSS Modules on the fly", | ||
"main": "lib/index.js", | ||
"engines": { | ||
"node": ">=0.12" | ||
"node": ">= 4" | ||
}, | ||
"scripts": { | ||
"test": "npm run test:babel", | ||
"test:babel": "NODE_PATH=$(pwd)/test/tokens/node_modules $npm_package_scripts_test_unit --compilers js:babel-register", | ||
"test:coverage": "NODE_PATH=$(pwd)/test/tokens/node_modules babel-node --presets es2015 `npm bin`/isparta cover --report text --report html `npm bin`/_mocha -- --require test/setup.js --ui tdd test/*/*.js", | ||
"test:node": "NODE_PATH=$(pwd)/test/tokens/node_modules $npm_package_scripts_test_unit", | ||
"test:watch": "NODE_PATH=$(pwd)/test/tokens/node_modules $npm_package_scripts_test_unit --watch", | ||
"test:unit": "mocha --require test/setup.js --ui tdd test/*/*.js" | ||
}, | ||
"repository": { | ||
@@ -35,3 +27,28 @@ "type": "git", | ||
"homepage": "https://github.com/css-modules/css-modules-require-hook", | ||
"pre-commit": [], | ||
"eslintConfig": { | ||
"extends": "@sullenor/eslint-config/node", | ||
"rules": { | ||
"max-len": [ | ||
2, | ||
120 | ||
] | ||
} | ||
}, | ||
"pre-commit": [ | ||
"lint" | ||
], | ||
"devDependencies": { | ||
"@sullenor/eslint-config": "next", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-register": "^6.26.0", | ||
"eslint": "^4.6.1", | ||
"gulp": "^3.9.1", | ||
"gulp-babel": "^7.0.0", | ||
"gulp-debug": "^3.1.0", | ||
"jest": "^21.0.2", | ||
"mocha": "^3.5.1", | ||
"postcss-less": "^1.1.0", | ||
"pre-commit": "^1.2.2", | ||
"sinon": "^3.2.1" | ||
}, | ||
"dependencies": { | ||
@@ -47,2 +64,3 @@ "debug": "^2.2.0", | ||
"postcss-modules-parser": "^1.1.0", | ||
"postcss-modules-resolve-imports": "next", | ||
"postcss-modules-scope": "^1.0.0", | ||
@@ -52,11 +70,9 @@ "postcss-modules-values": "^1.1.1", | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.5.1", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-register": "^6.5.2", | ||
"isparta": "^4.0.0", | ||
"mocha": "^3.4.2", | ||
"postcss-less": "^1.0.1", | ||
"sinon": "^2.3.2" | ||
"scripts": { | ||
"lint": "eslint src/**/*.js", | ||
"prepublish": "npm run transpile", | ||
"pretest": "npm run transpile", | ||
"test": "NODE_PATH=$(pwd)/test/tokens/node_modules mocha --require test/setup.js --ui tdd test/*/*.js --compilers js:babel-register", | ||
"transpile": "gulp --cwd . --gulpfile scripts/gulpfile.js transpile" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
172832
13
12
9
256
+ Addedicss-utils@3.0.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedpostcss-modules-resolve-imports@1.3.0(transitive)