Skip to content

Commit

Permalink
feat: added new source-maps plugin for loading files with existing so…
Browse files Browse the repository at this point in the history
…urce maps.
  • Loading branch information
prisis committed Nov 17, 2024
1 parent eb33c4a commit a3407d6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/packem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"@visulima/package": "3.1.5",
"@visulima/pail": "2.1.11",
"@visulima/path": "1.1.1",
"@visulima/source-map": "1.0.10",
"@visulima/tsconfig": "1.1.1",
"browserslist": "4.24.2",
"defu": "6.1.4",
Expand Down
1 change: 1 addition & 0 deletions packages/packem/src/packem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ const generateOptions = (
exclude: EXCLUDE_REGEXP,
include: ALLOWED_TRANSFORM_EXTENSIONS_REGEX,
},
sourcemap: {},
sucrase: {
disableESTransforms: true,
enableLegacyBabel5ModuleInterop: false,
Expand Down
5 changes: 5 additions & 0 deletions packages/packem/src/rollup/get-rollup-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import createSplitChunks from "./utils/chunks/create-split-chunks";
import getChunkFilename from "./utils/get-chunk-filename";
import getEntryFileNames from "./utils/get-entry-file-names";
import resolveAliases from "./utils/resolve-aliases";
import { sourcemapsPlugin } from "./plugins/source-maps";

const sortUserPlugins = (plugins: RollupPlugins | undefined, type: "build" | "dts"): [Plugin[], Plugin[], Plugin[]] => {
const prePlugins: Plugin[] = [];
Expand Down Expand Up @@ -453,6 +454,8 @@ export const getRollupOptions = async (context: BuildContext, fileCache: FileCac
].filter(Boolean),

plugins: [
context.options.sourcemap && sourcemapsPlugin(context.options.rollup.sourcemap),

cachingPlugin(resolveFileUrlPlugin(), fileCache),
cachingPlugin(resolveTypescriptMjsCtsPlugin(), fileCache),

Expand Down Expand Up @@ -741,6 +744,8 @@ export const getRollupDtsOptions = async (context: BuildContext, fileCache: File
].filter(Boolean),

plugins: [
context.options.sourcemap && sourcemapsPlugin(context.options.rollup.sourcemap),

cachingPlugin(resolveFileUrlPlugin(), fileCache),
cachingPlugin(resolveTypescriptMjsCtsPlugin(), fileCache),

Expand Down
67 changes: 67 additions & 0 deletions packages/packem/src/rollup/plugins/source-maps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { FilterPattern } from "@rollup/pluginutils";
import { createFilter } from "@rollup/pluginutils";
import { readFile } from "@visulima/fs";
import { loadSourceMap } from "@visulima/source-map";
import type { ExistingRawSourceMap, Plugin, PluginContext } from "rollup";

// eslint-disable-next-line import/no-unused-modules
export interface SourcemapsPluginOptions {
exclude?: FilterPattern;
include?: FilterPattern;
}

export const sourcemapsPlugin = ({ exclude, include }: SourcemapsPluginOptions = {}): Plugin => {
// Create a filter function based on the include and exclude options
const filter = createFilter(include, exclude);

return {
async load(this: PluginContext, id: string) {
if (!filter(id)) {
return null;
}

let code: string;

try {
code = await readFile(id, { buffer: false });

this.addWatchFile(id);
} catch {
try {
// If reading fails, try again without a query suffix that some plugins use
const cleanId = id.replace(/\?.*$/, "");

code = await readFile(cleanId, { buffer: false });

this.addWatchFile(cleanId);
} catch {
this.warn("Failed reading file");

return null;
}
}

let map: ExistingRawSourceMap;

try {
// Try to resolve the source map for the code
const result = loadSourceMap(id);

// If the code contained no sourceMappingURL, return the code
if (result === undefined) {
return code;
}

map = result as unknown as ExistingRawSourceMap;
} catch {
this.warn("Failed resolving source map");

return { code };
}

// Return the code and the map
return { code, map };
},
name: "packem:sourcemaps",
};
};
2 changes: 2 additions & 0 deletions packages/packem/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type { SwcPluginConfig } from "./rollup/plugins/swc/types";
import type { PatchTypesOptions } from "./rollup/plugins/typescript/patch-typescript-types";
import type FileCache from "./utils/file-cache";
import type { UrlOptions } from "./rollup/plugins/url";
import type { SourcemapsPluginOptions } from "./rollup/plugins/source-maps";

type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> };

Expand Down Expand Up @@ -110,6 +111,7 @@ export interface RollupBuildOptions {
url?: UrlOptions | false;
wasm?: RollupWasmOptions | false;
watch?: RollupOptions["watch"];
sourcemap?: SourcemapsPluginOptions
}

export type TypeDocumentOptions = {
Expand Down
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.

0 comments on commit a3407d6

Please sign in to comment.