Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add gatsby-parcel-namer-relative-to-cwd to monorepo & update Parcel to 2.5.0 #35446

Merged
merged 17 commits into from
May 20, 2022
Merged
39 changes: 20 additions & 19 deletions packages/gatsby-parcel-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,31 @@
"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.4.1",
"@parcel/compressor-raw": "^2.4.1",
"@parcel/namer-default": "^2.4.1",
"@parcel/optimizer-terser": "^2.4.1",
"@parcel/packager-js": "^2.4.1",
"@parcel/packager-raw": "^2.4.1",
"@parcel/reporter-dev-server": "^2.4.1",
"@parcel/resolver-default": "^2.4.1",
"@parcel/runtime-browser-hmr": "^2.4.1",
"@parcel/runtime-js": "^2.4.1",
"@parcel/runtime-react-refresh": "^2.4.1",
"@parcel/runtime-service-worker": "^2.4.1",
"@parcel/transformer-js": "^2.4.1",
"@parcel/transformer-json": "^2.4.1",
"@parcel/transformer-raw": "^2.4.1",
"@parcel/transformer-react-refresh-wrap": "^2.4.1"
},
"parcelDependencies": {
"@gatsbyjs/parcel-namer-relative-to-cwd": "0.0.2",
"@gatsbyjs/parcel-namer-relative-to-cwd": "^1.0.0-next.0",
"@parcel/optimizer-data-url": "^2.3.1",
"@parcel/packager-raw-url": "^2.3.1",
"@parcel/packager-ts": "^2.3.1",
Expand All @@ -49,6 +50,6 @@
"@parcel/transformer-yaml": "^2.3.1"
},
"peerDependencies": {
"@parcel/core": "^2.3.1"
"@parcel/core": "^2.4.1"
}
}
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
1 change: 1 addition & 0 deletions packages/gatsby-parcel-namer-relative-to-cwd/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
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`
39 changes: 39 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,39 @@
{
"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.4.1",
"@babel/runtime": "^7.15.4"
},
"devDependencies": {
"@babel/cli": "^7.15.4",
"@babel/core": "^7.15.5",
"babel-preset-gatsby-package": "^2.13.0-next.0",
"cross-env": "^7.0.3"
},
"peerDependencies": {
"@parcel/namer-default": "^2.4.1"
},
"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/*.js"
]
}
49 changes: 49 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,49 @@
import { Namer } from "@parcel/plugin"
import { FilePath, Namer as NamerOpts } from "@parcel/types"
import defaultNamer from "@parcel/namer-default"
import * as path from "path"

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 = process.cwd()

const sourceRelativeToRoot = path.posix.relative(
root,
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
},
})
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@gatsbyjs/reach-router": "^1.3.6",
"@gatsbyjs/webpack-hot-middleware": "^2.25.2",
"@nodelib/fs.walk": "^1.2.8",
"@parcel/core": "^2.3.2",
"@parcel/core": "^2.4.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
"@types/http-proxy": "^1.17.7",
"@typescript-eslint/eslint-plugin": "^4.33.0",
Expand Down