Angular 9 和 Storybook:添加 @angular/localize/init polyfill

R. *_*ter 5 angular storybook angular9

更新到 angular 9 时,我收到以下 Storybook 错误。

来自故事书的错误信息

错误说我需要为国际化添加一个 polyfill,但我不知道如何做。

我需要配置 webpack 并在那里添加 polyfill,但我无法让它工作。

有谁知道如何解决这一问题?

干杯

编辑:为了澄清我确实ng add @angular/localize在 angular 项目中运行了。

尽管如此,Storybook 并没有接受它。

这是我的 polyfills.ts 文件。

/***************************************************************************************************
 * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
 */
import '@angular/localize/init';
/**
 * This file includes polyfills needed by Angular and is loaded before the app.
 * You can add your own extra polyfills to this file.
 *
 * This file is divided into 2 sections:
 *   1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
 *   2. Application imports. Files imported after ZoneJS that should be loaded before your main
 *      file.
 *
 * The current setup is for so-called "evergreen" browsers; the last versions of browsers that
 * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera),
 * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile.
 *
 * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html
 */

/***************************************************************************************************
 * BROWSER POLYFILLS
 */

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es/symbol';
import 'core-js/es/object';
import 'core-js/es/function';
import 'core-js/es/parse-int';
import 'core-js/es/parse-float';
import 'core-js/es/number';
import 'core-js/es/math';
import 'core-js/es/string';
import 'core-js/es/date';
import 'core-js/es/array';
import 'core-js/es/regexp';
import 'core-js/es/map';
import 'core-js/es/set';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
// import 'classlist.js';  // Run `npm install --save classlist.js`.

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es/reflect';

/**
 * Required to support Web Animations `@angular/platform-browser/animations`.
 * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation
 **/
// import 'web-animations-js';  // Run `npm install --save web-animations-js`.



/***************************************************************************************************
 * Zone JS is required by default for Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.



/***************************************************************************************************
 * APPLICATION IMPORTS
 */
/* https://github.com/aws/aws-amplify/issues/678 fix: */
(window as any).global = window;
/* https://github.com/aws/aws-amplify/issues/678 fix end */

Run Code Online (Sandbox Code Playgroud)

编辑2:

配置文件

{
  "extends": "../src/tsconfig.app.json",
  "compilerOptions": {
    "types": [
      "node"
    ]
  },
  "exclude": [
    "../src/test.ts",
    "../src/**/*.spec.ts",
    "../projects/**/*.spec.ts",
    "../src/assets/webgl_2019_1/**/*"
  ],
  "include": [
    "../src/**/*",
    "../projects/**/*"
  ],
  "files": [
    "./typings.d.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

R. *_*ter 9

TLDR:添加import '@angular/localize/init';到.storybook 文件夹中config.js或添加到preview.js.storybook 文件夹中。

解释

问题是 Storybook 找不到 polyfill @angular/localize/init。由于 Storybook 使用 webpack 来提供 Storybook,因此我们需要将 polyfill 添加到 webpack。

将 polyfill 添加到 webpack 可以通过将其添加到entries数组来完成。https://webpack.js.org/guides/shimming/

通过查看此页面(https://storybook.js.org/docs/configurations/custom-webpack-config/),您可以了解如何配置 storybook 以添加您的 polyfill。这里有一个标题叫做This is what the config for storybook looks like when using CRA in dev-mode:

如果你展开它,它会在 storybook 中显示 webpack 的默认配置。在条目数组中,您可以看到有以下行: '<your-storybook-dir>/preview.js'. 如果你在这个文件中添加 polyfill,它也会被 webpack。

preview.js并被config.js视为相同的文件,因此将 polyfill 添加到其中任何一个都可以解决问题。