2017-03-11 4 views
0

This article은 React 앱을 위해로드하는 폴리 필을 외부화하고 영감을 받아 필요한 브라우저에만로드합니다. 간단한 테스트 : polyfills로드 할 필요가있는 경우 재생성 런타임 폴리 폴리은 조건부로 어떻게 사용할 수 있습니까?

function browserSupportsAllFeatures() { 
    return window.Promise && window.fetch && window.regeneratorRuntime 
} 

다음 결정한다 : 그러나

if (browserSupportsAllFeatures()) { 
    // Browsers that support all features run `main()` immediately. 
    main() 
} else { 
    // All other browsers loads polyfills and then run `main()`. 
    loadScript('/path/to/polyfills.js', main) 
} 

function main(err) { 
    // Initiate all other code paths. 
    // If there's an error loading the polyfills, handle that 
    // case gracefully and track that the error occurred. 
} 

, 내 코드 생성기를 사용하고 있는데 그 가장자리의 경우 약간 것 같다. 내가 바벨이 발전기 (https://babeljs.io/docs/plugins/transform-regenerator/)를 변환 한 것으로 알고 번역 된 코드도 regeneratorRuntime (this package)이 필요합니다.

App을 가져온 경우 (생성자를 사용하는 코드 포함) regeneratorRuntime이 정의되지 않았기 때문에 내 앱이 실패합니다.

// import dependencies 
import React from 'react' 
import { render } from 'react-dom' 
import { App } from './components/app' 

const renderApp =() => { 
    render(<App />, document.getElementById('app')) 
} 

function browserSupportsAllFeatures() { 
    return window.Promise && window.fetch && window.regeneratorRuntime 
} 

if (browserSupportsAllFeatures()) { 
    renderApp() 
} else { 
    loadScript('/path/to/polyfills.js', renderApp); 
} 

나는 내가 맨 위에 재생기를 가져 와서이 문제를 해결할 수 있습니다 알고 있지만, 여기에 의도는 polyfills을 외부화하고 조건부 만드는 것입니다 : 그래서 내 polyfills 너무 늦었입니다.

제 질문은; 어떻게 계속 발전기를 사용할 수 있지만 내 polyfills는 loadScript 호출에 포함되어 있습니까?

답변

0

당신은 사용하여 속성을 확인할 수 있습니다

if (window.hasOwnProperty("regeneratorRuntime")) ... 

이 동일하지 NPM 패키지 polyfill-IO-기능 감지, 그것은 특징 검출을 처리하는 방법을 보려면이 솔루션에 대해 살펴입니다 : https://github.com/jquintozamora/polyfill-io-feature-detection