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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure Windows platform support #9

Merged
merged 7 commits into from Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions .github/workflows/ci.yml
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
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
@@ -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
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