Skip to content
Open

esm #268

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
2 changes: 1 addition & 1 deletion .nx-cache-buster
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-03-23T18:18:45-07:00
2024-06-01T20:52:18-07:00
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const commonProjectConfig: Partial<Config.ProjectConfig> = {
setupFilesAfterEnv: ['./jest.d/setup-files-after-env/faker.ts'],
testEnvironment: 'node',
testPathIgnorePatterns: ['/dist/', '/node_modules/'],
transformIgnorePatterns: ['.*\\.mjs'],
};

const CI = !!process.env.CI;
Expand All @@ -51,7 +52,7 @@ const config: Config.GlobalConfig = {
(packagePath) => !packagePath.split(path.sep).includes('examples')
)
.flatMap((packagePath) => [
`<rootDir>/${packagePath}/**/?(*.)+(test).[tj]s?(x)`,
`<rootDir>/${packagePath}/**/?(*.)+(test).?(m)[tj]s?(x)`,
]),
},
// @ts-expect-error - types seem wrong
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"lint:types": "bash -c \"tsc --build $TSC_OPTIONS\"",
"postinstall": "patch-package",
"prepare": "husky install",
"test": "TZ=UTC jest"
"test": "TZ=UTC node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"devDependencies": {
"@aws-sdk/client-cloudformation": "3.188.0",
Expand Down
14 changes: 8 additions & 6 deletions packages/@clc/nx/executors/package-json/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {extractProjectName, extractProjectRoot, writePackageJson} from '../..';
import type {PackageJsonExecutor} from './schema';

const runExecutor: Executor<PackageJsonExecutor> = async (
{type = 'package'},
{mjs = false, mts = false, type = 'package'},
context
) => {
const root = extractProjectRoot(context);
Expand All @@ -21,7 +21,7 @@ const runExecutor: Executor<PackageJsonExecutor> = async (
if (type === 'example') {
await configExample(pkg, context);
} else {
await config(pkg, type, context);
await config(pkg, mjs, mts, type, context);
}

await writePackageJson(packageJsonPath, pkg);
Expand All @@ -43,7 +43,9 @@ async function configExample(

async function config(
pkg: JSONSchemaForNPMPackageJsonFiles,
type: 'cli' | 'package',
mjs: boolean,
mts: boolean,
type: 'cli' | 'package' | 'esm',
context: ExecutorContext
) {
const packageName = extractProjectName(context);
Expand All @@ -63,12 +65,12 @@ async function config(
// `types` should [always come first](https://nodejs.org/api/packages.html#community-conditions-definitions)
import: {
types: './dist/types/index.d.mts',
carpentry: './src/index.ts',
default: './dist/esm/index.mjs',
...(mjs ? {} : {carpentry: mts ? './src/index.mts' : './src/index.ts'}),
default: mjs ? './src/index.mjs' : './dist/esm/index.mjs',
},
require: {
types: './dist/cjs-types/index.d.ts',
carpentry: './src/index.ts',
...(mjs ? {} : {carpentry: mts ? './src/index.mts' : './src/index.ts'}),
default: './dist/cjs/index.cjs',
},
/* eslint-enable sort-keys */
Expand Down
2 changes: 2 additions & 0 deletions packages/@clc/nx/executors/package-json/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
*/

export interface PackageJsonExecutor {
mjs?: boolean;
mts?: boolean;
type?: 'cli' | 'example' | 'package';
}
8 changes: 8 additions & 0 deletions packages/@clc/nx/executors/package-json/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"description": "",
"type": "object",
"properties": {
"mjs": {
"type": "boolean",
"default": false
},
"mts": {
"type": "boolean",
"default": false
},
"type": {
"enum": ["cli", "example", "package"],
"type": "string",
Expand Down
37 changes: 23 additions & 14 deletions packages/@clc/nx/src/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const createNodes: CreateNodes = [
return {};
}

const mjs = existsSync(path.resolve(projectRoot, 'src/index.mjs'));
const mts = existsSync(path.resolve(projectRoot, 'src/index.mts'));

let targets: Record<string, TargetConfiguration> = {};
// Set up the basic phases of the build process

Expand Down Expand Up @@ -88,25 +91,30 @@ export const createNodes: CreateNodes = [
dependsOn: ['codegen'],
executor: '@clc/nx:esbuild',
options: {
entryPoints: ['{projectRoot}/src/**/*.[jt]s?(x)', '!**/*.test.*'],
entryPoints: ['{projectRoot}/src/**/*.?(m)[jt]s?(x)', '!**/*.test.*'],
format: 'cjs',
outDir: '{projectRoot}/dist/cjs',
},
outputs: ['{projectRoot}/dist/cjs'],
});

addTarget(targets, 'build', 'esm', {
cache: true,
dependsOn: ['codegen'],
executor: '@clc/nx:esbuild',
inputs: ['{projectRoot}/src/**/*'],
options: {
entryPoints: ['{projectRoot}/src/**/*.[jt]s?(x)', '!**/*.test.*'],
format: 'esm',
outDir: '{projectRoot}/dist/esm',
},
outputs: ['{projectRoot}/dist/esm'],
});
if (!mjs) {
addTarget(targets, 'build', 'esm', {
cache: true,
dependsOn: ['codegen'],
executor: '@clc/nx:esbuild',
inputs: ['{projectRoot}/src/**/*'],
options: {
entryPoints: [
'{projectRoot}/src/**/*.?(m)[jt]s?(x)',
'!**/*.test.*',
],
format: 'esm',
outDir: '{projectRoot}/dist/esm',
},
outputs: ['{projectRoot}/dist/esm'],
});
}

addTarget(targets, 'build', 'types', {
cache: true,
Expand Down Expand Up @@ -144,7 +152,7 @@ export const createNodes: CreateNodes = [
cache: true,
executor: '@clc/nx:package-json',
inputs: ['{workspaceRoot}/package.json'],
options: {type},
options: {mjs, mts, type},
outputs: ['{projectRoot}/package.json'],
});

Expand Down Expand Up @@ -223,6 +231,7 @@ export const createNodes: CreateNodes = [
if (projectName.split('/').pop()?.startsWith('tool-')) {
addTarget(targets, 'codegen', 'tool', {
cache: true,
dependsOn: ['^build'],
executor: '@code-like-a-carpenter/tool-tool:tool',
inputs: ['{projectRoot}/tools/*.json'],
options: {
Expand Down
4 changes: 1 addition & 3 deletions packages/@code-like-a-carpenter/assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
".": {
"import": {
"types": "./dist/types/index.d.mts",
"carpentry": "./src/index.ts",
"default": "./dist/esm/index.mjs"
"default": "./src/index.mjs"
},
"require": {
"types": "./dist/cjs-types/index.d.ts",
"carpentry": "./src/index.ts",
"default": "./dist/cjs/index.cjs"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
// eslint-disable-next-line no-restricted-imports
import nodeAssert, {AssertionError} from 'assert';

type Provider = () => string | Error;
/**
* @callback Provider
* @returns {string | Error}
*/

export function assert(
value: unknown,
message: string | Error | Provider
): asserts value {
/**
* @param value {unknown}
* @param message {string | Error | Provider}
* @returns {asserts value}
*/
export function assert(value, message) {
if (typeof message === 'string' || message instanceof Error) {
nodeAssert.ok(value, message);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import {AssertionError} from 'assert';

import {assert} from './assert';
import {assert} from './assert.mjs';

describe('assert', () => {
it('throws a string error', () => {
const fn = () => assert(false, 'error');
expect(fn).toThrowError(AssertionError);
expect(fn).toThrow(AssertionError);
expect(fn).toThrowErrorMatchingInlineSnapshot(`"error"`);
});

// From what I can tell, this text fails because node doesn't do what the docs
// say it does.
it.skip('throws a custom error', () => {
const fn = () => assert(false, new TypeError('error'));
expect(fn).toThrowError(TypeError);
expect(fn).toThrow(TypeError);
expect(fn).toThrowErrorMatchingInlineSnapshot();
});

it('throws a provided string error', () => {
const fn = () => assert(false, () => 'error');
expect(fn).toThrowError(AssertionError);
expect(fn).toThrow(AssertionError);
expect(fn).toThrowErrorMatchingInlineSnapshot(`"error"`);
});

it('throws a provided custom error', () => {
const fn = () => assert(false, () => new TypeError('error'));
expect(fn).toThrowError(TypeError);
expect(fn).toThrow(TypeError);
expect(fn).toThrowErrorMatchingInlineSnapshot(`"error"`);
});
});
1 change: 1 addition & 0 deletions packages/@code-like-a-carpenter/assert/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './assert.mjs';
1 change: 0 additions & 1 deletion packages/@code-like-a-carpenter/assert/src/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/@code-like-a-carpenter/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
".": {
"import": {
"types": "./dist/types/index.d.mts",
"carpentry": "./src/index.ts",
"carpentry": "./src/index.mts",
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs-types/index.d.ts",
"carpentry": "./src/index.ts",
"carpentry": "./src/index.mts",
"default": "./dist/cjs/index.cjs"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {EnvironmentError} from './errors/environment-error';
import {TypeNarrowingError} from './errors/type-narrowing-error';
import {EnvironmentError} from './errors/environment-error.mts';
import {TypeNarrowingError} from './errors/type-narrowing-error.mts';

/**
* Returns the value of the environment variable with the given key, using
Expand Down
3 changes: 3 additions & 0 deletions packages/@code-like-a-carpenter/env/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './env.mts';
export * from './errors/environment-error.mts';
export * from './errors/type-narrowing-error.mts';
3 changes: 0 additions & 3 deletions packages/@code-like-a-carpenter/env/src/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/@code-like-a-carpenter/tool-deps/src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {minimatch} from 'minimatch';

import {findLocalPackages} from '@code-like-a-carpenter/tooling-common';

import type {DepsExecutor} from './__generated__/deps-types.ts';
import type {DepsExecutor} from './__generated__/deps-types';
import {load} from './config';
import {runDepcheck} from './depcheck';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assert} from '@code-like-a-carpenter/assert';

import type {Foundation} from './__generated__/foundation-types.ts';
import type {Foundation} from './__generated__/foundation-types';
import {generateCode} from './lib';

export async function handler({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as t from '@babel/types';

import {assert} from '@code-like-a-carpenter/assert';

import type {InlinerExecutor} from './__generated__/inliner-types.ts';
import type {InlinerExecutor} from './__generated__/inliner-types';

export async function handler(args: InlinerExecutor): Promise<void> {
const {sourceFile, targetFile, exportName} = args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path';

import {glob} from 'glob';

import type {JsonSchemaTool} from './__generated__/json-schema-types.ts';
import type {JsonSchemaTool} from './__generated__/json-schema-types';
import {jsonSchemaToTypescript} from './json-schema-helpers';

export async function handler({
Expand Down
2 changes: 1 addition & 1 deletion packages/@code-like-a-carpenter/tool-stack/src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
readPackageJson,
} from '@code-like-a-carpenter/tooling-common';

import type {StackListSchema} from './__generated__/list-types.ts';
import type {StackListSchema} from './__generated__/list-types';

export async function handler(args: StackListSchema): Promise<void> {
const files =
Expand Down
2 changes: 1 addition & 1 deletion packages/@code-like-a-carpenter/tool-stack/src/name.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {assert, fail} from '@code-like-a-carpenter/assert';
import {getStackName} from '@code-like-a-carpenter/tooling-common';

import type {StackNameSchema} from './__generated__/name-types.ts';
import type {StackNameSchema} from './__generated__/name-types';

export async function handler(args: StackNameSchema): Promise<void> {
if ('projectName' in args) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@code-like-a-carpenter/tool-stack/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import vhost from 'vhost';
import {assert} from '@code-like-a-carpenter/assert';
import {findLocalPackages} from '@code-like-a-carpenter/tooling-common';

import type {StackProxySchema} from './__generated__/proxy-types.ts';
import type {StackProxySchema} from './__generated__/proxy-types';
import {findEndpoints, findStacks} from './stacks';

// eslint-disable-next-line complexity
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"allowImportingTsExtensions": true,
"allowJs": true,
"checkJs": true,
"declaration": true,
Expand All @@ -8,7 +9,7 @@
"jsx": "react",
"lib": ["dom", "es2022"],
"module": "es2022",
"moduleResolution": "node",
"moduleResolution": "Node",
"resolveJsonModule": true,
"strict": true,
"target": "es2022",
Expand Down
13 changes: 11 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@
},
"extends": "./tsconfig.base.json",
"include": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx",
"*.mjs",
"*.mts",
"*.ts",
"*.tsx",
"examples/*/executors/**/*.js",
"examples/*/executors/**/*.mjs",
"examples/*/executors/**/*.mts",
"examples/*/executors/**/*.ts",
"examples/*/src/**/*.js",
"examples/*/src/**/*.jsx",
"examples/*/src/**/*.mjs",
"examples/*/src/**/*.mts",
"examples/*/src/**/*.ts",
"examples/*/src/**/*.tsx",
"packages/*/*/src/**/*.js",
"packages/*/*/src/**/*.jsx",
"packages/*/*/src/**/*.mjs",
"packages/*/*/src/**/*.mts",
"packages/*/*/src/**/*.ts",
"packages/*/*/src/**/*.tsx",
"types/**/*.d.mts",
"types/**/*.d.ts"
],
"references": [
Expand Down