node-i18n-iso-countries icon indicating copy to clipboard operation
node-i18n-iso-countries copied to clipboard

Add rollup & support ES modules

Open hansottowirtz opened this issue 5 years ago • 6 comments

This is a WIP. Closes #186.

It adds Rollup and converts the source files to ES modules. When using ES modules in Node, it is not possible to import JSON files. Rollup therefore compiles the .json files to .mjs and .js files. codes.json is bundled in every distributed file, and entry-node.mjs bundles all languages into one file.

It makes it possible to do the following (from Node 13+, Webpack 5):

import en from "i18n-iso-countries/langs/en";
import countries from "i18n-iso-countries";
countries.registerLocale(countries);

or

import entryNode from "i18n-iso-countries/bundled";

I'm using it on our Angular 10 website and it works (as Angular CLI uses Webpack 4, I still need to import i18n-iso-countries/dist/langs/en).

Anyone can install this branch with NPM by running the command below. I also added a prepare script, which builds the code if the package is being installed from Github.

npm i hansottowirtz/node-i18n-iso-countries#add-rollup-support-esm

More testing is needed before merging this PR, with multiple node versions and with Webpack 5.

hansottowirtz avatar Jul 07 '20 09:07 hansottowirtz

Couldn't find your package on npm, can you share a link? Also, there is a npm package which is a Angular wrapper for this library: https://www.npmjs.com/package/@ngx-countries/countries

KingDarBoja avatar Aug 19 '20 22:08 KingDarBoja

@KingDarBoja I didn't publish a package, but you can use

"i18n-iso-countries": "github:hansottowirtz/node-i18n-iso-countries#add-rollup-support-esm"

in package.json.

hansottowirtz avatar Aug 20 '20 16:08 hansottowirtz

Hey @hansottowirtz, can you update the PR with latest changes from master?

KingDarBoja avatar Nov 19 '20 21:11 KingDarBoja

@KingDarBoja done 🙂 I also added typings for the dist/langs/*.mjs files. These typings aren't resolved yet when using the exports field due to https://github.com/microsoft/TypeScript/issues/33079.

hansottowirtz avatar Nov 21 '20 12:11 hansottowirtz

It seems like

require('i18n-iso-countries/langs/nl')

only started working somewhere after Node 14.5 (14.15 confirmed working). I added the legacy "./dist/": "./dist/" syntax to the exports field in order to make it work in older versions of Node as well (legacy because now a * is required). They can use

require('i18n-iso-countries/dist/langs/nl.json')

and this syntax will also work in later versions of Node.

hansottowirtz avatar Nov 24 '20 11:11 hansottowirtz

installation fails on Windows: `npm ERR! > [email protected] build C:\Users\UserX\AppData\Local\npm-cache_cacache\tmp\git-clone-71f5291a

npm ERR! > make build

npm ERR! /usr/bin/bash: make: command not found`

And doesn't work with yarn.

manzonif avatar Mar 15 '21 08:03 manzonif

any plans to merge this into master at some point?

bschnabel avatar Mar 14 '23 13:03 bschnabel

@bschnabel I don't think that the work here is done. More like a WIP. Correct me if I'm wrong @hansottowirtz ?

michaelwittig avatar Mar 14 '23 13:03 michaelwittig