postcss-custom-media
Advanced tools
Comparing version 7.0.4 to 7.0.5
# Changes to PostCSS Custom Media | ||
### 7.0.5 (October 5, 2018) | ||
- Fixed: Possible issues resolving paths to imports and exports | ||
- Added: Imports from `customMedia` and `custom-media` simultaneously | ||
- Updated: `postcss` to 7.0.5 | ||
### 7.0.4 (September 23, 2018) | ||
@@ -4,0 +10,0 @@ |
378
index.cjs.js
@@ -288,3 +288,3 @@ 'use strict'; | ||
const mediaRegExp = new RegExp(`^(?:${modifierRE}${spaceRE})?(?:${typeRE}(?:${andRE}${spaceRE}${expressionRE})?|${expressionRE})$`, 'i'); | ||
var getMediaAstFromMediaString = (string => new MediaQueryList(string)); | ||
var mediaASTFromString = (string => new MediaQueryList(string)); | ||
@@ -304,3 +304,3 @@ var getCustomMedia = ((root, opts) => { | ||
customMedias[name] = getMediaAstFromMediaString(selectors); // conditionally remove the custom selector atrule | ||
customMedias[name] = mediaASTFromString(selectors); // conditionally remove the custom selector atrule | ||
@@ -321,2 +321,146 @@ if (!Object(opts).preserve) { | ||
/* Get Custom Media from CSS File | ||
/* ========================================================================== */ | ||
function getCustomMediaFromCSSFile(_x) { | ||
return _getCustomMediaFromCSSFile.apply(this, arguments); | ||
} | ||
/* Get Custom Media from Object | ||
/* ========================================================================== */ | ||
function _getCustomMediaFromCSSFile() { | ||
_getCustomMediaFromCSSFile = _asyncToGenerator(function* (from) { | ||
const css = yield readFile(from); | ||
const root = postcss.parse(css, { | ||
from | ||
}); | ||
return getCustomMedia(root, { | ||
preserve: true | ||
}); | ||
}); | ||
return _getCustomMediaFromCSSFile.apply(this, arguments); | ||
} | ||
function getCustomMediaFromObject(object) { | ||
const customMedia = Object.assign({}, Object(object).customMedia, Object(object)['custom-media']); | ||
for (const key in customMedia) { | ||
customMedia[key] = mediaASTFromString(customMedia[key]); | ||
} | ||
return customMedia; | ||
} | ||
/* Get Custom Media from JSON file | ||
/* ========================================================================== */ | ||
function getCustomMediaFromJSONFile(_x2) { | ||
return _getCustomMediaFromJSONFile.apply(this, arguments); | ||
} | ||
/* Get Custom Media from JS file | ||
/* ========================================================================== */ | ||
function _getCustomMediaFromJSONFile() { | ||
_getCustomMediaFromJSONFile = _asyncToGenerator(function* (from) { | ||
const object = yield readJSON(from); | ||
return getCustomMediaFromObject(object); | ||
}); | ||
return _getCustomMediaFromJSONFile.apply(this, arguments); | ||
} | ||
function getCustomMediaFromJSFile(_x3) { | ||
return _getCustomMediaFromJSFile.apply(this, arguments); | ||
} | ||
/* Get Custom Media from Sources | ||
/* ========================================================================== */ | ||
function _getCustomMediaFromJSFile() { | ||
_getCustomMediaFromJSFile = _asyncToGenerator(function* (from) { | ||
const object = yield Promise.resolve(require(from)); | ||
return getCustomMediaFromObject(object); | ||
}); | ||
return _getCustomMediaFromJSFile.apply(this, arguments); | ||
} | ||
function getCustomMediaFromSources(sources) { | ||
return sources.map(source => { | ||
if (source instanceof Promise) { | ||
return source; | ||
} else if (source instanceof Function) { | ||
return source(); | ||
} // read the source as an object | ||
const opts = source === Object(source) ? source : { | ||
from: String(source) | ||
}; // skip objects with custom media | ||
if (Object(opts).customMedia || Object(opts)['custom-media']) { | ||
return opts; | ||
} // source pathname | ||
const from = path.resolve(String(opts.from || '')); // type of file being read from | ||
const type = (opts.type || path.extname(from).slice(1)).toLowerCase(); | ||
return { | ||
type, | ||
from | ||
}; | ||
}).reduce( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator(function* (customMedia, source) { | ||
const _ref2 = yield source, | ||
type = _ref2.type, | ||
from = _ref2.from; | ||
if (type === 'css') { | ||
return Object.assign((yield customMedia), (yield getCustomMediaFromCSSFile(from))); | ||
} | ||
if (type === 'js') { | ||
return Object.assign((yield customMedia), (yield getCustomMediaFromJSFile(from))); | ||
} | ||
if (type === 'json') { | ||
return Object.assign((yield customMedia), (yield getCustomMediaFromJSONFile(from))); | ||
} | ||
return Object.assign((yield customMedia), getCustomMediaFromObject((yield source))); | ||
}); | ||
return function (_x4, _x5) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(), {}); | ||
} | ||
/* Helper utilities | ||
/* ========================================================================== */ | ||
const readFile = from => new Promise((resolve, reject) => { | ||
fs.readFile(from, 'utf8', (error, result) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
const readJSON = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref3 = _asyncToGenerator(function* (from) { | ||
return JSON.parse((yield readFile(from))); | ||
}); | ||
return function readJSON(_x6) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(); | ||
// return transformed medias, replacing custom pseudo medias with custom medias | ||
@@ -423,3 +567,3 @@ function transformMediaList(mediaList, customMedias) { | ||
if (customPseudoRegExp$1.test(atrule.params)) { | ||
const mediaAST = getMediaAstFromMediaString(atrule.params); | ||
const mediaAST = mediaASTFromString(atrule.params); | ||
const params = String(transformMediaList(mediaAST, customMedia)); | ||
@@ -440,169 +584,14 @@ | ||
/* Import Custom Media from CSS AST | ||
/* Write Custom Media from CSS File | ||
/* ========================================================================== */ | ||
function importCustomMediaFromCSSAST(root) { | ||
return getCustomMedia(root, { | ||
preserve: true | ||
}); | ||
function writeCustomMediaToCssFile(_x, _x2) { | ||
return _writeCustomMediaToCssFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from CSS File | ||
/* Write Custom Media from JSON file | ||
/* ========================================================================== */ | ||
function importCustomMediaFromCSSFile(_x) { | ||
return _importCustomMediaFromCSSFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from Object | ||
/* ========================================================================== */ | ||
function _importCustomMediaFromCSSFile() { | ||
_importCustomMediaFromCSSFile = _asyncToGenerator(function* (from) { | ||
const css = yield readFile(path.resolve(from)); | ||
const root = postcss.parse(css, { | ||
from: path.resolve(from) | ||
}); | ||
return importCustomMediaFromCSSAST(root); | ||
}); | ||
return _importCustomMediaFromCSSFile.apply(this, arguments); | ||
} | ||
function importCustomMediaFromObject(object) { | ||
const customMedia = Object.assign({}, Object(object).customMedia || Object(object)['custom-media']); | ||
for (const key in customMedia) { | ||
customMedia[key] = getMediaAstFromMediaString(customMedia[key]); | ||
} | ||
return customMedia; | ||
} | ||
/* Import Custom Media from JSON file | ||
/* ========================================================================== */ | ||
function importCustomMediaFromJSONFile(_x2) { | ||
return _importCustomMediaFromJSONFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from JS file | ||
/* ========================================================================== */ | ||
function _importCustomMediaFromJSONFile() { | ||
_importCustomMediaFromJSONFile = _asyncToGenerator(function* (from) { | ||
const object = yield readJSON(path.resolve(from)); | ||
return importCustomMediaFromObject(object); | ||
}); | ||
return _importCustomMediaFromJSONFile.apply(this, arguments); | ||
} | ||
function importCustomMediaFromJSFile(_x3) { | ||
return _importCustomMediaFromJSFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from Sources | ||
/* ========================================================================== */ | ||
function _importCustomMediaFromJSFile() { | ||
_importCustomMediaFromJSFile = _asyncToGenerator(function* (from) { | ||
const object = yield Promise.resolve(require(path.resolve(from))); | ||
return importCustomMediaFromObject(object); | ||
}); | ||
return _importCustomMediaFromJSFile.apply(this, arguments); | ||
} | ||
function importCustomMediaFromSources(sources) { | ||
return sources.map(source => { | ||
if (source instanceof Promise) { | ||
return source; | ||
} else if (source instanceof Function) { | ||
return source(); | ||
} // read the source as an object | ||
const opts = source === Object(source) ? source : { | ||
from: String(source) | ||
}; // skip objects with custom media | ||
if (Object(opts).customMedia || Object(opts)['custom-media']) { | ||
return opts; | ||
} // source pathname | ||
const from = String(opts.from || ''); // type of file being read from | ||
const type = (opts.type || path.extname(from).slice(1)).toLowerCase(); | ||
return { | ||
type, | ||
from | ||
}; | ||
}).reduce( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator(function* (customMedia, source) { | ||
const _ref2 = yield source, | ||
type = _ref2.type, | ||
from = _ref2.from; | ||
if (type === 'ast') { | ||
return Object.assign(customMedia, importCustomMediaFromCSSAST(from)); | ||
} | ||
if (type === 'css') { | ||
return Object.assign(customMedia, (yield importCustomMediaFromCSSFile(from))); | ||
} | ||
if (type === 'js') { | ||
return Object.assign(customMedia, (yield importCustomMediaFromJSFile(from))); | ||
} | ||
if (type === 'json') { | ||
return Object.assign(customMedia, (yield importCustomMediaFromJSONFile(from))); | ||
} | ||
return Object.assign(customMedia, importCustomMediaFromObject((yield source))); | ||
}); | ||
return function (_x4, _x5) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(), {}); | ||
} | ||
/* Helper utilities | ||
/* ========================================================================== */ | ||
const readFile = from => new Promise((resolve, reject) => { | ||
fs.readFile(from, 'utf8', (error, result) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
const readJSON = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref3 = _asyncToGenerator(function* (from) { | ||
return JSON.parse((yield readFile(from))); | ||
}); | ||
return function readJSON(_x6) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(); | ||
/* Import Custom Media from CSS File | ||
/* ========================================================================== */ | ||
function exportCustomMediaToCssFile(_x, _x2) { | ||
return _exportCustomMediaToCssFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from JSON file | ||
/* ========================================================================== */ | ||
function _exportCustomMediaToCssFile() { | ||
_exportCustomMediaToCssFile = _asyncToGenerator(function* (to, customMedia) { | ||
function _writeCustomMediaToCssFile() { | ||
_writeCustomMediaToCssFile = _asyncToGenerator(function* (to, customMedia) { | ||
const cssContent = Object.keys(customMedia).reduce((cssLines, name) => { | ||
@@ -615,14 +604,14 @@ cssLines.push(`@custom-media ${name} ${customMedia[name]};`); | ||
}); | ||
return _exportCustomMediaToCssFile.apply(this, arguments); | ||
return _writeCustomMediaToCssFile.apply(this, arguments); | ||
} | ||
function exportCustomMediaToJsonFile(_x3, _x4) { | ||
return _exportCustomMediaToJsonFile.apply(this, arguments); | ||
function writeCustomMediaToJsonFile(_x3, _x4) { | ||
return _writeCustomMediaToJsonFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from Common JS file | ||
/* Write Custom Media from Common JS file | ||
/* ========================================================================== */ | ||
function _exportCustomMediaToJsonFile() { | ||
_exportCustomMediaToJsonFile = _asyncToGenerator(function* (to, customMedia) { | ||
function _writeCustomMediaToJsonFile() { | ||
_writeCustomMediaToJsonFile = _asyncToGenerator(function* (to, customMedia) { | ||
const jsonContent = JSON.stringify({ | ||
@@ -634,14 +623,14 @@ 'custom-media': customMedia | ||
}); | ||
return _exportCustomMediaToJsonFile.apply(this, arguments); | ||
return _writeCustomMediaToJsonFile.apply(this, arguments); | ||
} | ||
function exportCustomMediaToCjsFile(_x5, _x6) { | ||
return _exportCustomMediaToCjsFile.apply(this, arguments); | ||
function writeCustomMediaToCjsFile(_x5, _x6) { | ||
return _writeCustomMediaToCjsFile.apply(this, arguments); | ||
} | ||
/* Import Custom Media from Module JS file | ||
/* Write Custom Media from Module JS file | ||
/* ========================================================================== */ | ||
function _exportCustomMediaToCjsFile() { | ||
_exportCustomMediaToCjsFile = _asyncToGenerator(function* (to, customMedia) { | ||
function _writeCustomMediaToCjsFile() { | ||
_writeCustomMediaToCjsFile = _asyncToGenerator(function* (to, customMedia) { | ||
const jsContents = Object.keys(customMedia).reduce((jsLines, name) => { | ||
@@ -654,14 +643,14 @@ jsLines.push(`\t\t'${escapeForJS(name)}': '${escapeForJS(customMedia[name])}'`); | ||
}); | ||
return _exportCustomMediaToCjsFile.apply(this, arguments); | ||
return _writeCustomMediaToCjsFile.apply(this, arguments); | ||
} | ||
function exportCustomMediaToMjsFile(_x7, _x8) { | ||
return _exportCustomMediaToMjsFile.apply(this, arguments); | ||
function writeCustomMediaToMjsFile(_x7, _x8) { | ||
return _writeCustomMediaToMjsFile.apply(this, arguments); | ||
} | ||
/* Export Custom Media to Destinations | ||
/* Write Custom Media to Exports | ||
/* ========================================================================== */ | ||
function _exportCustomMediaToMjsFile() { | ||
_exportCustomMediaToMjsFile = _asyncToGenerator(function* (to, customMedia) { | ||
function _writeCustomMediaToMjsFile() { | ||
_writeCustomMediaToMjsFile = _asyncToGenerator(function* (to, customMedia) { | ||
const mjsContents = Object.keys(customMedia).reduce((mjsLines, name) => { | ||
@@ -674,6 +663,6 @@ mjsLines.push(`\t'${escapeForJS(name)}': '${escapeForJS(customMedia[name])}'`); | ||
}); | ||
return _exportCustomMediaToMjsFile.apply(this, arguments); | ||
return _writeCustomMediaToMjsFile.apply(this, arguments); | ||
} | ||
function exportCustomMediaToDestinations(customMedia, destinations) { | ||
function writeCustomMediaToExports(customMedia, destinations) { | ||
return Promise.all(destinations.map( | ||
@@ -708,15 +697,15 @@ /*#__PURE__*/ | ||
if (type === 'css') { | ||
yield exportCustomMediaToCssFile(to, customMediaJSON); | ||
yield writeCustomMediaToCssFile(to, customMediaJSON); | ||
} | ||
if (type === 'js') { | ||
yield exportCustomMediaToCjsFile(to, customMediaJSON); | ||
yield writeCustomMediaToCjsFile(to, customMediaJSON); | ||
} | ||
if (type === 'json') { | ||
yield exportCustomMediaToJsonFile(to, customMediaJSON); | ||
yield writeCustomMediaToJsonFile(to, customMediaJSON); | ||
} | ||
if (type === 'mjs') { | ||
yield exportCustomMediaToMjsFile(to, customMediaJSON); | ||
yield writeCustomMediaToMjsFile(to, customMediaJSON); | ||
} | ||
@@ -755,10 +744,10 @@ } | ||
var index = postcss.plugin('postcss-custom-media', opts => { | ||
// whether to preserve custom selectors and rules using them | ||
const preserve = Boolean(Object(opts).preserve); // sources to import custom selectors from | ||
// whether to preserve custom media and at-rules using them | ||
const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false; // sources to import custom media from | ||
const importFrom = [].concat(Object(opts).importFrom || []); // destinations to export custom selectors to | ||
const importFrom = [].concat(Object(opts).importFrom || []); // destinations to export custom media to | ||
const exportTo = [].concat(Object(opts).exportTo || []); // promise any custom selectors are imported | ||
const exportTo = [].concat(Object(opts).exportTo || []); // promise any custom media are imported | ||
const customMediaPromise = importCustomMediaFromSources(importFrom); | ||
const customMediaPromise = getCustomMediaFromSources(importFrom); | ||
return ( | ||
@@ -771,3 +760,3 @@ /*#__PURE__*/ | ||
})); | ||
yield exportCustomMediaToDestinations(customMedia, exportTo); | ||
yield writeCustomMediaToExports(customMedia, exportTo); | ||
transformAtrules(root, customMedia, { | ||
@@ -786,1 +775,2 @@ preserve | ||
module.exports = index; | ||
//# sourceMappingURL=index.cjs.js.map |
{ | ||
"name": "postcss-custom-media", | ||
"version": "7.0.4", | ||
"version": "7.0.5", | ||
"description": "Use Custom Media Queries in CSS", | ||
@@ -30,14 +30,14 @@ "author": "Jonathan Neal <jonathantneal@hotmail.com>", | ||
"dependencies": { | ||
"postcss": "^7.0.2" | ||
"postcss": "^7.0.5" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.1.0", | ||
"@babel/core": "^7.1.2", | ||
"@babel/plugin-syntax-dynamic-import": "^7.0.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"babel-eslint": "^9.0.0", | ||
"eslint": "^5.6.0", | ||
"babel-eslint": "^10.0.1", | ||
"eslint": "^5.6.1", | ||
"eslint-config-dev": "^2.0.0", | ||
"postcss-tape": "^2.2.0", | ||
"pre-commit": "^1.2.2", | ||
"rollup": "^0.66.2", | ||
"rollup": "^0.66.6", | ||
"rollup-plugin-babel": "^4.0.3" | ||
@@ -44,0 +44,0 @@ }, |
@@ -108,4 +108,4 @@ # PostCSS Custom Media [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][postcss] | ||
order they are received. JavaScript files, JSON files, functions, and objects | ||
will need to namespace custom media using the `customProperties` or | ||
`custom-properties` key. | ||
will need to namespace custom media using the `customMedia` or | ||
`custom-media` key. | ||
@@ -122,5 +122,5 @@ ```js | ||
() => { | ||
const customProperties = { '--small-viewport': '(max-width: 30em)' }; | ||
const customMedia = { '--small-viewport': '(max-width: 30em)' }; | ||
return { customProperties }; | ||
return { customMedia }; | ||
} | ||
@@ -145,4 +145,4 @@ ] | ||
in the order they are received. JavaScript files, JSON files, and objects will | ||
need to namespace custom media using the `customProperties` or | ||
`custom-properties` key. | ||
need to namespace custom media using the `customMedia` or | ||
`custom-media` key. | ||
@@ -159,4 +159,4 @@ ```js | ||
cachedObject, | ||
customProperties => { | ||
customProperties // { '--small-viewport': '(max-width: 30em)' } | ||
customMedia => { | ||
customMedia // { '--small-viewport': '(max-width: 30em)' } | ||
} | ||
@@ -167,2 +167,6 @@ ] | ||
See example exports written to [CSS](test/export-media.css), | ||
[JS](test/export-media.js), [MJS](test/export-media.mjs), and | ||
[JSON](test/export-media.json). | ||
[cli-img]: https://img.shields.io/travis/postcss/postcss-custom-media.svg | ||
@@ -169,0 +173,0 @@ [cli-url]: https://travis-ci.org/postcss/postcss-custom-media |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
179
55380
1258
Updatedpostcss@^7.0.5