包括使用webpack在Aurelia中获取polyfill

时间:2016-08-10 15:46:06

标签: typescript phantomjs webpack aurelia

所以我注意到我在PhantomJs中遇到这个错误,我原以为包含了polyfill,因为它有一个@type/whatwg-fetch

Error: HttpClient requires a Fetch API implementation, but the current environment doesn't support it. You may need to load a polyfill such as https://github.com/github/fetch. in spec-bundle.js (line 18057)

我不确定如何在这个webpack情况下加载推荐的polyfill,我需要安装什么npm模块?以及如何将其添加到webpack(基于typescript-weback骨架的webpack)

尝试了这个

import { Aurelia } from 'aurelia-framework';
import '../styles/styles.css';
import 'font-awesome/css/font-awesome.css';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap';
import * as Bluebird from 'bluebird';
import 'whatwg-fetch';
// we want font-awesome to load as soon as possible to show the fa-spinner

// comment out if you don't want a Promise polyfill (remove also from webpack.config.js)
Bluebird.config({ warnings: false });

export async function configure(aurelia: Aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();

  // Uncomment the line below to enable animation.
  // aurelia.use.plugin('aurelia-animator-css');
  // if the css animator is enabled, add swap-order="after" to all router-view elements

  // Anyone wanting to use HTMLImports to load views, will need to install the following plugin.
  // aurelia.use.plugin('aurelia-html-import-template-loader')

  await aurelia.start();
  aurelia.setRoot('app');

    // if you would like your website to work offline (Service Worker),
  // install and enable the @easy-webpack/config-offline package in webpack.config.js and uncomment the following code:
  /*
  const offline = await System.import('offline-plugin/runtime');
  offline.install();
  */
}

这就是我安装的内容

npm ls whatwg-fetch                                                slave-vi
aurelia-skeleton-navigation-webpack@1.0.0 /home/xenoterracide/IdeaProjects/rpf-ui
└── whatwg-fetch@1.0.0

我可以在app-bundle.js中看到获取代码,但我仍然看到PhantomJS抛出上述错误

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用包isomorphic-fetch,它提供了一个可在Node和浏览器中使用的Fetch polyfill(使用webpack和Browserify)。

如果您只想要一个浏览器polyfill,您也可以使用包whatwg-fetch

安装isomorphic-fetchwhatwg-fetch后,只需在输入点的开头导入,然后再输入所有其他非填充物:

import "isomorphic-fetch"; // or whatwg-fetch
// Other imports go here

这就是它的全部!在那之后导入window.fetch将在必要时进行polyfilled!

答案 1 :(得分:1)

您似乎需要在设置HttpClient的同一个班级中进行导入。对于typescript-webpack骨架,这可能位于app.ts

import 'isomorphic-fetch';
import { Router, RouterConfiguration } from 'aurelia-router';
import { Logger } from 'aurelia-logging';
import { Container, LogManager, autoinject } from 'aurelia-framework';
import { Route } from './main/Route';
import { HttpClient } from 'aurelia-fetch-client';

@autoinject
export class App {
    router: Router;
    private log: Logger = LogManager.getLogger( App );

    constructor( container: Container ) {
        let client: HttpClient = new HttpClient;
        client.configure( config => {
            config.useStandardConfiguration()
                .withBaseUrl( "http://localhost:8080/" )
                .withDefaults( {
                    credentials: 'include'
                } );
        } );
        container.registerSingleton( HttpClient, () => client );
    }