JavaScript용 모듈형 표준 라이브러리. Promise, 기호, 컬렉션, 반복자, 형식화된 배열, 기타 여러 기능, ECMAScript 제안, 일부 크로스 플랫폼 WHATWG/W3C 기능 및
URL
과 같은 제안 등 최대 2024년까지 ECMAScript에 대한 폴리필을 포함합니다. 꼭 필요한 기능만 로드하거나 전역 네임스페이스 오염 없이 사용할 수 있습니다.
더 이상 사용되지 않는 core-js@2
에 대한 문서를 찾고 있다면 이 분기를 확인하세요.
core-js
회사의 지원을 받지 않으므로 이 프로젝트의 미래는 귀하에게 달려 있습니다. core-js
에 관심이 있다면 후원자나 후원자가 되십시오: Open Collective , Patreon , Boosty , Bitcoin(bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz) , Alipay .
사용예 :
import 'core-js/actual' ;
Promise . resolve ( 42 ) . then ( it => console . log ( it ) ) ; // => 42
Array . from ( new Set ( [ 1 , 2 , 3 ] ) . union ( new Set ( [ 3 , 4 , 5 ] ) ) ) ; // => [1, 2, 3, 4, 5]
[ 1 , 2 ] . flatMap ( it => [ it , it ] ) ; // => [1, 1, 2, 2]
( function * ( i ) { while ( true ) yield i ++ ; } ) ( 1 )
. drop ( 1 ) . take ( 5 )
. filter ( it => it % 2 )
. map ( it => it ** 2 )
. toArray ( ) ; // => [9, 25]
structuredClone ( new Set ( [ 1 , 2 , 3 ] ) ) ; // => new Set([1, 2, 3])
필수 기능만 로드할 수 있습니다 .
import 'core-js/actual/promise' ;
import 'core-js/actual/set' ;
import 'core-js/actual/iterator' ;
import 'core-js/actual/array/from' ;
import 'core-js/actual/array/flat-map' ;
import 'core-js/actual/structured-clone' ;
Promise . resolve ( 42 ) . then ( it => console . log ( it ) ) ; // => 42
Array . from ( new Set ( [ 1 , 2 , 3 ] ) . union ( new Set ( [ 3 , 4 , 5 ] ) ) ) ; // => [1, 2, 3, 4, 5]
[ 1 , 2 ] . flatMap ( it => [ it , it ] ) ; // => [1, 1, 2, 2]
( function * ( i ) { while ( true ) yield i ++ ; } ) ( 1 )
. drop ( 1 ) . take ( 5 )
. filter ( it => it % 2 )
. map ( it => it ** 2 )
. toArray ( ) ; // => [9, 25]
structuredClone ( new Set ( [ 1 , 2 , 3 ] ) ) ; // => new Set([1, 2, 3])
아니면 전역 네임스페이스 오염 없이 사용하세요 .
import Promise from 'core-js-pure/actual/promise' ;
import Set from 'core-js-pure/actual/set' ;
import Iterator from 'core-js-pure/actual/iterator' ;
import from from 'core-js-pure/actual/array/from' ;
import flatMap from 'core-js-pure/actual/array/flat-map' ;
import structuredClone from 'core-js-pure/actual/structured-clone' ;
Promise . resolve ( 42 ) . then ( it => console . log ( it ) ) ; // => 42
from ( new Set ( [ 1 , 2 , 3 ] ) . union ( new Set ( [ 3 , 4 , 5 ] ) ) ) ; // => [1, 2, 3, 4, 5]
flatMap ( [ 1 , 2 ] , it => [ it , it ] ) ; // => [1, 1, 2, 2]
Iterator . from ( function * ( i ) { while ( true ) yield i ++ ; } ( 1 ) )
. drop ( 1 ) . take ( 5 )
. filter ( it => it % 2 )
. map ( it => it ** 2 )
. toArray ( ) ; // => [9, 25]
structuredClone ( new Set ( [ 1 , 2 , 3 ] ) ) ; // => new Set([1, 2, 3])
postinstall
메시지@babel/polyfill
@babel/preset-env
@babel/runtime
globalThis
Array.prototype.includes
Array.prototype.flat
/ Array.prototype.flatMap
Array
찾기Array
변경Array
그룹화ArrayBuffer.prototype.transfer
및 친구들Iterator
도우미Object.values
/ Object.entries
Object.fromEntries
Object.getOwnPropertyDescriptors
Object.prototype.hasOwnProperty
String
패딩String.prototype.matchAll
String.prototype.replaceAll
String.prototype.trimStart
/ String.prototype.trimEnd
RegExp
s
( dotAll
) 플래그RegExp
명명된 캡처 그룹Promise.allSettled
Promise.any
Promise.prototype.finally
Promise.try
Promise.withResolvers
Symbol.asyncIterator
Symbol.prototype.description
JSON.stringify
Set
메소드Array.fromAsync
JSON.parse
소스 텍스트 액세스Float16
메서드Uint8Array
와 base64 및 16진수 간RegExp
이스케이프Math.sumPrecise
Symbol.metadata
Iterator
시퀀싱AsyncIterator
도우미Iterator.range
Map
업서트Array.isTemplateObject
String.dedent
Symbol
술어Symbol.customMatcher
Observable
.of
및 .from
메소드compositeKey
및 compositeSymbol
Array
필터링Array
중복 제거DataView
Uint8Clamped
메소드를 가져오거나 설정합니다.Number.fromString
String.cooked
String.prototype.codePoints
Symbol.customMatcher
Function.prototype.demethodize
Function.{ isCallable, isConstructor }
Reflect
self
structuredClone
setTimeout
및 setInterval
setImmediate
queueMicrotask
URL
및 URLSearchParams
DOMException
// global version
npm install --save [email protected]
// version without global namespace pollution
npm install --save [email protected]
// bundled global version
npm install --save [email protected]
또는 CDN의 core-js
사용할 수 있습니다.
postinstall
메시지⬆ core-js
프로젝트에는 여러분의 도움이 필요하므로 패키지는 설치 후 이에 대한 메시지를 표시합니다. 문제가 발생하면 비활성화할 수 있습니다.
ADBLOCK=true npm install
// or
DISABLE_OPENCOLLECTIVE=true npm install
// or
npm install --loglevel silent
README.md
상단의 예시처럼 사용자에게만 필요한 폴리필을 가져올 수 있습니다. 모든 폴리필 메서드/생성자 및 네임스페이스에 대해 사용 가능한 CommonJS 진입점. 몇 가지 예를 들면 다음과 같습니다.
// polyfill all `core-js` features, including early-stage proposals:
import "core-js" ;
// or:
import "core-js/full" ;
// polyfill all actual features - stable ES, web standards and stage 3 ES proposals:
import "core-js/actual" ;
// polyfill only stable features - ES and web standards:
import "core-js/stable" ;
// polyfill only stable ES features:
import "core-js/es" ;
// if you want to polyfill `Set`:
// all `Set`-related features, with early-stage ES proposals:
import "core-js/full/set" ;
// stable required for `Set` ES features, features from web standards and stage 3 ES proposals:
import "core-js/actual/set" ;
// stable required for `Set` ES features and features from web standards
// (DOM collections iterator in this case):
import "core-js/stable/set" ;
// only stable ES features required for `Set`:
import "core-js/es/set" ;
// the same without global namespace pollution:
import Set from "core-js-pure/full/set" ;
import Set from "core-js-pure/actual/set" ;
import Set from "core-js-pure/stable/set" ;
import Set from "core-js-pure/es/set" ;
// if you want to polyfill just the required methods:
import "core-js/full/set/intersection" ;
import "core-js/actual/array/find-last" ;
import "core-js/stable/queue-microtask" ;
import "core-js/es/array/from" ;
// polyfill iterator helpers proposal:
import "core-js/proposals/iterator-helpers" ;
// polyfill all stage 2+ proposals:
import "core-js/stage/2" ;
팁
/actual/
네임스페이스는 실제 JavaScript 기능을 모두 포함하고 주로 실험용으로 사용할 수 있는 불안정한 초기 단계 제안을 포함하지 않으므로 사용을 권장합니다.
경고
modules
경로는 내부 API이며 필요한 모든 종속성을 주입하지 않으며 마이너 또는 패치 릴리스에서 변경될 수 있습니다. 사용자 정의 빌드 및/또는 수행 중인 작업을 알고 있는 경우에만 사용하십시오.core-js
사용하는 경우 애플리케이션 진입점 상단에 모든 core-js
모듈을 로드하는 것이 좋습니다. 그렇지 않으면 충돌이 발생할 수 있습니다.Symbol.iterator
사용하여 Array.from
, URLSearchParams
및/또는 core-js
의 다른 항목과 충돌합니다. 관련 문제를 참조하세요.core-js
에서 충돌하는 각 항목을 검색하고 수동으로 추가하여 해결할 수도 있습니다.core-js
는 극도로 모듈식이며 매우 작은 모듈을 많이 사용합니다. 왜냐하면 브라우저에서 사용하려면 각 파일에 대한 사용 로더 대신 core-js
번들로 묶어야 하기 때문입니다. 그렇지 않으면 수백 개의 요청이 발생하게 됩니다. pure
버전에서는 네이티브 생성자의 프로토타입을 오염시킬 수 없습니다. 이로 인해 프로토타입 메서드는 위의 예와 같이 정적 메서드로 변환되었습니다. 그러나 트랜스파일러를 사용하면 바인드 연산자와 가상 메서드라는 또 하나의 트릭을 사용할 수 있습니다. 이를 위해 특별히 사용 가능한 /virtual/
진입점이 있습니다. 예:
import fill from 'core-js-pure/actual/array/virtual/fill' ;
import findIndex from 'core-js-pure/actual/array/virtual/find-index' ;
Array ( 10 ) :: fill ( 0 ) . map ( ( a , b ) => b * b ) :: findIndex ( it => it && ! ( it % 8 ) ) ; // => 4
경고
바인드 연산자는 초기 단계의 ECMAScript 제안이며 이 구문을 사용하는 것은 위험할 수 있습니다.
core-js
babel
과 통합되어 있으며 폴리필링 관련 babel
기능의 기반입니다:
@babel/polyfill
⬆ @babel/polyfill
은 안정적인 core-js
기능과 생성기 및 비동기 기능을 위한 regenerator-runtime
가져오는 것이므로 @babel/polyfill
로드하면 ES 제안 없이 core-js
의 글로벌 버전을 로드하게 됩니다.
이제 core-js
및 regenerator-runtime
의 필수 부분을 별도로 포함하기 위해 더 이상 사용되지 않으며, 주요 변경을 방지하기 위해 core-js@2
에 남겨 둡니다.
@babel/polyfill
과 완전히 동일하게 다음을 사용할 수 있습니다.
import 'core-js/stable' ;
import 'regenerator-runtime/runtime' ;
@babel/preset-env
⬆ @babel/preset-env
전역 버전의 core-js
작업을 최적화하는 useBuiltIns
옵션이 있습니다. useBuiltIns
옵션을 사용하면 corejs
옵션을 corejs: '3.39'
처럼 사용된 core-js
버전으로 설정해야 합니다.
중요한
corejs: 3
대신 corejs: '3.39'
와 같은 사용된 마이너 core-js
버전을 지정하는 것이 좋습니다. 왜냐하면 corejs: 3
사용하면 마이너 core-js
릴리스에 추가된 모듈이 주입되지 않기 때문입니다.
useBuiltIns: 'entry'
core-js
가져오기를 대상 환경 모듈에만 필요한 가져오기로 대체합니다. 예를 들어, import 'core-js/stable' ;
chrome 71
대상은 다음과 같이 대체됩니다.
import 'core-js/modules/es.array.unscopables.flat' ;
import 'core-js/modules/es.array.unscopables.flat-map' ;
import 'core-js/modules/es.object.from-entries' ;
import 'core-js/modules/web.immediate' ;
이는 core-js
의 글로벌 버전과 그 조합의 모든 진입점에서 작동합니다.
import 'core-js/es' ;
import 'core-js/proposals/set-methods' ;
import 'core-js/full/set/map' ;
chrome 71
타겟을 사용하면 다음과 같은 결과를 얻을 수 있습니다.
import 'core-js/modules/es.array.unscopables.flat' ;
import 'core-js/modules/es.array.unscopables.flat-map' ;
import 'core-js/modules/es.object.from-entries' ;
import 'core-js/modules/esnext.set.difference' ;
import 'core-js/modules/esnext.set.intersection' ;
import 'core-js/modules/esnext.set.is-disjoint-from' ;
import 'core-js/modules/esnext.set.is-subset-of' ;
import 'core-js/modules/esnext.set.is-superset-of' ;
import 'core-js/modules/esnext.set.map' ;
import 'core-js/modules/esnext.set.symmetric-difference' ;
import 'core-js/modules/esnext.set.union' ;
useBuiltIns: 'usage'
이 파일에서 사용되고 대상 환경에서 지원되지 않는 기능에 대한 폴리필 가져오기의 각 파일 상단에 추가됩니다. // first file:
let set = new Set ( [ 1 , 2 , 3 ] ) ;
// second file:
let array = Array . of ( 1 , 2 , 3 ) ;
대상에 IE 11
과 같은 이전 환경이 포함되어 있으면 다음과 같습니다.
// first file:
import 'core-js/modules/es.array.iterator' ;
import 'core-js/modules/es.object.to-string' ;
import 'core-js/modules/es.set' ;
var set = new Set ( [ 1 , 2 , 3 ] ) ;
// second file:
import 'core-js/modules/es.array.of' ;
var array = Array . of ( 1 , 2 , 3 ) ;
기본적으로 useBuiltIns: 'usage'
옵션이 있는 @babel/preset-env
안정적인 기능만 폴리필하지만, corejs: { version: '3.39', proposals: true }
와 같이 proposals
옵션을 사용하여 제안의 폴리필을 활성화할 수 있습니다.
중요한
useBuiltIns: 'usage'
의 경우 core-js
가져오기를 직접 추가하면 안 되며 자동으로 추가됩니다.
@babel/runtime
⬆ @babel/runtime
with corejs: 3
옵션은 core-js-pure
작업을 단순화합니다. 전역 네임스페이스 오염 없이 core-js
버전에서 가져오기 위해 JS 표준 라이브러리의 최신 기능 사용을 자동으로 대체합니다.
import from from 'core-js-pure/stable/array/from' ;
import flat from 'core-js-pure/stable/array/flat' ;
import Set from 'core-js-pure/stable/set' ;
import Promise from 'core-js-pure/stable/promise' ;
from ( new Set ( [ 1 , 2 , 3 , 2 , 1 ] ) ) ;
flat ( [ 1 , [ 2 , 3 ] , [ 4 , [ 5 ] ] ] , 2 ) ;
Promise . resolve ( 32 ) . then ( x => console . log ( x ) ) ;
다음과 같이 작성할 수 있습니다.
Array . from ( new Set ( [ 1 , 2 , 3 , 2 , 1 ] ) ) ;
[ 1 , [ 2 , 3 ] , [ 4 , [ 5 ] ] ] . flat ( 2 ) ;
Promise . resolve ( 32 ) . then ( x => console . log ( x ) ) ;
기본적으로 @babel/runtime
안정적인 기능만 폴리필하지만 @babel/preset-env
와 마찬가지로 corejs: { version: 3, proposals: true }
와 같이 proposals
옵션으로 제안 폴리필을 활성화할 수 있습니다.
경고
@babel/preset-env
와 @babel/runtime
함께 사용하는 경우 corejs
옵션은 기능이 중복되어 충돌이 발생하므로 한 곳에서만 사용하세요.
빠른 JavaScript 변환기 swc
core-js
의 글로벌 버전과의 작업을 최적화하는 core-js
와의 통합이 포함되어 있습니다. @babel/preset-env
와 마찬가지로 usage
와 entry
의 2가지 모드가 있지만 usage
모드는 여전히 babel
만큼 잘 작동하지 않습니다. .swcrc
구성의 예:
{
"env" : {
"targets" : " > 0.25%, not dead " ,
"mode" : " entry " ,
"coreJs" : " 3.39 "
}
}
기본적으로 core-js
필요할 때만 폴리필을 설정합니다. 이는 core-js
기능이 사용 가능하고 올바르게 작동하는지 확인하고 문제가 없는 경우 core-js
기본 구현을 사용한다는 것을 의미합니다.
그러나 때로는 core-js
기능 감지가 귀하의 경우에 너무 엄격할 수 있습니다. 예를 들어 Promise
생성자는 처리되지 않은 거부 추적 및 @@species
지원이 필요합니다.
때때로 우리는 역 문제, 즉 core-js
기능 감지로 다루지 않는 문제로 인해 고의로 손상된 환경이 발생할 수 있습니다.
이러한 경우 특정 폴리필에 대해 이 동작을 재정의할 수 있습니다.
const configurator = require ( 'core-js/configurator' ) ;
configurator ( {
useNative : [ 'Promise' ] , // polyfills will be used only if natives are completely unavailable
usePolyfill : [ 'Array.from' , 'String.prototype.padEnd' ] , // polyfills will be used anyway
useFeatureDetection : [ 'Map' , 'Set' ] , // default behavior
} ) ;
require ( 'core-js/actual' ) ;
일부 기능에서는 작동하지 않습니다. 또한 기본 동작을 변경하면 core-js
내부도 제대로 작동하지 않을 수 있습니다.
어떤 경우에는 일부 core-js
기능을 제외하거나 대상 엔진에 대한 폴리필을 생성하는 것이 유용할 수 있습니다. 이를 위해 core-js-builder
패키지를 사용할 수 있습니다.
core-js
ES3 지원을 통해 가능한 모든 JS 엔진과 환경을 지원하려고 합니다. 일부 기능은 더 높은 하단 막대를 가지고 있습니다. 예를 들어, 일부 접근자는 ES5에서만 제대로 작동할 수 있고, 약속에는 마이크로 작업이나 작업을 설정하는 방법이 필요합니다.
그러나 예를 들어 IE7 및 기타 일부 고대의 테스트가 중단된 등 모든 곳에서 core-js
절대적으로 테스트할 가능성은 없습니다. 확실히 지원되는 엔진 목록은 아래 링크의 호환성 표에서 확인할 수 있습니다. 엔진 지원에 문제나 질문이 있으면 작성하세요.
core-js
프로젝트는 core-js
모듈의 필요성, 진입점 및 작업 도구에 대한 모든 필수 데이터를 ( core-js-compat
패키지로) 제공합니다. 이는 babel
또는 swc
와 같은 도구와 통합하는 데 유용합니다. 도움을 주고 싶다면 CONTRIBUTING.md
의 관련 섹션을 살펴보세요. 호환성 데이터 시각화 및 브라우저 테스트 실행기는 다음 예에서 확인할 수 있습니다.
CommonJS 진입점:
core-js(-pure)
CommonJS 진입점:
core-js(-pure)/es
모듈 es.object.assign
할당 , es.object.create
, es.object.define-getter
, es.object.define-property
, es.object.define-properties
, es.object.define-setter
, es.object.entries
, es.object.freeze
, es.object.from-entries
, es.object.get-own-property-descriptor
, es.object.get-own-property-descriptors
, es.object.get-own-property-names
, es.object.get-prototype-of
, es.object.group-by
, es.object.has-own
, es.object.is
, es.object.is-extensible
, es.object.is-frozen
, es.object.is-sealed
, es.object.keys
, es.object.lookup-setter
, es.object.lookup-getter
, es.object.prevent-extensions
, es.object.proto
, es.object.to-string
, es.object.seal
, es.object.set-prototype-of
, es.object.values
.
class Object {
toString ( ) : string ; // ES2015+ fix: @@toStringTag support
__defineGetter__ ( property : PropertyKey , getter : Function ) : void ;
__defineSetter__ ( property : PropertyKey , setter : Function ) : void ;
__lookupGetter__ ( property : PropertyKey ) : Function | void ;
__lookupSetter__ ( property : PropertyKey ) : Function | void ;
__proto__ : Object | null ; // required a way setting of prototype - will not in IE10-, it's for modern engines like Deno
static assign ( target : Object , ... sources : Array < Object > ) : Object ;
static create ( prototype : Object | null , properties ?: { [ property : PropertyKey ] : PropertyDescriptor } ) : Object ;
static defineProperties ( object : Object , properties : { [ property : PropertyKey ] : PropertyDescriptor } ) ) :