This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnuxt.config.js
216 lines (190 loc) · 5.11 KB
/
nuxt.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require('dotenv').config()
const PurgecssPlugin = require('purgecss-webpack-plugin')
const glob = require('glob-all')
const path = require('path')
const axios = require('axios') // we'll need this later for our dynamic routes
const collect = require('collect.js')
const perPage = Number(process.env.PER_PAGE)
class TailwindExtractor {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g) || [];
}
}
module.exports = {
mode: 'universal',
env: {
searchUrl: process.env.SEARCH_URL,
contactUrl: `${process.env.BASE_URL}/api/forms/submit/contact?token=${process.env.FORMS_TOKEN}`,
commentUrl: `${process.env.BASE_URL}/api/forms/submit/comments?token=${process.env.FORMS_TOKEN}`,
},
/*
** Headers of the page
*/
head: {
title: 'Nuxt Cockpit Static Blog',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: 'A static blog built with Nuxt.js and Cockpit headless CMS.' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
/*
** Customize the progress-bar color
*/
loading: { color: '#fff' },
/*
** Global CSS
*/
css: [
'@/assets/css/main.css',
'highlight.js/styles/dracula.css'
],
/*
** Plugins to load before mounting the App
*/
plugins: [
'~/plugins/filters.js'
],
/*
** Nuxt.js modules
*/
modules: [
// Doc: https://github.com/nuxt-community/axios-module#usage
'@nuxtjs/axios',
'@nuxtjs/sitemap'
],
/*
** Axios module configuration
*/
axios: {
// See https://github.com/nuxt-community/axios-module#options
},
generate: {
routes: async () => {
let { data } = await axios.post(process.env.POSTS_URL,
JSON.stringify({
filter: { published: true },
sort: {_created:-1},
populate: 1
}),
{
headers: { 'Content-Type': 'application/json' }
})
const collection = collect(data.entries)
let tags = collection.map(post => post.tags)
.flatten()
.unique()
.map(tag => {
let payload = collection.filter(item => {
return collect(item.tags).contains(tag)
}).all()
return {
route: `category/${tag}`,
payload: payload
}
}).all()
let posts = collection.map(post => {
return {
route: post.title_slug,
payload: post
}
}).all()
if(perPage < data.total) {
let pages = collection
.take(perPage-data.total)
.chunk(perPage)
.map((items, key) => {
let currentPage = key + 2
return {
route: `blog/${currentPage}`,
payload: {
posts: items.all(),
hasNext: data.total > currentPage*perPage
}
}
}).all()
return posts.concat(tags,pages)
}
return posts.concat(tags)
}
},
sitemap: {
path: '/sitemap.xml',
hostname: process.env.URL,
cacheTime: 1000 * 60 * 15,
generate: true, // Enable me when using nuxt generate
async routes () {
let { data } = await axios.post(process.env.POSTS_URL,
JSON.stringify({
filter: { published: true },
sort: {_created:-1},
populate: 1
}),
{
headers: { 'Content-Type': 'application/json' }
})
const collection = collect(data.entries)
let tags = collection.map(post => post.tags)
.flatten()
.unique()
.map(tag => `category/${tag}`)
.all()
let posts = collection.map(post => post.title_slug).all()
if(perPage < data.total) {
let pages = collection
.take(perPage-data.total)
.chunk(perPage)
.map((items, key) => `blog/${key+2}`)
.all()
return posts.concat(tags,pages)
}
return posts.concat(tags)
}
},
/*
** Build configuration
*/
build: {
extractCSS: true,
/*
** You can extend webpack config here
*/
extend (config, { isDev }) {
if (!isDev) {
// Remove unused CSS using purgecss. See https://github.com/FullHuman/purgecss
// for more information about purgecss.
config.plugins.push(
new PurgecssPlugin({
// Specify the locations of any files you want to scan for class names.
paths: glob.sync([
path.join(__dirname, './pages/**/*.vue'),
path.join(__dirname, './layouts/**/*.vue'),
path.join(__dirname, './components/**/*.vue')
]),
extractors: [
{
extractor: TailwindExtractor,
// Specify the file extensions to include when scanning for
// class names.
extensions: ["html", "vue"]
}
],
whitelist: [
"html",
"body",
"ul",
"ol",
"pre",
"code",
"blockquote"
],
whitelistPatterns: [/\bhljs\S*/]
})
)
}
}
}
}