Skip to content

Commit

Permalink
Update implementation to work properly with ES modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Nov 3, 2021
1 parent ab716c8 commit 85705fe
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'path';
import { createTempDir, TempDir } from 'broccoli-test-helper';
import slash from 'slash';

const COMPILED_BIN_PATH = path.join(__dirname, '../lib/bin.js');
const COMPILED_BIN_PATH = (new URL('./bin.js', import.meta.url)).pathname;
if (!existsSync(COMPILED_BIN_PATH)) {
throw new Error('Missing compiled output, run `yarn build`!');
}
Expand Down
11 changes: 9 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/usr/bin/env node

import * as os from 'os';
import { readFileSync } from 'fs';
import { program } from 'commander';
import run from './runner';
import run from './runner.js';

const version = JSON.parse(
readFileSync(
new URL('../package.json', import.meta.url),
{ encoding: 'utf-8' }
)
);
program
.version(require('../package').version)
.version(version)
.usage('<files> -t transform-plugin.js')
.option(
'-t, --transform <file>',
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { traverse, builders, Walker, print as glimmerPrint } from '@glimmer/syntax';
import type { ASTv1 as AST, NodeVisitor } from '@glimmer/syntax';
import ParseResult, { NodeInfo } from './parse-result';
import ParseResult, { NodeInfo } from './parse-result.js';

const PARSE_RESULT_FOR = new WeakMap<AST.Node, ParseResult>();
const NODE_INFO = new WeakMap<AST.Node, NodeInfo>();
Expand Down Expand Up @@ -143,4 +143,4 @@ export function transform(
export type { AST, NodeVisitor } from '@glimmer/syntax';

export { builders, traverse } from '@glimmer/syntax';
export { sourceForLoc } from './utils';
export { sourceForLoc } from './utils.js';
2 changes: 1 addition & 1 deletion src/parse-result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { preprocess, builders, print as _print, traverse, ASTv1 as AST } from '@glimmer/syntax';
import { getLines, sortByLoc, sourceForLoc } from './utils';
import { getLines, sortByLoc, sourceForLoc } from './utils.js';

const leadingWhitespace = /(^\s+)/;
const attrNodeParts = /(^[^=]+)(\s+)?(=)?(\s+)?(['"])?(\S+)?/;
Expand Down
6 changes: 4 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as http from 'http';
import * as https from 'https';
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import colors from 'colors/safe';
import colors from 'colors/safe.js';
import slash from 'slash';
import globby from 'globby';
import ora from 'ora';
Expand All @@ -12,6 +12,8 @@ import workerpool from 'workerpool';

tmp.setGracefulCleanup();

const WORKER_URL = new URL('./worker.js', import.meta.url);

class NoFilesError extends Error {}

class SilentLogger {
Expand Down Expand Up @@ -219,7 +221,7 @@ async function spawnWorkers(

logger.spin('Processed 0 files');

const pool = workerpool.pool(require.resolve('./worker'), { maxWorkers: cpus });
const pool = workerpool.pool(WORKER_URL.pathname, { maxWorkers: cpus });

let i = 0;
const worker = (queue as any).async.asyncify(async (file: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import workerpool from 'workerpool';
import { transform } from './index';
import { transform } from './index.js';
import type { TransformPluginBuilder } from './index';

interface TransformResult {
Expand All @@ -14,7 +14,7 @@ interface TransformOptions {
}

async function run(transformPath: string, filePath: string, options: TransformOptions) {
const module = require(transformPath);
const module = await import(transformPath);
const plugin: TransformPluginBuilder =
typeof module.default === 'function' ? module.default : module;

Expand Down

0 comments on commit 85705fe

Please sign in to comment.