Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transforms for TypeScript #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dantman
Copy link

@dantman dantman commented May 20, 2020

TypeScript's transforms are a bit different from Babel's resulting in globalize-compiler not always working on pre-compiled TypeScript code.

The following basic example will be transpiled by TypeScript as such:

import Globalize from 'globalize';

Globalize.formatDate(new Date());
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var globalize_1 = __importDefault(require("globalize"));
globalize_1["default"].formatDate(new Date());

Sometimes you cannot configure aliases to override globalize to the globalize-runtime so it's reasonable to require globalize-runtime directly. In TypeScript this would be transpiled as such.

import Globalize from 'globalize/dist/globalize-runtime';

Globalize.formatDate(new Date());
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var globalize_runtime_1 = __importDefault(require("globalize/dist/globalize-runtime"));
globalize_runtime_1["default"].formatDate(new Date());

@rxaviers
Copy link
Member

LGTM. Please how can we test these changes?

@dantman
Copy link
Author

dantman commented May 26, 2020

globalize-compiler didn't have tests for the Babel behaviour so I wasn't sure how to add tests for the TypeScript behaviour.

To test yourself you could use node bin/globalize-compiler.js -l en -o test-out.js test.js and put the transpiled JS code I've provided into test.js.

If you run that on the stable version, it won't find any formatters and will error out. If you try it on the PR version it will find the transpiled formatter and output like normal.

If you wanted you could also validate the TypeScript output I provided. You should just need to run npx -p typescript tsc --esModuleInterop test.ts with the example TypeScript code in test.ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants