Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@checkdigit/typescript-config",
"version": "10.1.1",
"version": "10.2.0",
"description": "Check Digit standard Typescript configuration",
"homepage": "https://github.com/checkdigit/typescript-config#readme",
"bugs": {
Expand Down
43 changes: 41 additions & 2 deletions src/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import compile from './compile.ts';

const commonJsCompatabilityBanner = `import { createRequire as __createRequire } from "node:module";
import { fileURLToPath as __fileURLToPath } from "node:url";
import { default as __path } from "node:path";
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __path.dirname(__filename);
const require = __createRequire(import.meta.url);`;

const dirnameInject = `import path from "node:path";
var __dirname = path.dirname(__filename);`;

const singleModule = {
[`index.ts`]: `export const hello = 'world';`,
};
Expand Down Expand Up @@ -271,6 +272,7 @@ describe('compile', () => {
assert.deepEqual(await read(outDir), {
'two-modules.mjs':
`${commonJsCompatabilityBanner}\n\n` +
`${dirnameInject}\n\n` +
`var hello = "world";\n` +
`\n` +
`var two_modules_default = hello + "world";\n` +
Expand All @@ -282,6 +284,41 @@ describe('compile', () => {
assert.equal(output.default, 'worldworld');
});

it('should bundle an ESM module that declares __dirname', async () => {
const id = crypto.randomUUID();
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
await writeInput(inDir, {
'index.ts': `
const __dirname = 'declared in project';
export const localDirname: string = __dirname;
export { injectedDirname } from './dependency.ts';
`,
'dependency.ts': `export const injectedDirname: string = __dirname;`,
});
await writeOutput(
await compile({
type: 'module',
entryPoint: 'index.ts',
outFile: 'index.mjs',
inDir,
outDir,
}),
);

const output = await import(path.join(outDir, 'index.mjs'));
assert.deepEqual(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be simpler:

assert.equal(output.injectedDirname, await fs.realpath(outDir));
assert.equal(output.localDirname, 'declared in project');

{
injectedDirname: output.injectedDirname as unknown,
localDirname: output.localDirname as unknown,
},
{
injectedDirname: await fs.realpath(outDir),
localDirname: 'declared in project',
},
);
});

it('should bundle an ESM module that imports external modules', async () => {
const id = crypto.randomUUID();
const moduleDir = path.join(os.tmpdir(), `in-dir-${id}`);
Expand All @@ -301,6 +338,7 @@ describe('compile', () => {
assert.deepEqual(await read(outDir), {
'index.mjs':
`${commonJsCompatabilityBanner}\n\n` +
`${dirnameInject}\n\n` +
`var hello = "world";\n` +
`\n` +
`import util from "node:util";\n` +
Expand Down Expand Up @@ -334,6 +372,7 @@ describe('compile', () => {
assert.deepEqual(convert(result.outputFiles), {
'index.mjs':
`${commonJsCompatabilityBanner}\n\n` +
`${dirnameInject}\n\n` +
`import { hello as test } from "test-esm-module";\n` +
`import util from "node:util";\n` +
`var hello = { test, message: util.format("hello %s", "world") };\n` +
Expand Down
10 changes: 7 additions & 3 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import tsConfigJson from '../tsconfig.json' with { type: 'json' };

const commonJsCompatabilityBanner = `import { createRequire as __createRequire } from "node:module";
import { fileURLToPath as __fileURLToPath } from "node:url";
import { default as __path } from "node:path";
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __path.dirname(__filename);
const require = __createRequire(import.meta.url);`;

const packageRoot = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const dirNameJsCompatibilityInjection = `import path from 'node:path';
export const __dirname = path.dirname(__filename);`; // relies on __filename from commonJsCompatabilityBanner
const dirNameJsCompatibilityInjectionURL = `data:text/javascript,${encodeURIComponent(dirNameJsCompatibilityInjection)}`;

Comment thread
adcreare marked this conversation as resolved.
export type ImportKind =
| 'entry-point'
| 'import-statement'
Expand Down Expand Up @@ -274,7 +277,7 @@ export default async function ({
tsConfigJson,
typescript.sys,
// @checkdigit/typescript-config package root:
path.dirname(path.dirname(fileURLToPath(import.meta.url))),
packageRoot,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to have this run at the module level? not sure I understand this change

).options;
const program = typescript.createProgram(productionSourceFiles, {
...compilerOptions,
Expand Down Expand Up @@ -337,6 +340,7 @@ export default async function ({
metafile: outFile !== undefined,
sourcesContent: false,
logLevel: 'error',
inject: outFile === undefined ? [] : [dirNameJsCompatibilityInjectionURL],
banner:
outFile === undefined
? {}
Expand Down
Loading