Skip to content

Commit

Permalink
chore(examples): add "typescript.module" demo;
Browse files Browse the repository at this point in the history
- Similar to "typescript" example, but uses `"type": "module"` and source files only include ".js" files with ESM syntax. This matches the scenario(s) presented in #123

Also includes two approaches for solving this problem; see npm "scripts" section.

Closes #123
  • Loading branch information
lukeed committed Oct 5, 2021
1 parent 44a437d commit 75b6573
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/typescript.module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
3 changes: 3 additions & 0 deletions examples/typescript.module/loadr.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// NOTE: used for "test:alt" script only
// @see https://github.com/lukeed/loadr
export const loaders = ['tsm'];
13 changes: 13 additions & 0 deletions examples/typescript.module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"type": "module",
"scripts": {
"test": "tsm node_modules/uvu/bin.js tests",
"test:alt": "loadr -- uvu tests"
},
"devDependencies": {
"loadr": "^0.1.1",
"tsm": "^2.0.0",
"uvu": "^0.5.1"
}
}
17 changes: 17 additions & 0 deletions examples/typescript.module/src/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {number} a
* @param {number} b
*/
export const sum = (a, b) => a + b;

/**
* @param {number} a
* @param {number} b
*/
export const div = (a, b) => a / b;

/**
* @param {number} a
* @param {number} b
*/
export const mod = (a, b) => a % b;
13 changes: 13 additions & 0 deletions examples/typescript.module/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {string} str
*/
export function capitalize(str) {
return str[0].toUpperCase() + str.substring(1);
}

/**
* @param {string} str
*/
export function dashify(str) {
return str.replace(/([a-zA-Z])(?=[A-Z\d])/g, '$1-').toLowerCase();
}
26 changes: 26 additions & 0 deletions examples/typescript.module/tests/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import * as math from '../src/math.js';

test('sum', () => {
assert.type(math.sum, 'function');
assert.is(math.sum(1, 2), 3);
assert.is(math.sum(-1, -2), -3);
assert.is(math.sum(-1, 1), 0);
});

test('div', () => {
assert.type(math.div, 'function');
assert.is(math.div(1, 2), 0.5);
assert.is(math.div(-1, -2), 0.5);
assert.is(math.div(-1, 1), -1);
});

test('mod', () => {
assert.type(math.mod, 'function');
assert.is(math.mod(1, 2), 1);
assert.is(math.mod(-3, -2), -1);
assert.is(math.mod(7, 4), 3);
});

test.run();
18 changes: 18 additions & 0 deletions examples/typescript.module/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import * as utils from '../src/utils.js';

test('capitalize', () => {
assert.type(utils.capitalize, 'function');
assert.is(utils.capitalize('hello'), 'Hello');
assert.is(utils.capitalize('foo bar'), 'Foo bar');
});

test('dashify', () => {
assert.type(utils.dashify, 'function');
assert.is(utils.dashify('fooBar'), 'foo-bar');
assert.is(utils.dashify('FooBar'), 'foo-bar');
assert.is(utils.dashify('foobar'), 'foobar');
});

test.run();
21 changes: 21 additions & 0 deletions examples/typescript.module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"target": "esnext",
"noImplicitAny": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"alwaysStrict": true,
"module": "esnext",
"checkJs": true,
"noEmit": true
},
"include": [
"src",
"tests"
],
"exclude": [
"node_modules"
]
}

0 comments on commit 75b6573

Please sign in to comment.