Skip to content

Commit 85705fe

Browse files
committed
Update implementation to work properly with ES modules
1 parent ab716c8 commit 85705fe

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
lines changed

src/bin.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from 'path';
55
import { createTempDir, TempDir } from 'broccoli-test-helper';
66
import slash from 'slash';
77

8-
const COMPILED_BIN_PATH = path.join(__dirname, '../lib/bin.js');
8+
const COMPILED_BIN_PATH = (new URL('./bin.js', import.meta.url)).pathname;
99
if (!existsSync(COMPILED_BIN_PATH)) {
1010
throw new Error('Missing compiled output, run `yarn build`!');
1111
}

src/bin.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#!/usr/bin/env node
22

33
import * as os from 'os';
4+
import { readFileSync } from 'fs';
45
import { program } from 'commander';
5-
import run from './runner';
6+
import run from './runner.js';
67

8+
const version = JSON.parse(
9+
readFileSync(
10+
new URL('../package.json', import.meta.url),
11+
{ encoding: 'utf-8' }
12+
)
13+
);
714
program
8-
.version(require('../package').version)
15+
.version(version)
916
.usage('<files> -t transform-plugin.js')
1017
.option(
1118
'-t, --transform <file>',

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { traverse, builders, Walker, print as glimmerPrint } from '@glimmer/syntax';
22
import type { ASTv1 as AST, NodeVisitor } from '@glimmer/syntax';
3-
import ParseResult, { NodeInfo } from './parse-result';
3+
import ParseResult, { NodeInfo } from './parse-result.js';
44

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

145145
export { builders, traverse } from '@glimmer/syntax';
146-
export { sourceForLoc } from './utils';
146+
export { sourceForLoc } from './utils.js';

src/parse-result.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { preprocess, builders, print as _print, traverse, ASTv1 as AST } from '@glimmer/syntax';
2-
import { getLines, sortByLoc, sourceForLoc } from './utils';
2+
import { getLines, sortByLoc, sourceForLoc } from './utils.js';
33

44
const leadingWhitespace = /(^\s+)/;
55
const attrNodeParts = /(^[^=]+)(\s+)?(=)?(\s+)?(['"])?(\S+)?/;

src/runner.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as http from 'http';
22
import * as https from 'https';
33
import { writeFileSync } from 'fs';
44
import { resolve } from 'path';
5-
import colors from 'colors/safe';
5+
import colors from 'colors/safe.js';
66
import slash from 'slash';
77
import globby from 'globby';
88
import ora from 'ora';
@@ -12,6 +12,8 @@ import workerpool from 'workerpool';
1212

1313
tmp.setGracefulCleanup();
1414

15+
const WORKER_URL = new URL('./worker.js', import.meta.url);
16+
1517
class NoFilesError extends Error {}
1618

1719
class SilentLogger {
@@ -219,7 +221,7 @@ async function spawnWorkers(
219221

220222
logger.spin('Processed 0 files');
221223

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

224226
let i = 0;
225227
const worker = (queue as any).async.asyncify(async (file: string) => {

src/worker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import workerpool from 'workerpool';
3-
import { transform } from './index';
3+
import { transform } from './index.js';
44
import type { TransformPluginBuilder } from './index';
55

66
interface TransformResult {
@@ -14,7 +14,7 @@ interface TransformOptions {
1414
}
1515

1616
async function run(transformPath: string, filePath: string, options: TransformOptions) {
17-
const module = require(transformPath);
17+
const module = await import(transformPath);
1818
const plugin: TransformPluginBuilder =
1919
typeof module.default === 'function' ? module.default : module;
2020

0 commit comments

Comments
 (0)