Skip to content

Commit 7465483

Browse files
committed
Enable multi-build for esm and cjs support
1 parent 4d5e97d commit 7465483

10 files changed

Lines changed: 358 additions & 414 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/*.js
44
/*.js.map
55
/build/
6+
/dist/
67
/*.log
78
/typedoc
89
!.eslintrc.js

index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,20 @@ export class Scenarios {
316316
}
317317
}
318318

319-
export const seenScenarios: Scenario[] = [];
319+
declare global {
320+
// eslint-disable-next-line no-var
321+
var scenarioTesterSeenScenarios: Scenario[];
322+
}
323+
324+
global.scenarioTesterSeenScenarios = [];
320325

321326
export class Scenario {
322327
constructor(
323328
public name: string,
324329
private callbackCreateProject: CallbackCreateProject,
325330
private mutators: CallbackMutateProject[]
326331
) {
327-
seenScenarios.push(this);
332+
global.scenarioTesterSeenScenarios.push(this);
328333
}
329334

330335
async prepare(outdir?: string): Promise<PreparedApp> {

list.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { Scenario, seenScenarios } from './index.js';
1+
import { Scenario } from './index.js';
22
import glob from 'glob';
33
import { resolve } from 'path';
44
import { format } from 'util';
5-
65
import { createRequire } from 'node:module';
7-
const require = createRequire(import.meta.url);
86

97
const { sync: globSync } = glob;
108

@@ -17,15 +15,25 @@ export interface ListParams {
1715
export async function list(params: ListParams): Promise<Scenario[]> {
1816
if (params.require) {
1917
for (let r of params.require) {
20-
await import(require.resolve(r, { paths: [process.cwd()]}));
18+
if(import.meta.url) {
19+
// @ts-ignore
20+
const require = createRequire(import.meta.url);
21+
await import(require.resolve(r, { paths: [process.cwd()]}));
22+
} else {
23+
require(require.resolve(r, { paths: [process.cwd()]}));
24+
}
2125
}
2226
}
2327
for (let pattern of params.files) {
2428
for (let file of globSync(pattern)) {
25-
await import(resolve(file));
29+
if(import.meta.url) {
30+
await import(resolve(file));
31+
} else {
32+
require(resolve(file));
33+
}
2634
}
2735
}
28-
return seenScenarios;
36+
return global.scenarioTesterSeenScenarios;
2937
}
3038

3139
export async function printList(params: ListParams) {

package.json

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
"repository": "https://github.com/ef4/scenario-tester",
55
"version": "2.1.2",
66
"bin": {
7-
"scenario-tester": "./build/cli.js"
7+
"scenario-tester": "./dist/cli.js",
8+
"scenario-tester-esm": "./dist/cli.mjs"
89
},
9-
"type": "module",
1010
"scripts": {
11-
"prepare": "tsc",
11+
"prepare": "yarn build",
12+
"build": "yarn tsup cli.ts index.ts list.ts output.ts --dts --format esm,cjs",
1213
"start": "tsc --watch",
1314
"test": "concurrently --no-color \"npm:test:*\" --names \"test:\"",
14-
"test:ts": "node --loader ts-node/esm node_modules/.bin/qunit --require ts-node/register tests/test.ts",
15+
"test:ts": "node node_modules/.bin/qunit --require ts-node/register tests/test.ts",
1516
"test:cjs": "qunit tests/test.cjs",
16-
"test:mjs": "qunit tests/test.mjs",
17+
"test:esm": "qunit tests/test.mjs",
1718
"docs": "typedoc index.ts --out typedoc/",
1819
"docs:watch": "yarn docs -- --watch",
1920
"docs:serve": "browser-sync start --server \"typedoc/\" --files \"**/*.html\"",
@@ -51,22 +52,26 @@
5152
"browser-sync": "^2.29.3",
5253
"concurrently": "^8.2.0",
5354
"eslint": "^8.46.0",
54-
"execa": "^5.0.1",
5555
"eslint-plugin-tsdoc": "^0.2.17",
56+
"execa": "^5.0.1",
5657
"lite-server": "^2.6.1",
5758
"qunit": "^2.18.0",
58-
"typedoc": "^0.24.8",
5959
"ts-node": "^10.9.1",
60+
"tsconfig-to-dual-package": "^1.2.0",
61+
"tsup": "^7.2.0",
62+
"typedoc": "^0.24.8",
6063
"typescript": "^5.1.6"
6164
},
6265
"exports": {
6366
".": {
64-
"import": "./build/index.js"
67+
"types": "./dist/index.d.ts",
68+
"import": "./dist/index.mjs",
69+
"require": "./dist/index.js"
6570
},
6671
"./*": {
67-
"types": "./build/*.d.ts",
68-
"import": "./build/*.js",
69-
"default": "./build/*.js"
72+
"types": "./dist/*.d.ts",
73+
"import": "./dist/*.mjs",
74+
"require": "./dist/*.js"
7075
}
7176
}
7277
}

tests/test.cjs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
"use strict";
2-
var __importDefault = (this && this.__importDefault) || function (mod) {
3-
return (mod && mod.__esModule) ? mod : { "default": mod };
4-
};
5-
Object.defineProperty(exports, "__esModule", { value: true });
6-
const index_js_1 = require("./index.js");
7-
const qunit_1 = __importDefault(require("qunit"));
8-
const child_process_1 = __importDefault(require("child_process"));
2+
3+
const { Scenarios } = require('scenario-tester');
4+
const qunit = require("qunit");
5+
const child_process = require("child_process");
6+
const execa = require('execa');
7+
98
function hello1(project) {
109
project.linkDependency('hello', {
11-
baseDir: './fixtures',
10+
baseDir: './tests/fixtures',
1211
resolveName: 'hello1',
1312
});
1413
}
1514
function hello2(project) {
1615
project.linkDependency('hello', {
17-
baseDir: './fixtures',
16+
baseDir: './tests/fixtures',
1817
resolveName: 'hello',
1918
});
2019
}
21-
const scenarios = index_js_1.Scenarios.fromDir('./fixtures/app').expand({
20+
const scenarios = Scenarios.fromDir('./tests/fixtures/app').expand({
2221
hello1,
2322
hello2,
2423
});
2524
scenarios.forEachScenario((scenario) => {
26-
qunit_1.default.module(scenario.name, (hooks) => {
25+
qunit.module(scenario.name, (hooks) => {
2726
hooks.before(async function () {
2827
this.app = await scenario.prepare();
2928
});
30-
qunit_1.default.test('yarn test', async function (assert) {
29+
qunit.test('yarn test', async function (assert) {
3130
const result = await this.app.execute('yarn --silent test');
3231
assert.equal(result.stdout, `TAP version 13
3332
ok 1 project > createHello
@@ -38,25 +37,28 @@ ok 1 project > createHello
3837
# fail 0
3938
`);
4039
});
41-
qunit_1.default.test('yarn bin inside app', async function (assert) {
40+
qunit.test('yarn bin inside app', async function (assert) {
4241
let result = await this.app.execute('yarn --silent bin');
4342
const yarnBin = result.stdout.trimRight();
4443
assert.ok(yarnBin.startsWith(this.app.dir));
4544
result = await this.app.execute('yarn --silent exec which qunit');
4645
assert.ok(result.stdout.startsWith(yarnBin));
4746
});
48-
qunit_1.default.test('check scenario', async function (assert) {
47+
qunit.test('check scenario', async function (assert) {
4948
let result = await this.app.execute(`node -p 'require("./index").polyfilled'`);
5049
assert.equal(result.stdout.trim(), ('hello1' === scenario.name).toString());
5150
});
5251
});
5352
});
54-
qunit_1.default.module('cli', () => {
55-
qunit_1.default.test('list', (assert) => {
56-
assert.deepEqual(child_process_1.default
57-
.execFileSync(process.execPath, ['cli.js', 'list', '--files', 'test.js', '--matrix'], { encoding: 'utf8' })
58-
.trimRight()
59-
.split('\n'), ['hello1', 'hello2']);
53+
qunit.module('cli', () => {
54+
qunit.test('list', async (assert) => {
55+
const result = await execa('npx', ['scenario-tester', 'list', '--files', 'tests/test.cjs', '--matrix'])
56+
57+
const { stdout } = result;
58+
assert.deepEqual(
59+
stdout.split('\n'),
60+
['hello1', 'hello2']
61+
);
6062
});
6163
});
62-
//# sourceMappingURL=test.js.map
64+
//# sourceMappingURL=test.js.map

tests/test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ok 1 project > createHello
7474

7575
Qunit.module('cli', () => {
7676
Qunit.test('list', async (assert) => {
77-
const result = await execa('npx', ['.', 'list', '--files', 'tests/test.mjs', '--matrix'])
77+
const result = await execa('npx', ['scenario-tester-esm', 'list', '--files', 'tests/test.mjs', '--matrix'])
7878

7979
const { stdout } = result;
8080
assert.deepEqual(

tests/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Project } from 'fixturify-project';
2-
import { Scenarios } from '../index.js';
3-
import type { PreparedApp } from '../index.js';
2+
import { Scenarios } from '../';
3+
import type { PreparedApp } from '../';
44
import Qunit from 'qunit';
55
import execa from 'execa';
66

@@ -79,7 +79,7 @@ Qunit.module('cli', () => {
7979
Qunit.test('list', async (assert) => {
8080
// I tried to test this using the ts file dirrectly but I couldn't get ts-node/require to work correctly
8181
// so I'm just testing against the compiled esm output
82-
const result = await execa('npx', ['.', 'list', '--files', './build/tests/test.js', '--matrix'])
82+
const result = await execa('npx', ['scenario-tester', 'list', '--loader', 'ts-node/esm', '--require', 'ts-node/register', '--files', './tests/test.ts', '--matrix'])
8383

8484
const { stdout } = result;
8585
assert.deepEqual(

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"experimentalSpecifierResolution": "node"
55
},
66
"compilerOptions": {
7-
"outDir": "./build/",
87
"target": "ES2020",
98
"module": "ES2020",
109
"moduleResolution": "node",

tsconfig.types.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)