thekelvinliu / country-code-emoji Goto Github PK
View Code? Open in Web Editor NEWconvert country codes (ISO 3166-1 alpha-2) to corresponding emoji flags (unicode regional indicator symbols)
License: MIT License
convert country codes (ISO 3166-1 alpha-2) to corresponding emoji flags (unicode regional indicator symbols)
License: MIT License
With recent versions of TypeScript (currently v4.7.4) and with project TypeScript config compilerOptions.module
set to nodenext
, the types for this package are not available when importing from within an ESM module:
// @ts-check
import { countryCodeEmoji } from "country-code-emoji";
The hover intellisense doesn't include the right types (cc
arg should be type string
):
If you CMD + click the import, it opens the country-code-emoji/lib/index.mjs
module instead of the country-code-emoji/lib/index.d.ts
file you would expect if the types were working.
I think the reason is because the package exports
field incorrectly doesn't start with a types
property:
country-code-emoji/package.json
Lines 11 to 14 in 38faa2b
"exports": {
+ "types": "./lib/index.d.ts",
"import": "./lib/index.mjs",
"require": "./lib/index.js"
},
See:
country-code-emoji/package.json
Line 12 in 7eef3bc
This is incorrect, and result in a Node.js runtime error:
import countryCodeEmoji from 'country-code-emoji';
(node:26808) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/[redacted]/node_modules/country-code-emoji/src/index.js:15
export function countryCodeEmoji(cc) {
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:195:29)
at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
at async Loader.import (internal/modules/esm/loader.js:182:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
Currently country-code-emoji
ships separate CJS and ESM builds, which load conditionally depending if it's being imported or required. This is susceptible to the dual package hazard:
https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#packages_dual_package_hazard
E.g. if one part of a codebase uses require
to access countryCodeEmoji
, and another import
, then the same function will load twice in memory at runtime, and if bundling, will get bundled twice doubling the bundle size impact.
The best way to tackle this is to either:
require
. To do this, only publish the API in one set of ESM files. While this forces your users to write ESM, the code can be fully browser compatible as there is no CJS used anywhere by the public API's code (assuming no dependencies are used that are CJS, which is the case here for country-code-emoji
).import
and require
by publishing the API in one set of CJS files, with one default export (module.exports =
) in each file. The package should provide one deep-importable file for each function in the API (see #11), but for analysis tools like https://bundlephobia.com an index is still useful as the main entry point. To support both require
and import
(with named imports) from the index, conditional package exports
can be used to direct to relevant index.js
(CJS) and index.mjs
(ESM) files.Here is an example package to reference how that works:
Thanks, I'll be using this in https://github.com/crocodilejs/crocodile-node-mvc-framework for translation dropdown menu items.
Is there a way to tell if a proper flag was found?
In the README you write:
to keep things simple, if cc a two letter string, but not an actual ISO 3166-1 alpha-2 code, the regional indicator symbols corresponding to the letters in cc are returned.
Is there a way to see if this happened?
If so, I don't want to display the letters with the border, but just a string:
I believe the country code "AN" is missing here. It could well be that there is no emoji for this, yet.
Hi , Can you please add Netherlands Antilles flag to your library . Thank you.
Right now, the API is only available from index files:
https://unpkg.com/browse/[email protected]/lib/index.mjs
Ideally, countryCodeEmoji
and emojiCountryCode
would be available separately, e.g:
import countryCodeEmoji from 'country-code-emoji/countryCodeEmoji.mjs';
import emojiCountryCode from 'country-code-emoji/emojiCountryCode.mjs';
This way only the necessary code can be imported and loaded into memory in a given context, and it also ensures perfect bundling without depending on slow and unreliable "tree-shaking".
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.