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 #20043 from storybookjs/valentin/fix-angular-15-in…
…compatibility-sb-6.5.x Fix "__webpack_require__.nmd is not a function issue" in Angular 15
- Loading branch information
Showing
6 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
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
49 changes: 49 additions & 0 deletions
49
app/angular/src/server/plugins/storybook-normalize-angular-entry-plugin.js
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,49 @@ | ||
const PLUGIN_NAME = 'storybook-normalize-angular-entry-plugin'; | ||
|
||
/** | ||
* Angular's webpack plugin @angular-devkit/build-angular/src/webpack/plugins/styles-webpack-plugin.js | ||
* transforms the original webpackOptions.entry point array into a structure like this: | ||
* | ||
* ```js | ||
* { | ||
* main: { | ||
* import: [...] | ||
* }, | ||
* | ||
* styles: { | ||
* import: [...] | ||
* }, | ||
* } | ||
* ``` | ||
* | ||
* Storybook throws an __webpack_require__.nmd is not a function error, when another runtime bundle (styles~runtime.iframe.bundle.js) is loaded. | ||
* To prevent this error, we have to normalize the entry point to only generate one runtime bundle (main~runtime.iframe.bundle.js). | ||
*/ | ||
export default class StorybookNormalizeAngularEntryPlugin { | ||
constructor(options) { | ||
this.options = options; | ||
} | ||
|
||
apply(compiler) { | ||
const webpackOptions = compiler.options; | ||
const entry = | ||
typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry; | ||
|
||
webpackOptions.entry = async () => { | ||
const entryResult = await entry; | ||
|
||
if (entryResult.main && entryResult.styles) { | ||
return { | ||
main: { | ||
import: Array.from(new Set([...entryResult.main.import, ...entryResult.styles.import])), | ||
}, | ||
}; | ||
} | ||
|
||
return entry; | ||
}; | ||
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { | ||
this.compilation = compilation; | ||
}); | ||
} | ||
} |
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