Skip to content

Commit 459fab4

Browse files
LekoArtsrenovate-botpieh
authored
feat: Add gatsby-parcel-namer-relative-to-cwd to monorepo & update Parcel to 2.5.0 (#35446)
* fix(deps): update parcel to ^2.4.1 * update peerDep * add namer plugin * update versions * add npmignore * update parcel to 2.5.0 * install gatsby-dev-cli@next * deps * slash * update lock file * pin to 2.5.0 * also pin gatsby's parcel packages, to prevent potential for transitive parcel packages version mismatches Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]>
1 parent f7f8ffe commit 459fab4

File tree

9 files changed

+557
-446
lines changed

9 files changed

+557
-446
lines changed

.circleci/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ commands:
166166
name: Upgrade React to << parameters.react_version >>
167167
command: "REACT_VERSION=<< parameters.react_version >> TEST_PATH=<< parameters.test_path >> node ./scripts/upgrade-react"
168168
- run:
169-
name: Install gatsby-dev
170-
command: yarn global add gatsby-dev-cli
169+
name: Install gatsby-dev@next
170+
command: yarn global add gatsby-dev-cli@next
171171
- run:
172172
name: Run tests (using defaults)
173173
command: ./scripts/e2e-test.sh "<< parameters.test_path >>" "<< parameters.test_command >>"

packages/gatsby-parcel-config/package.json

+34-33
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,47 @@
99
"url": "https://github.com/gatsbyjs/gatsby.git",
1010
"directory": "packages/gatsby-parcel-config"
1111
},
12+
"license": "MIT",
1213
"engines": {
1314
"parcel": "2.x"
1415
},
1516
"dependencies": {
16-
"@gatsbyjs/parcel-namer-relative-to-cwd": "0.0.2",
17-
"@parcel/bundler-default": "^2.3.2",
18-
"@parcel/compressor-raw": "^2.3.2",
19-
"@parcel/namer-default": "^2.3.2",
20-
"@parcel/optimizer-terser": "^2.3.2",
21-
"@parcel/packager-js": "^2.3.2",
22-
"@parcel/packager-raw": "^2.3.2",
23-
"@parcel/reporter-dev-server": "^2.3.2",
24-
"@parcel/resolver-default": "^2.3.2",
25-
"@parcel/runtime-browser-hmr": "^2.3.2",
26-
"@parcel/runtime-js": "^2.3.2",
27-
"@parcel/runtime-react-refresh": "^2.3.2",
28-
"@parcel/runtime-service-worker": "^2.3.2",
29-
"@parcel/transformer-js": "^2.3.2",
30-
"@parcel/transformer-json": "^2.3.2",
31-
"@parcel/transformer-raw": "^2.3.2",
32-
"@parcel/transformer-react-refresh-wrap": "^2.3.2"
17+
"@gatsbyjs/parcel-namer-relative-to-cwd": "1.0.0-next.0",
18+
"@parcel/bundler-default": "2.5.0",
19+
"@parcel/compressor-raw": "2.5.0",
20+
"@parcel/namer-default": "2.5.0",
21+
"@parcel/optimizer-terser": "2.5.0",
22+
"@parcel/packager-js": "2.5.0",
23+
"@parcel/packager-raw": "2.5.0",
24+
"@parcel/reporter-dev-server": "2.5.0",
25+
"@parcel/resolver-default": "2.5.0",
26+
"@parcel/runtime-browser-hmr": "2.5.0",
27+
"@parcel/runtime-js": "2.5.0",
28+
"@parcel/runtime-react-refresh": "2.5.0",
29+
"@parcel/runtime-service-worker": "2.5.0",
30+
"@parcel/transformer-js": "2.5.0",
31+
"@parcel/transformer-json": "2.5.0",
32+
"@parcel/transformer-raw": "2.5.0",
33+
"@parcel/transformer-react-refresh-wrap": "2.5.0"
3334
},
3435
"parcelDependencies": {
35-
"@gatsbyjs/parcel-namer-relative-to-cwd": "0.0.2",
36-
"@parcel/optimizer-data-url": "^2.3.1",
37-
"@parcel/packager-raw-url": "^2.3.1",
38-
"@parcel/packager-ts": "^2.3.1",
39-
"@parcel/packager-xml": "^2.3.1",
40-
"@parcel/transformer-graphql": "^2.3.1",
41-
"@parcel/transformer-inline-string": "^2.3.1",
42-
"@parcel/transformer-jsonld": "^2.3.1",
43-
"@parcel/transformer-mdx": "^2.3.1",
44-
"@parcel/transformer-toml": "^2.3.1",
45-
"@parcel/transformer-typescript-types": "^2.3.1",
46-
"@parcel/transformer-webmanifest": "^2.3.1",
47-
"@parcel/transformer-worklet": "^2.3.1",
48-
"@parcel/transformer-xml": "^2.3.1",
49-
"@parcel/transformer-yaml": "^2.3.1"
36+
"@gatsbyjs/parcel-namer-relative-to-cwd": "1.0.0-next.0",
37+
"@parcel/optimizer-data-url": "2.5.0",
38+
"@parcel/packager-raw-url": "2.5.0",
39+
"@parcel/packager-ts": "2.5.0",
40+
"@parcel/packager-xml": "2.5.0",
41+
"@parcel/transformer-graphql": "2.5.0",
42+
"@parcel/transformer-inline-string": "2.5.0",
43+
"@parcel/transformer-jsonld": "2.5.0",
44+
"@parcel/transformer-mdx": "2.5.0",
45+
"@parcel/transformer-toml": "2.5.0",
46+
"@parcel/transformer-typescript-types": "2.5.0",
47+
"@parcel/transformer-webmanifest": "2.5.0",
48+
"@parcel/transformer-worklet": "2.5.0",
49+
"@parcel/transformer-xml": "2.5.0",
50+
"@parcel/transformer-yaml": "2.5.0"
5051
},
5152
"peerDependencies": {
52-
"@parcel/core": "^2.3.1"
53+
"@parcel/core": "2.5.0"
5354
}
5455
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["babel-preset-gatsby-package"]]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `@gatsbyjs/parcel-namer-relative-to-cwd`
2+
3+
Parcel by default is trying to find common/shared directory between entries and output paths are impacted by it. See https://github.com/parcel-bundler/parcel/issues/5476#issuecomment-769058504.
4+
5+
With these inputs files:
6+
7+
```
8+
a.html
9+
sub/b.html
10+
```
11+
12+
You get:
13+
14+
- `parcel build a.html` => `dist/a.html`
15+
- `parcel build sub/b.html` => `dist/b.html`
16+
- `parcel build a.html sub/b.html` => `dist/a.html`, `dist/sub/b.html`
17+
18+
We can see that `sub/b.html` entry might result in either `dist/b.html` or `dist/sub/b.html` (depending wether `a.html` is entry or not). This makes builds not deterministic, which is very problematic where entries are "optional".
19+
20+
This namer plugin stabilize output, so inside `distDir` the hierarchy is the same as entry file in relation to current working directory (CWD):
21+
22+
- `parcel build a.html` => `dist/a.html`
23+
- `parcel build sub/b.html` => `dist/sub/b.html`
24+
- `parcel build a.html sub/b.html` => `dist/a.html`, `dist/sub/b.html`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@gatsbyjs/parcel-namer-relative-to-cwd",
3+
"main": "lib/index.js",
4+
"version": "1.0.0-next.0",
5+
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-parcel-namer-relative-to-cwd#readme",
6+
"description": "Parcel namer that preserve directory structure to stabilize output and keep the hierarchy.",
7+
"author": "Michal Piechowiak <[email protected]>",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/gatsbyjs/gatsby.git",
11+
"directory": "packages/gatsby-parcel-namer-relative-to-cwd"
12+
},
13+
"engines": {
14+
"node": ">=14.15.0",
15+
"parcel": "2.x"
16+
},
17+
"license": "MIT",
18+
"dependencies": {
19+
"@parcel/plugin": "2.5.0",
20+
"@babel/runtime": "^7.18.0",
21+
"gatsby-core-utils": "^3.15.0-next.1"
22+
},
23+
"devDependencies": {
24+
"@babel/cli": "^7.17.10",
25+
"@babel/core": "^7.18.0",
26+
"babel-preset-gatsby-package": "^2.15.0-next.0",
27+
"cross-env": "^7.0.3"
28+
},
29+
"peerDependencies": {
30+
"@parcel/namer-default": "2.5.0"
31+
},
32+
"scripts": {
33+
"build": "babel src --out-dir lib/ --ignore \"**/__tests__\" --extensions \".ts\"",
34+
"prepare": "cross-env NODE_ENV=production npm run build",
35+
"watch": "babel -w src --out-dir lib/ --ignore \"**/__tests__\" --extensions \".ts\""
36+
},
37+
"files": [
38+
"lib/index.js"
39+
]
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Namer } from "@parcel/plugin"
2+
import { FilePath, Namer as NamerOpts } from "@parcel/types"
3+
import defaultNamer from "@parcel/namer-default"
4+
import * as path from "path"
5+
import { slash } from "gatsby-core-utils"
6+
7+
const CONFIG = Symbol.for(`parcel-plugin-config`)
8+
const defaultNamerOpts = defaultNamer[CONFIG] as NamerOpts<unknown>
9+
10+
export default new Namer({
11+
async name(opts): Promise<FilePath | null | undefined> {
12+
// @parcel/namer-default will find "most common denominator" directory
13+
// depending on entries and make it a "relative root" for output.
14+
// This means that output is not deterministic based JUST on configuration
15+
// as adding/removing entries can change output directory structure.
16+
// To make it deterministic we add "middleware" namer which will use
17+
// @parcel/namer-default for filename, but (possibly) adjust directory
18+
// structure.
19+
20+
const relativePathFromDefaultNamer = await defaultNamerOpts.name(opts)
21+
if (relativePathFromDefaultNamer) {
22+
const mainEntry = opts.bundle.getMainEntry()
23+
if (!mainEntry) {
24+
return null
25+
}
26+
27+
// For now treating CWD as root. For current gatsby use case
28+
// this is enough, for various other projects it might need to be configurable
29+
// or just smarter (for example cover common dirs like `src` or `lib` etc)
30+
const root = slash(process.cwd())
31+
32+
const sourceRelativeToRoot = path.posix.relative(
33+
root,
34+
slash(path.dirname(mainEntry.filePath))
35+
)
36+
37+
// newPath will be output relative to "distDir".
38+
// we want to preserve directory structure in distDir that we have
39+
// between entries and root
40+
const newPath = path.posix.join(
41+
sourceRelativeToRoot,
42+
path.basename(relativePathFromDefaultNamer)
43+
)
44+
45+
return newPath
46+
}
47+
48+
return null
49+
},
50+
})

packages/gatsby/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@graphql-tools/code-file-loader": "^7.2.14",
3030
"@graphql-tools/load": "^7.5.10",
3131
"@nodelib/fs.walk": "^1.2.8",
32-
"@parcel/core": "^2.3.2",
32+
"@parcel/core": "2.5.0",
3333
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
3434
"@types/http-proxy": "^1.17.7",
3535
"@typescript-eslint/eslint-plugin": "^4.33.0",
@@ -93,7 +93,7 @@
9393
"gatsby-legacy-polyfills": "^2.15.0-next.1",
9494
"gatsby-link": "^4.15.0-next.1",
9595
"gatsby-page-utils": "^2.15.0-next.1",
96-
"gatsby-parcel-config": "^0.6.0-next.0",
96+
"gatsby-parcel-config": "0.6.0-next.0",
9797
"gatsby-plugin-page-creator": "^4.15.0-next.2",
9898
"gatsby-plugin-typescript": "^4.15.0-next.1",
9999
"gatsby-plugin-utils": "^3.9.0-next.2",

0 commit comments

Comments
 (0)