Skip to content

Commit

Permalink
update minification code
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Dec 8, 2024
1 parent ffb2b63 commit 5532f3c
Show file tree
Hide file tree
Showing 10 changed files with 19,055 additions and 77 deletions.
37 changes: 37 additions & 0 deletions packages/react-country-region-selector/config/minify-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This script is called on bootstrap to generate a minified version of the source data from
* country-region-data:
* https://github.com/country-regions/country-region-data
*
* Example: `npm run minify-data -- --config-countries=GB,CA,US
*/
const argv = require('minimist');
const args = argv(process.argv.slice(2));
const fs = require('fs');
const path = require('path');

let countryRegions = require('country-region-data/data.json');

let countries = [];
if (args.hasOwnProperty('config-countries')) {
countries = args['config-countries'].split(',');
}

// filter out those countries that the user wants
if (countries.length > 0) {
countryRegions = countryRegions.filter(
(countryData) => countries.indexOf(countryData.countryShortCode) !== -1
);
}

const srcFolder = path.resolve(__dirname, '../src');

const data = JSON.stringify(countryRegions, undefined, ' ');
const tsData = `
import { CountryRegionDataMinified } from './types';
const data = ${data};
export default data;
`;
fs.writeFileSync(`${srcFolder}/_data.ts`, tsData);

This file was deleted.

21 changes: 5 additions & 16 deletions packages/react-country-region-selector/config/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ import url from '@rollup/plugin-url';
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';
import pkg from '../package.json' with { type: 'json' };
import argv from 'minimist';
import parseCountryList from './rollup-plugin-parse-country-list';
import typescript from '@rollup/plugin-typescript';
import { dts } from 'rollup-plugin-dts';
import type { RollupOptions } from 'rollup';

const args = argv(process.argv.slice(2));

// e.g. rollup -c --config-countries=GB,CA,US
let countries = [];
if (args.hasOwnProperty('config-countries')) {
countries = args['config-countries'].split(',');
}

const config: RollupOptions[] = [
{
input: './src/index.ts',
Expand All @@ -36,7 +26,6 @@ const config: RollupOptions[] = [
},
],
plugins: [
parseCountryList({ countries }),
json(),
url(),
babel({
Expand All @@ -54,11 +43,11 @@ const config: RollupOptions[] = [
external: ['react', 'react-dom', 'react/jsx-runtime'],
},

{
input: './src/types.d.ts',
output: [{ file: 'dist/types.d.ts', format: 'es' }],
plugins: [dts()],
},
// {
// input: './src/types.d.ts',
// output: [{ file: 'dist/types.d.ts', format: 'es' }],
// plugins: [dts()],
// },
];

export default config;
4 changes: 3 additions & 1 deletion packages/react-country-region-selector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"scripts": {
"test": "jest",
"build": "rollup --config config/rollup.config.ts --configPlugin typescript",
"dev": "rollup -c -w --config config/rollup.config.ts --configPlugin typescript"
"dev": "rollup -c -w --config config/rollup.config.ts --configPlugin typescript",
"minify-data": "node config/minify-data.js",
"prepare": "minify-data"
},
"peerDependencies": {
"react": ">=16.8.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, useMemo } from 'react';
import CountryRegionData from 'country-region-data/data.json' with { type: 'json' };
import CountryRegionData from './_data';
import { filterCountries, defaultRender } from './helpers';
import type {
CountryDropdownProps,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-country-region-selector/src/RegionDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, useMemo } from 'react';
import CountryRegionData from '../node_modules/country-region-data/data.json' with { type: 'json' };
import CountryRegionData from './_data';
import { defaultRender, filterRegions, findDuplicates } from './helpers';
import * as C from './constants';
import type {
Expand Down Expand Up @@ -63,7 +63,7 @@ export const RegionDropdown: FC<RegionDropdownProps> = ({
return [];
}

return filteredRegions[2]
return (filteredRegions[2] as string)
.split(C.REGION_LIST_DELIMITER)
.map((regionPair: string) => {
let [regionName, regionShortCode = null] = regionPair.split(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// the rollup build converts the raw data from country-region-data into a smaller format, which is why this
// imports from the dist. So run `yarn` prior to running the tests
import { RegionDropdown } from '../../dist/rcrs.es';
import { RegionDropdown } from '../RegionDropdown';
import { RegionDropdownProps } from '../types';
import { render } from '@testing-library/react';
import { fireEvent, screen } from '@testing-library/dom';
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('RegionDropdown', () => {

it('confirm default label when there are countries is "Select Region"', () => {
const select = setupTest({ country: 'Canada' });
const defaultOption = select.options[0].text;
const defaultOption = select.options![0].text;
expect(defaultOption).toBe('Select Region');
});

Expand Down
Loading

0 comments on commit 5532f3c

Please sign in to comment.