generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
053738e
commit 92033f3
Showing
10 changed files
with
637 additions
and
197 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"include": ["**/Button.tsx", "**/Page.tsx"] | ||
} |
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,108 @@ | ||
import { fromSource, fromMapFileSource } from "convert-source-map"; | ||
import { | ||
createInstrumenter, | ||
InstrumenterOptions, | ||
} from "istanbul-lib-instrument"; | ||
// @ts-expect-error no types | ||
import mergeSourceMap from "merge-source-map"; | ||
import { LoaderContext } from "webpack"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { AddonOptionsWebpack } from "../types"; | ||
|
||
export type Options = Partial<InstrumenterOptions> & AddonOptionsWebpack; | ||
|
||
type RawSourceMap = { | ||
version: number; | ||
sources: string[]; | ||
mappings: string; | ||
file?: string; | ||
sourceRoot?: string; | ||
sourcesContent?: string[]; | ||
names?: string[]; | ||
}; | ||
|
||
export const defaultOptions: Partial<InstrumenterOptions> = { | ||
preserveComments: true, | ||
produceSourceMap: true, | ||
autoWrap: true, | ||
esModules: true, | ||
compact: false, | ||
}; | ||
|
||
export default function ( | ||
this: LoaderContext<Options>, | ||
source: string, | ||
sourceMap?: RawSourceMap | ||
) { | ||
let map = sourceMap; | ||
let options = Object.assign(defaultOptions, this.getOptions()); | ||
|
||
// If there's no external sourceMap file, then check for an inline sourceMap | ||
if (!map) { | ||
map = sourceMap = getInlineSourceMap.call(this, source); | ||
} | ||
|
||
// Instrument the code | ||
let instrumenter = createInstrumenter(options); | ||
instrumenter.instrument( | ||
source, | ||
this.resourcePath, | ||
(error, instrumentedSource) => { | ||
let instrumentedSourceMap = instrumenter.lastSourceMap(); | ||
|
||
if (sourceMap && instrumentedSourceMap) { | ||
// Re-map the source map to the original source code | ||
instrumentedSourceMap = mergeSourceMap( | ||
sourceMap, | ||
instrumentedSourceMap | ||
); | ||
} | ||
|
||
this.callback( | ||
error, | ||
instrumentedSource, | ||
instrumentedSourceMap as any as RawSourceMap | ||
); | ||
}, | ||
sourceMap as any | ||
); | ||
} | ||
|
||
/** | ||
* If the source code has an inline base64-encoded source map, | ||
* then this function decodes it, parses it, and returns it. | ||
*/ | ||
function getInlineSourceMap( | ||
this: LoaderContext<Options>, | ||
source: string | ||
): RawSourceMap | undefined { | ||
try { | ||
// Check for an inline source map | ||
const inlineSourceMap = | ||
fromSource(source) || | ||
fromMapFileSource(source, function (filename) { | ||
return fs.readFileSync( | ||
path.resolve(path.dirname(this.resourcePath), filename), | ||
"utf-8" | ||
); | ||
}); | ||
|
||
if (inlineSourceMap) { | ||
// Use the inline source map | ||
return inlineSourceMap.sourcemap as RawSourceMap; | ||
} | ||
} catch (e) { | ||
// Exception is thrown by fromMapFileSource when there is no source map file | ||
if ( | ||
e instanceof Error && | ||
e.message.includes( | ||
"An error occurred while trying to read the map file at" | ||
) | ||
) { | ||
this.emitWarning(e); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} |
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,14 @@ | ||
import { AddonOptionsWebpack } from "./types"; | ||
// @ts-expect-error no types | ||
import { loadNycConfig } from "@istanbuljs/load-nyc-config"; | ||
|
||
export async function getNycConfig( | ||
opts: Pick<AddonOptionsWebpack["istanbul"], "cwd" | "nycrcPath"> = {} | ||
) { | ||
const cwd = opts.cwd ?? process.cwd(); | ||
|
||
return loadNycConfig({ | ||
cwd, | ||
nycrcPath: opts.nycrcPath, | ||
}); | ||
} |
Oops, something went wrong.