Skip to content

Commit

Permalink
Merge pull request #12 from kimulaco/feat/support-postcss-v8
Browse files Browse the repository at this point in the history
feat: support postcss v8
  • Loading branch information
kimulaco committed Dec 30, 2020
2 parents adf591f + b4c7f29 commit 6908905
Show file tree
Hide file tree
Showing 18 changed files with 4,136 additions and 3,663 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module.exports = {
env: {
jest: true
}
}
}
12 changes: 7 additions & 5 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: ['12']
node-version: ['14']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: yarn install
run: yarn
- name: yarn test
run: yarn test
- name: npm install
run: npm install
- name: npm run lint
run: npm run lint
- name: npm run test
run: npm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.node-version
npm-debug.log
node_modules
test/**/dist
6 changes: 5 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.DS_Store
.node-version
npm-debug.log
node_modules
.github
test
.editorconfig
.eslintrc.js
.eslintignore
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Change Log

## 1.4.0

- Support PostCSS v7 and v8.
- Updated [amp-custom](https://github.com/kimulaco/amp-custom) to v1.4.0.
- Updated devDependencies.
- Fixed CI config.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![npm version](https://badge.fury.io/js/postcss-amp-custom.svg)](https://badge.fury.io/js/postcss-amp-custom)
[![Build Status](https://travis-ci.org/kimulaco/postcss-amp-custom.svg?branch=master)](https://travis-ci.org/kimulaco/postcss-amp-custom)
[![Build Status](https://github.com/kimulaco/postcss-amp-custom/workflows/Test/badge.svg)](https://github.com/kimulaco/postcss-amp-custom/actions)

PostCSS plugin to optimize CSS source for AMP HTML.
PostCSS plugin to optimize CSS source for AMP HTML. Removes styles that are prohibited by AMP HTML.

[amp-custom](https://github.com/kimulaco/amp-custom) is the core library.
Supports PostCSS v7 and v8.

[amp-custom](https://github.com/kimulaco/amp-custom) is the core library. However, [amp-custom](https://github.com/kimulaco/amp-custom) will be deprecated in the future, and postcss-amp-custom will be mainly developed.

## Install

Expand All @@ -18,13 +20,15 @@ npm install --save-dev postcss-amp-custom

```js
// postcss.config.js
const postcssAmpCustom = require('postcss-amp-custom')

module.exports = () => ({
plugins: [
require('postcss-amp-custom')({
enableByteLimit: true
})
]
});
plugins: [
postcssAmpCustom({
enableByteLimit: true
})
]
})
```

```css
Expand Down Expand Up @@ -66,12 +70,7 @@ body{font-size:16px}a{color:#39c;text-decoration:none}

## Options

- enableByteLimit `Boolean` - If the CSS source exceeds 50 KB, it issues an error.(Default: `false`)

## Plugins

- [amp-custom](https://github.com/kimulaco/amp-custom)
- [gulp-amp-custom](https://github.com/kimulaco/gulp-amp-custom)
- enableByteLimit `Boolean` - If the CSS source exceeds 75KB, it issues an error.(Default: `false`)

## License

Expand Down
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const postcss = require('postcss')
const compileAmpCustom = require('./libs/compileAmpCustom')
const isPostCSSv8 = require('./libs/isPostCSSv8')
const PLUGIN_NAME = 'postcss-amp-custom'

module.exports = (option = {}) => {
option = Object.assign({
enableByteLimit: false
}, option || {})

if (isPostCSSv8(postcss)) {
return {
postcssPlugin: PLUGIN_NAME,
Root (root, postcss) {
postcss.result.root = compileAmpCustom(root, postcss, option)
}
}
} else {
return postcss.plugin(PLUGIN_NAME, (option) => {
return (root, result) => {
return new Promise((resolve, reject) => {
try {
const postcss = require('postcss')
const style = compileAmpCustom(root, postcss, option)
result.root = postcss.parse(style)
resolve()
} catch (error) {
reject(error)
}
})
}
})
}
}

module.exports.postcss = true
17 changes: 17 additions & 0 deletions libs/compileAmpCustom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const AmpCustom = require('amp-custom')
const ampCustom = new AmpCustom()

const compileAmpCustom = (root, postcss, option = {}) => {
const style = ampCustom.optimize(root.toString())

if (
option.enableByteLimit &&
ampCustom.isOverMaxByte(style)
) {
throw new Error('AMP stylesheet exceeds the 75KB limit.')
}

return postcss.parse(style)
}

module.exports = compileAmpCustom
5 changes: 5 additions & 0 deletions libs/isPostCSSv8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isPostCSSv8 = (postcss) => {
return typeof postcss.Root !== 'undefined'
}

module.exports = isPostCSSv8

0 comments on commit 6908905

Please sign in to comment.