Skip to content

Commit 47cd25d

Browse files
authored
feat: Add Parcel plugin (#4)
1 parent 424d472 commit 47cd25d

File tree

11 files changed

+1552
-10
lines changed

11 files changed

+1552
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
dist/
33
.DS_Store
4+
.parcel-cache/

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,16 @@ export default {
107107
sourcemap: true,
108108
},
109109
};
110+
```
111+
112+
## Parcel
113+
114+
`.parcelrc`
115+
```json
116+
{
117+
"extends": "@parcel/config-default",
118+
"optimizers": {
119+
"*.{js,cjs,mjs}": ["...", "@sentry/debug-ids/parcel"]
120+
}
121+
}
110122
```

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
"rolldown": {
2727
"require": "./dist/cjs/rolldown.js",
2828
"import": "./dist/esm/rolldown.mjs"
29+
},
30+
"parcel": {
31+
"require": "./dist/cjs/parcel.js",
32+
"import": "./dist/esm/parcel.js"
2933
}
3034
},
3135
"scripts": {
@@ -42,6 +46,7 @@
4246
"@types/node": "14",
4347
"esbuild": "^0.24.0",
4448
"prettier": "^3.3.3",
49+
"parcel": "^2.12.0",
4550
"rolldown": "^0.13.2",
4651
"rollup": "^4.22.5",
4752
"typescript": "^5.6.2",

rollup.config.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ function transpileNode(format, input, outDir) {
2929
};
3030
}
3131

32-
const inputs = ['src/esbuild.ts', 'src/rolldown.ts', 'src/rollup.ts', 'src/rspack.ts', 'src/vite.ts', 'src/webpack.ts'];
32+
const inputs = [
33+
'src/esbuild.ts',
34+
'src/parcel.ts',
35+
'src/rolldown.ts',
36+
'src/rollup.ts',
37+
'src/rspack.ts',
38+
'src/vite.ts',
39+
'src/webpack.ts',
40+
];
3341

3442
export default [transpileNode('cjs', inputs, 'dist/cjs'), transpileNode('esm', inputs, 'dist/esm')];

src/parcel.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Optimizer } from '@parcel/plugin';
2+
import { stringToUUID, addDebugIdToSource } from './common';
3+
import type { SourceMap } from 'rollup';
4+
5+
export default new Optimizer({
6+
async optimize({ contents, map }) {
7+
// We only add debugId where there is a linked sourcemap file
8+
if (!map) {
9+
return { contents, map };
10+
}
11+
12+
const contentsString = contents.toString();
13+
const debugId = stringToUUID(contentsString);
14+
15+
// This is nasty but Parcel uses a native module to handle sourcemaps, so
16+
// the only way I could find to add the debugId to the sourcemap is to proxy
17+
// the map 'toVLQ()' method to add the debugId to the sourcemap object...
18+
const proxiedMap = new Proxy(map, {
19+
get: function (target, prop, receiver) {
20+
if (prop === 'toVLQ') {
21+
const original = Reflect.get(target, prop, receiver);
22+
23+
return function toVLQ(this: SourceMap) {
24+
const result = original.apply(this);
25+
result.debugId = debugId;
26+
return result;
27+
};
28+
}
29+
30+
return Reflect.get(target, prop, receiver);
31+
},
32+
});
33+
34+
return {
35+
contents: addDebugIdToSource(contentsString, debugId),
36+
map: proxiedMap,
37+
};
38+
},
39+
});

test/parcel/parcel.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, test } from 'vitest';
2+
import { execFileSync } from 'child_process';
3+
import { join } from 'path';
4+
import { TestOptions, cleanDir, testResults } from '../utils';
5+
6+
const __dirname = new URL('.', import.meta.url).pathname;
7+
8+
function parcelTest(path: string, options: TestOptions) {
9+
const baseDir = join(__dirname, path);
10+
cleanDir(baseDir, 'dist');
11+
execFileSync(
12+
'parcel',
13+
[
14+
'build',
15+
'--no-cache',
16+
'--dist-dir',
17+
'./dist',
18+
'--config',
19+
'./.parcelrc',
20+
'--cache-dir',
21+
'./.parcel-cache',
22+
'./src/index.html',
23+
],
24+
{
25+
cwd: baseDir,
26+
stdio: 'inherit',
27+
},
28+
);
29+
30+
testResults(baseDir, options);
31+
}
32+
33+
describe('parcel', () => {
34+
test('with sourcemaps', () => {
35+
parcelTest('with-sourcemaps', {
36+
'index.9ed5572c.js': { hasDebugIds: true, hasSourceMapUrl: true },
37+
'another.3bf0f9d4.js': { hasDebugIds: true, hasSourceMapUrl: true },
38+
});
39+
});
40+
});

test/parcel/with-sourcemaps/.parcelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@parcel/config-default",
3+
"optimizers": {
4+
"*.{js,cjs,mjs}": [
5+
"...",
6+
"../../../dist/cjs/parcel.js"
7+
]
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('anther');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Parcel App</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="./main.js"></script>
11+
</body>
12+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('do nothing');
2+
await import('./another.js');

0 commit comments

Comments
 (0)