Skip to content

Commit

Permalink
fix: ensure Windows support (#9)
Browse files Browse the repository at this point in the history
* fix(bin): use relative file in argv loader

* fix(loader): use "file:///" prefix for config file

* chore(loader): use named `url` imports

* fix(require): ensure valid file paths after URL work

* chore(bin): include `node bin` tests

* chore(bin): include windows in test matrix

* chore(bin): revert to npm :(

- don't want to deal with multiple "install / env" steps
  • Loading branch information
lukeed authored Oct 10, 2021
1 parent fbbb29f commit acb1071
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
21 changes: 13 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ on:

jobs:
test:
name: Node.js v${{ matrix.nodejs }}
runs-on: ubuntu-latest
name: Node.js v${{ matrix.nodejs }} (${{ matrix.os }})
runs-on: ${{ matrix.os }}
timeout-minutes: 3
strategy:
matrix:
nodejs: [12, 14, 16]
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.nodejs }}

- name: (env) pnpm
run: curl -L https://raw.githubusercontent.com/pnpm/self-installer/master/install.js | node

- name: Install
run: pnpm install
run: npm install

- name: Compiles
run: pnpm run build
run: npm run build

- name: Type Checks
run: pnpm run types
run: npm run types

- name: Tests <~ ESM
run: node --loader ./loader.mjs test/index.mjs
Expand All @@ -44,3 +43,9 @@ jobs:

- name: Tests <~ CommonJS <~ TypeScript
run: node -r ./require.js test/config/index.ts --tsmconfig test/config/tsm.js

- name: Tests <~ CLI
run: node bin.js test/index.mjs

- name: Tests <~ CLI <~ TypeScript
run: node bin.js test/config/index.ts --tsmconfig test/config/tsm.js
6 changes: 2 additions & 4 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ if (argv.includes('-v') || argv.includes('--version')) {
process.exit(0);
}

let file = require('path').join(__dirname, 'loader.mjs');
require('child_process').spawn('node', ['--loader', file, ...argv], {
stdio: 'inherit'
}).on('exit', process.exit);
argv = ['--loader', './loader.mjs', ...argv];
require('child_process').spawn('node', argv, { stdio: 'inherit' }).on('exit', process.exit);
10 changes: 5 additions & 5 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as url from 'url';
import { existsSync } from 'fs';
import { fileURLToPath, URL } from 'url';
import * as tsm from './utils.js';

import type { Config, Extension, Options } from 'tsm/config';
Expand All @@ -9,7 +9,7 @@ let config: Config;
let esbuild: typeof import('esbuild');

let env = (tsm as TSM).$defaults('esm');
let setup = env.file && import(env.file);
let setup = env.file && import('file:///' + env.file);

type Promisable<T> = Promise<T> | T;
type Source = string | SharedArrayBuffer | Uint8Array;
Expand Down Expand Up @@ -52,18 +52,18 @@ async function toOptions(uri: string): Promise<Options|void> {
}

function check(fileurl: string): string | void {
let tmp = url.fileURLToPath(fileurl);
let tmp = fileURLToPath(fileurl);
if (existsSync(tmp)) return fileurl;
}

const root = url.pathToFileURL(process.cwd() + '/');
const root = new URL('file:///' + process.cwd() + '/');
export const resolve: Resolve = async function (ident, context, fallback) {
// ignore "prefix:" and non-relative identifiers
if (/^\w+\:?/.test(ident)) return fallback(ident, context, fallback);

let match: RegExpExecArray | null;
let idx: number, ext: Extension, path: string | void;
let output = new url.URL(ident, context.parentURL || root);
let output = new URL(ident, context.parentURL || root);

// source ident includes extension
if (match = EXTN.exec(output.href)) {
Expand Down
8 changes: 4 additions & 4 deletions src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let config: Config = (tsm as TSM).$finalize(env, uconf);

declare const $$req: NodeJS.Require;
const tsrequire = 'var $$req=require("module").createRequire(__filename);require=(' + function () {
let { existsSync } = $$req('fs');
let { URL, pathToFileURL } = $$req('url');
let { existsSync } = $$req('fs') as typeof import('fs');
let $url = $$req('url') as typeof import('url');

return new Proxy(require, {
// NOTE: only here if source is TS
Expand All @@ -34,8 +34,8 @@ const tsrequire = 'var $$req=require("module").createRequire(__filename);require
let match = /\.([mc])?js(?=\?|$)/.exec(ident);
if (match == null) return $$req(ident);

let base = pathToFileURL(__filename) as import('url').URL;
let file = new URL(ident, base).pathname as string;
let base = $url.pathToFileURL(__filename);
let file = $url.fileURLToPath(new $url.URL(ident, base));
if (existsSync(file)) return $$req(ident);

// ?js -> ?ts file
Expand Down

0 comments on commit acb1071

Please sign in to comment.