Skip to content

Commit f786e52

Browse files
refactor: extract constants & defaults
1 parent 3dba75e commit f786e52

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

lib/constants.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const MODES = {
2+
webpack: 'webpack',
3+
postcss: 'postcss'
4+
}
5+
6+
module.exports = {
7+
MODES,
8+
DEFAULTS: {
9+
mode: MODES.postcss,
10+
enabled: ({ isDev, isClient }) => (!isDev && isClient),
11+
paths: [
12+
'components/**/*.vue',
13+
'layouts/**/*.vue',
14+
'pages/**/*.vue',
15+
'plugins/**/*.js',
16+
'nuxt.config.js'
17+
],
18+
styleExtensions: ['.css'],
19+
whitelist: ['body', 'html', 'nuxt-progress', '__nuxt', '__layout'],
20+
whitelistPatterns: [
21+
/-(leave|enter|appear)(|-(to|from|active))$/, // Normal transitions
22+
/^nuxt-link(|-exact)-active$/, // Nuxt link classes
23+
/^(?!cursor-move).+-move$/, // Move transitions
24+
/data-v-.*/ // Keep scoped styles
25+
],
26+
whitelistPatternsChildren: [],
27+
extractors: [
28+
{
29+
extractor: (content) => {
30+
const contentWithoutStyleBlocks = content.replace(/<style[^]+?<\/style>/gi, '') // Remove inline vue styles
31+
return contentWithoutStyleBlocks.match(/[\w-.:/]+(?<!:)/g) || []
32+
},
33+
extensions: ['html', 'vue', 'js']
34+
}
35+
]
36+
}
37+
}

lib/module.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ const glob = require('glob-all')
33

44
const logger = require('./logger')
55

6-
const MODES = {
7-
webpack: 'webpack',
8-
postcss: 'postcss'
9-
}
6+
const { MODES, DEFAULTS } = require('./constants')
107

118
function nuxtPurgeCss (moduleOptions) {
129
const { srcDir, purgeCSS = {} } = Object.assign({}, this.options)
@@ -15,42 +12,12 @@ function nuxtPurgeCss (moduleOptions) {
1512

1613
logger.start('Loading module')
1714

18-
const defaults = {
19-
mode: MODES.postcss,
20-
enabled: this.options.debug ? false : ({ isDev, isClient }) => (!isDev && isClient),
21-
paths: [
22-
'components/**/*.vue',
23-
'layouts/**/*.vue',
24-
'pages/**/*.vue',
25-
'plugins/**/*.js',
26-
'nuxt.config.js'
27-
],
28-
styleExtensions: ['.css'],
29-
whitelist: ['body', 'html', 'nuxt-progress', '__nuxt', '__layout'],
30-
whitelistPatterns: [
31-
/-(leave|enter|appear)(|-(to|from|active))$/, // Normal transitions
32-
/^nuxt-link(|-exact)-active$/, // Nuxt link classes
33-
/^(?!cursor-move).+-move$/, // Move transitions
34-
/data-v-.*/ // Keep scoped styles
35-
],
36-
whitelistPatternsChildren: [],
37-
extractors: [
38-
{
39-
extractor: (content) => {
40-
const contentWithoutStyleBlocks = content.replace(/<style[^]+?<\/style>/gi, '') // Remove inline vue styles
41-
return contentWithoutStyleBlocks.match(/[\w-.:/]+(?<!:)/g) || []
42-
},
43-
extensions: ['html', 'vue', 'js']
44-
}
45-
]
46-
}
47-
4815
/*
4916
* Defaults
5017
*/
5118

5219
const mergedConfig = Object.entries(purgeCSS).reduce((options, [key, value]) => {
53-
const defaultValue = defaults[key]
20+
const defaultValue = DEFAULTS[key]
5421

5522
if (value && typeof value !== 'function' && Array.isArray(defaultValue)) {
5623
// Merge value with default value if array
@@ -65,7 +32,7 @@ function nuxtPurgeCss (moduleOptions) {
6532
return options
6633
}, {})
6734

68-
const { mode, enabled, ...config } = Object.assign({}, defaults, mergedConfig)
35+
const { mode, enabled, ...config } = Object.assign({}, DEFAULTS, mergedConfig)
6936

7037
// transform relative paths
7138
config.paths = glob.sync(config.paths.map(p => path.join(srcDir, p)))

0 commit comments

Comments
 (0)