generated from anolilab/monorepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added new source-maps plugin for loading files with existing so…
…urce maps.
- Loading branch information
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.