Skip to content

Commit db04eb5

Browse files
authored
feat(core): Add ignoreFile option (#80)
Implement the `ignoreFile` option and rewrite the `ignore` option behaviour. Previously, we used `glob`'s `ignore` option to pass an array of ignore rules to the library. This meant we'd get back a set of filtered files where ignore rules were already applied. However, `glob` does not support negated ignore rules, meaning ignore patterns like "ignore everything except file x" wouldn't work. With this change, we introduce a new library to the project: [ignore](https://www.npmjs.com/package/ignore). It lets us add rules to a rule checker (regardless of if these rules come from a file or an array). We now first collect all files with `glob` and subsequently filter out the ones that are ignored by the specified ignore rules. This rule checker supports all rules as specified in `.gitignore` specs. Therefore, it also supports negated ignore rules.
1 parent 17b4f9a commit db04eb5

File tree

4 files changed

+77
-37
lines changed

4 files changed

+77
-37
lines changed

packages/bundler-plugin-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"axios": "^0.27.2",
4040
"form-data": "^4.0.0",
4141
"glob": "8.0.3",
42+
"ignore": "^5.2.0",
4243
"magic-string": "0.26.2",
4344
"unplugin": "0.10.1"
4445
},

packages/bundler-plugin-core/src/sentry/sourcemaps.ts

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,55 @@ import fs, { Stats } from "fs";
33
import glob from "glob";
44
import { InternalIncludeEntry } from "../options-mapping";
55

6+
import ignore, { Ignore } from "ignore";
7+
68
export type FileRecord = {
79
name: string;
810
content: string;
911
};
1012

13+
type FileNameRecord = {
14+
absolutePath: string;
15+
// Contains relative paths without leading `.` (e.g. "foo/bar.js" or "asdf\\a\\b.js.map")
16+
relativePath: string;
17+
// Holds the path of the file relative to the CWD (used for ignore matching)
18+
relativeCwdPath: string;
19+
};
20+
1121
export function getFiles(includePath: string, includeEntry: InternalIncludeEntry): FileRecord[] {
22+
// Start with getting all (unfiltered) files for the given includePath.
23+
const files = collectAllFiles(includePath);
24+
if (!files.length) {
25+
return [];
26+
}
27+
28+
const ignore = getIgnoreRules(includeEntry);
29+
const filteredFiles = files
30+
.filter(({ relativeCwdPath }) => !ignore.ignores(relativeCwdPath))
31+
.filter(({ absolutePath }) => includeEntry.ext.includes(path.extname(absolutePath)));
32+
33+
// TODO do sourcemap rewriting?
34+
// TODO do sourcefile rewriting? (adding source map reference to bottom - search for "guess_sourcemap_reference")
35+
return filteredFiles.map(({ absolutePath, relativePath }) => {
36+
const content = fs.readFileSync(absolutePath, { encoding: "utf-8" });
37+
return {
38+
name:
39+
(includeEntry.urlPrefix ?? "~/") +
40+
convertWindowsPathToPosix(relativePath) +
41+
(includeEntry.urlSuffix ?? ""),
42+
content,
43+
};
44+
});
45+
}
46+
47+
/**
48+
* Collects all (unfiltered) files from @param includePath
49+
* @param includePath
50+
* @returns an array of files
51+
*/
52+
function collectAllFiles(includePath: string): FileNameRecord[] {
1253
let fileStat: Stats;
54+
1355
const absolutePath = path.isAbsolute(includePath)
1456
? includePath
1557
: path.resolve(process.cwd(), includePath);
@@ -19,52 +61,42 @@ export function getFiles(includePath: string, includeEntry: InternalIncludeEntry
1961
return [];
2062
}
2163

22-
let files: {
23-
absolutePath: string;
24-
// Contains relative paths without leading `.` (e.g. "foo/bar.js" or "asdf\\a\\b.js.map")
25-
relativePath: string;
26-
}[];
27-
2864
if (fileStat.isFile()) {
29-
files = [{ absolutePath, relativePath: path.basename(absolutePath) }];
65+
return [
66+
{
67+
absolutePath,
68+
relativePath: path.basename(absolutePath),
69+
relativeCwdPath: path.relative(process.cwd(), absolutePath),
70+
},
71+
];
3072
} else if (fileStat.isDirectory()) {
31-
const absoluteIgnores = includeEntry.ignore.map((ignoreEntry) =>
32-
path.join(process.cwd(), ignoreEntry)
33-
);
34-
35-
files = glob
73+
return glob
3674
.sync(path.join(absolutePath, "**"), {
3775
nodir: true,
3876
absolute: true,
39-
ignore: absoluteIgnores,
4077
})
4178
.map((globPath) => ({
4279
absolutePath: globPath,
4380
relativePath: globPath.slice(absolutePath.length + 1),
81+
relativeCwdPath: path.relative(process.cwd(), globPath),
4482
}));
4583
} else {
4684
return [];
4785
}
86+
}
4887

49-
const filteredFiles = files.filter(({ absolutePath }) => {
50-
return includeEntry.ext.includes(path.extname(absolutePath));
51-
});
52-
53-
// TODO ignore files
54-
// TODO ignorefile
55-
// TODO do sourcemap rewriting?
56-
// TODO do sourcefile rewriting? (adding source map reference to bottom - search for "guess_sourcemap_reference")
57-
58-
return filteredFiles.map(({ absolutePath, relativePath }) => {
59-
const content = fs.readFileSync(absolutePath, { encoding: "utf-8" });
60-
return {
61-
name:
62-
(includeEntry.urlPrefix ?? "~/") +
63-
convertWindowsPathToPosix(relativePath) +
64-
(includeEntry.urlSuffix ?? ""),
65-
content,
66-
};
67-
});
88+
/**
89+
* Adds rules specified in `ignore` and `ignoreFile` to the ignore rule
90+
* checker and returns the checker for further use.
91+
*/
92+
function getIgnoreRules(includeEntry: InternalIncludeEntry): Ignore {
93+
const ignoreChecker = ignore();
94+
if (includeEntry.ignoreFile) {
95+
const ignoreFileContent = fs.readFileSync(includeEntry.ignoreFile).toString();
96+
ignoreChecker.add(ignoreFileContent);
97+
}
98+
ignoreChecker.add(includeEntry.ignore);
99+
return ignoreChecker;
68100
}
69101

70102
function convertWindowsPathToPosix(windowsPath: string): string {

packages/playground/.sentryignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#out/vite-smallNodeApp/\*\*
2+
3+
# some comment
4+
5+
!out/vite-smallNodeApp/index.js

packages/playground/vite.config.smallNodeApp.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ export default defineConfig({
1717
},
1818
plugins: [
1919
sentryVitePlugin({
20-
url: process.env.SENTRY_URL,
21-
authToken: process.env.SENTRY_AUTH_TOKEN,
22-
org: process.env.SENTRY_ORG,
23-
project: process.env.SENTRY_PROJECT,
20+
authToken: process.env.SENTRY_AUTH_TOKEN || "",
21+
org: process.env.SENTRY_ORG || "",
22+
project: process.env.SENTRY_PROJECT || "",
2423
debug: true,
25-
release: "0.0.11",
24+
release: "0.0.14",
2625
include: "out/vite-smallNodeApp",
2726
cleanArtifacts: true,
27+
// ignore: ["out/*", "!out/vite-smallNodeApp/index.js.map"],
28+
ignore: ["!out/vite-smallNodeApp/index.js.map"],
29+
ignoreFile: ".sentryignore",
2830
}),
2931
],
3032
});

0 commit comments

Comments
 (0)