Skip to content

Commit

Permalink
refactor all internal module imports to use fully-specified paths (wi…
Browse files Browse the repository at this point in the history
…th extensions)

- use the '.js' (*not* '.ts') extension to force correct fully specified `tsc` output

# Discussion

Browsers/ESM/Deno require fully specified import paths/URLs and don't perform any
extension substitution "magic" when searching for the import file. `tsc` does not
and will likely *never* change the emitted JS code [^1], including rewriting import
paths/URLs. So, the for `tsc` compilation to produce working code these targets, a
fully specified path/URL must be used in the '.ts' source code.

As of TypeScript 2.0, import paths using the '.js' extension as a replacement for
'.ts' are resolved correctly in the compiled code [^2]. The `rollup` bundler is also
noted to support the same construction [^3].

So, the most compatible way construction is to use fully specified import paths which
replace the trailing '.ts' with '.js'. Odd, yes, but it's the best working practice.

# refs

[1]: microsoft/TypeScript#16577 (comment)
[2]: microsoft/TypeScript#16577 (comment)
[3]: microsoft/TypeScript#16577 (comment)
  • Loading branch information
rivy committed Feb 8, 2021
1 parent ff5161b commit ae017d4
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:ignore maint rivy
import { Adapt, OSPaths } from './lib/OSPaths';
import { adapter } from './platform-adapters/node';
import { Adapt, OSPaths } from './lib/OSPaths.js';
import { adapter } from './platform-adapters/node.js';

const default_: OSPaths = Adapt(adapter).OSPaths;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/OSPaths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey

import { Platform } from '../platform-adapters/_base';
import { Platform } from '../platform-adapters/_base.js';

/** Determine common OS/platform paths (home, temp, ...) */
type OSPaths = {
Expand Down
2 changes: 1 addition & 1 deletion src/platform-adapters/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as os from 'os';
import * as path from 'path';

import { Platform } from './_base';
import { Platform } from './_base.js';

export const adapter: Platform.Adapter = {
env: {
Expand Down

0 comments on commit ae017d4

Please sign in to comment.