From 379704baa1621e92e5a718fc9a66edcf9429e27c Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 14 Sep 2020 10:50:41 +0200 Subject: [PATCH 1/4] chore(gatsby-cli): bundle ink logger --- packages/gatsby-cli/.babelrc | 3 - packages/gatsby-cli/.babelrc.json | 13 ++++ .../gatsby-cli/non-rollup-babel.config.js | 7 ++ packages/gatsby-cli/package.json | 23 ++++-- packages/gatsby-cli/rollup.config.js | 70 +++++++++++++++++++ yarn.lock | 10 ++- 6 files changed, 117 insertions(+), 9 deletions(-) delete mode 100644 packages/gatsby-cli/.babelrc create mode 100644 packages/gatsby-cli/.babelrc.json create mode 100644 packages/gatsby-cli/non-rollup-babel.config.js create mode 100644 packages/gatsby-cli/rollup.config.js diff --git a/packages/gatsby-cli/.babelrc b/packages/gatsby-cli/.babelrc deleted file mode 100644 index ac0ad292bb087..0000000000000 --- a/packages/gatsby-cli/.babelrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "presets": [["babel-preset-gatsby-package"]] -} diff --git a/packages/gatsby-cli/.babelrc.json b/packages/gatsby-cli/.babelrc.json new file mode 100644 index 0000000000000..321375b36ac47 --- /dev/null +++ b/packages/gatsby-cli/.babelrc.json @@ -0,0 +1,13 @@ +{ + "presets": [ + ["@babel/env", { "modules": false, "targets": { "node": "10.13.0" } }], + "@babel/preset-react" + ], + "plugins": ["@babel/plugin-transform-runtime"], + "overrides": [ + { + "test": ["**/*.ts", "**/*.tsx"], + "plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]] + } + ] +} diff --git a/packages/gatsby-cli/non-rollup-babel.config.js b/packages/gatsby-cli/non-rollup-babel.config.js new file mode 100644 index 0000000000000..ebde19e411202 --- /dev/null +++ b/packages/gatsby-cli/non-rollup-babel.config.js @@ -0,0 +1,7 @@ +// This being a babel.config.js file instead of a .babelrc file allows the +// packages in `internal-plugins` to be compiled with the rest of the source. +// Ref: https://github.com/babel/babel/pull/7358 + +const configPath = require(`path`).join(__dirname, `..`, `..`, `.babelrc.js`) + +module.exports = require(configPath) diff --git a/packages/gatsby-cli/package.json b/packages/gatsby-cli/package.json index 53a6f615b0e7f..f231a36102793 100644 --- a/packages/gatsby-cli/package.json +++ b/packages/gatsby-cli/package.json @@ -27,8 +27,6 @@ "gatsby-recipes": "^0.2.24", "gatsby-telemetry": "^1.3.32", "hosted-git-info": "^3.0.4", - "ink": "^2.7.1", - "ink-spinner": "^3.1.0", "is-valid-path": "^0.1.1", "lodash": "^4.17.20", "meant": "^1.0.1", @@ -37,7 +35,6 @@ "pretty-error": "^2.1.1", "progress": "^2.0.3", "prompts": "^2.3.2", - "react": "^16.8.0", "redux": "^4.0.5", "resolve-cwd": "^3.0.0", "semver": "^7.3.2", @@ -53,11 +50,23 @@ "devDependencies": { "@babel/cli": "^7.10.3", "@babel/core": "^7.10.3", + "@rollup/plugin-babel": "^5.1.0", + "@rollup/plugin-commonjs": "^14.0.0", + "@rollup/plugin-json": "^4.1.0", + "@rollup/plugin-node-resolve": "^8.4.0", + "@rollup/plugin-replace": "^2.3.3", "@types/hosted-git-info": "^3.0.0", "@types/yargs": "^15.0.4", "babel-preset-gatsby-package": "^0.5.2", + "concurrently": "^5.0.0", "cross-env": "^5.2.1", "rimraf": "^3.0.2", + "ink": "^2.7.1", + "ink-spinner": "^3.1.0", + "react": "^16.8.0", + "rollup": "^2.23.0", + "rollup-plugin-auto-external": "^2.0.0", + "rollup-plugin-internal": "^1.0.0", "typescript": "^3.9.5" }, "files": [ @@ -77,10 +86,14 @@ "directory": "packages/gatsby-cli" }, "scripts": { - "build": "babel src --out-dir lib --ignore \"**/__tests__\" --extensions \".ts,.js,.tsx\"", + "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\"", + "build:rollup": "rollup -c", + "build": "concurrently \"npm run build:babel\" \"npm run build:rollup\"", "prepare": "cross-env NODE_ENV=production npm run build && npm run typegen", "typegen": "rimraf \"lib/**/*.d.ts\" && tsc --emitDeclarationOnly --declaration --declarationDir lib/", - "watch": "babel -w src --out-dir lib --ignore \"**/__tests__\" --extensions \".ts,.js,.tsx\"", + "watch:babel": "npm run build:babel -- --watch", + "watch:rollup": "npm run build:rollup -- -w", + "watch": "concurrently \"npm run watch:babel\" \"npm run watch:rollup\"", "postinstall": "node scripts/postinstall.js" }, "engines": { diff --git a/packages/gatsby-cli/rollup.config.js b/packages/gatsby-cli/rollup.config.js new file mode 100644 index 0000000000000..b5c6607f46c90 --- /dev/null +++ b/packages/gatsby-cli/rollup.config.js @@ -0,0 +1,70 @@ +import resolve from "@rollup/plugin-node-resolve" +import babel from "@rollup/plugin-babel" +import commonjs from "@rollup/plugin-commonjs" +import json from "@rollup/plugin-json" +import replace from "@rollup/plugin-replace"; +import autoExternal from "rollup-plugin-auto-external" +import internal from "rollup-plugin-internal" + +import path from "path" + +// Rollup hoists Ink's dynamic require of react-devtools-core which causes +// a window not found error so we exclude Ink's devtools file for now. +function excludeDevTools() { + const re = /ink/ + return { + name: `ignoreDevTools`, + + load(id) { + if (id.match(re)) { + if (path.parse(id).name === `devtools`) { + return { code: `` } + } + } + }, + } +} + + +export default { + input: `src/reporter/loggers/ink/index.tsx`, + output: { + file: `lib/reporter/loggers/ink/index.js`, + format: `cjs`, + }, + cache: false, + plugins: [ + replace({ + values: { + "process.env.NODE_ENV": JSON.stringify(`production`) + } + }), + excludeDevTools(), + json(), + babel({ + extensions: [`.js`, `.jsx`, `.es6`, `.es`, `.mjs`, `.ts`, `.tsx`] , + babelHelpers: `runtime`, + skipPreflightCheck: true, + exclude: `node_modules/**`, + }), + resolve({ + extensions: [`.mjs`, `.js`, `.json`, `.node`, `.ts`, `.tsx`], + dedupe: [ `react`, `ink` ] + }), + commonjs(), + autoExternal(), + internal([ + `react`, + `ink`, + `ink-spinner` + ]), + ], + external: [ + `yoga-layout-prebuilt`, + // Next one deserve explanation: ... it's because ink logger imports + // getStore, onLogAction from higher up (../../redux). But we don't want + // two copies of it - one bundled and one not, because it would result + // in multiple store copies + `../../redux` + ] +} diff --git a/yarn.lock b/yarn.lock index 89e8d994d8522..4fb284c30df04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3094,6 +3094,14 @@ is-module "^1.0.0" resolve "^1.17.0" +"@rollup/plugin-replace@^2.3.3": + version "2.3.3" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.3.tgz#cd6bae39444de119f5d905322b91ebd4078562e7" + integrity sha512-XPmVXZ7IlaoWaJLkSCDaa0Y6uVo5XQYHhiMFzOd5qSv5rE+t/UJToPIOE56flKIxBFQI27ONsxb7dqHnwSsjKQ== + dependencies: + "@rollup/pluginutils" "^3.0.8" + magic-string "^0.25.5" + "@rollup/pluginutils@^3.0.0", "@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.1.0": version "3.1.0" resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b" @@ -15684,7 +15692,7 @@ magic-string@^0.22.4: dependencies: vlq "^0.2.2" -magic-string@^0.25.2, magic-string@^0.25.3: +magic-string@^0.25.2, magic-string@^0.25.3, magic-string@^0.25.5: version "0.25.7" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== From 563a3f19fafdbc8baa2dd89cfdd52a7d6b43bc97 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 14 Sep 2020 11:54:55 +0200 Subject: [PATCH 2/4] use shippedProposals (to handle class properties etc) --- packages/gatsby-cli/.babelrc.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-cli/.babelrc.json b/packages/gatsby-cli/.babelrc.json index 321375b36ac47..eb6ed780bceff 100644 --- a/packages/gatsby-cli/.babelrc.json +++ b/packages/gatsby-cli/.babelrc.json @@ -1,6 +1,13 @@ { "presets": [ - ["@babel/env", { "modules": false, "targets": { "node": "10.13.0" } }], + [ + "@babel/env", + { + "modules": false, + "shippedProposals": true, + "targets": { "node": "10.13.0" } + } + ], "@babel/preset-react" ], "plugins": ["@babel/plugin-transform-runtime"], From c6c2368e02961f5c0d02213ffcaddb237c50b053 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 14 Sep 2020 12:48:21 +0200 Subject: [PATCH 3/4] 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: https://github.com/facebook/yoga/issues/798 the solution there is pretty hacky and I would rather not use it. --- packages/gatsby-cli/package.json | 1 + packages/gatsby-cli/rollup.config.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-cli/package.json b/packages/gatsby-cli/package.json index f231a36102793..08a1364f76828 100644 --- a/packages/gatsby-cli/package.json +++ b/packages/gatsby-cli/package.json @@ -45,6 +45,7 @@ "update-notifier": "^4.1.0", "uuid": "3.4.0", "yargs": "^15.3.1", + "yoga-layout-prebuilt": "^1.9.6", "yurnalist": "^1.1.2" }, "devDependencies": { diff --git a/packages/gatsby-cli/rollup.config.js b/packages/gatsby-cli/rollup.config.js index b5c6607f46c90..7d8655398b406 100644 --- a/packages/gatsby-cli/rollup.config.js +++ b/packages/gatsby-cli/rollup.config.js @@ -25,7 +25,6 @@ function excludeDevTools() { } } - export default { input: `src/reporter/loggers/ink/index.tsx`, output: { From 8837d22ccce36d87ffb67e394e8bb360924e9afc Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 14 Sep 2020 16:43:21 +0200 Subject: [PATCH 4/4] .babelrc.json -> .babelrc --- packages/gatsby-cli/{.babelrc.json => .babelrc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/gatsby-cli/{.babelrc.json => .babelrc} (100%) diff --git a/packages/gatsby-cli/.babelrc.json b/packages/gatsby-cli/.babelrc similarity index 100% rename from packages/gatsby-cli/.babelrc.json rename to packages/gatsby-cli/.babelrc