Skip to content

Commit

Permalink
upgrade to PostCSS 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpolletvillard committed Sep 27, 2020
1 parent 253ff0f commit 95b6dfc
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 127 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Use different `grid-kiss` declarations in media queries to easily get responsive
- with [npm](https://www.npmjs.com/)

```bash
npm install postcss-grid-kiss --save-dev
npm install --save-dev postcss postcss-grid-kiss
```

- with [yarn](https://yarnpkg.com/)
Expand Down
5 changes: 0 additions & 5 deletions lib/index.js

This file was deleted.

178 changes: 92 additions & 86 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const
postcss = require("postcss"),
optimizeRule = require("postcss-merge-grid-template/dist/optimize"),
optimizeRule = require("postcss-merge-grid-template/lib/optimize"),

caniuse = require("./caniuse"),
parse = require("./parse"),
Expand Down Expand Up @@ -28,94 +27,101 @@ module.exports = function (options) {
isIEHackNeeded = options.fallback;
}

return (css, result) => css.walkDecls('grid-kiss', function (decl) {

const
{ rows, cols, zones, rowIndexes, colIndexes } = parse(decl, options),
grid = { props: new Map, rule: decl.parent },
indent = decl.raws.before.match(/.*$/)[0],
nameMapping = new Map(),
rowDims = getGridRows({ rows, colIndexes, rowIndexes, options }),
colDims = getGridCols({ decl, rows, zones, colIndexes, rowIndexes, options })

grid.props.set("display", "grid");
grid.props.set("align-content", getAlignContent({ rows }));
grid.props.set("justify-content", getJustifyContent({ cols }));
grid.props.set("grid-template-rows", rowDims.join(" "));
grid.props.set("grid-template-columns", colDims.join(" "));
grid.props.set("grid-template-areas", getGridAreas({ zones, rowIndexes, colIndexes, indent }));

// grid properties
for (let [prop, value] of grid.props) {
if (value) {
decl.cloneBefore({ prop, value, raws: { before: '\n\t', after: '' } });
}
}

if (options.optimize) {
optimizeRule(grid.rule, nameMapping);
}

// zone declarations
zones.filter(zone => zone.selector).forEach((zone, i, zonesWithSelector) => {
zone.props = new Map;

let name = zone.name;
if (options.optimize && nameMapping.has(zone.name)) {
name = nameMapping.get(zone.name);
}

zone.props.set("grid-area", name);
zone.props.set("justify-self", getJustifySelf(zone));
zone.props.set("align-self", getAlignSelf(zone));

zone.rule = postcss.rule({
selector: `${grid.rule.selector} > ${zone.selector}`,
source: decl.source,
raws: { before: '\n\n', after: '\n' }
});

for (let [prop, value] of zone.props) {
if (value) {
zone.rule.append({ prop, value, raws: { before: '\n\t', after: '' } });
return {
postcssPlugin: 'postcss-grid-kiss',
Declaration: {
"grid-kiss": (decl, { result, Rule, AtRule }) => {
const
{ rows, cols, zones, rowIndexes, colIndexes } = parse(decl, options),
grid = { props: new Map, rule: decl.parent },
indent = decl.raws.before.match(/.*$/)[0],
nameMapping = new Map(),
rowDims = getGridRows({ rows, colIndexes, rowIndexes, options }),
colDims = getGridCols({ decl, rows, zones, colIndexes, rowIndexes, options })

let lastRule = grid.rule;

grid.props.set("display", "grid");
grid.props.set("align-content", getAlignContent({ rows }));
grid.props.set("justify-content", getJustifyContent({ cols }));
grid.props.set("grid-template-rows", rowDims.join(" "));
grid.props.set("grid-template-columns", colDims.join(" "));
grid.props.set("grid-template-areas", getGridAreas({ zones, rowIndexes, colIndexes, indent }));

// grid properties
for (let [prop, value] of grid.props) {
if (value) {
decl.cloneBefore({ prop, value, raws: { before: '\n\t', after: '' } });
}
}
}

const lastRule = (zonesWithSelector[i - 1] || grid).rule;
grid.rule.parent.insertAfter(lastRule, zone.rule);
})

if (isFallbackNeeded) {
const fallback = getFallback({
zones, grid, decl, result, options, colIndexes, rowIndexes, colDims, rowDims
});

const supportsRule = postcss.atRule({
name: "supports",
params: 'not (grid-template-areas:"test")'
});

const ieHackRule = postcss.atRule({
name: "media",
params: 'screen and (min-width:0\\0)'
});

supportsRule.append(fallback.grid.rule.clone({ raws: { before: '\n\t', after: '\n\t' } }));
ieHackRule.append(fallback.grid.rule.clone({ raws: { before: '\n\t', after: '\n\t' } }));
for (let zoneFallback of fallback.zones.values()) {
supportsRule.append(zoneFallback.rule.clone({ raws: { before: '\n\n\t', after: '\n\t' } }));
ieHackRule.append(zoneFallback.rule.clone({ raws: { before: '\n\n\t', after: '\n\t' } }));
}
if (options.optimize) {
optimizeRule(grid.rule, nameMapping);
}

const lastRule = (zones[zones.length - 1] || grid).rule;
// zone declarations

zones.filter(zone => zone.selector).forEach((zone, i, zonesWithSelector) => {
zone.props = new Map;

let name = zone.name;
if (options.optimize && nameMapping.has(zone.name)) {
name = nameMapping.get(zone.name);
}

zone.props.set("grid-area", name);
zone.props.set("justify-self", getJustifySelf(zone));
zone.props.set("align-self", getAlignSelf(zone));

zone.rule = new Rule({
selector: `${grid.rule.selector} > ${zone.selector}`,
source: decl.source,
raws: { before: '\n\n', after: '\n' }
});

for (let [prop, value] of zone.props) {
if (value) {
zone.rule.append({ prop, value, raws: { before: '\n\t', after: '' } });
}
}

grid.rule.parent.insertAfter(lastRule, zone.rule);
lastRule = zone.rule
})

if (isFallbackNeeded) {
const fallback = getFallback({
zones, grid, decl, result, options, colIndexes, rowIndexes, colDims, rowDims
});

const supportsRule = new AtRule({
name: "supports",
params: 'not (grid-template-areas:"test")'
});

const ieHackRule = new AtRule({
name: "media",
params: 'screen and (min-width:0\\0)'
});

supportsRule.append(fallback.grid.rule.clone({ raws: { before: '\n\t', after: '\n\t' } }));
ieHackRule.append(fallback.grid.rule.clone({ raws: { before: '\n\t', after: '\n\t' } }));
for (let zoneFallback of fallback.zones.values()) {
supportsRule.append(zoneFallback.rule.clone({ raws: { before: '\n\n\t', after: '\n\t' } }));
ieHackRule.append(zoneFallback.rule.clone({ raws: { before: '\n\n\t', after: '\n\t' } }));
}

if (isIEHackNeeded) {
grid.rule.parent.insertAfter(lastRule, ieHackRule);
}

grid.rule.parent.insertAfter(lastRule, supportsRule);
}

if (isIEHackNeeded) {
grid.rule.parent.insertAfter(lastRule, ieHackRule);
decl.remove(); // remove grid-kiss rule in output
}

grid.rule.parent.insertAfter(lastRule, supportsRule);
}
}
};

decl.remove(); // remove grid-kiss rule in output
})
};
module.exports.postcss = true
84 changes: 60 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 95b6dfc

Please sign in to comment.