Skip to content

Commit 8543e52

Browse files
authored
chore(gatsby-cli): bundle dependencies (#27058)
* feat(cli): bundle package to workaround node_moduless tree resolution problems with npm * use bundled babel helpers * concurrently -> npm-run-all (for consistency) * inline babel config for rollup, get rif of "non-rollup-baell.config.js" * DRY extensions in config
1 parent 77143e8 commit 8543e52

File tree

3 files changed

+114
-6
lines changed

3 files changed

+114
-6
lines changed

packages/gatsby-cli/.babelrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"presets": [["babel-preset-gatsby-package"]]
2+
"presets": [
3+
[
4+
"babel-preset-gatsby-package"
5+
]
6+
]
37
}

packages/gatsby-cli/package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
"gatsby-recipes": "^0.2.33",
2828
"gatsby-telemetry": "^1.3.38",
2929
"hosted-git-info": "^3.0.6",
30-
"ink": "^2.7.1",
31-
"ink-spinner": "^3.1.0",
3230
"is-valid-path": "^0.1.1",
3331
"lodash": "^4.17.20",
3432
"meant": "^1.0.2",
@@ -37,7 +35,6 @@
3735
"pretty-error": "^2.1.1",
3836
"progress": "^2.0.3",
3937
"prompts": "^2.3.2",
40-
"react": "^16.8.0",
4138
"redux": "^4.0.5",
4239
"resolve-cwd": "^3.0.0",
4340
"semver": "^7.3.2",
@@ -48,16 +45,29 @@
4845
"update-notifier": "^4.1.3",
4946
"uuid": "3.4.0",
5047
"yargs": "^15.4.1",
48+
"yoga-layout-prebuilt": "^1.9.6",
5149
"yurnalist": "^1.1.2"
5250
},
5351
"devDependencies": {
5452
"@babel/cli": "^7.11.6",
5553
"@babel/core": "^7.11.6",
54+
"@rollup/plugin-babel": "^5.1.0",
55+
"@rollup/plugin-commonjs": "^14.0.0",
56+
"@rollup/plugin-json": "^4.1.0",
57+
"@rollup/plugin-node-resolve": "^8.4.0",
58+
"@rollup/plugin-replace": "^2.3.3",
5659
"@types/hosted-git-info": "^3.0.1",
5760
"@types/yargs": "^15.0.8",
5861
"babel-preset-gatsby-package": "^0.5.3",
5962
"cross-env": "^7.0.2",
6063
"rimraf": "^3.0.2",
64+
"ink": "^2.7.1",
65+
"ink-spinner": "^3.1.0",
66+
"npm-run-all": "4.1.5",
67+
"react": "^16.8.0",
68+
"rollup": "^2.23.0",
69+
"rollup-plugin-auto-external": "^2.0.0",
70+
"rollup-plugin-internal": "^1.0.0",
6171
"typescript": "^3.9.7"
6272
},
6373
"files": [
@@ -77,10 +87,14 @@
7787
"directory": "packages/gatsby-cli"
7888
},
7989
"scripts": {
80-
"build": "babel src --out-dir lib --ignore \"**/__tests__\" --extensions \".ts,.js,.tsx\"",
90+
"build:babel": "babel src --out-dir lib --ignore \"**/__tests__\" --ignore \"src/reporter/loggers/ink/**/*\" --extensions \".ts,.js,.tsx\"",
91+
"build:rollup": "rollup -c",
92+
"build": "npm-run-all -p build:babel build:rollup",
8193
"prepare": "cross-env NODE_ENV=production npm run build && npm run typegen",
8294
"typegen": "rimraf \"lib/**/*.d.ts\" && tsc --emitDeclarationOnly --declaration --declarationDir lib/",
83-
"watch": "babel -w src --out-dir lib --ignore \"**/__tests__\" --extensions \".ts,.js,.tsx\"",
95+
"watch:babel": "npm run build:babel -- --watch",
96+
"watch:rollup": "npm run build:rollup -- -w",
97+
"watch": "npm-run-all -p watch:babel watch:rollup",
8498
"postinstall": "node scripts/postinstall.js"
8599
},
86100
"engines": {

packages/gatsby-cli/rollup.config.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import resolve from "@rollup/plugin-node-resolve"
2+
import babel from "@rollup/plugin-babel"
3+
import commonjs from "@rollup/plugin-commonjs"
4+
import json from "@rollup/plugin-json"
5+
import replace from "@rollup/plugin-replace";
6+
import autoExternal from "rollup-plugin-auto-external"
7+
import internal from "rollup-plugin-internal"
8+
9+
import path from "path"
10+
11+
// Rollup hoists Ink's dynamic require of react-devtools-core which causes
12+
// a window not found error so we exclude Ink's devtools file for now.
13+
function excludeDevTools() {
14+
const re = /ink/
15+
return {
16+
name: `ignoreDevTools`,
17+
18+
load(id) {
19+
if (id.match(re)) {
20+
if (path.parse(id).name === `devtools`) {
21+
return { code: `` }
22+
}
23+
}
24+
},
25+
}
26+
}
27+
28+
const extensions = [`.mjs`, `.js`, `.json`, `.node`, `.ts`, `.tsx`]
29+
30+
export default {
31+
input: `src/reporter/loggers/ink/index.tsx`,
32+
output: {
33+
file: `lib/reporter/loggers/ink/index.js`,
34+
format: `cjs`,
35+
},
36+
cache: false,
37+
plugins: [
38+
replace({
39+
values: {
40+
"process.env.NODE_ENV": JSON.stringify(`production`)
41+
}
42+
}),
43+
excludeDevTools(),
44+
json(),
45+
babel({
46+
extensions,
47+
babelHelpers: `bundled`,
48+
skipPreflightCheck: true,
49+
exclude: `node_modules/**`,
50+
babelrc: false,
51+
presets: [
52+
[
53+
"@babel/env",
54+
{
55+
"modules": false,
56+
"shippedProposals": true,
57+
"targets": { "node": "10.13.0" }
58+
}
59+
],
60+
"@babel/preset-react"
61+
],
62+
plugins: ["@babel/plugin-transform-runtime"],
63+
overrides: [
64+
{
65+
"test": ["**/*.ts", "**/*.tsx"],
66+
"plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]]
67+
}
68+
]
69+
}),
70+
resolve({
71+
extensions,
72+
dedupe: [ `react`, `ink` ]
73+
}),
74+
commonjs(),
75+
autoExternal(),
76+
internal([
77+
`react`,
78+
`ink`,
79+
`ink-spinner`
80+
]),
81+
],
82+
external: [
83+
`yoga-layout-prebuilt`,
84+
// Next one deserve explanation: ... it's because ink logger imports
85+
// getStore, onLogAction from higher up (../../redux). But we don't want
86+
// two copies of it - one bundled and one not, because it would result
87+
// in multiple store copies
88+
`../../redux`
89+
]
90+
}

0 commit comments

Comments
 (0)