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
This commit is contained in:
Alvis HT Tang 2023-06-08 10:50:25 +01:00
parent 5bb53481b0
commit bc498596b8
4 changed files with 34 additions and 24 deletions

View File

@ -38,7 +38,7 @@
[**FULL DOCUMENTATION IS AVAILABLE HERE**](https://github.com/alvis/presetter/blob/master/README.md) [**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 ```shell
npx presetter use presetter-preset presetter-preset-rollup 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 ## 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`. Implement your business logic under `source` and prepare tests under `spec`.
@ -108,7 +108,7 @@ interface PresetterRC {
name: string | string[]; name: string | string[];
/** additional configuration passed to the preset for generating the configuration files */ /** additional configuration passed to the preset for generating the configuration files */
config?: { 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 */ /** additional configuration for rollup */

View File

@ -17,6 +17,7 @@
}, },
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",
"type": "module",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/alvis/presetter.git" "url": "git+https://github.com/alvis/presetter.git"
@ -48,7 +49,6 @@
}, },
"devDependencies": { "devDependencies": {
"presetter": "file:../presetter", "presetter": "file:../presetter",
"presetter-preset-strict": "file:../preset-strict",
"type-fest": "^2.0.0" "type-fest": "^2.0.0"
}, },
"dependencies": { "dependencies": {

View File

@ -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'; import { loadFile, template } from 'presetter';
@ -23,9 +24,11 @@ import type { PresetAsset } from 'presetter-types';
import type { RollupConfig } from './rollup'; import type { RollupConfig } from './rollup';
const DIR = fileURLToPath(dirname(import.meta.url));
// paths to the template directory // paths to the template directory
const TEMPLATES = resolve(__dirname, '..', 'templates'); const TEMPLATES = resolve(DIR, '..', 'templates');
const CONFIGS = resolve(__dirname, '..', 'configs'); const CONFIGS = resolve(DIR, '..', 'configs');
/** config for this preset */ /** config for this preset */
export type PresetConfig = { export type PresetConfig = {

View File

@ -13,21 +13,28 @@
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
*/ */
import { readdirSync } from 'node:fs'; import { existsSync, readdirSync } from 'node:fs';
import { resolve } from 'node:path'; import { dirname, resolve } from 'node:path';
import { loadDynamicMap, resolveContext } from 'presetter'; import * as pathNode from 'node:path';
import { fileURLToPath } from 'node:url';
import getPresetAsset from '#index'; import { jest } from '@jest/globals';
jest.mock('node:path', () => ({ const __dir = fileURLToPath(dirname(import.meta.url));
__esModule: true,
...jest.requireActual('node:path'), jest.unstable_mockModule('node:path', () => ({
resolve: jest.fn(jest.requireActual('node:path').resolve), ...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', () => { describe('fn:getPresetAsset', () => {
it('use all templates', async () => { it('use all templates', async () => {
const asset = getPresetAsset(); const asset = await getPresetAsset();
const context = await resolveContext({ const context = await resolveContext({
graph: [{ name: 'preset', asset, nodes: [] }], graph: [{ name: 'preset', asset, nodes: [] }],
context: { context: {
@ -40,16 +47,16 @@ describe('fn:getPresetAsset', () => {
await loadDynamicMap(asset.supplementaryConfig, context); await loadDynamicMap(asset.supplementaryConfig, context);
await loadDynamicMap(asset.template, context); await loadDynamicMap(asset.template, context);
const TEMPLATES = resolve(__dirname, '..', 'templates'); const CONFIGS = resolve(__dir, '..', 'configs');
const allTemplates = readdirSync(TEMPLATES); const configs = (existsSync(CONFIGS) && readdirSync(CONFIGS)) || [];
const CONFIGS = resolve(__dirname, '..', 'configs'); const TEMPLATES = resolve(__dir, '..', 'templates');
const supplementaryConfig = readdirSync(CONFIGS); const templates = (existsSync(TEMPLATES) && readdirSync(TEMPLATES)) || [];
for (const path of allTemplates) { for (const path of configs) {
expect(resolve).toBeCalledWith(TEMPLATES, path); expect(resolveSpyed).toBeCalledWith(CONFIGS, path);
} }
for (const path of supplementaryConfig) { for (const path of templates) {
expect(resolve).toBeCalledWith(CONFIGS, path); expect(resolveSpyed).toBeCalledWith(TEMPLATES, path);
} }
}); });
}); });