Skip to content

Commit

Permalink
chore: change jest-transform to be CJS module
Browse files Browse the repository at this point in the history
  • Loading branch information
uhyo committed Feb 18, 2024
1 parent 9b4d246 commit 13f78fe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 43 deletions.
8 changes: 4 additions & 4 deletions packages/jest-transform/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ const config = {
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)",
// ],
testMatch: [
"**/__tests__/**/*.(m)[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)",
],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
Expand Down
1 change: 0 additions & 1 deletion packages/jest-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"url": "https://github.com/uhyo/nitrogql.git",
"directory": "packages/jest-transform"
},
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
91 changes: 53 additions & 38 deletions packages/jest-transform/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { readFileSync } from "node:fs";
import type { SyncTransformer, TransformedSource } from "@jest/transform";
import { executeConfigFileSync } from "@nitrogql/core";
import { init } from "@nitrogql/loader-core";
import type {
SyncTransformer,
TransformedSource,
TransformerCreator,
} from "@jest/transform";

const { initiateTask, getLog } = await init();

let lastLoadedConfigPath: string | undefined = undefined;
// Note: this package uses CommonJS because jest-chain-transform does not support loading
// ESM modules.
// See: https://github.com/anc95/jest-chain-transform/issues/11

export type TransformerConfig = {
/**
Expand All @@ -14,43 +16,56 @@ export type TransformerConfig = {
configFile?: string;
};

const transformer: SyncTransformer<TransformerConfig> = {
process(sourceText, sourcePath, options): TransformedSource {
const configFile = options.transformerConfig.configFile;
const task = initiateTask(sourcePath, sourceText);

if (lastLoadedConfigPath !== configFile && configFile) {
const configFileSource = configFileIsJS(configFile)
? executeConfigFileSync(configFile)
: readFileSync(configFile, "utf-8");
task.loadConfig(configFileSource);
}
lastLoadedConfigPath = configFile;

while (true) {
const status = task.status();
switch (status.status) {
case "fileRequired": {
const requiredFiles = status.files;
for (const requiredFile of requiredFiles) {
const requiredFileSource = readFileSync(requiredFile, "utf-8");
task.supplyFile(requiredFile, requiredFileSource);
const createTransformer: TransformerCreator<
SyncTransformer<TransformerConfig>,
TransformerConfig
> = async () => {
const { init } = await import("@nitrogql/loader-core");
const { executeConfigFileSync } = await import("@nitrogql/core");
const { initiateTask, getLog } = await init();

let lastLoadedConfigPath: string | undefined = undefined;
const transformer: SyncTransformer<TransformerConfig> = {
process(sourceText, sourcePath, options): TransformedSource {
const configFile = options.transformerConfig.configFile;
const task = initiateTask(sourcePath, sourceText);

if (lastLoadedConfigPath !== configFile && configFile) {
const configFileSource = configFileIsJS(configFile)
? executeConfigFileSync(configFile)
: readFileSync(configFile, "utf-8");
task.loadConfig(configFileSource);
}
lastLoadedConfigPath = configFile;

while (true) {
const status = task.status();
switch (status.status) {
case "fileRequired": {
const requiredFiles = status.files;
for (const requiredFile of requiredFiles) {
const requiredFileSource = readFileSync(requiredFile, "utf-8");
task.supplyFile(requiredFile, requiredFileSource);
}
break;
}
case "ready": {
const result = task.emit();
task.free();
return {
code: result,
};
}
break;
}
case "ready": {
const result = task.emit();
task.free();
return {
code: result,
};
}
}
}
},
},
};
return transformer;
};

export default transformer;
export default {
createTransformer,
};

function configFileIsJS(configFile: string) {
return /\.[cm]?[jt]s$/.test(configFile);
Expand Down

0 comments on commit 13f78fe

Please sign in to comment.