generated from netlify/build-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.ts
97 lines (83 loc) · 2.86 KB
/
index.ts
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
import path, { dirname, join } from 'path'
import process from 'process'
import { NetlifyPluginOptions } from '@netlify/build'
import { stripIndent } from 'common-tags'
import { existsSync } from 'fs-extra'
import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache'
import {
checkGatsbyConfig,
mutateConfig,
shouldSkipFunctions,
spliceConfig,
} from './helpers/config'
import { patchFile, relocateBinaries } from './helpers/files'
import { deleteFunctions, writeFunctions } from './helpers/functions'
import { checkZipSize } from './helpers/verification'
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
export async function onPreBuild({
constants: { PUBLISH_DIR },
utils,
netlifyConfig,
}): Promise<void> {
// Print a helpful message if the publish dir is misconfigured
if (!PUBLISH_DIR || 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.`,
)
}
await restoreCache({ utils, publish: PUBLISH_DIR })
await checkGatsbyConfig({ utils, netlifyConfig })
}
export async function onBuild({
constants,
netlifyConfig,
}: NetlifyPluginOptions): Promise<void> {
const {
PUBLISH_DIR,
FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC,
INTERNAL_FUNCTIONS_SRC,
} = constants
const cacheDir = normalizedCacheDir(PUBLISH_DIR)
if (
INTERNAL_FUNCTIONS_SRC &&
existsSync(path.join(FUNCTIONS_SRC, 'gatsby'))
) {
console.log(stripIndent`
Detected the function "${path.join(
FUNCTIONS_SRC,
'gatsby',
)}" that seem to have been generated by an old version of the Essential Gatsby plugin.
The plugin no longer uses this and it should be deleted to avoid conflicts.\n`)
}
if (shouldSkipFunctions(cacheDir)) {
await deleteFunctions(constants)
return
}
const compiledFunctionsDir = path.join(cacheDir, '/functions')
await writeFunctions(constants)
mutateConfig({ netlifyConfig, cacheDir, compiledFunctionsDir })
const root = dirname(netlifyConfig.build.publish)
await patchFile(root)
await relocateBinaries(root)
// Editing _redirects so it works with ntl dev
spliceConfig({
startMarker: '# @netlify/plugin-gatsby redirects start',
endMarker: '# @netlify/plugin-gatsby redirects end',
contents: '/api/* /.netlify/functions/__api 200',
fileName: join(netlifyConfig.build.publish, '_redirects'),
})
netlifyConfig.redirects.push({
from: '/*',
to: '/.netlify/builders/__dsg',
status: 200,
})
}
export async function onPostBuild({
constants: { PUBLISH_DIR, FUNCTIONS_DIST },
utils,
}): Promise<void> {
await saveCache({ publish: PUBLISH_DIR, utils })
for (const func of ['api', 'dsg', 'ssr']) {
await checkZipSize(path.join(FUNCTIONS_DIST, `__${func}.zip`))
}
}