postcss-modules
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -6,3 +6,6 @@ 'use strict'; | ||
}); | ||
exports.defaultPlugins = undefined; | ||
exports.defaultPlugins = exports.behaviours = undefined; | ||
var _defaultPlugins; | ||
exports.isValidBehaviour = isValidBehaviour; | ||
@@ -16,10 +19,13 @@ | ||
var defaultPlugins = exports.defaultPlugins = { | ||
local: [_cssModulesLoaderCore2.default.values, _cssModulesLoaderCore2.default.localByDefault, _cssModulesLoaderCore2.default.extractImports, _cssModulesLoaderCore2.default.scope], | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
global: [_cssModulesLoaderCore2.default.values, _cssModulesLoaderCore2.default.extractImports, _cssModulesLoaderCore2.default.scope] | ||
var behaviours = exports.behaviours = { | ||
LOCAL: 'local', | ||
GLOBAL: 'global' | ||
}; | ||
var defaultPlugins = exports.defaultPlugins = (_defaultPlugins = {}, _defineProperty(_defaultPlugins, behaviours.LOCAL, [_cssModulesLoaderCore2.default.values, _cssModulesLoaderCore2.default.localByDefault, _cssModulesLoaderCore2.default.extractImports, _cssModulesLoaderCore2.default.scope]), _defineProperty(_defaultPlugins, behaviours.GLOBAL, [_cssModulesLoaderCore2.default.values, _cssModulesLoaderCore2.default.extractImports, _cssModulesLoaderCore2.default.scope]), _defaultPlugins); | ||
function isValidBehaviour(behaviour) { | ||
return behaviour === 'local' || behaviour === 'global'; | ||
return Object.values(behaviours).includes(behaviour); | ||
} |
@@ -37,38 +37,59 @@ 'use strict'; | ||
module.exports = _postcss2.default.plugin('postcss-modules', function () { | ||
var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var PLUGIN_NAME = 'postcss-modules'; | ||
var scopedNameGenerator = opts.generateScopedName || _generateScopedName2.default; | ||
var getJSON = opts.getJSON || _saveJSON2.default; | ||
var scopeBehaviour = 'local'; | ||
function getDefaultScopeBehaviour(opts) { | ||
if (opts.scopeBehaviour && (0, _behaviours.isValidBehaviour)(opts.scopeBehaviour)) { | ||
scopeBehaviour = opts.scopeBehaviour; | ||
return opts.scopeBehaviour; | ||
} | ||
if (typeof scopedNameGenerator === 'function') { | ||
_cssModulesLoaderCore2.default.scope.generateScopedName = scopedNameGenerator; | ||
} else { | ||
_cssModulesLoaderCore2.default.scope.generateScopedName = (0, _genericNames2.default)(scopedNameGenerator, { | ||
context: process.cwd() | ||
return _behaviours.behaviours.LOCAL; | ||
} | ||
function getScopedNameGenerator(opts) { | ||
var scopedNameGenerator = opts.generateScopedName || _generateScopedName2.default; | ||
if (typeof scopedNameGenerator === 'function') return scopedNameGenerator; | ||
return (0, _genericNames2.default)(scopedNameGenerator, { context: process.cwd() }); | ||
} | ||
function getLoader(opts, plugins) { | ||
return typeof opts.Loader === 'function' ? new opts.Loader('/', plugins) : new _fileSystemLoader2.default('/', plugins); | ||
} | ||
function getDefaultPlugins(opts, behaviour, inputFile) { | ||
var globalModulesWhitelist = opts.globalModulePaths || null; | ||
if (globalModulesWhitelist) { | ||
var isGlobalModule = globalModulesWhitelist.some(function (regex) { | ||
return inputFile.match(regex); | ||
}); | ||
return _behaviours.defaultPlugins[isGlobalModule ? _behaviours.behaviours.GLOBAL : _behaviours.behaviours.LOCAL]; | ||
} | ||
return function (css, result) { | ||
var resultPlugins = result.processor.plugins.filter(function (plugin) { | ||
return plugin.postcssPlugin !== 'postcss-modules'; | ||
}); | ||
return _behaviours.defaultPlugins[behaviour]; | ||
} | ||
var plugins = [].concat(_toConsumableArray(_behaviours.defaultPlugins[scopeBehaviour]), _toConsumableArray(resultPlugins)); | ||
function isResultPlugin(plugin) { | ||
return plugin.postcssPlugin !== PLUGIN_NAME; | ||
} | ||
var loader = typeof opts.Loader === 'function' ? new opts.Loader('/', plugins) : new _fileSystemLoader2.default('/', plugins); | ||
module.exports = _postcss2.default.plugin(PLUGIN_NAME, function () { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var getJSON = opts.getJSON || _saveJSON2.default; | ||
var defaultScopeBehaviour = getDefaultScopeBehaviour(opts); | ||
_cssModulesLoaderCore2.default.scope.generateScopedName = getScopedNameGenerator(opts); | ||
return function (css, result) { | ||
var inputFile = css.source.input.file; | ||
var resultPlugins = result.processor.plugins.filter(isResultPlugin); | ||
var pluginList = getDefaultPlugins(opts, defaultScopeBehaviour, inputFile); | ||
var plugins = [].concat(_toConsumableArray(pluginList), _toConsumableArray(resultPlugins)); | ||
var loader = getLoader(opts, plugins); | ||
var parser = new _parser2.default(loader.fetch.bind(loader)); | ||
var promise = new Promise(function (resolve, reject) { | ||
(0, _postcss2.default)([].concat(_toConsumableArray(plugins), [parser.plugin])).process(css, { from: css.source.input.file }).then(function () { | ||
return new Promise(function (resolve, reject) { | ||
(0, _postcss2.default)([].concat(_toConsumableArray(plugins), [parser.plugin])).process(css, { from: inputFile }).then(function () { | ||
var out = loader.finalSource; | ||
if (out) { | ||
css.prepend(out); | ||
} | ||
if (out) css.prepend(out); | ||
@@ -80,5 +101,3 @@ getJSON(css.source.input.file, parser.exportTokens); | ||
}); | ||
return promise; | ||
}; | ||
}); |
@@ -0,1 +1,5 @@ | ||
## 0.6.0 | ||
* Added `globalModulePaths` option (Thanks to @pospi). | ||
* Refactored all the things. | ||
## 0.5.2 | ||
@@ -2,0 +6,0 @@ * Updated dependencies |
{ | ||
"name": "postcss-modules", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "PostCSS plugin to use CSS Modules everywhere", | ||
@@ -22,17 +22,17 @@ "main": "build/index.js", | ||
"css-modules-loader-core": "^1.0.1", | ||
"generic-names": "^1.0.1", | ||
"postcss": "^5.1.2", | ||
"string-hash": "^1.1.0" | ||
"generic-names": "^1.0.2", | ||
"postcss": "^5.2.8", | ||
"string-hash": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"autoprefixer": "^6.4.0", | ||
"ava": "^0.16.0", | ||
"autoprefixer": "^6.6.0", | ||
"ava": "^0.17.0", | ||
"babel": "^6.5.2", | ||
"babel-cli": "^6.11.4", | ||
"babel-core": "^6.13.2", | ||
"babel-eslint": "^6.1.2", | ||
"babel-preset-es2015": "^6.13.2", | ||
"eslint": "^3.3.1", | ||
"eslint-config-airbnb-base": "^5.0.2", | ||
"eslint-plugin-import": "^1.13.0", | ||
"babel-cli": "^6.18.0", | ||
"babel-core": "^6.21.0", | ||
"babel-eslint": "^7.1.1", | ||
"babel-preset-es2015": "^6.18.0", | ||
"eslint": "^3.12.2", | ||
"eslint-config-airbnb-base": "^11.0.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"file-exists": "^2.0.0" | ||
@@ -42,7 +42,8 @@ }, | ||
"pretest": "$(npm bin)/eslint src test", | ||
"test": "$(npm bin)/ava --require babel-core/register", | ||
"test": "$(npm bin)/babel-node $(npm bin)/ava", | ||
"transpile": "$(npm bin)/babel src -d build", | ||
"prepublish": "npm run transpile", | ||
"postpublish": "rm -rf build && git push --follow-tags" | ||
} | ||
}, | ||
"require": "babel-core/register" | ||
} |
@@ -94,3 +94,3 @@ # postcss-modules [![Build Status][ci-img]][ci] | ||
} | ||
}); | ||
}) | ||
]); | ||
@@ -108,2 +108,13 @@ ``` | ||
scopeBehaviour: 'global' // can be 'global' or 'local', | ||
}) | ||
]); | ||
``` | ||
To define paths for global modules, use the `globalModulePaths` option. | ||
It is an array with regular expressions defining the paths: | ||
```js | ||
postcss([ | ||
require('postcss-modules')({ | ||
globalModulePaths: [/path\/to\/legacy-styles/, /another\/paths/], | ||
}); | ||
@@ -126,3 +137,3 @@ ]); | ||
} | ||
}); | ||
}) | ||
]); | ||
@@ -138,3 +149,3 @@ ``` | ||
generateScopedName: '[name]__[local]___[hash:base64:5]', | ||
}); | ||
}) | ||
]); | ||
@@ -151,3 +162,3 @@ ``` | ||
Loader: CustomLoader, | ||
}); | ||
}) | ||
]); | ||
@@ -154,0 +165,0 @@ ``` |
47664
11
107
243
Updatedgeneric-names@^1.0.2
Updatedpostcss@^5.2.8
Updatedstring-hash@^1.1.1