From f2bce35acf38715d507f4a83870226b823888347 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Tue, 1 Mar 2022 14:12:43 +0100 Subject: [PATCH] chore(gatsby-parcel-config): stabilize ts compilation parcel output (#34943) --- packages/gatsby-parcel-config/lib/index.json | 2 +- packages/gatsby-parcel-config/package.json | 2 + .../parcel/__tests__/compile-gatsby-files.ts | 38 +++++++++++++++++++ .../gatsby-plugin-local/gatsby-config.ts | 2 + .../gatsby-plugin-local/gatsby-node.ts | 1 + yarn.lock | 30 +++++++++++++++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-config.ts create mode 100644 packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-node.ts diff --git a/packages/gatsby-parcel-config/lib/index.json b/packages/gatsby-parcel-config/lib/index.json index 192b00c859c82..931c557b9fca4 100644 --- a/packages/gatsby-parcel-config/lib/index.json +++ b/packages/gatsby-parcel-config/lib/index.json @@ -23,7 +23,7 @@ "*.{xml,rss,atom}": ["@parcel/transformer-xml"], "url:*": ["...", "@parcel/transformer-raw"] }, - "namers": ["@parcel/namer-default"], + "namers": ["@gatsbyjs/parcel-namer-relative-to-cwd", "@parcel/namer-default"], "runtimes": [ "@parcel/runtime-js", "@parcel/runtime-browser-hmr", diff --git a/packages/gatsby-parcel-config/package.json b/packages/gatsby-parcel-config/package.json index 5f68573d070c2..c4c2da3a5d449 100644 --- a/packages/gatsby-parcel-config/package.json +++ b/packages/gatsby-parcel-config/package.json @@ -13,6 +13,7 @@ "parcel": "2.x" }, "dependencies": { + "@gatsbyjs/parcel-namer-relative-to-cwd": "0.0.2", "@parcel/bundler-default": "^2.3.1", "@parcel/compressor-raw": "^2.3.1", "@parcel/namer-default": "^2.3.1", @@ -31,6 +32,7 @@ "@parcel/transformer-react-refresh-wrap": "^2.3.1" }, "parcelDependencies": { + "@gatsbyjs/parcel-namer-relative-to-cwd": "0.0.2", "@parcel/optimizer-data-url": "^2.3.1", "@parcel/packager-raw-url": "^2.3.1", "@parcel/packager-ts": "^2.3.1", diff --git a/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts b/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts index 80eabbf6e7981..6cc0762f22115 100644 --- a/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts +++ b/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts @@ -13,6 +13,7 @@ import { readFile, remove, pathExists } from "fs-extra" const dir = { js: `${__dirname}/fixtures/js`, ts: `${__dirname}/fixtures/ts`, + tsOnlyInLocal: `${__dirname}/fixtures/ts-only-in-local-plugin`, } jest.mock(`@parcel/core`, () => { @@ -37,6 +38,15 @@ interface IMockedParcel extends Parcel { options: unknown } +let cwdToRestore +beforeAll(() => { + cwdToRestore = process.cwd() +}) + +afterAll(() => { + process.chdir(cwdToRestore) +}) + describe(`gatsby file compilation`, () => { describe(`constructBundler`, () => { it(`should construct Parcel relative to passed directory`, () => { @@ -60,6 +70,7 @@ describe(`gatsby file compilation`, () => { describe(`compileGatsbyFiles`, () => { describe(`js files are not touched`, () => { beforeAll(async () => { + process.chdir(dir.js) await remove(`${dir.js}/.cache`) await compileGatsbyFiles(dir.js) }) @@ -81,6 +92,7 @@ describe(`gatsby file compilation`, () => { describe(`ts files are compiled`, () => { beforeAll(async () => { + process.chdir(dir.ts) await remove(`${dir.ts}/.cache`) await compileGatsbyFiles(dir.ts) }) @@ -105,5 +117,31 @@ describe(`gatsby file compilation`, () => { expect(compiledGatsbyNode).toContain(`I am working!`) }) }) + + describe(`ts only in local plugin files are compiled and outputted where expected`, () => { + beforeAll(async () => { + process.chdir(dir.tsOnlyInLocal) + await remove(`${dir.tsOnlyInLocal}/.cache`) + await compileGatsbyFiles(dir.tsOnlyInLocal) + }) + + it(`should compile gatsby-config.ts`, async () => { + const compiledGatsbyConfig = await readFile( + `${dir.tsOnlyInLocal}/.cache/compiled/plugins/gatsby-plugin-local/gatsby-config.js`, + `utf-8` + ) + + expect(compiledGatsbyConfig).toContain(`gatsby-config is working`) + }) + + it(`should compile gatsby-node.ts`, async () => { + const compiledGatsbyNode = await readFile( + `${dir.tsOnlyInLocal}/.cache/compiled/plugins/gatsby-plugin-local/gatsby-node.js`, + `utf-8` + ) + + expect(compiledGatsbyNode).toContain(`gatsby-node is working`) + }) + }) }) }) diff --git a/packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-config.ts b/packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-config.ts new file mode 100644 index 0000000000000..3a7a7529e1fba --- /dev/null +++ b/packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-config.ts @@ -0,0 +1,2 @@ +console.log(`gatsby-config is working`) + diff --git a/packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-node.ts b/packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-node.ts new file mode 100644 index 0000000000000..ba80e8a3b279f --- /dev/null +++ b/packages/gatsby/src/utils/parcel/__tests__/fixtures/ts-only-in-local-plugin/plugins/gatsby-plugin-local/gatsby-node.ts @@ -0,0 +1 @@ +console.log(`gatsby-node is working`) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index f65082aee4b8e..c06c2ead01a52 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1671,6 +1671,15 @@ unique-filename "^1.1.1" which "^1.3.1" +"@gatsbyjs/parcel-namer-relative-to-cwd@0.0.2": + version "0.0.2" + resolved "https://registry.yarnpkg.com/@gatsbyjs/parcel-namer-relative-to-cwd/-/parcel-namer-relative-to-cwd-0.0.2.tgz#e1586f1796aa773e53e0909025ea16e423c14391" + integrity sha512-ZeGxCbx13+zjpE/0HuJ/tjox9zfiYq9fGoAAi+RHP5vHSJCmJVO5hZbexQ/umlUyAkkkzC4p1WIpw1cYQTA8SA== + dependencies: + "@babel/runtime" "^7.15.4" + "@parcel/plugin" "2.3.1" + gatsby-core-utils "^3.8.2" + "@gatsbyjs/reach-router@^1.3.6": version "1.3.6" resolved "https://registry.yarnpkg.com/@gatsbyjs/reach-router/-/reach-router-1.3.6.tgz#4e8225836959be247890b66f21a3198a0589e34d" @@ -11228,6 +11237,27 @@ gather-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/gather-stream/-/gather-stream-1.0.0.tgz#b33994af457a8115700d410f317733cbe7a0904b" +gatsby-core-utils@^3.8.2: + version "3.8.2" + resolved "https://registry.yarnpkg.com/gatsby-core-utils/-/gatsby-core-utils-3.8.2.tgz#9c2869f93a740d20536b1e92c8020efd85d43e5f" + integrity sha512-UwANr9yd8ayLinPDoRbU/rRgoOBOS715qe2LYCxq6hAtRabHWTEM8Vj0wh7LmyVGexx8MFgvp2NKikG2TZ5pzQ== + dependencies: + "@babel/runtime" "^7.15.4" + ci-info "2.0.0" + configstore "^5.0.1" + fastq "^1.13.0" + file-type "^16.5.3" + fs-extra "^10.0.0" + got "^11.8.3" + import-from "^4.0.0" + lmdb "^2.1.7" + lock "^1.1.0" + node-object-hash "^2.3.10" + proper-lockfile "^4.1.2" + resolve-from "^5.0.0" + tmp "^0.2.1" + xdg-basedir "^4.0.0" + gauge@~2.7.3: version "2.7.4" resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"