postcss-modules-values-replace
Advanced tools
Comparing version 3.4.0 to 4.0.0
137
index.js
@@ -5,3 +5,3 @@ const postcss = require('postcss'); | ||
const { CachedInputFileSystem, NodeJsInputFileSystem, ResolverFactory } = require('enhanced-resolve'); | ||
const valuesParser = require('postcss-values-parser'); | ||
const { parse } = require('postcss-values-parser'); | ||
const { urlToRequest } = require('loader-utils'); | ||
@@ -23,3 +23,3 @@ const ICSSUtils = require('icss-utils'); | ||
const replaceValueSymbols = (valueString, replacements) => { | ||
const value = valuesParser(valueString, { loose: true }).parse(); | ||
const value = parse(valueString, { ignoreUnknownWords: true }); | ||
@@ -137,3 +137,9 @@ value.walk((node) => { | ||
const walkerPlugin = postcss.plugin(INNER_PLUGIN, (fn, ...args) => fn.bind(null, ...args)); | ||
const walkerPlugin = (fn, ...args) => ({ | ||
postcssPlugin: INNER_PLUGIN, | ||
Once(root, { result }) { | ||
return fn.call(null, ...args, root, result); | ||
}, | ||
}); | ||
walkerPlugin.postcss = true; | ||
@@ -147,68 +153,85 @@ const factory = ({ | ||
replaceInSelectors = false, | ||
} = {}) => async (root, rootResult) => { | ||
const resolver = ResolverFactory.createResolver(Object.assign( | ||
{ fileSystem: fs }, | ||
resolveOptions, | ||
)); | ||
const resolve = promisify(resolver.resolve, resolver); | ||
const readFile = promisify(fs.readFile, fs); | ||
} = {}) => ({ | ||
postcssPlugin: PLUGIN, | ||
prepare(rootResult) { | ||
let definitions; | ||
let preprocessPlugins = []; | ||
if (preprocessValues) { | ||
const rootPlugins = rootResult.processor.plugins; | ||
const oursPluginIndex = rootPlugins | ||
.findIndex(plugin => plugin.postcssPlugin === PLUGIN); | ||
preprocessPlugins = rootPlugins.slice(0, oursPluginIndex); | ||
} | ||
return { | ||
async Once(root) { | ||
const resolver = ResolverFactory.createResolver(Object.assign( | ||
{ fileSystem: fs }, | ||
resolveOptions, | ||
)); | ||
const resolve = promisify(resolver.resolve, resolver); | ||
const readFile = promisify(fs.readFile, fs); | ||
const definitionCache = new Map(); | ||
async function walkFile(from, dir, requiredDefinitions) { | ||
const request = importsAsModuleRequests ? urlToRequest(from) : from; | ||
const resolvedFrom = await resolve(concordContext, dir, request); | ||
let preprocessPlugins = []; | ||
if (preprocessValues) { | ||
const rootPlugins = rootResult.processor.plugins; | ||
const oursPluginIndex = rootPlugins | ||
.findIndex(plugin => plugin.postcssPlugin === PLUGIN); | ||
preprocessPlugins = rootPlugins.slice(0, oursPluginIndex); | ||
} | ||
const cached = definitionCache.get(resolvedFrom); | ||
if (cached) { | ||
return cached; | ||
} | ||
const definitionCache = new Map(); | ||
async function walkFile(from, dir, requiredDefinitions) { | ||
const request = importsAsModuleRequests ? urlToRequest(from) : from; | ||
const resolvedFrom = await resolve(concordContext, dir, request); | ||
const content = await readFile(resolvedFrom); | ||
const plugins = [ | ||
...preprocessPlugins, | ||
walkerPlugin(walk, requiredDefinitions, walkFile), | ||
]; | ||
const result = await postcss(plugins) | ||
.process(content, { from: resolvedFrom }); | ||
const cached = definitionCache.get(resolvedFrom); | ||
if (cached) { | ||
return cached; | ||
} | ||
definitionCache.set(resolvedFrom, result.messages[0].value); | ||
const content = await readFile(resolvedFrom); | ||
const plugins = [ | ||
...preprocessPlugins, | ||
walkerPlugin(walk, requiredDefinitions, walkFile), | ||
]; | ||
const result = await postcss(plugins) | ||
.process(content, { from: resolvedFrom }); | ||
return result.messages[0].value; | ||
} | ||
definitionCache.set(resolvedFrom, result.messages[0].value); | ||
const definitions = await walk(null, walkFile, root, rootResult); | ||
rootResult.messages.push({ | ||
plugin: PLUGIN, | ||
type: 'values', | ||
values: definitions, | ||
}); | ||
return result.messages[0].value; | ||
} | ||
root.walk((node) => { | ||
if (node.type === 'decl') { | ||
// eslint-disable-next-line no-param-reassign | ||
node.value = replaceValueSymbols(node.value, definitions); | ||
} else if (node.type === 'atrule' && node.name === 'media') { | ||
// eslint-disable-next-line no-param-reassign | ||
node.params = replaceValueSymbols(node.params, definitions); | ||
} else if (replaceInSelectors && node.type === 'rule') { | ||
// eslint-disable-next-line no-param-reassign | ||
node.selector = ICSSUtils.replaceValueSymbols(node.selector, definitions); | ||
} else if (noEmitExports && node.type === 'atrule' && node.name === 'value') { | ||
node.remove(); | ||
} | ||
}); | ||
}; | ||
definitions = await walk(null, walkFile, root, rootResult); | ||
rootResult.messages.push({ | ||
plugin: PLUGIN, | ||
type: 'values', | ||
values: definitions, | ||
}); | ||
}, | ||
Declaration(node) { | ||
// eslint-disable-next-line no-param-reassign | ||
node.value = replaceValueSymbols(node.value, definitions); | ||
}, | ||
AtRule: { | ||
media(node) { | ||
// eslint-disable-next-line no-param-reassign | ||
node.params = replaceValueSymbols(node.params, definitions); | ||
}, | ||
value(node) { | ||
if (noEmitExports) { | ||
node.remove(); | ||
} | ||
}, | ||
}, | ||
Rule(node) { | ||
if (replaceInSelectors) { | ||
// eslint-disable-next-line no-param-reassign | ||
node.selector = ICSSUtils.replaceValueSymbols(node.selector, definitions); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); | ||
const plugin = postcss.plugin(PLUGIN, factory); | ||
const plugin = factory; | ||
plugin.postcss = true; | ||
module.exports = plugin; | ||
exports.default = plugin; | ||
{ | ||
"name": "postcss-modules-values-replace", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"description": "PostCSS plugin to work around CSS Modules values limitations", | ||
@@ -24,17 +24,19 @@ "keywords": [ | ||
"es6-promisify": "^5.0.0", | ||
"icss-utils": "^4.0.0", | ||
"icss-utils": "^5.1.0", | ||
"loader-utils": "^2.0.0", | ||
"postcss": "^7.0.0", | ||
"postcss-values-parser": "^1.3.1" | ||
"postcss-values-parser": "^6.0.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.24.0", | ||
"eslint": "^4.15.0", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"eslint-plugin-import": "^2.5.0" | ||
"eslint-plugin-import": "^2.5.0", | ||
"vitest": "^0.29.2" | ||
}, | ||
"peerDependencies": { | ||
"postcss": "^8.2.9" | ||
}, | ||
"scripts": { | ||
"lint": "eslint *.js", | ||
"prepublish": "npm test", | ||
"test": "npm run lint && ava" | ||
"prepublish": "npm run lint && npm test -- --run", | ||
"test": "vitest" | ||
}, | ||
@@ -41,0 +43,0 @@ "eslintConfig": { |
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
13580
188
+ Addedcolor-name@1.1.4(transitive)
+ Addedicss-utils@5.1.0(transitive)
+ Addedis-url-superb@4.0.0(transitive)
+ Addednanoid@3.3.8(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpostcss@8.4.49(transitive)
+ Addedpostcss-values-parser@6.0.2(transitive)
+ Addedquote-unquote@1.0.0(transitive)
+ Addedsource-map-js@1.2.1(transitive)
- Removedpostcss@^7.0.0
- Removedflatten@1.0.3(transitive)
- Removedicss-utils@4.1.1(transitive)
- Removedindexes-of@1.0.1(transitive)
- Removedpicocolors@0.2.1(transitive)
- Removedpostcss@7.0.39(transitive)
- Removedpostcss-values-parser@1.5.0(transitive)
- Removedsource-map@0.6.1(transitive)
- Removeduniq@1.0.1(transitive)
Updatedicss-utils@^5.1.0
Updatedpostcss-values-parser@^6.0.2