Skip to content

Commit fe596ec

Browse files
authoredSep 15, 2020
chore(gatsby-cli): bundle ink logger (#26887)
* chore(gatsby-cli): bundle ink logger * use shippedProposals (to handle class properties etc) * add yoga-layout-prebuilt as dependency This is needed for PnP as setting `yoga-layout-prebuilt` as external imports it from node_modules, but it's not declared by gatsby-cli itself. Alternatively we could bundle it too, but this has problems too: facebook/yoga#798 the solution there is pretty hacky and I would rather not use it. * .babelrc.json -> .babelrc
1 parent 617cc1d commit fe596ec

File tree

5 files changed

+122
-7
lines changed

5 files changed

+122
-7
lines changed
 

‎packages/gatsby-cli/.babelrc

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
{
2-
"presets": [["babel-preset-gatsby-package"]]
2+
"presets": [
3+
[
4+
"@babel/env",
5+
{
6+
"modules": false,
7+
"shippedProposals": true,
8+
"targets": { "node": "10.13.0" }
9+
}
10+
],
11+
"@babel/preset-react"
12+
],
13+
"plugins": ["@babel/plugin-transform-runtime"],
14+
"overrides": [
15+
{
16+
"test": ["**/*.ts", "**/*.tsx"],
17+
"plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]]
18+
}
19+
]
320
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This being a babel.config.js file instead of a .babelrc file allows the
2+
// packages in `internal-plugins` to be compiled with the rest of the source.
3+
// Ref: https://github.com/babel/babel/pull/7358
4+
5+
const configPath = require(`path`).join(__dirname, `..`, `..`, `.babelrc.js`)
6+
7+
module.exports = require(configPath)

‎packages/gatsby-cli/package.json

+19-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
"gatsby-recipes": "^0.2.26",
2828
"gatsby-telemetry": "^1.3.34",
2929
"hosted-git-info": "^3.0.4",
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.1",
@@ -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.0",
4946
"uuid": "3.4.0",
5047
"yargs": "^15.3.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.0",
5760
"@types/yargs": "^15.0.4",
5861
"babel-preset-gatsby-package": "^0.5.3",
62+
"concurrently": "^5.0.0",
5963
"cross-env": "^5.2.1",
6064
"rimraf": "^3.0.2",
65+
"ink": "^2.7.1",
66+
"ink-spinner": "^3.1.0",
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.5"
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 --config-file ./non-rollup-babel.config.js src --out-dir lib --ignore \"**/__tests__\" --ignore \"src/reporter/loggers/ink/**/*\" --extensions \".ts,.js,.tsx\"",
91+
"build:rollup": "rollup -c",
92+
"build": "concurrently \"npm run build:babel\" \"npm run 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": "concurrently \"npm run watch:babel\" \"npm run watch:rollup\"",
8498
"postinstall": "node scripts/postinstall.js"
8599
},
86100
"engines": {

‎packages/gatsby-cli/rollup.config.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
export default {
29+
input: `src/reporter/loggers/ink/index.tsx`,
30+
output: {
31+
file: `lib/reporter/loggers/ink/index.js`,
32+
format: `cjs`,
33+
},
34+
cache: false,
35+
plugins: [
36+
replace({
37+
values: {
38+
"process.env.NODE_ENV": JSON.stringify(`production`)
39+
}
40+
}),
41+
excludeDevTools(),
42+
json(),
43+
babel({
44+
extensions: [`.js`, `.jsx`, `.es6`, `.es`, `.mjs`, `.ts`, `.tsx`] ,
45+
babelHelpers: `runtime`,
46+
skipPreflightCheck: true,
47+
exclude: `node_modules/**`,
48+
}),
49+
resolve({
50+
extensions: [`.mjs`, `.js`, `.json`, `.node`, `.ts`, `.tsx`],
51+
dedupe: [ `react`, `ink` ]
52+
}),
53+
commonjs(),
54+
autoExternal(),
55+
internal([
56+
`react`,
57+
`ink`,
58+
`ink-spinner`
59+
]),
60+
],
61+
external: [
62+
`yoga-layout-prebuilt`,
63+
// Next one deserve explanation: ... it's because ink logger imports
64+
// getStore, onLogAction from higher up (../../redux). But we don't want
65+
// two copies of it - one bundled and one not, because it would result
66+
// in multiple store copies
67+
`../../redux`
68+
]
69+
}

‎yarn.lock

+9-1
Original file line numberDiff line numberDiff line change
@@ -3107,6 +3107,14 @@
31073107
is-module "^1.0.0"
31083108
resolve "^1.17.0"
31093109

3110+
"@rollup/plugin-replace@^2.3.3":
3111+
version "2.3.3"
3112+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.3.tgz#cd6bae39444de119f5d905322b91ebd4078562e7"
3113+
integrity sha512-XPmVXZ7IlaoWaJLkSCDaa0Y6uVo5XQYHhiMFzOd5qSv5rE+t/UJToPIOE56flKIxBFQI27ONsxb7dqHnwSsjKQ==
3114+
dependencies:
3115+
"@rollup/pluginutils" "^3.0.8"
3116+
magic-string "^0.25.5"
3117+
31103118
"@rollup/pluginutils@^3.0.0", "@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.1.0":
31113119
version "3.1.0"
31123120
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
@@ -15696,7 +15704,7 @@ magic-string@^0.22.4:
1569615704
dependencies:
1569715705
vlq "^0.2.2"
1569815706

15699-
magic-string@^0.25.2, magic-string@^0.25.3:
15707+
magic-string@^0.25.2, magic-string@^0.25.3, magic-string@^0.25.5:
1570015708
version "0.25.7"
1570115709
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
1570215710
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==

0 commit comments

Comments
 (0)
Please sign in to comment.