-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
compile-gatsby-files.ts
268 lines (232 loc) · 7.44 KB
/
compile-gatsby-files.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { Parcel } from "@parcel/core"
import {
constructParcel,
compileGatsbyFiles,
gatsbyFileRegex,
COMPILED_CACHE_DIR,
PARCEL_CACHE_DIR,
} from "../compile-gatsby-files"
import { siteMetadata } from "./fixtures/utils/site-metadata"
import { moreDataConfig } from "./fixtures/utils/more-data-config"
import { readFile, remove, pathExists } from "fs-extra"
import reporter from "gatsby-cli/lib/reporter"
const dir = {
js: `${__dirname}/fixtures/js`,
ts: `${__dirname}/fixtures/ts`,
tsOnlyInLocal: `${__dirname}/fixtures/ts-only-in-local-plugin`,
misnamedJS: `${__dirname}/fixtures/misnamed-js`,
misnamedTS: `${__dirname}/fixtures/misnamed-ts`,
gatsbyNodeAsDirectory: `${__dirname}/fixtures/gatsby-node-as-directory`,
errorInCode: `${__dirname}/fixtures/error-in-code-ts`,
}
jest.setTimeout(60_000)
jest.mock(`@parcel/core`, () => {
const parcelCore = jest.requireActual(`@parcel/core`)
const { Parcel: OriginalParcel } = parcelCore
class MockedParcel extends OriginalParcel {
constructor(options) {
super(options)
this.options = options // Expose for assertions
}
}
return {
...parcelCore,
Parcel: MockedParcel,
}
})
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panic: jest.fn(),
}
})
const reporterPanicMock = reporter.panic as jest.MockedFunction<
typeof reporter.panic
>
interface IMockedParcel extends Parcel {
options: unknown
}
let cwdToRestore
beforeAll(() => {
cwdToRestore = process.cwd()
})
afterAll(() => {
process.chdir(cwdToRestore)
})
describe(`gatsby file compilation`, () => {
describe(`constructBundler`, () => {
it(`should construct Parcel relative to passed directory`, () => {
const { options } = constructParcel(dir.js) as IMockedParcel
expect(options).toMatchObject({
entries: [
`${dir.js}/${gatsbyFileRegex}`,
`${dir.js}/plugins/**/${gatsbyFileRegex}`,
],
targets: {
root: {
distDir: `${dir.js}/${COMPILED_CACHE_DIR}`,
engines: {
node: _CFLAGS_.GATSBY_MAJOR !== `5` ? `>= 14.15.0` : `>= 18.0.0`,
},
},
},
cacheDir: `${dir.js}/${PARCEL_CACHE_DIR}`,
})
})
})
describe(`compileGatsbyFiles`, () => {
describe(`js files are not touched`, () => {
beforeAll(async () => {
process.chdir(dir.js)
await remove(`${dir.js}/.cache`)
await compileGatsbyFiles(dir.js)
})
beforeEach(() => {
reporterPanicMock.mockClear()
})
it(`should not compile gatsby-config.js`, async () => {
const isCompiled = await pathExists(
`${dir.js}/.cache/compiled/gatsby-config.js`
)
expect(isCompiled).toEqual(false)
expect(reporterPanicMock).not.toHaveBeenCalled()
})
it(`should not compile gatsby-node.js`, async () => {
const isCompiled = await pathExists(
`${dir.js}/.cache/compiled/gatsby-node.js`
)
expect(isCompiled).toEqual(false)
expect(reporterPanicMock).not.toHaveBeenCalled()
})
})
describe(`ts files are compiled`, () => {
beforeAll(async () => {
process.chdir(dir.ts)
await remove(`${dir.ts}/.cache`)
await compileGatsbyFiles(dir.ts)
})
beforeEach(() => {
reporterPanicMock.mockClear()
})
it(`should compile gatsby-config.ts`, async () => {
const compiledGatsbyConfig = await readFile(
`${dir.ts}/.cache/compiled/gatsby-config.js`,
`utf-8`
)
expect(compiledGatsbyConfig).toContain(siteMetadata.title)
expect(compiledGatsbyConfig).toContain(siteMetadata.siteUrl)
expect(compiledGatsbyConfig).toContain(moreDataConfig.options.name)
expect(reporterPanicMock).not.toHaveBeenCalled()
})
it(`should compile gatsby-node.ts`, async () => {
const compiledGatsbyNode = await readFile(
`${dir.ts}/.cache/compiled/gatsby-node.js`,
`utf-8`
)
expect(compiledGatsbyNode).toContain(`I am working!`)
expect(reporterPanicMock).not.toHaveBeenCalled()
})
})
describe(`ts only in local plugin files are compiled and outputted where expected`, () => {
beforeAll(async () => {
process.chdir(dir.tsOnlyInLocal)
await remove(`${dir.tsOnlyInLocal}/.cache`)
await compileGatsbyFiles(dir.tsOnlyInLocal)
})
it(`should compile gatsby-config.ts`, async () => {
const compiledGatsbyConfig = await readFile(
`${dir.tsOnlyInLocal}/.cache/compiled/plugins/gatsby-plugin-local/gatsby-config.js`,
`utf-8`
)
expect(compiledGatsbyConfig).toContain(`gatsby-config is working`)
})
it(`should compile gatsby-node.ts`, async () => {
const compiledGatsbyNode = await readFile(
`${dir.tsOnlyInLocal}/.cache/compiled/plugins/gatsby-plugin-local/gatsby-node.js`,
`utf-8`
)
expect(compiledGatsbyNode).toContain(`gatsby-node is working`)
})
})
})
it(`handles errors in TS code`, async () => {
process.chdir(dir.errorInCode)
await remove(`${dir.errorInCode}/.cache`)
await compileGatsbyFiles(dir.errorInCode)
expect(reporterPanicMock).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"context": Object {
"filePath": "<PROJECT_ROOT>/gatsby-node.ts",
"generalMessage": "Expected ';', '}' or <eof>",
"hints": null,
"origin": "@parcel/transformer-js",
"specificMessage": "This is the expression part of an expression statement",
},
"id": "11901",
},
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`)
})
})
describe(`gatsby-node directory is allowed`, () => {
beforeAll(async () => {
process.chdir(dir.gatsbyNodeAsDirectory)
await remove(`${dir.gatsbyNodeAsDirectory}/.cache`)
})
beforeEach(() => {
reporterPanicMock.mockClear()
})
it(`should not panic on gatsby-node dir`, async () => {
await compileGatsbyFiles(dir.gatsbyNodeAsDirectory)
expect(reporterPanicMock).not.toHaveBeenCalled()
})
it(`should compile gatsby-node file and its dir files`, async () => {
const compiledGatsbyNode = await readFile(
`${dir.gatsbyNodeAsDirectory}/.cache/compiled/gatsby-node.js`,
`utf-8`
)
expect(compiledGatsbyNode).toContain(`I am working!`)
expect(reporterPanicMock).not.toHaveBeenCalled()
})
})
describe(`misnamed gatsby-node files`, () => {
beforeEach(() => {
reporterPanicMock.mockClear()
})
it(`should panic on gatsby-node.jsx`, async () => {
process.chdir(dir.misnamedJS)
await remove(`${dir.misnamedJS}/.cache`)
await compileGatsbyFiles(dir.misnamedJS)
expect(reporterPanicMock).toBeCalledWith({
id: `10128`,
context: {
configName: `gatsby-node`,
isTSX: false,
nearMatch: `gatsby-node.jsx`,
},
})
})
it(`should panic on gatsby-node.tsx`, async () => {
process.chdir(dir.misnamedTS)
await remove(`${dir.misnamedTS}/.cache`)
await compileGatsbyFiles(dir.misnamedTS)
expect(reporterPanicMock).toBeCalledWith({
id: `10128`,
context: {
configName: `gatsby-node`,
isTSX: true,
nearMatch: `gatsby-node.tsx`,
},
})
})
})