globalize-compiler

raw JSON →
1.1.1 verified Fri May 01 auth: no javascript

globalize-compiler is a build tool that statically extracts Global.js formatters and parsers from source code and compiles them into a standalone JavaScript bundle. Version 1.1.1 is the current stable release with no recent updates. It is used alongside Globalize v1.5.0+ and CLDR data (>=25). Key differentiators: (1) eliminates runtime message/CLDR data dependencies for performance, (2) provides both CLI and programmatic API (compile, extract, compileExtracts), (3) supports custom templates for output. Primarily for Node.js build pipelines; not meant for direct browser use.

error Error: Cannot find module 'globalize-compiler'
cause Package not installed in node_modules.
fix
Run npm install globalize-compiler --save-dev.
error TypeError: globalizeCompiler.compile is not a function
cause CommonJS require returns an object with methods; destructure incorrectly.
fix
Use const globalizeCompiler = require('globalize-compiler'); then globalizeCompiler.compile(...) or const { compile } = require('globalize-compiler');.
error Error: No CLDR data provided for locale en
cause Missing CLDR data when using compiled bundle.
fix
Ensure you load CLDR data via -c flag or provide it in the API call.
gotcha Requires peer dependencies cldr-data, globalize, and iana-tz-data to be installed. Missing peer deps cause runtime errors.
fix Run `npm install cldr-data globalize iana-tz-data --save-dev`.
gotcha The `extract` function only recognizes static calls to Globalize methods; dynamic or computed method names will not be extracted.
fix Ensure formatter/parser creation calls use literal method names (e.g. `Globalize.formatNumber` not `Globalize['format'+'Number']`).
breaking Version 1.0.0 changed the API: `compile` now expects an array or object of formatters/parsers instead of the previous argument structure.
fix Update code to pass formatters/parsers as an array or object; see migration guide.
deprecated The `--cldr` CLI flag is deprecated in favor of loading CLDR data via the API or separate data files.
fix Use the programmatic API to pass CLDR data directly.
gotcha CLI does not support loading CLDR data from multiple files; only one file via `-c` flag.
fix Combine CLDR data into a single JSON file before passing to CLI.
npm install globalize-compiler
yarn add globalize-compiler
pnpm add globalize-compiler

Shows how to extract formatters from a source file and compile them into a JavaScript bundle using globalize-compiler.

import { compile, extract } from 'globalize-compiler';
import Globalize from 'globalize';

// Extract formatters from a source file
const extractFn = extract('path/to/your/source.js');

// Create Globalize instance with locale data
// (In real usage you'd load CLDR data)
const globalize = new Globalize('en');

// Get formatters/parsers by calling extract function
const formattersAndParsers = extractFn(globalize);

// Compile them into a bundle string
const bundle = compile(formattersAndParsers, { template: null });

console.log(bundle); // Output JavaScript bundle