Skip to content

Commit 424d472

Browse files
authored
feat: Build with rollup and lint (#3)
1 parent b470331 commit 424d472

File tree

93 files changed

+523
-729
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+523
-729
lines changed

.github/workflows/CI.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: 'Build & Test'
2-
on: [push, pull_request]
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
37

48
jobs:
59
build:
@@ -12,6 +16,8 @@ jobs:
1216
node-version-file: 'package.json'
1317
- name: Install
1418
run: yarn install
19+
- name: Lint
20+
run: yarn lint
1521
- name: Build
1622
run: yarn build
1723
- name: Run Unit Tests

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"printWidth": 120,
3+
"proseWrap": "always",
4+
"singleQuote": true,
5+
"trailingComma": "all"
6+
}

package.json

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,44 @@
44
"license": "MIT",
55
"exports": {
66
"rollup": {
7-
"require": "./dist/rollup.js",
8-
"import": "./dist/rollup.mjs"
7+
"require": "./dist/cjs/rollup.js",
8+
"import": "./dist/esm/rollup.mjs"
99
},
1010
"webpack": {
11-
"require": "./dist/webpack.js",
12-
"import": "./dist/webpack.mjs"
11+
"require": "./dist/cjs/webpack.js",
12+
"import": "./dist/esm/webpack.mjs"
1313
},
1414
"esbuild": {
15-
"require": "./dist/esbuild.js",
16-
"import": "./dist/esbuild.mjs"
15+
"require": "./dist/cjs/esbuild.js",
16+
"import": "./dist/esm/esbuild.mjs"
1717
},
1818
"vite": {
19-
"require": "./dist/vite.js",
20-
"import": "./dist/vite.mjs"
19+
"require": "./dist/cjs/vite.js",
20+
"import": "./dist/esm/vite.mjs"
2121
},
2222
"rspack": {
23-
"require": "./dist/rspack.js",
24-
"import": "./dist/rspack.mjs"
23+
"require": "./dist/cjs/rspack.js",
24+
"import": "./dist/esm/rspack.mjs"
2525
},
2626
"rolldown": {
27-
"require": "./dist/rolldown.js",
28-
"import": "./dist/rolldown.mjs"
27+
"require": "./dist/cjs/rolldown.js",
28+
"import": "./dist/esm/rolldown.mjs"
2929
}
3030
},
3131
"scripts": {
32-
"build": "pkgroll --clean-dist --target=es2018 --target=node14 --sourcemap",
33-
"test": "vitest run ./test/**/*.test.ts"
32+
"build": "rollup --config rollup.config.mjs",
33+
"test": "vitest run ./test/**/*.test.ts",
34+
"lint": "prettier --check \"{src,test}/**/*.{ts,js}\"",
35+
"fix": "prettier --write \"{src,test}/**/*.{ts,js}\""
3436
},
3537
"devDependencies": {
38+
"@rollup/plugin-typescript": "^12.1.0",
3639
"@rspack/cli": "^1.0.8",
3740
"@rspack/core": "^1.0.8",
41+
"@sentry-internal/typescript": "^8.32.0",
3842
"@types/node": "14",
3943
"esbuild": "^0.24.0",
40-
"pkgroll": "^2.5.0",
44+
"prettier": "^3.3.3",
4145
"rolldown": "^0.13.2",
4246
"rollup": "^4.22.5",
4347
"typescript": "^5.6.2",

rollup.config.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { builtinModules } from 'node:module';
2+
import { resolve } from 'node:path';
3+
import { readFileSync } from 'node:fs';
4+
import typescript from '@rollup/plugin-typescript';
5+
6+
const pkgJson = JSON.parse(readFileSync(resolve(process.cwd(), 'package.json')));
7+
const external = [...builtinModules, ...Object.keys(pkgJson.dependencies || {})];
8+
9+
// a simple plugin that adds a package.json file with type: module
10+
const modulePackageJson = {
11+
name: 'package-json-module-type',
12+
generateBundle() {
13+
this.emitFile({ type: 'asset', fileName: 'package.json', source: '{"type": "module"}' });
14+
},
15+
};
16+
17+
function transpileNode(format, input, outDir) {
18+
return {
19+
input,
20+
output: {
21+
sourcemap: true,
22+
strict: false,
23+
format,
24+
dir: outDir,
25+
preserveModules: true,
26+
},
27+
plugins: [typescript({ outDir, tsconfig: './tsconfig.json' }), format === 'esm' ? modulePackageJson : {}],
28+
external,
29+
};
30+
}
31+
32+
const inputs = ['src/esbuild.ts', 'src/rolldown.ts', 'src/rollup.ts', 'src/rspack.ts', 'src/vite.ts', 'src/webpack.ts'];
33+
34+
export default [transpileNode('cjs', inputs, 'dist/cjs'), transpileNode('esm', inputs, 'dist/esm')];

src/common.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
import * as crypto from "crypto";
1+
import * as crypto from 'crypto';
22

3-
export const DEFAULT_EXTENSIONS = [".js", ".mjs", ".cjs"];
3+
export const DEFAULT_EXTENSIONS = ['.js', '.mjs', '.cjs'];
44

55
export function stringToUUID(str: string): string {
6-
const md5sum = crypto.createHash("md5");
6+
const md5sum = crypto.createHash('md5');
77
md5sum.update(str);
8-
const md5Hash = md5sum.digest("hex");
8+
const md5Hash = md5sum.digest('hex');
99

1010
// Position 16 is fixed to either 8, 9, a, or b in the uuid v4 spec (10xx in binary)
1111
// RFC 4122 section 4.4
12-
const v4variant = ["8", "9", "a", "b"][
13-
md5Hash.substring(16, 17).charCodeAt(0) % 4
14-
] as string;
12+
const v4variant = ['8', '9', 'a', 'b'][md5Hash.substring(16, 17).charCodeAt(0) % 4] as string;
1513

1614
return (
1715
md5Hash.substring(0, 8) +
18-
"-" +
16+
'-' +
1917
md5Hash.substring(8, 12) +
20-
"-4" +
18+
'-4' +
2119
md5Hash.substring(13, 16) +
22-
"-" +
20+
'-' +
2321
v4variant +
2422
md5Hash.substring(17, 20) +
25-
"-" +
23+
'-' +
2624
md5Hash.substring(20)
2725
).toLowerCase();
2826
}
2927

3028
export function addDebugIdToSource(input: string, debugId: string): string {
31-
return input.replace(
32-
/\s*(?:\/\/# debugId=.+)?\s*(\/\/# sourceMappingURL=.+)?\s*$/,
33-
`\n//# debugId=${debugId}\n$1`
34-
);
29+
return input.replace(/\s*(?:\/\/# debugId=.+)?\s*(\/\/# sourceMappingURL=.+)?\s*$/, `\n//# debugId=${debugId}\n$1`);
3530
}
3631

3732
export function addDebugIdToSourcemap(input: string, debugId: string): string {

src/esbuild.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
import type { PluginBuild } from "esbuild";
2-
import {
3-
addDebugIdToSource,
4-
DEFAULT_EXTENSIONS,
5-
stringToUUID,
6-
addDebugIdToSourcemap,
7-
} from "./common";
8-
import { readFile, writeFile } from "fs/promises";
9-
import * as path from "path";
1+
import type { PluginBuild } from 'esbuild';
2+
import { addDebugIdToSource, DEFAULT_EXTENSIONS, stringToUUID, addDebugIdToSourcemap } from './common';
3+
import { readFile, writeFile } from 'fs/promises';
4+
import * as path from 'path';
105

116
export default {
12-
name: "esbuild-plugin-debug-ids",
7+
name: 'esbuild-plugin-debug-ids',
138
setup({ onEnd, initialOptions }: PluginBuild) {
14-
if (
15-
initialOptions.bundle !== true ||
16-
initialOptions.sourcemap === undefined
17-
) {
9+
if (initialOptions.bundle !== true || initialOptions.sourcemap === undefined) {
1810
return;
1911
}
2012

2113
initialOptions.metafile = true;
2214

2315
onEnd(async (result) => {
2416
const assets = Object.keys(result.metafile?.outputs || {});
25-
const assetsWithCorrectExt = assets.filter((file) =>
26-
DEFAULT_EXTENSIONS.some((ext) => file.endsWith(ext))
27-
);
17+
const assetsWithCorrectExt = assets.filter((file) => DEFAULT_EXTENSIONS.some((ext) => file.endsWith(ext)));
2818

2919
if (assetsWithCorrectExt.length === 0) {
3020
return;
@@ -39,13 +29,13 @@ export default {
3929
}
4030

4131
const sourcePath = path.join(process.cwd(), key);
42-
const source = await readFile(sourcePath, { encoding: "utf-8" });
32+
const source = await readFile(sourcePath, { encoding: 'utf-8' });
4333
const debugId = stringToUUID(source);
4434
const updatedSource = addDebugIdToSource(source, debugId);
4535
await writeFile(sourcePath, updatedSource);
4636

4737
const sourcemapPath = path.join(process.cwd(), sourcemapKey);
48-
const sourcemap = await readFile(sourcemapPath, { encoding: "utf-8" });
38+
const sourcemap = await readFile(sourcemapPath, { encoding: 'utf-8' });
4939
const updatedSourcemap = addDebugIdToSourcemap(sourcemap, debugId);
5040
await writeFile(sourcemapPath, updatedSourcemap);
5141
});

src/rolldown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import rollupPlugin from "./rollup";
1+
import rollupPlugin from './rollup';
22

33
export default rollupPlugin;

src/rollup.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import { OutputAsset, OutputChunk, Plugin } from "rollup";
2-
import {
3-
addDebugIdToSourcemap,
4-
addDebugIdToSource,
5-
stringToUUID,
6-
DEFAULT_EXTENSIONS,
7-
} from "./common";
1+
import { OutputAsset, OutputChunk, Plugin } from 'rollup';
2+
import { addDebugIdToSourcemap, addDebugIdToSource, stringToUUID, DEFAULT_EXTENSIONS } from './common';
3+
import { TextDecoder } from 'util';
4+
5+
function getString(input: string | Uint8Array): string {
6+
if (typeof input === 'string') {
7+
return input;
8+
}
9+
10+
return new TextDecoder().decode(input);
11+
}
812

913
export default function debugIds(): Plugin {
1014
return {
11-
name: "rollup-plugin-debug-ids",
12-
generateBundle: function (
13-
_,
14-
bundle: { [fileName: string]: OutputAsset | OutputChunk }
15-
) {
15+
name: 'rollup-plugin-debug-ids',
16+
generateBundle: function (_, bundle: { [fileName: string]: OutputAsset | OutputChunk }) {
1617
for (const [key, value] of Object.entries(bundle)) {
1718
// We only add debugId where there is a linked sourcemap file
18-
if (!("sourcemapFileName" in value) || !value.sourcemapFileName) {
19+
if (!('sourcemapFileName' in value) || !value.sourcemapFileName) {
1920
continue;
2021
}
2122

@@ -26,32 +27,26 @@ export default function debugIds(): Plugin {
2627

2728
// Check we have a sourcemap in the output and it has source property
2829
const sourceMapFile = bundle[value.sourcemapFileName];
29-
if (!sourceMapFile || !("source" in sourceMapFile)) {
30+
if (!sourceMapFile || !('source' in sourceMapFile)) {
3031
continue;
3132
}
3233

3334
const debugId = stringToUUID(value.code);
3435
value.code = addDebugIdToSource(value.code, debugId);
35-
sourceMapFile.source = addDebugIdToSourcemap(
36-
sourceMapFile.source.toString(),
37-
debugId
38-
);
36+
sourceMapFile.source = addDebugIdToSourcemap(sourceMapFile.source.toString(), debugId);
3937

40-
// vite has plugins that run after us which can modify the sourcemap so we
38+
// vite has a plugin that runs after us which can modify the sourcemap so we
4139
// proxy the sourceMapFile to re-add the debugId if the source gets set again
42-
bundle[value.sourcemapFileName] = new Proxy(
43-
bundle[value.sourcemapFileName],
44-
{
45-
set: function (target, prop, value) {
46-
if (prop === "source") {
47-
target[prop] = addDebugIdToSourcemap(value, debugId);
48-
} else {
49-
target[prop] = value;
50-
}
51-
return true;
52-
},
53-
}
54-
);
40+
bundle[value.sourcemapFileName] = new Proxy(bundle[value.sourcemapFileName] as OutputAsset, {
41+
set: function <K extends keyof OutputAsset>(target: OutputAsset, prop: K, value: OutputAsset[K]) {
42+
if (prop === 'source') {
43+
(target as any)[prop] = addDebugIdToSourcemap(getString(value as string | Uint8Array), debugId);
44+
} else {
45+
target[prop] = value;
46+
}
47+
return true;
48+
},
49+
}) as OutputAsset;
5550
}
5651
},
5752
};

src/rspack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { DebugIdWebpackPlugin } from "./webpack";
1+
import { DebugIdWebpackPlugin } from './webpack';
22

33
export class DebugIdRspackPlugin extends DebugIdWebpackPlugin {}

src/vite.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import rollupPlugin from "./rollup";
2-
import { Plugin } from "vite";
1+
import rollupPlugin from './rollup';
2+
import { Plugin } from 'vite';
33

44
export default function debugIds(): Plugin {
55
return {
66
...rollupPlugin(),
7-
name: "vite-plugin-debug-ids",
8-
apply: "build",
9-
enforce: "post",
7+
name: 'vite-plugin-debug-ids',
8+
apply: 'build',
9+
enforce: 'post',
1010
};
1111
}

0 commit comments

Comments
 (0)