Skip to content

Commit

Permalink
Replaced post-processing "use client" build step with Parcel plug-in
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed May 27, 2023
1 parent b5d3d0e commit 32ec60f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@parcel/config-default",
"optimizers": {
"*.{js,mjs,cjs}": ["./parcel-optimizer-use-client.js"]
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"clear:builds": "rm -rf ./dist",
"clear:parcel-cache": "rm -rf ./.parcel-cache",
"clear:node_modules": "rm -rf ./node_modules",
"prerelease": "rm -rf ./.parcel-cache && parcel build && ./postprocess.js",
"prerelease": "rm -rf ./.parcel-cache && parcel build",
"prettier": "prettier --write \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
"prettier:ci": "prettier --check \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
"test": "jest",
Expand All @@ -33,9 +33,13 @@
"@babel/runtime": "^7.12.5"
},
"devDependencies": {
"@parcel/config-default": "^2.9.0",
"@parcel/core": "^2.9.0",
"@parcel/packager-ts": "^2.9.0",
"@parcel/plugin": "^2.9.0",
"@parcel/source-map": "^2.1.1",
"@parcel/transformer-typescript-types": "^2.9.0",
"@parcel/utils": "^2.9.0",
"@types/jest": "^26.0.15",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
17 changes: 17 additions & 0 deletions parcel-optimizer-use-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { Optimizer } = require("@parcel/plugin");
const { default: SourceMap } = require("@parcel/source-map/dist/node.js");
const { blobToBuffer } = require("@parcel/utils");

module.exports = new Optimizer({
async optimize({ contents, map, options }) {
let correctMap;
if (map != null) {
correctMap = new SourceMap(options.projectRoot);
correctMap.addSourceMap(map, 2); // Offset lines by 2
}
return {
contents: `"use client";\n\n` + (await blobToBuffer(contents)).toString(),
map: correctMap,
};
},
});
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions postprocess.js

This file was deleted.

0 comments on commit 32ec60f

Please sign in to comment.