Skip to content

Commit

Permalink
feat: Add gatsby-parcel-namer-relative-to-cwd to monorepo & update …
Browse files Browse the repository at this point in the history
…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 <bot@renovateapp.com>
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
3 people authored May 20, 2022
1 parent f7f8ffe commit 459fab4
Show file tree
Hide file tree
Showing 9 changed files with 557 additions and 446 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ commands:
name: Upgrade React to << parameters.react_version >>
command: "REACT_VERSION=<< parameters.react_version >> TEST_PATH=<< parameters.test_path >> node ./scripts/upgrade-react"
- run:
name: Install gatsby-dev
command: yarn global add gatsby-dev-cli
name: Install gatsby-dev@next
command: yarn global add gatsby-dev-cli@next
- run:
name: Run tests (using defaults)
command: ./scripts/e2e-test.sh "<< parameters.test_path >>" "<< parameters.test_command >>"
Expand Down
67 changes: 34 additions & 33 deletions packages/gatsby-parcel-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,47 @@
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "packages/gatsby-parcel-config"
},
"license": "MIT",
"engines": {
"parcel": "2.x"
},
"dependencies": {
"@gatsbyjs/parcel-namer-relative-to-cwd": "0.0.2",
"@parcel/bundler-default": "^2.3.2",
"@parcel/compressor-raw": "^2.3.2",
"@parcel/namer-default": "^2.3.2",
"@parcel/optimizer-terser": "^2.3.2",
"@parcel/packager-js": "^2.3.2",
"@parcel/packager-raw": "^2.3.2",
"@parcel/reporter-dev-server": "^2.3.2",
"@parcel/resolver-default": "^2.3.2",
"@parcel/runtime-browser-hmr": "^2.3.2",
"@parcel/runtime-js": "^2.3.2",
"@parcel/runtime-react-refresh": "^2.3.2",
"@parcel/runtime-service-worker": "^2.3.2",
"@parcel/transformer-js": "^2.3.2",
"@parcel/transformer-json": "^2.3.2",
"@parcel/transformer-raw": "^2.3.2",
"@parcel/transformer-react-refresh-wrap": "^2.3.2"
"@gatsbyjs/parcel-namer-relative-to-cwd": "1.0.0-next.0",
"@parcel/bundler-default": "2.5.0",
"@parcel/compressor-raw": "2.5.0",
"@parcel/namer-default": "2.5.0",
"@parcel/optimizer-terser": "2.5.0",
"@parcel/packager-js": "2.5.0",
"@parcel/packager-raw": "2.5.0",
"@parcel/reporter-dev-server": "2.5.0",
"@parcel/resolver-default": "2.5.0",
"@parcel/runtime-browser-hmr": "2.5.0",
"@parcel/runtime-js": "2.5.0",
"@parcel/runtime-react-refresh": "2.5.0",
"@parcel/runtime-service-worker": "2.5.0",
"@parcel/transformer-js": "2.5.0",
"@parcel/transformer-json": "2.5.0",
"@parcel/transformer-raw": "2.5.0",
"@parcel/transformer-react-refresh-wrap": "2.5.0"
},
"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",
"@parcel/packager-xml": "^2.3.1",
"@parcel/transformer-graphql": "^2.3.1",
"@parcel/transformer-inline-string": "^2.3.1",
"@parcel/transformer-jsonld": "^2.3.1",
"@parcel/transformer-mdx": "^2.3.1",
"@parcel/transformer-toml": "^2.3.1",
"@parcel/transformer-typescript-types": "^2.3.1",
"@parcel/transformer-webmanifest": "^2.3.1",
"@parcel/transformer-worklet": "^2.3.1",
"@parcel/transformer-xml": "^2.3.1",
"@parcel/transformer-yaml": "^2.3.1"
"@gatsbyjs/parcel-namer-relative-to-cwd": "1.0.0-next.0",
"@parcel/optimizer-data-url": "2.5.0",
"@parcel/packager-raw-url": "2.5.0",
"@parcel/packager-ts": "2.5.0",
"@parcel/packager-xml": "2.5.0",
"@parcel/transformer-graphql": "2.5.0",
"@parcel/transformer-inline-string": "2.5.0",
"@parcel/transformer-jsonld": "2.5.0",
"@parcel/transformer-mdx": "2.5.0",
"@parcel/transformer-toml": "2.5.0",
"@parcel/transformer-typescript-types": "2.5.0",
"@parcel/transformer-webmanifest": "2.5.0",
"@parcel/transformer-worklet": "2.5.0",
"@parcel/transformer-xml": "2.5.0",
"@parcel/transformer-yaml": "2.5.0"
},
"peerDependencies": {
"@parcel/core": "^2.3.1"
"@parcel/core": "2.5.0"
}
}
3 changes: 3 additions & 0 deletions packages/gatsby-parcel-namer-relative-to-cwd/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["babel-preset-gatsby-package"]]
}
1 change: 1 addition & 0 deletions packages/gatsby-parcel-namer-relative-to-cwd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
24 changes: 24 additions & 0 deletions packages/gatsby-parcel-namer-relative-to-cwd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# `@gatsbyjs/parcel-namer-relative-to-cwd`

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.

With these inputs files:

```
a.html
sub/b.html
```

You get:

- `parcel build a.html` => `dist/a.html`
- `parcel build sub/b.html` => `dist/b.html`
- `parcel build a.html sub/b.html` => `dist/a.html`, `dist/sub/b.html`

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".

This namer plugin stabilize output, so inside `distDir` the hierarchy is the same as entry file in relation to current working directory (CWD):

- `parcel build a.html` => `dist/a.html`
- `parcel build sub/b.html` => `dist/sub/b.html`
- `parcel build a.html sub/b.html` => `dist/a.html`, `dist/sub/b.html`
40 changes: 40 additions & 0 deletions packages/gatsby-parcel-namer-relative-to-cwd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@gatsbyjs/parcel-namer-relative-to-cwd",
"main": "lib/index.js",
"version": "1.0.0-next.0",
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-parcel-namer-relative-to-cwd#readme",
"description": "Parcel namer that preserve directory structure to stabilize output and keep the hierarchy.",
"author": "Michal Piechowiak <misiek.piechowiak@gmail.com>",
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "packages/gatsby-parcel-namer-relative-to-cwd"
},
"engines": {
"node": ">=14.15.0",
"parcel": "2.x"
},
"license": "MIT",
"dependencies": {
"@parcel/plugin": "2.5.0",
"@babel/runtime": "^7.18.0",
"gatsby-core-utils": "^3.15.0-next.1"
},
"devDependencies": {
"@babel/cli": "^7.17.10",
"@babel/core": "^7.18.0",
"babel-preset-gatsby-package": "^2.15.0-next.0",
"cross-env": "^7.0.3"
},
"peerDependencies": {
"@parcel/namer-default": "2.5.0"
},
"scripts": {
"build": "babel src --out-dir lib/ --ignore \"**/__tests__\" --extensions \".ts\"",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir lib/ --ignore \"**/__tests__\" --extensions \".ts\""
},
"files": [
"lib/index.js"
]
}
50 changes: 50 additions & 0 deletions packages/gatsby-parcel-namer-relative-to-cwd/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Namer } from "@parcel/plugin"
import { FilePath, Namer as NamerOpts } from "@parcel/types"
import defaultNamer from "@parcel/namer-default"
import * as path from "path"
import { slash } from "gatsby-core-utils"

const CONFIG = Symbol.for(`parcel-plugin-config`)
const defaultNamerOpts = defaultNamer[CONFIG] as NamerOpts<unknown>

export default new Namer({
async name(opts): Promise<FilePath | null | undefined> {
// @parcel/namer-default will find "most common denominator" directory
// depending on entries and make it a "relative root" for output.
// This means that output is not deterministic based JUST on configuration
// as adding/removing entries can change output directory structure.
// To make it deterministic we add "middleware" namer which will use
// @parcel/namer-default for filename, but (possibly) adjust directory
// structure.

const relativePathFromDefaultNamer = await defaultNamerOpts.name(opts)
if (relativePathFromDefaultNamer) {
const mainEntry = opts.bundle.getMainEntry()
if (!mainEntry) {
return null
}

// For now treating CWD as root. For current gatsby use case
// this is enough, for various other projects it might need to be configurable
// or just smarter (for example cover common dirs like `src` or `lib` etc)
const root = slash(process.cwd())

const sourceRelativeToRoot = path.posix.relative(
root,
slash(path.dirname(mainEntry.filePath))
)

// newPath will be output relative to "distDir".
// we want to preserve directory structure in distDir that we have
// between entries and root
const newPath = path.posix.join(
sourceRelativeToRoot,
path.basename(relativePathFromDefaultNamer)
)

return newPath
}

return null
},
})
4 changes: 2 additions & 2 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@graphql-tools/code-file-loader": "^7.2.14",
"@graphql-tools/load": "^7.5.10",
"@nodelib/fs.walk": "^1.2.8",
"@parcel/core": "^2.3.2",
"@parcel/core": "2.5.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
"@types/http-proxy": "^1.17.7",
"@typescript-eslint/eslint-plugin": "^4.33.0",
Expand Down Expand Up @@ -93,7 +93,7 @@
"gatsby-legacy-polyfills": "^2.15.0-next.1",
"gatsby-link": "^4.15.0-next.1",
"gatsby-page-utils": "^2.15.0-next.1",
"gatsby-parcel-config": "^0.6.0-next.0",
"gatsby-parcel-config": "0.6.0-next.0",
"gatsby-plugin-page-creator": "^4.15.0-next.2",
"gatsby-plugin-typescript": "^4.15.0-next.1",
"gatsby-plugin-utils": "^3.9.0-next.2",
Expand Down
Loading

0 comments on commit 459fab4

Please sign in to comment.