-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
e1cb740
commit 0299a35
Showing
13 changed files
with
683 additions
and
310 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
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 |
---|---|---|
@@ -1,40 +1,5 @@ | ||
import { createUnplugin } from 'unplugin'; | ||
import fs from 'fs/promises'; | ||
import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools'; | ||
import type { EnrichCsfOptions } from '@storybook/csf-tools'; | ||
|
||
export type CsfPluginOptions = EnrichCsfOptions; | ||
export default {}; | ||
|
||
const STORIES_REGEX = /\.(story|stories)\.[tj]sx?$/; | ||
|
||
const logger = console; | ||
|
||
export const unplugin = createUnplugin<CsfPluginOptions>((options) => { | ||
return { | ||
name: 'unplugin-csf', | ||
enforce: 'pre', | ||
loadInclude(id) { | ||
return STORIES_REGEX.test(id); | ||
}, | ||
async load(fname) { | ||
const code = await fs.readFile(fname, 'utf-8'); | ||
try { | ||
const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse(); | ||
enrichCsf(csf, options); | ||
return formatCsf(csf, { sourceMaps: true }); | ||
} catch (err: any) { | ||
// This can be called on legacy storiesOf files, so just ignore | ||
// those errors. But warn about other errors. | ||
if (!err.message?.startsWith('CSF:')) { | ||
logger.warn(err.message); | ||
} | ||
return code; | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
export const { esbuild } = unplugin; | ||
export const { webpack } = unplugin; | ||
export const { rollup } = unplugin; | ||
export const { vite } = unplugin; | ||
export type CsfOptions = EnrichCsfOptions; |
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,45 @@ | ||
import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools'; | ||
import fs from 'fs/promises'; | ||
import type { Plugin } from 'vite'; | ||
import type { CsfOptions } from '.'; | ||
|
||
const STORIES_REGEX = /\.(story|stories)\.[tj]sx?$/; | ||
|
||
const logger = console; | ||
|
||
function CsfVitePluginFn(options: CsfOptions = {}): Plugin { | ||
return { | ||
name: 'csf-vite-plugin', | ||
|
||
async transform(code: string, id: string) { | ||
if (!STORIES_REGEX.test(id)) { | ||
return null; | ||
} | ||
|
||
try { | ||
const originalCode = await fs.readFile(id, 'utf-8'); | ||
const csf = loadCsf(code, originalCode, { | ||
makeTitle: (userTitle) => userTitle || 'default', | ||
}).parse(); | ||
enrichCsf(csf, options); | ||
const result = formatCsf(csf, { sourceMaps: true }); | ||
if (typeof result === 'string') { | ||
return result; | ||
} | ||
return { | ||
code: result.code, | ||
map: result.map, | ||
}; | ||
} catch (err: any) { | ||
if (!err.message?.startsWith('CSF:')) { | ||
logger.warn(err.message); | ||
} | ||
return { | ||
code, | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
export default CsfVitePluginFn as any; |
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,50 @@ | ||
import type { LoaderContext } from 'webpack'; | ||
import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools'; | ||
import fs from 'fs/promises'; | ||
import type { CsfOptions } from '.'; | ||
|
||
type LoaderFunction = ( | ||
this: LoaderContext<CsfOptions>, | ||
source: string | Buffer, | ||
sourceMap?: any, | ||
meta?: any | ||
) => void; | ||
|
||
const logger = console; | ||
|
||
const CsfWebpackLoaderFn: LoaderFunction = async function CsfWebpackLoaderFn( | ||
source, | ||
sourceMap, | ||
meta | ||
) { | ||
// Indicate that the loader is asynchronous. | ||
const callback = this.async(); | ||
const filename = this.resourcePath; | ||
|
||
// Access the loader options | ||
const options = this.getOptions() || {}; | ||
|
||
try { | ||
const originalCode = await fs.readFile(filename, 'utf-8'); | ||
const csf = loadCsf(source as string, originalCode, { | ||
makeTitle: (userTitle) => userTitle || 'default', | ||
}).parse(); | ||
enrichCsf(csf, options); | ||
const result = formatCsf(csf, { sourceMaps: true, inputSourceMap: sourceMap }); | ||
|
||
if (typeof result === 'string') { | ||
callback(null, result); | ||
} else { | ||
callback(null, result.code, result.map || undefined); | ||
} | ||
} catch (err: any) { | ||
// Handle errors. | ||
if (!err.message?.startsWith('CSF:')) { | ||
logger.warn(err.message); | ||
} | ||
|
||
callback(null, source, sourceMap); | ||
} | ||
}; | ||
|
||
export default CsfWebpackLoaderFn as (source: string, sourceMap: any, meta: any) => void; |
Oops, something went wrong.