Skip to content

Commit 128ab61

Browse files
chore: initial commit
0 parents  commit 128ab61

25 files changed

+10959
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_size = 2
6+
indent_style = space
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
parser: 'babel-eslint',
5+
sourceType: 'module'
6+
},
7+
extends: [
8+
'@nuxtjs'
9+
]
10+
}

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
*.iml
3+
.idea
4+
*.log*
5+
.nuxt*
6+
.vscode
7+
.DS_STORE
8+
coverage
9+
dist

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
- "9"
5+
- "10"
6+
cache:
7+
yarn: true
8+
directories:
9+
- node_modules
10+
install:
11+
- yarn
12+
script:
13+
- yarn test
14+
after_success:
15+
- yarn coverage

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Alexander Lichter <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Nuxt PurgeCSS - Drop superfluous CSS!
2+
[![npm (scoped with tag)](https://img.shields.io/npm/v/nuxt-purgecss/latest.svg?style=flat-square)](https://npmjs.com/package/nuxt-purgecss)
3+
[![npm](https://img.shields.io/npm/dt/nuxt-purgecss.svg?style=flat-square)](https://npmjs.com/package/nuxt-purgecss)
4+
[![Build Status](https://travis-ci.com/Developmint/nuxt-purgecss.svg?branch=master)](https://travis-ci.com/Developmint/nuxt-purgecss)
5+
[![codecov](https://codecov.io/gh/Developmint/nuxt-purgecss/branch/master/graph/badge.svg)](https://codecov.io/gh/Developmint/nuxt-purgecss)
6+
[![Dependencies](https://david-dm.org/Developmint/nuxt-purgecss/status.svg?style=flat-square)](https://david-dm.org/Developmint/nuxt-purgecss)
7+
[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)
8+
9+
>
10+
11+
[📖 **Release Notes**](./CHANGELOG.md)
12+
13+
## Features
14+
15+
* Remove unneeded CSS with ease
16+
* Webpack or PostCSS mode
17+
* Already comes with mighty default settings
18+
* Built on top of [purgecss](https://github.com/FullHuman/purgecss)
19+
* Nuxt 2 (and only Nuxt 2) support
20+
* Fully tested!
21+
22+
## Setup
23+
24+
- Add `nuxt-purgecss` dependency using yarn or npm to your project
25+
- Add `nuxt-purgecss` to `modules` section of `nuxt.config.js`:
26+
27+
```js
28+
{
29+
modules: [
30+
'nuxt-purgecss',
31+
],
32+
33+
purgeCSS: {
34+
// your settings here
35+
}
36+
}
37+
```
38+
39+
## Options
40+
41+
### Defaults
42+
43+
Before diving into the individual attributes, here are the default settings of the module:
44+
45+
```js
46+
{
47+
mode: MODES.webpack,
48+
enabled: ({ isDev, isClient }) => (!isDev && isClient),
49+
paths: [
50+
'components/**/*.vue',
51+
'layouts/**/*.vue',
52+
'pages/**/*.vue',
53+
'plugins/**/*.js'
54+
],
55+
styleExtensions: ['.css'],
56+
whitelist: ['body', 'html', 'nuxt-progress'],
57+
extractors: [
58+
{
59+
extractor: class {
60+
static extract(content) {
61+
return content.match(/[A-z0-9-:\\/]+/g)
62+
}
63+
},
64+
extensions: ['html', 'vue', 'js']
65+
}
66+
]
67+
}
68+
```
69+
70+
This settings should be a good foundation for a variety of projects.
71+
72+
### Merging defaults
73+
74+
You can define every option either as function or as static value (primitives, objects, arrays, ...).
75+
if you use a function, the default value will be provided as the first argument.
76+
77+
If you *don't* use a function to define you properties, the module will try to
78+
merge them with the default values. This can be handy for `paths`, `whitelist` and so on because
79+
the defaults are quite sensible. If you don't want to have the defaults include, just use a function.
80+
81+
### Properties in-depth
82+
83+
#### mode
84+
85+
* Type: `String` (webpack or postcss)
86+
* Default: `webpack`
87+
88+
Defines the mode, PurgeCSS should be used in.
89+
90+
* Webpack mode can only be used with `build.extractCSS: true`
91+
* PostCSS mode can only be used with a `build.postcss` **object** (no array) or default settings
92+
93+
#### enabled
94+
95+
* Type: `Boolean` or `Function` (only for webpack mode, will receive the build.extend ctx)
96+
* Default: `({ isDev, isClient }) => (!isDev && isClient)` (Only activates in production mode)
97+
98+
Enables/Disables the module
99+
100+
* If false, the module won't be activated at all
101+
* If a function is given, it'll be properly evaluated in webpack mode (in postcss mode it'll be handled as true)
102+
103+
104+
#### PurgeCSS options
105+
106+
Please read [the PurgeCSS docs](https://www.purgecss.com/configuration) for information about
107+
PurgeCSS-related information.
108+
109+
Instead of `content` we use `paths` to specify the paths PurgeCSS should look into (explained [here](https://www.purgecss.com/with-webpack#options).
110+
This applies to **both modes**, not only to `webpack mode`.
111+
112+
## Examples
113+
114+
### Default setup
115+
116+
```js
117+
//nuxt.config.js
118+
export default {
119+
modules: [
120+
'nuxt-purgecss',
121+
]
122+
}
123+
```
124+
125+
### Override a default value
126+
127+
128+
```js
129+
//nuxt.config.js
130+
export default {
131+
modules: [
132+
'nuxt-purgecss',
133+
],
134+
135+
purgeCSS: {
136+
whitelist: () => ['only-this-class']
137+
}
138+
}
139+
```
140+
141+
### Append a value to the defaults
142+
143+
144+
```js
145+
//nuxt.config.js
146+
export default {
147+
modules: [
148+
'nuxt-purgecss',
149+
],
150+
151+
purgeCSS: {
152+
whitelist: ['defaults-and-this-class']
153+
}
154+
}
155+
```
156+
157+
### Override a default value
158+
159+
160+
```js
161+
//nuxt.config.js
162+
export default {
163+
modules: [
164+
'nuxt-purgecss',
165+
],
166+
167+
purgeCSS: {
168+
whitelist: (defaultWhitelst) => defaultWhitelst.slice(1)
169+
}
170+
}
171+
```
172+
173+
174+
## Development
175+
176+
- Clone this repository
177+
- Install dependencies using `yarn install` or `npm install`
178+
- Start development server using `npm run dev`
179+
180+
## License
181+
182+
[MIT License](./LICENSE)
183+
184+
Copyright (c) Alexander Lichter

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ['@commitlint/config-conventional'] }

lib/module.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const path = require('path')
2+
const glob = require('glob-all')
3+
4+
const logger = require('consola').withScope('nuxt-purgecss')
5+
6+
const MODES = {
7+
webpack: 'webpack',
8+
postcss: 'postcss'
9+
}
10+
11+
export default function nuxtPurgeCss() {
12+
const { srcDir, purgeCSS = {} } = this.options
13+
14+
logger.start('Loading module')
15+
16+
const defaults = {
17+
mode: MODES.webpack,
18+
enabled: ({ isDev, isClient }) => (!isDev && isClient),
19+
paths: [
20+
'components/**/*.vue',
21+
'layouts/**/*.vue',
22+
'pages/**/*.vue',
23+
'plugins/**/*.js'
24+
],
25+
styleExtensions: ['.css'],
26+
whitelist: ['body', 'html', 'nuxt-progress'],
27+
extractors: [
28+
{
29+
extractor: class {
30+
static extract(content) {
31+
return content.match(/[A-z0-9-:\\/]+/g)
32+
}
33+
},
34+
extensions: ['html', 'vue', 'js']
35+
}
36+
]
37+
}
38+
39+
/*
40+
* Defaults
41+
*/
42+
43+
const mergedConfig = Object.entries(purgeCSS).reduce((options, [key, value]) => {
44+
const defaultValue = defaults[key]
45+
46+
if (value && typeof value !== 'function' && Array.isArray(defaultValue)) {
47+
// Merge value with default value if array
48+
value = defaultValue.concat(value)
49+
} else if (typeof value === 'function') {
50+
// Executed value functions and provide default value as param
51+
value = value(defaultValue)
52+
}
53+
54+
// Finally assign
55+
options[key] = value
56+
return options
57+
}, {})
58+
59+
const { mode, enabled, ...config } = Object.assign({}, defaults, mergedConfig)
60+
61+
// transform relative paths
62+
config.paths = glob.sync(config.paths.map(p => path.join(srcDir, p)))
63+
64+
if (!enabled) {
65+
logger.warn('Module is not enabled')
66+
return
67+
}
68+
69+
if (!Object.values(MODES).includes(mode)) {
70+
logger.error(`Could not load PurgeCSS. Invalid type ${mode}.`)
71+
return
72+
}
73+
74+
if (mode === MODES.webpack) {
75+
if (!this.options.build.extractCSS) {
76+
logger.error(`Webpack mode only works with build.extractCSS set to *true*. Either extract your CSS or use 'postcss' mode`)
77+
return
78+
}
79+
this.extendBuild(webpackFn(enabled, config))
80+
return
81+
}
82+
83+
if (Array.isArray(this.options.build.postcss)) {
84+
logger.error(`build.postcss array option detected. Purgecss will only work in postcss mode with an option object`)
85+
return
86+
}
87+
88+
const purgeCssPluginsObject = {
89+
'@fullhuman/postcss-purgecss': {
90+
content: config.paths,
91+
...config
92+
}
93+
}
94+
95+
if (!this.options.build.postcss.plugins || !Object.keys(this.options.build.postcss.plugins).length) {
96+
this.options.build.postcss.plugins = {}
97+
}
98+
99+
Object.assign(this.options.build.postcss.plugins, purgeCssPluginsObject)
100+
101+
logger.success('Module initialized in postcss mode')
102+
}
103+
104+
const webpackFn = (enabled, purgeCssConfig) => (config, ctx) => {
105+
if (typeof enabled === 'function' && !enabled(ctx)) {
106+
return
107+
}
108+
const PurgeCssPlugin = require('purgecss-webpack-plugin')
109+
config.plugins.push(new PurgeCssPlugin(purgeCssConfig))
110+
logger.success('Module initialized in webpack mode')
111+
}
112+
113+
module.exports.meta = require('../package.json')

0 commit comments

Comments
 (0)