postcss-modules
Advanced tools
Comparing version 4.3.1 to 5.0.0
@@ -6,28 +6,21 @@ "use strict"; | ||
}); | ||
exports.behaviours = undefined; | ||
exports.behaviours = void 0; | ||
exports.getDefaultPlugins = getDefaultPlugins; | ||
exports.isValidBehaviour = isValidBehaviour; | ||
var _postcssModulesLocalByDefault = require("postcss-modules-local-by-default"); | ||
var _postcssModulesLocalByDefault = _interopRequireDefault(require("postcss-modules-local-by-default")); | ||
var _postcssModulesLocalByDefault2 = _interopRequireDefault(_postcssModulesLocalByDefault); | ||
var _postcssModulesExtractImports = _interopRequireDefault(require("postcss-modules-extract-imports")); | ||
var _postcssModulesExtractImports = require("postcss-modules-extract-imports"); | ||
var _postcssModulesScope = _interopRequireDefault(require("postcss-modules-scope")); | ||
var _postcssModulesExtractImports2 = _interopRequireDefault(_postcssModulesExtractImports); | ||
var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values")); | ||
var _postcssModulesScope = require("postcss-modules-scope"); | ||
var _postcssModulesScope2 = _interopRequireDefault(_postcssModulesScope); | ||
var _postcssModulesValues = require("postcss-modules-values"); | ||
var _postcssModulesValues2 = _interopRequireDefault(_postcssModulesValues); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const behaviours = exports.behaviours = { | ||
const behaviours = { | ||
LOCAL: "local", | ||
GLOBAL: "global" | ||
}; | ||
exports.behaviours = behaviours; | ||
@@ -39,9 +32,14 @@ function getDefaultPlugins({ | ||
}) { | ||
const scope = (0, _postcssModulesScope2.default)({ generateScopedName, exportGlobals }); | ||
const scope = (0, _postcssModulesScope.default)({ | ||
generateScopedName, | ||
exportGlobals | ||
}); | ||
const plugins = { | ||
[behaviours.LOCAL]: [_postcssModulesValues2.default, (0, _postcssModulesLocalByDefault2.default)({ mode: 'local' }), _postcssModulesExtractImports2.default, scope], | ||
[behaviours.GLOBAL]: [_postcssModulesValues2.default, (0, _postcssModulesLocalByDefault2.default)({ mode: 'global' }), _postcssModulesExtractImports2.default, scope] | ||
[behaviours.LOCAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({ | ||
mode: 'local' | ||
}), _postcssModulesExtractImports.default, scope], | ||
[behaviours.GLOBAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({ | ||
mode: 'global' | ||
}), _postcssModulesExtractImports.default, scope] | ||
}; | ||
return plugins[behaviour]; | ||
@@ -48,0 +46,0 @@ } |
@@ -6,23 +6,15 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
var _postcss = require("postcss"); | ||
var _postcss = _interopRequireDefault(require("postcss")); | ||
var _postcss2 = _interopRequireDefault(_postcss); | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _fs = require("fs"); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _parser = _interopRequireDefault(require("./parser")); | ||
var _path = require("path"); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _parser = require("./parser"); | ||
var _parser2 = _interopRequireDefault(_parser); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Copied from https://github.com/css-modules/css-modules-loader-core | ||
class Core { | ||
@@ -34,5 +26,6 @@ constructor(plugins) { | ||
load(sourceString, sourcePath, trace, pathFetcher) { | ||
let parser = new _parser2.default(pathFetcher, trace); | ||
return (0, _postcss2.default)(this.plugins.concat([parser.plugin()])).process(sourceString, { from: "/" + sourcePath }).then(result => { | ||
let parser = new _parser.default(pathFetcher, trace); | ||
return (0, _postcss.default)(this.plugins.concat([parser.plugin()])).process(sourceString, { | ||
from: sourcePath | ||
}).then(result => { | ||
return { | ||
@@ -44,5 +37,4 @@ injectableSource: result.css, | ||
} | ||
} | ||
// Sorts dependencies in the following way: | ||
} // Sorts dependencies in the following way: | ||
// AAA comes before AA and A | ||
@@ -54,2 +46,4 @@ // AB comes after AA and before A | ||
// - After all their dependencies | ||
const traceKeySorter = (a, b) => { | ||
@@ -67,2 +61,12 @@ if (a.length < b.length) { | ||
constructor(root, plugins) { | ||
if (root === '/' && process.platform === "win32") { | ||
const cwdDrive = process.cwd().slice(0, 3); | ||
if (!/^[A-Z]:\\$/.test(cwdDrive)) { | ||
throw new Error(`Failed to obtain root from "${process.cwd()}".`); | ||
} | ||
root = cwdDrive; | ||
} | ||
this.root = root; | ||
@@ -79,13 +83,13 @@ this.sources = {}; | ||
trace = _trace || String.fromCharCode(this.importNr++); | ||
return new Promise((resolve, reject) => { | ||
let relativeDir = _path2.default.dirname(relativeTo), | ||
rootRelativePath = _path2.default.resolve(relativeDir, newPath), | ||
fileRelativePath = _path2.default.resolve(_path2.default.join(this.root, relativeDir), newPath); | ||
let relativeDir = _path.default.dirname(relativeTo), | ||
rootRelativePath = _path.default.resolve(relativeDir, newPath), | ||
fileRelativePath = _path.default.resolve(_path.default.resolve(this.root, relativeDir), newPath); // if the path is not relative or absolute, try to resolve it in node_modules | ||
// if the path is not relative or absolute, try to resolve it in node_modules | ||
if (newPath[0] !== "." && newPath[0] !== "/") { | ||
if (newPath[0] !== "." && !_path.default.isAbsolute(newPath)) { | ||
try { | ||
fileRelativePath = require.resolve(newPath); | ||
} catch (e) { | ||
// noop | ||
} catch (e) {// noop | ||
} | ||
@@ -95,2 +99,3 @@ } | ||
const tokens = this.tokensByFile[fileRelativePath]; | ||
if (tokens) { | ||
@@ -100,5 +105,8 @@ return resolve(tokens); | ||
_fs2.default.readFile(fileRelativePath, "utf-8", (err, source) => { | ||
_fs.default.readFile(fileRelativePath, "utf-8", (err, source) => { | ||
if (err) reject(err); | ||
this.core.load(source, rootRelativePath, trace, this.fetch.bind(this)).then(({ injectableSource, exportTokens }) => { | ||
this.core.load(source, rootRelativePath, trace, this.fetch.bind(this)).then(({ | ||
injectableSource, | ||
exportTokens | ||
}) => { | ||
this.sources[fileRelativePath] = injectableSource; | ||
@@ -117,14 +125,16 @@ this.traces[trace] = fileRelativePath; | ||
let written = new Set(); | ||
return Object.keys(traces).sort(traceKeySorter).map(key => { | ||
const filename = traces[key]; | ||
if (written.has(filename)) { | ||
return null; | ||
} | ||
written.add(filename); | ||
return sources[filename]; | ||
}).join(""); | ||
} | ||
} | ||
exports.default = FileSystemLoader; |
@@ -6,12 +6,11 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
var _icssReplaceSymbols = require("icss-replace-symbols"); | ||
var _icssReplaceSymbols = _interopRequireDefault(require("icss-replace-symbols")); | ||
var _icssReplaceSymbols2 = _interopRequireDefault(_icssReplaceSymbols); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Copied from https://github.com/css-modules/css-modules-loader-core | ||
const importRegexp = /^:import\((.+)\)$/; | ||
const importRegexp = /^:import\((.+)\)$/; | ||
class Parser { | ||
@@ -30,5 +29,7 @@ constructor(pathFetcher, trace) { | ||
postcssPlugin: "css-modules-parser", | ||
OnceExit(css) { | ||
return Promise.all(parser.fetchAllImports(css)).then(() => parser.linkImportedSymbols(css)).then(() => parser.extractExports(css)); | ||
} | ||
}; | ||
@@ -48,3 +49,3 @@ } | ||
linkImportedSymbols(css) { | ||
(0, _icssReplaceSymbols2.default)(css, this.translations); | ||
(0, _icssReplaceSymbols.default)(css, this.translations); | ||
} | ||
@@ -82,3 +83,5 @@ | ||
} | ||
} | ||
exports.default = Parser; |
@@ -8,6 +8,4 @@ "use strict"; | ||
var _stringHash = require("string-hash"); | ||
var _stringHash = _interopRequireDefault(require("string-hash")); | ||
var _stringHash2 = _interopRequireDefault(_stringHash); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -18,5 +16,4 @@ | ||
const lineNumber = css.substr(0, i).split(/[\r\n]/).length; | ||
const hash = (0, _stringHash2.default)(css).toString(36).substr(0, 5); | ||
const hash = (0, _stringHash.default)(css).toString(36).substr(0, 5); | ||
return `_${name}_${hash}_${lineNumber}`; | ||
} |
"use strict"; | ||
var _postcss = require("postcss"); | ||
var _postcss = _interopRequireDefault(require("postcss")); | ||
var _postcss2 = _interopRequireDefault(_postcss); | ||
var _lodash = _interopRequireDefault(require("lodash.camelcase")); | ||
var _lodash = require("lodash.camelcase"); | ||
var _genericNames = _interopRequireDefault(require("generic-names")); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _unquote = _interopRequireDefault(require("./unquote")); | ||
var _genericNames = require("generic-names"); | ||
var _parser = _interopRequireDefault(require("./css-loader-core/parser")); | ||
var _genericNames2 = _interopRequireDefault(_genericNames); | ||
var _loader = _interopRequireDefault(require("./css-loader-core/loader")); | ||
var _unquote = require("./unquote"); | ||
var _generateScopedName = _interopRequireDefault(require("./generateScopedName")); | ||
var _unquote2 = _interopRequireDefault(_unquote); | ||
var _saveJSON = _interopRequireDefault(require("./saveJSON")); | ||
var _parser = require("./css-loader-core/parser"); | ||
var _parser2 = _interopRequireDefault(_parser); | ||
var _loader = require("./css-loader-core/loader"); | ||
var _loader2 = _interopRequireDefault(_loader); | ||
var _generateScopedName = require("./generateScopedName"); | ||
var _generateScopedName2 = _interopRequireDefault(_generateScopedName); | ||
var _saveJSON = require("./saveJSON"); | ||
var _saveJSON2 = _interopRequireDefault(_saveJSON); | ||
var _behaviours = require("./behaviours"); | ||
@@ -39,4 +23,2 @@ | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
const PLUGIN_NAME = "postcss-modules"; | ||
@@ -53,6 +35,5 @@ | ||
function getScopedNameGenerator(opts) { | ||
const scopedNameGenerator = opts.generateScopedName || _generateScopedName2.default; | ||
const scopedNameGenerator = opts.generateScopedName || _generateScopedName.default; | ||
if (typeof scopedNameGenerator === "function") return scopedNameGenerator; | ||
return (0, _genericNames2.default)(scopedNameGenerator, { | ||
return (0, _genericNames.default)(scopedNameGenerator, { | ||
context: process.cwd(), | ||
@@ -65,3 +46,3 @@ hashPrefix: opts.hashPrefix | ||
const root = typeof opts.root === "undefined" ? "/" : opts.root; | ||
return typeof opts.Loader === "function" ? new opts.Loader(root, plugins) : new _loader2.default(root, plugins); | ||
return typeof opts.Loader === "function" ? new opts.Loader(root, plugins) : new _loader.default(root, plugins); | ||
} | ||
@@ -105,79 +86,76 @@ | ||
postcssPlugin: PLUGIN_NAME, | ||
OnceExit(css, { result }) { | ||
return _asyncToGenerator(function* () { | ||
const getJSON = opts.getJSON || _saveJSON2.default; | ||
const inputFile = css.source.input.file; | ||
const pluginList = getDefaultPluginsList(opts, inputFile); | ||
const resultPluginIndex = result.processor.plugins.findIndex(function (plugin) { | ||
return isOurPlugin(plugin); | ||
}); | ||
if (resultPluginIndex === -1) { | ||
throw new Error('Plugin missing from options.'); | ||
} | ||
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex); | ||
const loaderPlugins = [...earlierPlugins, ...pluginList]; | ||
const loader = getLoader(opts, loaderPlugins); | ||
const fetcher = function fetcher(file, relativeTo, depTrace) { | ||
const unquoteFile = (0, _unquote2.default)(file); | ||
const resolvedResult = typeof opts.resolve === 'function' && opts.resolve(unquoteFile); | ||
const resolvedFile = resolvedResult instanceof Promise ? resolvedResult : Promise.resolve(resolvedResult); | ||
return resolvedFile.then(function (f) { | ||
return loader.fetch.call(loader, `"${f || unquoteFile}"`, relativeTo, depTrace); | ||
}); | ||
}; | ||
const parser = new _parser2.default(fetcher); | ||
async OnceExit(css, { | ||
result | ||
}) { | ||
const getJSON = opts.getJSON || _saveJSON.default; | ||
const inputFile = css.source.input.file; | ||
const pluginList = getDefaultPluginsList(opts, inputFile); | ||
const resultPluginIndex = result.processor.plugins.findIndex(plugin => isOurPlugin(plugin)); | ||
yield (0, _postcss2.default)([...pluginList, parser.plugin()]).process(css, { | ||
from: inputFile | ||
}); | ||
if (resultPluginIndex === -1) { | ||
throw new Error('Plugin missing from options.'); | ||
} | ||
const out = loader.finalSource; | ||
if (out) css.prepend(out); | ||
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex); | ||
const loaderPlugins = [...earlierPlugins, ...pluginList]; | ||
const loader = getLoader(opts, loaderPlugins); | ||
if (opts.localsConvention) { | ||
const isFunc = typeof opts.localsConvention === "function"; | ||
const fetcher = (file, relativeTo, depTrace) => { | ||
const unquoteFile = (0, _unquote.default)(file); | ||
const resolvedResult = typeof opts.resolve === 'function' && opts.resolve(unquoteFile); | ||
const resolvedFile = resolvedResult instanceof Promise ? resolvedResult : Promise.resolve(resolvedResult); | ||
return resolvedFile.then(f => { | ||
return loader.fetch.call(loader, `"${f || unquoteFile}"`, relativeTo, depTrace); | ||
}); | ||
}; | ||
parser.exportTokens = Object.entries(parser.exportTokens).reduce(function (tokens, [className, value]) { | ||
if (isFunc) { | ||
tokens[opts.localsConvention(className, value, inputFile)] = value; | ||
const parser = new _parser.default(fetcher); | ||
await (0, _postcss.default)([...pluginList, parser.plugin()]).process(css, { | ||
from: inputFile | ||
}); | ||
const out = loader.finalSource; | ||
if (out) css.prepend(out); | ||
return tokens; | ||
} | ||
if (opts.localsConvention) { | ||
const isFunc = typeof opts.localsConvention === "function"; | ||
parser.exportTokens = Object.entries(parser.exportTokens).reduce((tokens, [className, value]) => { | ||
if (isFunc) { | ||
tokens[opts.localsConvention(className, value, inputFile)] = value; | ||
return tokens; | ||
} | ||
switch (opts.localsConvention) { | ||
case "camelCase": | ||
tokens[className] = value; | ||
tokens[(0, _lodash2.default)(className)] = value; | ||
switch (opts.localsConvention) { | ||
case "camelCase": | ||
tokens[className] = value; | ||
tokens[(0, _lodash.default)(className)] = value; | ||
break; | ||
break; | ||
case "camelCaseOnly": | ||
tokens[(0, _lodash2.default)(className)] = value; | ||
case "camelCaseOnly": | ||
tokens[(0, _lodash.default)(className)] = value; | ||
break; | ||
break; | ||
case "dashes": | ||
tokens[className] = value; | ||
tokens[dashesCamelCase(className)] = value; | ||
case "dashes": | ||
tokens[className] = value; | ||
tokens[dashesCamelCase(className)] = value; | ||
break; | ||
break; | ||
case "dashesOnly": | ||
tokens[dashesCamelCase(className)] = value; | ||
case "dashesOnly": | ||
tokens[dashesCamelCase(className)] = value; | ||
break; | ||
} | ||
break; | ||
} | ||
return tokens; | ||
}, {}); | ||
} | ||
return tokens; | ||
}, {}); | ||
} | ||
result.messages.push({ | ||
type: "export", | ||
plugin: "postcss-modules", | ||
exportTokens: parser.exportTokens | ||
}); // getJSON may return a promise | ||
result.messages.push({ | ||
type: "export", | ||
plugin: "postcss-modules", | ||
exportTokens: parser.exportTokens | ||
}); | ||
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to); | ||
} | ||
// getJSON may return a promise | ||
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to); | ||
})(); | ||
} | ||
}; | ||
@@ -184,0 +162,0 @@ }; |
@@ -8,3 +8,2 @@ "use strict"; | ||
// copied from https://github.com/lakenen/node-unquote | ||
var reg = /['"]/; | ||
@@ -16,9 +15,12 @@ | ||
} | ||
if (reg.test(str.charAt(0))) { | ||
str = str.substr(1); | ||
} | ||
if (reg.test(str.charAt(str.length - 1))) { | ||
str = str.substr(0, str.length - 1); | ||
} | ||
return str; | ||
} |
# Changelog | ||
## 5.0.0 | ||
- Fixed `composes` on Windows by @sapphi-red https://github.com/madyankin/postcss-modules/pull/135 | ||
- Updated Babel to v7 by @sapphi-red. Minimal supported version of Node.js is 10 from now https://github.com/madyankin/postcss-modules/pull/134 | ||
## 4.3.1 | ||
@@ -4,0 +9,0 @@ |
{ | ||
"name": "postcss-modules", | ||
"version": "4.3.1", | ||
"version": "5.0.0", | ||
"description": "PostCSS plugin to use CSS Modules everywhere", | ||
@@ -35,7 +35,8 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.17.10", | ||
"@babel/core": "^7.18.0", | ||
"@babel/eslint-parser": "^7.17.0", | ||
"@babel/preset-env": "^7.18.0", | ||
"@babel/register": "^7.17.7", | ||
"autoprefixer": "^10.0.2", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-eslint": "^10.1.0", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^7.3.1", | ||
@@ -65,3 +66,3 @@ "eslint-plugin-import": "^2.21.2", | ||
}, | ||
"require": "babel-core/register" | ||
"require": "@babel/register" | ||
} |
@@ -8,9 +8,2 @@ # postcss-modules | ||
## Support the developer | ||
I maintain the plugin in my free time, so I don't receive any payment for this work. | ||
To have better docs, new features and integrations with frameworks, you can [support me on Patreon](https://www.patreon.com/bePatron?u=25976212). | ||
The sponsors are listed [below in the README](#sponsors) | ||
## What is this? | ||
@@ -17,0 +10,0 @@ |
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
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
13
436
31640
14
326