Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed May 31, 2021
2 parents 851d2ae + 63b9ab9 commit f543ec2
Show file tree
Hide file tree
Showing 26 changed files with 7,165 additions and 13 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Node.js CI
on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master, develop ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
node-version: [12.x, 14.x, 15.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm run test --if-present
# Only run the coverage once
- if: ${{ matrix.node-version == '14.x' }}
name: Get Coverage for badge
run: |
# var SUMMARY = [
# '',
# '=============================== Coverage summary ===============================',
# 'Statements : 32.5% ( 39/120 )',
# 'Branches : 38.89% ( 21/54 )',
# 'Functions : 21.74% ( 5/23 )',
# 'Lines : 31.93% ( 38/119 )',
# '================================================================================',
# ''
# ];
# SUMMARY = SUMMARY.split('\n')[5]; // 'Lines : 31.93% ( 38/119 )'
# SUMMARY = SUMMARY.split(':')[1].split('(')[0].trim(); // '31.93%'
SUMMARY="$(npm test -- --coverageReporters='text-summary' | tail -2 | head -1)"
TOKENS=($SUMMARY)
# process.env.COVERAGE = '31.93%';
echo "COVERAGE=$(echo ${TOKENS[2]})" >> $GITHUB_ENV
# var REF = 'refs/pull/27/merge.json';
REF=${{ github.ref }}
# console.log('github.ref: ' + REF);
echo "github.ref: $REF"
# var PATHS = REF.split('/');
IFS='/' read -ra PATHS <<< "$REF"
# var BRANCH_NAME = PATHS[1] + PATHS[2];
BRANCH_NAME="${PATHS[1]}_${PATHS[2]}"
# console.log(BRANCH_NAME); // 'pull_27'
echo $BRANCH_NAME
# process.env.BRANCH = 'pull_27';
echo "BRANCH=$(echo ${BRANCH_NAME})" >> $GITHUB_ENV
- if: ${{ matrix.node-version == '14.x' }}
name: Create the Badge
uses: schneegans/[email protected]
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: 550f6ee414a26e0c8eae7cb6af3c214e
filename: rollup-plugin-critical__${{ env.BRANCH }}.json
label: Test Coverage
message: ${{ env.COVERAGE }}
color: green
namedLogo: jest
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# BUILD FILES
node_modules/
yarn-error.log
npm-debug.log
coverage/
src/__tests__/test_critical.min.css

# MISC FILES
.cache
.DS_Store
.idea
.project
.settings
*.esproj
*.sublime-workspace
*.sublime-project
*.tmproj
*.tmproject
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
config.codekit3
prepros-6.config
18 changes: 18 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
checks:
javascript: true
build:
environment:
node: v14.17.0
nodes:
analysis:
tests:
override:
- js-scrutinizer-run
tests: true
coverage:
tests:
override:
- command: 'npm run test'
coverage:
file: 'coverage/clover.xml'
format: 'clover'
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Rollup Plugin Critical

All notable changes to this project will be documented in this file.

## 1.0.0 - 2021-05-30
### Added
* Initial release
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG TAG=14-alpine
FROM nystudio107/node-dev-base:$TAG

WORKDIR /app/

CMD ["run build"]

ENTRYPOINT ["npm"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Petr Tsymbarovich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
TAG?=14-alpine
CONTAINER?=$(shell basename $(CURDIR))
DOCKERRUN=docker container run \
--name ${CONTAINER} \
--rm \
-t \
-v `pwd`:/app \
${CONTAINER}:${TAG}

.PHONY: docker build install test update npm

docker:
docker build \
. \
-t ${CONTAINER}:${TAG} \
--build-arg TAG=${TAG} \
--no-cache
build: docker install update
${DOCKERRUN} \
run build
install: docker
${DOCKERRUN} \
install
test: docker install
${DOCKERRUN} \
run test
update: docker
${DOCKERRUN} \
update
npm: docker
${DOCKERRUN} \
$(filter-out $@,$(MAKECMDGOALS))
%:
@:
# ref: https://stackoverflow.com/questions/6273608/how-to-pass-argument-to-makefile-from-command-line
60 changes: 47 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# rollup-plugin-critical
![Build](https://github.com/nystudio107/rollup-plugin-critical/actions/workflows/node.js.yml/badge.svg)
![Coverage Badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/khalwat/550f6ee414a26e0c8eae7cb6af3c214e/raw/rollup-plugin-critical__heads_master.json)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/rollup-plugin-critical/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nystudio107/rollup-plugin-critical/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/rollup-plugin-critical/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/nystudio107/rollup-plugin-critical/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/nystudio107/rollup-plugin-critical/badges/build.png?b=master)](https://scrutinizer-ci.com/g/nystudio107/rollup-plugin-critical/build-status/master)

[Rollup](https://github.com/rollup/rollup) plugin for generating critical css.
# rollup-plugin-critical

It uses the [critical](https://github.com/addyosmani/critical) generator under the hood.
[Vite.js](https://vitejs.dev/) & [Rollup](https://rollupjs.org/) plugin for generating critical CSS that uses the [critical](https://github.com/addyosmani/critical) generator under the hood.

## Install

```sh
```bash
npm i -D rollup-plugin-critical
```

Expand All @@ -25,26 +29,56 @@ export default {
},
plugins: [
critical({
criticalUrl: 'https://nystudio107.com',
pages: [
{
uri: '/',
template: 'index'
}
criticalUrl: 'https://nystudio107.com/',
criticalBase: './',
criticalPages: [
{ uri: '', template: 'index' },
{ uri: 'about', template: 'about/index' },
],
criticalConfig: {
},
}),
],
}
```

## Options

### `source: string`
### `criticalUrl: string`

The base URL to use in combination with the `criticalPages` `uri`s to determine the URLs to scrape for Critical CSS

### `criticalBase: string`

The base file system path to where the generated Critical CSS file should be stored

### `criticalPages: array of objects`

A path to a source image which would be used to generate icons.
An array objects that contain the page `uri`s that are combined with the `criticalUrl` to determine the URLs to scrape for Critical CSS. The resulting files are named with the `template` path, and saved to the `criticalBase` directory

### `criticalConfig: object`

This is the full [config for critical](https://github.com/addyosmani/critical#options) that is passed through to the `critical` package.

You may optionally override any properties you like here. The default values passed in are:

```js
const defaultCriticalConfig = {
inline: false,
minify: true,
extract: false,
width: 1200,
height: 1200,
concurrency: 4,
penthouse: {
blockJSRequests: false
}
};
```

### `configuration: object`
The `base` property is set to `criticalBase`, but can be overridden as need.

The `src` and `target` properties are dynamically computed from `criticalUrl` and `criticalPages` settings, but they can be overridden as needed.

## License

Expand Down
18 changes: 18 additions & 0 deletions config/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
"sourceMap": false,
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"newLine": "lf",
"esModuleInterop": true
},
"include": [
"../src"
],
"exclude": [
"../src/__tests__"
]
}
7 changes: 7 additions & 0 deletions config/cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./base",
"compilerOptions": {
"module": "commonjs",
"outDir": "../dist/cjs"
}
}
7 changes: 7 additions & 0 deletions config/esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./base",
"compilerOptions": {
"module": "es6",
"outDir": "../dist/esm"
}
}
8 changes: 8 additions & 0 deletions config/types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./base",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "../dist"
}
}
66 changes: 66 additions & 0 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const critical = require('critical');
const criticalSuffix = '_critical.min.css';
const defaultCriticalConfig = {
inline: false,
minify: true,
extract: false,
width: 1200,
height: 1200,
concurrency: 4,
penthouse: {
blockJSRequests: false
}
};
function PluginCritical(pluginConfig, callback) {
return {
name: 'critical',
async writeBundle(outputOptions, bundle) {
const css = [];
// Find all of the generated CSS assets
if (bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {
if (outputOptions.dir !== undefined) {
const cssFile = path_1.default.join(outputOptions.dir, chunk.fileName);
css.push(cssFile);
}
}
}
// If we have no CSS, skip bundle
if (!css.length) {
return;
}
}
// Iterate through the pages
for (const page of pluginConfig.criticalPages) {
const criticalBase = pluginConfig.criticalBase;
const criticalSrc = pluginConfig.criticalUrl + page.uri;
const criticalDest = page.template + criticalSuffix;
// Merge in our options
const options = Object.assign({ css }, defaultCriticalConfig, {
base: criticalBase,
src: criticalSrc,
target: criticalDest,
}, pluginConfig.criticalConfig);
// Generate the Critical CSS
console.log(`Generating critical CSS from ${criticalSrc} to ${criticalDest}`);
await critical.generate(options, (err) => {
if (err) {
console.error(err);
}
if (callback) {
callback(err);
}
});
}
}
};
}
;
exports.default = PluginCritical;
Loading

0 comments on commit f543ec2

Please sign in to comment.