generated from netlify/build-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
144 lines (123 loc) · 4.41 KB
/
index.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
// @ts-check
const path = require('path')
const fs = require('fs-extra')
const { spliceConfig } = require('./helpers/config')
const normalizedCacheDir = (PUBLISH_DIR) =>
path.normalize(`${PUBLISH_DIR}/../.cache`)
const getCacheDirs = (PUBLISH_DIR) => [
PUBLISH_DIR,
normalizedCacheDir(PUBLISH_DIR),
]
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
const hasPlugin = (plugins, pluginName) =>
plugins &&
plugins.some((plugin) =>
typeof plugin === 'string'
? plugin === pluginName
: plugin.resolve === pluginName,
)
module.exports = {
async onPreBuild({ constants: { PUBLISH_DIR }, utils, netlifyConfig }) {
try {
// print a helpful message if the publish dir is misconfigured
if (process.cwd() === PUBLISH_DIR) {
utils.build.failBuild(
`Gatsby sites must publish the public directory, but your site’s publish directory is set to “${PUBLISH_DIR}”. Please set your publish directory to your Gatsby site’s public directory.`,
)
}
const cacheDirs = getCacheDirs(PUBLISH_DIR)
if (await utils.cache.restore(cacheDirs)) {
console.log('Found a Gatsby cache. We’re about to go FAST. ⚡️')
} else {
console.log('No Gatsby cache found. Building fresh.')
}
// warn if gatsby-plugin-netlify is missing
const pluginName = 'gatsby-plugin-netlify'
const gatsbyConfigFile = path.resolve(process.cwd(), 'gatsby-config.js')
const gatsbyConfig = fs.existsSync(gatsbyConfigFile)
? require(gatsbyConfigFile)
: {}
if (!hasPlugin(gatsbyConfig.plugins, pluginName)) {
console.warn(
'Add `gatsby-plugin-netlify` to `gatsby-config.js` if you would like to support Gatsby redirects. 🎉',
)
}
if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify-cache')) {
console.error(
"The plugin 'gatsby-plugin-netlify-cache' is not compatible with the Gatsby build plugin",
)
console.error(
'Please uninstall gatsby-plugin-netlify-cache and remove it from your gatsby-config.js',
)
utils.build.failBuild('Incompatible Gatsby plugin installed')
}
if (
netlifyConfig.plugins.some(
(plugin) => plugin.package === 'netlify-plugin-gatsby-cache',
)
) {
console.warn(
"The plugin 'netlify-plugin-gatsby-cache' is no longer required and should be removed.",
)
}
} catch (error) {
utils.build.failBuild('Error message', { error })
}
},
async onBuild({
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
utils,
}) {
try {
// copying gatsby functions to functions directory
const compiledFunctions = path.join(
normalizedCacheDir(PUBLISH_DIR),
'/functions',
)
if (!fs.existsSync(compiledFunctions)) {
return
}
// copying netlify wrapper functions into functions directory
await fs.copy(
path.join(__dirname, 'templates'),
path.join(FUNCTIONS_SRC, 'gatsby'),
)
await fs.copy(
compiledFunctions,
path.join(FUNCTIONS_SRC, 'gatsby', 'functions'),
)
const redirectsPath = path.resolve(`${PUBLISH_DIR}/_redirects`)
await spliceConfig({
startMarker: '# @netlify/plugin-gatsby redirects start',
endMarker: '# @netlify/plugin-gatsby redirects end',
contents: '/api/* /.netlify/functions/gatsby 200',
fileName: redirectsPath,
})
// add gatsby functions to .gitignore if doesn't exist
const gitignorePath = path.resolve('.gitignore')
await spliceConfig({
startMarker: '# @netlify/plugin-gatsby ignores start',
endMarker: '# @netlify/plugin-gatsby ignores end',
contents: `${FUNCTIONS_SRC}/gatsby`,
fileName: gitignorePath,
})
} catch (error) {
utils.build.failBuild('Error message', { error })
}
},
async onPostBuild({ constants: { PUBLISH_DIR }, utils }) {
try {
const cacheDirs = getCacheDirs(PUBLISH_DIR)
if (await utils.cache.save(cacheDirs)) {
utils.status.show({
title: 'Essential Gatsby Build Plugin ran successfully',
summary: 'Stored the Gatsby cache to speed up future builds. 🔥',
})
} else {
console.log('No Gatsby build found.')
}
} catch (error) {
utils.build.failBuild('Error message', { error })
}
},
}