From bc498596b835b4d600bf066938dcd47b4eab19d0 Mon Sep 17 00:00:00 2001 From: Alvis HT Tang Date: Thu, 8 Jun 2023 10:50:25 +0100 Subject: [PATCH] feat: convert the whole project into ESM BREAKING CHANGE: - by default, client project emit ESM and for clarity preset-essentials has been renamed to preset-esm - ts-node is removed in favor of a more powerful and less buggy tsx --- README.md | 6 +++--- package.json | 2 +- source/index.ts | 9 ++++++--- spec/index.spec.ts | 41 ++++++++++++++++++++++++----------------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 68d2912..d263a99 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ [**FULL DOCUMENTATION IS AVAILABLE HERE**](https://github.com/alvis/presetter/blob/master/README.md) -1. Bootstrap your project with `presetter-preset-essentials` & `presetter-preset-rollup` +1. Bootstrap your project with `presetter-preset-esm` & `presetter-preset-rollup` ```shell npx presetter use presetter-preset presetter-preset-rollup @@ -72,7 +72,7 @@ For NodeJS to import the correct export, remember to specify the following in yo ## Project Structure -After installation, your project file structure should resemble the following or with more configuration files if you also installed other presets such as [`presetter-preset-essentials`](https://github.com/alvis/presetter/blob/master/packages/preset-essentials). +After installation, your project file structure should resemble the following or with more configuration files if you also installed other presets such as [`presetter-preset-esm`](https://github.com/alvis/presetter/blob/master/packages/preset-essentials). Implement your business logic under `source` and prepare tests under `spec`. @@ -108,7 +108,7 @@ interface PresetterRC { name: string | string[]; /** additional configuration passed to the preset for generating the configuration files */ config?: { - // ┌─ configuration for other tools via other presets (e.g. presetter-preset-essentials) + // ┌─ configuration for other tools via other presets (e.g. presetter-preset-esm) // ... /** additional configuration for rollup */ diff --git a/package.json b/package.json index 26f327d..e30792e 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "main": "lib/index.js", "types": "lib/index.d.ts", + "type": "module", "repository": { "type": "git", "url": "git+https://github.com/alvis/presetter.git" @@ -48,7 +49,6 @@ }, "devDependencies": { "presetter": "file:../presetter", - "presetter-preset-strict": "file:../preset-strict", "type-fest": "^2.0.0" }, "dependencies": { diff --git a/source/index.ts b/source/index.ts index 18cb5ad..2e691bd 100644 --- a/source/index.ts +++ b/source/index.ts @@ -13,7 +13,8 @@ * ------------------------------------------------------------------------- */ -import { resolve } from 'node:path'; +import { dirname, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { loadFile, template } from 'presetter'; @@ -23,9 +24,11 @@ import type { PresetAsset } from 'presetter-types'; import type { RollupConfig } from './rollup'; +const DIR = fileURLToPath(dirname(import.meta.url)); + // paths to the template directory -const TEMPLATES = resolve(__dirname, '..', 'templates'); -const CONFIGS = resolve(__dirname, '..', 'configs'); +const TEMPLATES = resolve(DIR, '..', 'templates'); +const CONFIGS = resolve(DIR, '..', 'configs'); /** config for this preset */ export type PresetConfig = { diff --git a/spec/index.spec.ts b/spec/index.spec.ts index eed1a9d..1e57354 100644 --- a/spec/index.spec.ts +++ b/spec/index.spec.ts @@ -13,21 +13,28 @@ * ------------------------------------------------------------------------- */ -import { readdirSync } from 'node:fs'; -import { resolve } from 'node:path'; -import { loadDynamicMap, resolveContext } from 'presetter'; +import { existsSync, readdirSync } from 'node:fs'; +import { dirname, resolve } from 'node:path'; +import * as pathNode from 'node:path'; +import { fileURLToPath } from 'node:url'; -import getPresetAsset from '#index'; +import { jest } from '@jest/globals'; -jest.mock('node:path', () => ({ - __esModule: true, - ...jest.requireActual('node:path'), - resolve: jest.fn(jest.requireActual('node:path').resolve), +const __dir = fileURLToPath(dirname(import.meta.url)); + +jest.unstable_mockModule('node:path', () => ({ + ...pathNode, + // spy on resolve to check if a template is referenced + resolve: jest.fn(resolve), })); +const { resolve: resolveSpyed } = await import('node:path'); +const { loadDynamicMap, resolveContext } = await import('presetter'); + +const { default: getPresetAsset } = await import('#index'); describe('fn:getPresetAsset', () => { it('use all templates', async () => { - const asset = getPresetAsset(); + const asset = await getPresetAsset(); const context = await resolveContext({ graph: [{ name: 'preset', asset, nodes: [] }], context: { @@ -40,16 +47,16 @@ describe('fn:getPresetAsset', () => { await loadDynamicMap(asset.supplementaryConfig, context); await loadDynamicMap(asset.template, context); - const TEMPLATES = resolve(__dirname, '..', 'templates'); - const allTemplates = readdirSync(TEMPLATES); - const CONFIGS = resolve(__dirname, '..', 'configs'); - const supplementaryConfig = readdirSync(CONFIGS); + const CONFIGS = resolve(__dir, '..', 'configs'); + const configs = (existsSync(CONFIGS) && readdirSync(CONFIGS)) || []; + const TEMPLATES = resolve(__dir, '..', 'templates'); + const templates = (existsSync(TEMPLATES) && readdirSync(TEMPLATES)) || []; - for (const path of allTemplates) { - expect(resolve).toBeCalledWith(TEMPLATES, path); + for (const path of configs) { + expect(resolveSpyed).toBeCalledWith(CONFIGS, path); } - for (const path of supplementaryConfig) { - expect(resolve).toBeCalledWith(CONFIGS, path); + for (const path of templates) { + expect(resolveSpyed).toBeCalledWith(TEMPLATES, path); } }); });