Skip to content

Commit 32cdc11

Browse files
Publish v32.0.3 (#66)
* fix loading of dynamic modules in windows, export map, .cjs and .mjs support
1 parent a3ea8da commit 32cdc11

File tree

9 files changed

+148
-32
lines changed

9 files changed

+148
-32
lines changed

packages/cli/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './index.cjs';

packages/cli/package.json

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@ag-grid-devtools/cli",
4-
"version": "32.0.2",
4+
"version": "32.0.3",
55
"license": "MIT",
66
"description": "AG Grid developer toolkit",
77
"author": "AG Grid <[email protected]>",
@@ -33,7 +33,31 @@
3333
},
3434
"pkg": {
3535
"main": "./index.cjs",
36-
"bin": "./index.cjs"
36+
"bin": "./index.cjs",
37+
"exports": {
38+
".": {
39+
"import": "./index.mjs",
40+
"require": "./index.cjs",
41+
"default": "./index.mjs"
42+
},
43+
"./index": {
44+
"import": "./index.mjs",
45+
"require": "./index.cjs",
46+
"default": "./index.mjs"
47+
},
48+
"./index.js": "./index.mjs",
49+
"./index.mjs": "./index.mjs",
50+
"./index.cjs": "./index.cjs",
51+
"./user-config": {
52+
"import": "./user-config.mjs",
53+
"require": "./user-config.cjs",
54+
"default": "./user-config.mjs"
55+
},
56+
"./user-config.js": "./user-config.mjs",
57+
"./user-config.mjs": "./user-config.mjs",
58+
"./user-config.cjs": "./user-config.cjs",
59+
"./package.json": "./package.json"
60+
}
3761
},
3862
"types": "./index.d.ts",
3963
"bundleDependencies": [
@@ -58,7 +82,8 @@
5882
"graceful-fs": "4.2.11",
5983
"ignore": "5.3.1",
6084
"semver": "7.6.2",
61-
"vite-plugin-dts": "3.9.1"
85+
"vite-plugin-dts": "3.9.1",
86+
"vite-plugin-static-copy": "1.0.6"
6287
},
6388
"peerDependencies": {
6489
"eslint": "^8",

packages/cli/src/cli.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ export async function cli(args: Array<string>, cli: CliOptions): Promise<void> {
7171
throw null;
7272
}
7373

74-
// Add typescript support by loading tsx
75-
try {
76-
dynamicRequire.require('tsx/cjs', import.meta);
77-
} catch {
78-
// ignore error if tsx could not be loaded
79-
}
74+
dynamicRequire.initialize();
8075

8176
const task = match(options.command, {
8277
Migrate: ({ args }) => migrate(args, cli),

packages/cli/src/commands/migrate.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -446,29 +446,29 @@ async function migrate(
446446
// Create a worker pool to run the codemods in parallel
447447
let scriptPath: string | URL = dynamicRequire.resolve(WORKER_PATH, import.meta);
448448

449-
const resolvedCodemodPaths = codemodPaths.map((codemodPath) =>
450-
dynamicRequire.resolve(codemodPath, import.meta),
451-
);
449+
// This will be true if we are trying to run from the codemod repo source code using tsx or vitest
450+
const isTsSourceCodeWorker = scriptPath.endsWith('.ts');
452451

453452
const config: WorkerOptions = {
454453
// Pass the list of codemod paths to the worker via workerData
455454
workerData: {
456-
codemodPaths: resolvedCodemodPaths,
455+
codemodPaths: isTsSourceCodeWorker
456+
? codemodPaths.map((codemodPath) => dynamicRequire.resolve(codemodPath, import.meta))
457+
: codemodPaths,
457458
userConfigPath,
458459
},
459460
env: process.env,
460461
argv: [scriptPath],
461-
eval: true,
462+
eval: isTsSourceCodeWorker,
462463
};
463464

465+
const workerCodeOrPath = isTsSourceCodeWorker
466+
? `try { require("tsx/cjs"); } catch (_) {} require(${JSON.stringify(scriptPath)});`
467+
: scriptPath;
468+
464469
const workers = Array.from(
465470
{ length: numWorkers },
466-
() =>
467-
new Worker(
468-
// Try to add typescript support by loading tsx and load the worker script
469-
`try { require("tsx/cjs"); } catch (_) {} require(${JSON.stringify(scriptPath)});`,
470-
config,
471-
),
471+
() => new Worker(workerCodeOrPath, config),
472472
);
473473
const workerPool = new WorkerTaskQueue<CodemodTaskInput, CodemodTaskWorkerResult>(workers);
474474
return executeCodemodMultiThreaded(workerPool, inputFilePaths, {

packages/cli/user-config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './user-config.cjs';

packages/cli/vite.config.mts

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { resolve } from 'path';
22
import { defineConfig, mergeConfig } from 'vite';
33
import dts from 'vite-plugin-dts';
4+
import { viteStaticCopy } from 'vite-plugin-static-copy';
45

56
import base from '../build-config/templates/vite/cli.vite.config';
67

@@ -29,6 +30,12 @@ export default mergeConfig(
2930
bundledPackages: ['@ag-grid-devtools/types'],
3031
exclude: ['node_modules/**', '*.config.mts', '**/*.test.ts', 'package.json', 'index.ts'],
3132
}),
33+
viteStaticCopy({
34+
targets: [
35+
{ src: 'index.mjs', dest: '.' },
36+
{ src: 'user-config.mjs', dest: '.' },
37+
],
38+
}),
3239
],
3340
}),
3441
);

packages/utils/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"@ag-grid-devtools/types": "workspace:*"
4444
},
4545
"devDependencies": {
46-
"@ag-grid-devtools/build-config": "workspace:*"
46+
"@ag-grid-devtools/build-config": "workspace:*",
47+
"@types/app-module-path": "^2.2.2",
48+
"app-module-path": "2.2.0"
4749
},
4850
"peerDependencies": {
4951
"eslint": "^8",

packages/utils/src/module.ts

+69-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,68 @@
1-
import { createRequire } from 'node:module';
1+
import { fileURLToPath } from 'node:url';
2+
import { createRequire, Module } from 'node:module';
3+
import { existsSync } from 'node:fs';
4+
import { addPath as addNodeModulePath } from 'app-module-path';
5+
import { dirname, resolve as pathResolve, join as pathJoin } from 'node:path';
6+
7+
let initialized = false;
8+
9+
const thisDir = pathResolve(
10+
import.meta.url
11+
? dirname(fileURLToPath(import.meta.url))
12+
: typeof __dirname !== 'undefined'
13+
? __dirname
14+
: process.cwd(),
15+
);
216

317
export const dynamicRequire = {
4-
resolve(path: string, meta: ImportMeta): string {
5-
if (meta.url === undefined && typeof require !== undefined) {
6-
// import.meta not available, maybe running a ts file with tsx? use default cjs require
7-
return require.resolve(path);
18+
initialize() {
19+
if (initialized) {
20+
return;
21+
}
22+
initialized = true;
23+
24+
// Register node_modules paths
25+
26+
let currentDir = thisDir;
27+
while (currentDir) {
28+
if (currentDir.endsWith('node_modules')) {
29+
tryAddNodeModulePath(currentDir);
30+
break;
31+
}
32+
tryAddNodeModulePath(pathJoin(currentDir, 'node_modules'));
33+
let parentDir = dirname(currentDir);
34+
if (parentDir === currentDir) {
35+
break;
36+
}
37+
currentDir = parentDir;
38+
}
39+
40+
// Add typescript support by loading tsx
41+
try {
42+
dynamicRequire.require('tsx/cjs', import.meta);
43+
} catch {
44+
// ignore error if tsx could not be loaded
45+
}
46+
47+
// Register .cjs and .cts extensions
48+
49+
const exts = (Module as any)._extensions;
50+
if (exts && !('.cjs' in exts)) {
51+
exts['.cjs'] = exts['.js'];
52+
}
53+
if (exts && !('.cts' in exts) && '.ts' in exts) {
54+
exts['.cts'] = exts['.ts'];
855
}
9-
return createRequire(meta.url).resolve(path);
56+
},
57+
58+
resolve(path: string, meta: ImportMeta): string {
59+
dynamicRequire.initialize();
60+
return createRequire(meta.url || pathResolve(thisDir, 'index.js')).resolve(path);
1061
},
1162

1263
require<T = unknown>(path: string, meta: ImportMeta): T {
13-
if (meta.url === undefined && typeof require !== undefined) {
14-
// import.meta not available, maybe running a ts file with tsx? use default cjs require
15-
return require(path);
16-
}
17-
return createRequire(meta.url)(path);
64+
dynamicRequire.initialize();
65+
return createRequire(meta.url || pathResolve(thisDir, 'index.js'))(path);
1866
},
1967

2068
/** Like require, but supports modules with a default export transpiled to cjs */
@@ -32,3 +80,13 @@ export const dynamicRequire = {
3280
return required;
3381
},
3482
};
83+
84+
function tryAddNodeModulePath(nodeModulesPath: string) {
85+
try {
86+
if (existsSync(nodeModulesPath)) {
87+
addNodeModulePath(nodeModulesPath);
88+
}
89+
} catch {
90+
// ignore error
91+
}
92+
}

pnpm-lock.yaml

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)