-
-
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.
Merge pull request #25598 from storybookjs/version-patch-from-7.6.8
Release: Patch 7.6.9
- Loading branch information
Showing
35 changed files
with
911 additions
and
1,352 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
66 changes: 45 additions & 21 deletions
66
code/builders/builder-webpack5/src/loaders/export-order-loader.ts
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,34 +1,58 @@ | ||
import { parse } from 'es-module-lexer'; | ||
import assert from 'node:assert'; | ||
import { parse as parseCjs, init as initCjsParser } from 'cjs-module-lexer'; | ||
import { parse as parseEs } from 'es-module-lexer'; | ||
import MagicString from 'magic-string'; | ||
import type { LoaderContext } from 'webpack'; | ||
|
||
export default async function loader(this: LoaderContext<any>, source: string) { | ||
export default async function loader( | ||
this: LoaderContext<any>, | ||
source: string, | ||
map: any, | ||
meta: any | ||
) { | ||
const callback = this.async(); | ||
|
||
try { | ||
// Do NOT remove await here. The types are wrong! It has to be awaited, | ||
// otherwise it will return a Promise<Promise<...>> when wasm isn't loaded. | ||
const [, exports = []] = await parse(source); | ||
const magicString = new MagicString(source); | ||
|
||
// Trying to parse as ES module | ||
try { | ||
// Do NOT remove await here. The types are wrong! It has to be awaited, | ||
// otherwise it will return a Promise<Promise<...>> when wasm isn't loaded. | ||
const parseResult = await parseEs(source); | ||
const namedExportsOrder = (parseResult[1] || []) | ||
.map((e) => source.substring(e.s, e.e)) | ||
.filter((e) => e !== 'default'); | ||
|
||
assert( | ||
namedExportsOrder.length > 0, | ||
'No named exports found. Very likely that this is not a ES module.' | ||
); | ||
|
||
const namedExportsOrder = exports.some( | ||
(e) => source.substring(e.s, e.e) === '__namedExportsOrder' | ||
); | ||
magicString.append( | ||
`;export const __namedExportsOrder = ${JSON.stringify(namedExportsOrder)};` | ||
); | ||
// Try to parse as CJS module | ||
} catch { | ||
await initCjsParser(); | ||
const namedExportsOrder = (parseCjs(source).exports || []).filter( | ||
(e: string) => e !== 'default' && e !== '__esModule' | ||
); | ||
|
||
if (namedExportsOrder) { | ||
return callback(null, source); | ||
assert( | ||
namedExportsOrder.length > 0, | ||
'No named exports found. Very likely that this is not a CJS module.' | ||
); | ||
|
||
magicString.append( | ||
`;module.exports.__namedExportsOrder = ${JSON.stringify(namedExportsOrder)};` | ||
); | ||
} | ||
|
||
const magicString = new MagicString(source); | ||
const orderedExports = exports.filter((e) => source.substring(e.s, e.e) !== 'default'); | ||
magicString.append( | ||
`;export const __namedExportsOrder = ${JSON.stringify( | ||
orderedExports.map((e) => source.substring(e.s, e.e)) | ||
)};` | ||
); | ||
|
||
const map = magicString.generateMap({ hires: true }); | ||
return callback(null, magicString.toString(), map); | ||
const generatedMap = magicString.generateMap({ hires: true }); | ||
|
||
return callback(null, magicString.toString(), generatedMap, meta); | ||
} catch (err) { | ||
return callback(err as any); | ||
return callback(null, source, map, meta); | ||
} | ||
} |
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
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
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
Oops, something went wrong.