forked from coralproject/talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
216 lines (201 loc) · 6.3 KB
/
gatsby-node.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
const fs = require("fs");
const path = require("path");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
/** Path to postCSSConfig */
const postCSSConfigPath = path.resolve(
__dirname,
"../src/core/build/postcss.config"
);
const rootDir = path.resolve(__dirname, "../");
const srcDir = path.resolve(rootDir, "./src");
const appTsconfig = path.resolve(rootDir, "./src/core/client/tsconfig.json");
const CSS_PATTERN = /\.css$/;
const MODULE_CSS_PATTERN = /\.module\.css$/;
// Define `RegExp.toJSON` so that we can stringify RegExp.
Object.defineProperty(RegExp.prototype, "toJSON", {
value: RegExp.prototype.toString,
});
const isCssRules = (rule) =>
rule.test &&
(rule.test.toString() === CSS_PATTERN.toString() ||
rule.test.toString() === MODULE_CSS_PATTERN.toString());
const findCssRules = (config) =>
config.module.rules.find(
(rule) => Array.isArray(rule.oneOf) && rule.oneOf.every(isCssRules)
);
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
getConfig,
}) => {
// Get webpack config.
const config = getConfig();
if (stage === "develop") {
config.entry.commons.push(
// Add our stream css variables file.
`${srcDir}/core/client/ui/theme/stream.css.ts`
);
}
/*
TODO: (cvle) couldn't get build to work...
if (stage === "build-javascript") {
config.entry.app = [
config.entry.app,
`${appDir}/core/client/ui/theme/stream.css.ts`,
];
}
*/
// Find the gatsby CSS rules.
const cssRules = findCssRules(config);
// Exclude them from our src dir because they are incomaptible with our
// CSS rules.
cssRules.exclude = srcDir;
// Add .tx .tsx to modules
config.resolve.extensions.push(".ts", ".tsx");
actions.replaceWebpackConfig(config);
// Write out webpack config to .docz folder.
fs.writeFileSync(
path.resolve(__dirname, "webpack-" + stage),
JSON.stringify(config, {}, 2)
);
// Turn on sourceMap during develop.
const sourceMap = stage.startsWith("develop");
// CSS loaders to prepend.
const prependCSSLoaders = [];
if (stage === "develop") {
prependCSSLoaders.push(loaders.style());
}
/*
TODO: (cvle) couldn't get build to work...
if (stage === "build-javascript") {
moreLoaders.push(loaders.style());
}
*/
actions.setWebpackConfig({
resolve: {
plugins: [
// Resolve our custom paths.
new TsconfigPathsPlugin({
extensions: [".ts", ".tsx", ".js"],
configFile: path.resolve(rootDir, "./src/core/client/tsconfig.json"),
}),
],
},
module: {
rules: [
{
include: srcDir,
oneOf: [
{
test: /\.css\.ts$/,
use: [
...prependCSSLoaders,
{
loader: require.resolve("css-loader"),
options: {
modules: {
localIdentName: "[name]-[local]-[hash:base64:5]",
},
importLoaders: 2,
sourceMap,
},
},
{
loader: require.resolve("postcss-loader"),
options: {
config: {
path: postCSSConfigPath,
},
parser: "postcss-js",
},
},
{
loader: require.resolve("babel-loader"),
options: {
configFile: false,
babelrc: false,
presets: [
"@babel/typescript",
[
"@babel/env",
{ targets: { node: "current" }, modules: "commonjs" },
],
],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
},
},
],
},
{
test: /\.css$/,
use: [
...prependCSSLoaders,
{
loader: require.resolve("css-loader"),
options: {
modules: {
localIdentName: "[name]-[local]-[hash:base64:5]",
},
importLoaders: 1,
sourceMap,
},
},
{
loader: require.resolve("postcss-loader"),
options: {
config: {
path: postCSSConfigPath,
},
},
},
],
},
{
test: /\.tsx?$/,
use: [
{
loader: require.resolve("babel-loader"),
options: {
root: rootDir,
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
},
},
{
loader: require.resolve("ts-loader"),
options: {
configFile: appTsconfig,
compilerOptions: {
target: "es2015",
module: "esnext",
jsx: "preserve",
noEmit: false,
},
transpileOnly: true,
// Overwrites the behavior of `include` and `exclude` to only
// include files that are actually being imported and which
// are necessary to compile the bundle.
onlyCompileBundledFiles: true,
},
},
],
},
],
},
],
},
});
// Write out processed webpack config to .docz folder.
fs.writeFileSync(
path.resolve(__dirname, "webpack-" + stage + "-processed"),
JSON.stringify(getConfig(), {}, 2)
);
};