From 71bb35175496929bfe52eef03d84c7678504cd93 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 16 May 2023 15:03:54 +0000 Subject: [PATCH 1/6] feat: add `--prepare` mode for local types support --- example/tsconfig.json | 2 +- package.json | 5 +++++ pnpm-lock.yaml | 3 +++ src/cli.ts | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/example/tsconfig.json b/example/tsconfig.json index 9dd826f..4b34df1 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -1,3 +1,3 @@ { - "extends": "./playground/.nuxt/tsconfig.json" + "extends": "./.nuxt/tsconfig.json" } diff --git a/package.json b/package.json index 03f1003..dbfa04d 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,10 @@ "pathe": "^1.1.0", "unbuild": "^1.2.1" }, + "peerDependencies": { + "@nuxt/kit": "^3.5.0", + "nuxi": "^3.5.0" + }, "devDependencies": { "@nuxt/kit": "^3.5.0", "@nuxt/schema": "^3.5.0", @@ -45,6 +49,7 @@ "@vitest/coverage-c8": "^0.31.0", "eslint": "^8.40.0", "jiti": "^1.18.2", + "nuxi": "^3.5.0", "nuxt": "^3.5.0", "standard-version": "^9.5.0", "vitest": "^0.31.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 37321ff..6e20962 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -44,6 +44,9 @@ importers: jiti: specifier: ^1.18.2 version: 1.18.2 + nuxi: + specifier: ^3.5.0 + version: 3.5.0 nuxt: specifier: ^3.5.0 version: 3.5.0(@types/node@20.1.3)(eslint@8.40.0)(rollup@3.21.7)(typescript@5.0.4) diff --git a/src/cli.ts b/src/cli.ts index ae217a0..8d70123 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -5,7 +5,16 @@ import mri from 'mri' import { resolve } from 'pathe' import { buildModule } from './build' +// TODO: use citty async function main () { + const args = mri(process.argv.slice(2)) + if ('prepare' in args) { + return prepare() + } + return build() +} + +async function build () { const args = mri(process.argv.slice(2)) const rootDir = resolve(args._[0] || '.') await buildModule({ @@ -15,6 +24,33 @@ async function main () { }) } +async function prepare () { + const { runCommand } = await import('nuxi') + const args = mri(process.argv.slice(2)) + const rootDir = resolve(args._[0] || '.') + + return runCommand('prepare', [rootDir], { + overrides: { + typescript: { + builder: 'shared' + }, + imports: { + autoImport: false + }, + modules: [ + resolve(rootDir, './src/module'), + function (options, nuxt) { + nuxt.hooks.hook('app:templates', app => { + for (const template of app.templates) { + template.write = true + } + }) + } + ] + } + }) +} + main().catch((err) => { console.error(err) process.exit(1) From 46bc7292cd439db73a3893f741e73ab2b96d37ed Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 16 May 2023 18:31:57 +0100 Subject: [PATCH 2/6] chore: update prepare script --- example/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/package.json b/example/package.json index 5a79a30..8e164c7 100644 --- a/example/package.json +++ b/example/package.json @@ -20,7 +20,7 @@ "prepack": "jiti ../src/cli.ts", "dev": "nuxi dev playground", "dev:build": "nuxi build playground", - "dev:prepare": "jiti ../src/cli.ts --stub && nuxi prepare playground" + "dev:prepare": "jiti ../src/cli.ts --stub && jiti ../src/cli.ts --prepare" }, "dependencies": { "@nuxt/kit": "^3.5.0" From cb5b9521450d43ec9dd4887beb738458f56a0de5 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 16 May 2023 23:13:15 +0100 Subject: [PATCH 3/6] test: use esm resolution with jiti --- example/package.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/package.json b/example/package.json index 8e164c7..f6a82c4 100644 --- a/example/package.json +++ b/example/package.json @@ -17,10 +17,10 @@ "dist" ], "scripts": { - "prepack": "jiti ../src/cli.ts", + "prepack": "JITI_ESM_RESOLVE=1 jiti ../src/cli.ts", "dev": "nuxi dev playground", "dev:build": "nuxi build playground", - "dev:prepare": "jiti ../src/cli.ts --stub && jiti ../src/cli.ts --prepare" + "dev:prepare": "JITI_ESM_RESOLVE=1 jiti ../src/cli.ts --stub && JITI_ESM_RESOLVE=1 jiti ../src/cli.ts --prepare" }, "dependencies": { "@nuxt/kit": "^3.5.0" diff --git a/package.json b/package.json index dbfa04d..7aef761 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "build": "unbuild", "example:build": "pnpm nuxt-module-build ./example", "lint": "eslint --ext .ts,.js,.mjs .", - "nuxt-module-build": "jiti ./src/cli.ts", + "nuxt-module-build": "JITI_ESM_RESOLVE=1 jiti ./src/cli.ts", "prepack": "pnpm build", "release": "pnpm test && standard-version && git push --follow-tags && npm publish", "test": "pnpm vitest" From 8eb04021b01ec4872dc2531b802e22478b2c773e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 16 May 2023 23:15:24 +0100 Subject: [PATCH 4/6] chore: lint --- src/cli.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 8d70123..1cfc28b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -6,7 +6,7 @@ import { resolve } from 'pathe' import { buildModule } from './build' // TODO: use citty -async function main () { +function main () { const args = mri(process.argv.slice(2)) if ('prepare' in args) { return prepare() @@ -39,8 +39,8 @@ async function prepare () { }, modules: [ resolve(rootDir, './src/module'), - function (options, nuxt) { - nuxt.hooks.hook('app:templates', app => { + function (_options, nuxt) { + nuxt.hooks.hook('app:templates', (app) => { for (const template of app.templates) { template.write = true } From da3a8ad7fd709b507e905a363a24efc5e3404603 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 26 May 2023 14:24:15 -0500 Subject: [PATCH 5/6] refactor: extract into subcommand --- src/cli.ts | 44 ++++++++------------------------------------ src/prepare.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 src/prepare.ts diff --git a/src/cli.ts b/src/cli.ts index 1cfc28b..2213c2e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,53 +4,25 @@ import mri from 'mri' import { resolve } from 'pathe' import { buildModule } from './build' +import { prepareModule } from './prepare' // TODO: use citty function main () { const args = mri(process.argv.slice(2)) - if ('prepare' in args) { - return prepare() + + if (args._[0] === 'prepare') { + return prepareModule({ + rootDir: resolve(args._[1] || '.') + }) } - return build() -} -async function build () { - const args = mri(process.argv.slice(2)) - const rootDir = resolve(args._[0] || '.') - await buildModule({ - rootDir, + return buildModule({ + rootDir: resolve(args._[0] || '.'), outDir: args.outDir, stub: args.stub }) } -async function prepare () { - const { runCommand } = await import('nuxi') - const args = mri(process.argv.slice(2)) - const rootDir = resolve(args._[0] || '.') - - return runCommand('prepare', [rootDir], { - overrides: { - typescript: { - builder: 'shared' - }, - imports: { - autoImport: false - }, - modules: [ - resolve(rootDir, './src/module'), - function (_options, nuxt) { - nuxt.hooks.hook('app:templates', (app) => { - for (const template of app.templates) { - template.write = true - } - }) - } - ] - } - }) -} - main().catch((err) => { console.error(err) process.exit(1) diff --git a/src/prepare.ts b/src/prepare.ts new file mode 100644 index 0000000..d1137b4 --- /dev/null +++ b/src/prepare.ts @@ -0,0 +1,30 @@ +import { resolve } from 'pathe' + +export interface PrepareModuleOptions { + rootDir: string +} + +export async function prepareModule (options: PrepareModuleOptions) { + const { runCommand } = await import('nuxi') + + return runCommand('prepare', [options.rootDir], { + overrides: { + typescript: { + builder: 'shared' + }, + imports: { + autoImport: false + }, + modules: [ + resolve(options.rootDir, './src/module'), + function (_options, nuxt) { + nuxt.hooks.hook('app:templates', (app) => { + for (const template of app.templates) { + template.write = true + } + }) + } + ] + } + }) +} From 637e9f5a396200adcfc185412ffe0aa8ff4e930d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 26 May 2023 14:25:57 -0500 Subject: [PATCH 6/6] chore: update usage --- example/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/package.json b/example/package.json index 1150754..46b58a5 100644 --- a/example/package.json +++ b/example/package.json @@ -20,7 +20,7 @@ "prepack": "JITI_ESM_RESOLVE=1 jiti ../src/cli.ts", "dev": "nuxi dev playground", "dev:build": "nuxi build playground", - "dev:prepare": "JITI_ESM_RESOLVE=1 jiti ../src/cli.ts --stub && JITI_ESM_RESOLVE=1 jiti ../src/cli.ts --prepare" + "dev:prepare": "JITI_ESM_RESOLVE=1 jiti ../src/cli.ts --stub && JITI_ESM_RESOLVE=1 jiti ../src/cli.ts prepare" }, "dependencies": { "@nuxt/kit": "^3.5.1"