Giter VIP home page Giter VIP logo

polyfill-library's Introduction

Polyfill-library · license PRs Welcome

NodeJS module to create polyfill bundles tailored to individual user-agents

Install

npm install polyfill-library --save

Usage

const polyfillLibrary = require('polyfill-library');

const polyfillBundle = polyfillLibrary.getPolyfillString({
	uaString: 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
	minify: true,
	features: {
		'es6': { flags: ['gated'] }
	}
}).then(function(bundleString) {
	console.log(bundleString);
});

API

polyfillLibrary.listAllPolyfills()

Get a list of all the polyfills which exist within the collection of polyfill sources.

Returns a Promise which resolves with an array of all the polyfills within the collection.

polyfillLibrary.describePolyfill(featureName)

Get the metadata for a specific polyfill within the collection of polyfill sources.

  • @param {String} featureName - The name of a polyfill whose metadata should be returned.

Returns a Promise which resolves with the metadata or with undefined if no metadata exists for the polyfill.

polyfillLibrary.getOptions(opts = {})

Create an options object for use with getPolyfills or getPolyfillString.

  • @param {object} opts - Valid keys are uaString, minify, unknown, excludes, rum and features.
  • @param {Boolean} [opts.minify=true] - Whether to return the minified or raw implementation of the polyfills.
  • @param {'ignore'|'polyfill'} [opts.unknown='polyfill'] - Whether to return all polyfills or no polyfills if the user-agent is unknown or unsupported.
  • @param {Object} [opts.features={}] - Which features should be returned if the user-agent does not support them natively.
  • @param {Array<String>} [opts.excludes=[]] - Which features should be excluded from the returned object.
  • @param {String} [opts.uaString=''] - The user-agent string to check each feature against.
  • @param {Boolean} [opts.rum=false] - Whether to include a script that reports anonymous usage data in the polyfill bundle.

Returns an object which has merged opts with the defaults option values.

polyfillLibrary.getPolyfills(opts)

Given a set of features that should be polyfilled in 'opts.features' (with flags i.e. {<featurename>: {flags:Set[<flaglist>]}, ...}), determine which have a configuration valid for the given opts.uaString, and return a promise of set of canonical (unaliased) features (with flags) and polyfills.

  • @param {object} opts - Valid keys are uaString, minify, unknown, excludes, rum and features.
  • @param {Boolean} [opts.minify=true] - Whether to return the minified or raw implementation of the polyfills.
  • @param {'ignore'|'polyfill'} [opts.unknown='polyfill'] - Whether to return all polyfills or no polyfills if the user-agent is unknown or unsupported.
  • @param {Object} [opts.features={}] - Which features should be returned if the user-agent does not support them natively.
  • @param {Array<String>} [opts.excludes=[]] - Which features should be excluded from the returned object.
  • @param {String} [opts.uaString=''] - The user-agent string to check each feature against.
  • @param {Boolean} [opts.rum=false] - Whether to include a script that reports anonymous usage data in the polyfill bundle.

Returns a Promise which resolves to an Object which contains the canonicalised feature definitions filtered for UA.

polyfillLibrary.getPolyfillString(opts)

Create a polyfill bundle.

  • @param {object} opts - Valid keys are uaString, minify, unknown, excludes, rum and features.
  • @param {Boolean} [opts.minify=true] - Whether to return the minified or raw implementation of the polyfills.
  • @param {'ignore'|'polyfill'} [opts.unknown='polyfill'] - Whether to return all polyfills or no polyfills if the user-agent is unknown or unsupported.
  • @param {Object} [opts.features={}] - Which features should be returned if the user-agent does not support them natively.
  • @param {Array<String>} [opts.excludes=[]] - Which features should be excluded from the returned object.
  • @param {String} [opts.uaString=''] - The user-agent string to check each feature against.
  • @param {Boolean} [opts.rum=false] - Whether to include a script that reports anonymous usage data in the polyfill bundle.
  • @param {Boolean} [opts.stream=false] - Whether to return a stream or a string of the polyfill bundle.

Returns a polyfill bundle as either a utf-8 ReadStream or as a Promise of a utf-8 String.

AWS Lambda

To use this package in an AWS Lambda function, you need to include the distribution Polyfills located in ./node_modules/polyfill-library/polyfills/__dist in the root directory of your Lambda. In AWS, Lambdas are executed in the /var/task/... directory. Therefore, during execution, the directory where the polyfills will be located will be /var/task/polyfill-library/__dist.

Example of a script to copy files

The following snippet will allow us to copy the polyfills to our already compiled Lambda. To do this, we will first install the necessary dependencies.

yarn add -D make-dir fs-extra

Once the dependencies are installed, we will create the file with the script at /scripts/polyfills-serverless.mjs and replace YOUR_BUNDELED_LAMBDA_DIRECTORY with the directory that contains our packaged Lambda.

In the example, we will use the directory ./.serverless_nextjs/api-lambda, which is the one used when using Serverless Next.js.

import { copySync } from 'fs-extra/esm';
import makeDir from 'make-dir';

const DIR_POLYFILLS = './node_modules/polyfill-library/polyfills/__dist';
// const DIR_SERVERLESS = 'YOUR_BUNDELED_LAMBDA_DIRECTORY/polyfills/__dist';
const DIR_SERVERLESS = './.serverless_nextjs/api-lambda/polyfills/__dist';

const paths = await makeDir(DIR_SERVERLESS);
console.log(`The directory ${paths} is created successfully.`);

try {
  console.log('Copying polyfills to serverless directory...');
  copySync(DIR_POLYFILLS, DIR_SERVERLESS, { overwrite: false });
  console.log('Polyfills copied successfully!');
} catch (err) {
  console.error(err);
}

To execute the script, you will need to run the following command:

node ./scripts/polyfills-serverless.mjs

Contributing

Development of polyfill-library happens on GitHub. Read below to learn how you can take part in contributing to Polyfill.io.

Read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes.

# To test on BrowserStack you will need to have a BrowserStack account
# We test pull-requests using BrowserStack
npm run test-all-polyfills # Run the tests for all polyfills using BrowserStack
npm run test-polyfills -- --features=Array.from # Run the tests for Array.from
npm run test-polyfills -- --features=Array.from --browserstack # Run the tests for Array.from using BrowserStack

License

Polyfill-library is MIT licensed.

polyfill-library's People

Contributors

polyfillpolyfill avatar

Stargazers

឵឵ avatar M. Adilla Ramadhan avatar Huang Zhehu avatar Sven Hazel avatar Hui Liu avatar Joseph Miao avatar destroyCity() avatar Lee Pang avatar 葱花应用科学家 avatar Aika Saito ママさん avatar Engineer Manager avatar Pavlose avatar Nathaniel Ironhorse avatar Jerad Kling avatar Grokking avatar Wisnu Pradana avatar Kenji Saito avatar Code Breaker avatar Alien § Energy avatar Kalli Grace avatar ueda miki avatar Top 3% of Developer avatar Hanne Yanoy avatar Lucky Qiren avatar Priya Singh avatar Sean Lien avatar 宁愿 avatar 达梦一场 avatar Harmony of Jade avatar 致富奔小康研究中心 avatar Qian Yue avatar Isaac Momoko avatar Sarah Venus Hime avatar Jason Wu avatar 野猪佩奇 avatar 撕心裂肺的代碼 avatar Samuel Adams avatar 海王·鑫 avatar Sui Tang avatar Bai Xiqin avatar The Dark Mocha avatar Mohamed Emmerich avatar 程序猿の歌 avatar Bug Hunter avatar Ryan Lee avatar Arigato Gozaimasu avatar Liang Pan avatar Adan Automaton avatar 手作代碼 avatar Buddy Fei avatar 仙人1号 avatar iacker avatar Ririko avatar SMART=SEXY avatar Sailor Moon avatar Nuno Chung avatar 幻城 avatar Hiroshi Fujiwara avatar The Imagineer avatar James Zhu avatar Spiceworks avatar Nurul A. Rahman avatar 大阪住み avatar Kaylee Jack Lin avatar Block.Six avatar Mark Johnson avatar Cai Lumu avatar Long avatar 游戏主人 avatar 権力者 avatar Shan Liu avatar Євгенія avatar Madison Jing avatar 魏玮晨 avatar Lizhan Xiong avatar Jordan Jones avatar Dulé Ge avatar 翁仁賢 avatar Kai He avatar Manifold Garden avatar Jackson Kang avatar 昵称不能为空 avatar fundamental developer avatar Brandi Love avatar Sweetest Expert avatar  avatar John von Locke avatar Shiyuan Ma avatar Sandwich Cat avatar 庄司智 avatar Richard (Bear) Pi avatar 林 云庆 avatar Justin Pfifer avatar Diwu Lechao avatar Ji Kang avatar Nan Yu (BJ) avatar Wu Huizhi avatar Bicerin Gu avatar ชนิดาภา ดรน้อยโม้ avatar Colorido Baby avatar

Watchers

Andrii Dembitskyi avatar  avatar Junho Choi avatar Wu Wu avatar Lisa Wu avatar Miki A332 avatar I'm Javon Marks avatar Eisbock avatar Tong Wu (吴桐) avatar ZenGi 七曦 avatar 戦闘斥候 avatar Sakura {Find} °On the floor° avatar Phạm Tuân avatar 万绮雯 avatar U'landa avatar Outer System [Team] avatar Miuji Ou avatar Quantum Miracle avatar Miss Katy avatar Dr. Longhai Zhao avatar Hang Zhang (张航) avatar Mairou Gu avatar xinhu chen avatar Hailong Ou avatar 张 棪琰 avatar I'maoka avatar Charles Goodyear avatar Qiming Deng avatar Jaehwa Choi avatar Serena Bai avatar Jennifer Zeng 曾錚 avatar Yunfan Wu avatar Zellux Lu avatar Bowen You avatar Jae Hoon avatar User:Tiago486 avatar Shum Chun avatar Dillon Xu avatar Old Fashion Researcher avatar Lemon Star Bassett avatar Zeek Rolling avatar MJ ✊ avatar Gerry Lee avatar Yeemay Huang avatar I'm Dungeoneer ⚡ avatar Eun Ji Kim avatar Xoray Schafer avatar Lee Jae-min avatar Moncler 7 FRGMT avatar Domen Yeo avatar Murray Purdy avatar Meow, the Coder avatar Easy Companies Inc. avatar Tom Clancy avatar tHE ProGrAmMeR avatar Emily Mei avatar Wayne Ren avatar Aaron Ramirez avatar Pepper Shibaru avatar Melody Chu avatar MZ COLA avatar Li Yingyun avatar June Han avatar Helena Zhao avatar Boyang Cai avatar An Unofficial Coder avatar Chloe Martin avatar ¯\_(ツ)_/¯ avatar Mr. Davis avatar Harry, Go Go Go! avatar Lily Chen avatar The Pool Party avatar 星之吻痕 avatar /* Michael Liwei */ avatar six freedom avatar Alaska Pollock avatar Ava K. Zhou avatar Daniel Kim avatar Better Than Seek avatar ミルバス avatar Taro Kobayashi avatar Lisa Kim avatar Owen Reed avatar destroyCity() avatar 无论何时都选择我 avatar don't code for program avatar Star Xu avatar Takayuki Horikawa avatar Dawn Langosh avatar 보안(해킹) avatar BonBon Boys 305 avatar Johan Thomas avatar 戦慄の依酱 avatar Robert Warshall avatar Alex Chang avatar Glorang avatar Americano avatar Bailing Guo avatar Tatsuo Nakajima avatar Max Shu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.