-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
bundle-webpack.ts
153 lines (144 loc) · 4.32 KB
/
bundle-webpack.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
/* eslint-disable @typescript-eslint/naming-convention */
import * as path from "path"
import * as fs from "fs-extra"
import webpack from "webpack"
import { printQueryEnginePlugins } from "./print-plugins"
import mod from "module"
import { WebpackLoggingPlugin } from "../../utils/webpack/plugins/webpack-logging"
import reporter from "gatsby-cli/lib/reporter"
type Reporter = typeof reporter
const extensions = [`.mjs`, `.js`, `.json`, `.node`, `.ts`, `.tsx`]
const outputDir = path.join(process.cwd(), `.cache`, `query-engine`)
const cacheLocation = path.join(
process.cwd(),
`.cache`,
`webpack`,
`query-engine`
)
export async function createGraphqlEngineBundle(
rootDir: string,
reporter: Reporter,
isVerbose?: boolean
): Promise<webpack.Compilation | undefined> {
const schemaSnapshotString = await fs.readFile(
path.join(rootDir, `.cache`, `schema.gql`),
`utf-8`
)
await printQueryEnginePlugins()
const compiler = webpack({
name: `Query Engine`,
// mode: `production`,
mode: `none`,
entry: path.join(__dirname, `entry.js`),
output: {
path: outputDir,
filename: `index.js`,
libraryTarget: `commonjs`,
},
target: `node`,
externalsPresets: {
node: false,
},
cache: {
type: `filesystem`,
name: `graphql-engine`,
cacheLocation,
buildDependencies: {
config: [__filename],
},
},
// those are required in some runtime paths, but we don't need them
externals: [
`cbor-x`, // optional dep of lmdb-store, but we are using `msgpack` (default) encoding, so we don't need it
`babel-runtime/helpers/asyncToGenerator`, // undeclared dep of yurnalist (but used in code path we don't use)
`electron`, // :shrug: `got` seems to have electron specific code path
mod.builtinModules.reduce((acc, builtinModule) => {
if (builtinModule === `fs`) {
acc[builtinModule] = `global _actualFsWrapper`
} else {
acc[builtinModule] = `commonjs ${builtinModule}`
}
return acc
}, {}),
],
module: {
rules: [
{
test: /node_modules[/\\]lmdb[/\\]/,
parser: { amd: false },
use: [
{
loader: require.resolve(`@vercel/webpack-asset-relocator-loader`),
options: {
outputAssetBase: `assets`,
},
},
{
loader: require.resolve(`./lmdb-bundling-patch`),
},
],
},
{
test: /\.m?js$/,
type: `javascript/auto`,
resolve: {
byDependency: {
esm: {
fullySpecified: false,
},
},
},
},
{
// For node binary relocations, include ".node" files as well here
test: /\.(m?js|node)$/,
// it is recommended for Node builds to turn off AMD support
parser: { amd: false },
use: {
loader: require.resolve(`@vercel/webpack-asset-relocator-loader`),
options: {
outputAssetBase: `assets`,
},
},
},
{
test: /\.txt/,
type: `asset/resource`,
},
],
},
resolve: {
extensions,
alias: {
".cache": process.cwd() + `/.cache/`,
[require.resolve(`gatsby-cli/lib/reporter/loggers/ink/index.js`)]:
false,
inquirer: false,
},
},
plugins: [
new webpack.DefinePlugin({
// "process.env.GATSBY_LOGGER": JSON.stringify(`yurnalist`),
"process.env.GATSBY_EXPERIMENTAL_LMDB_STORE": `true`,
"process.env.GATSBY_SKIP_WRITING_SCHEMA_TO_FILE": `true`,
SCHEMA_SNAPSHOT: JSON.stringify(schemaSnapshotString),
"process.env.GATSBY_LOGGER": JSON.stringify(`yurnalist`),
}),
process.env.GATSBY_WEBPACK_LOGGING?.includes(`query-engine`) &&
new WebpackLoggingPlugin(rootDir, reporter, isVerbose),
].filter(Boolean) as Array<webpack.WebpackPluginInstance>,
})
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
compiler.close(closeErr => {
if (err) {
return reject(err)
}
if (closeErr) {
return reject(closeErr)
}
return resolve(stats?.compilation)
})
})
})
}