@nuxt/module-builder 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -34,7 +34,7 @@ npx nuxi init -t module my-module
34
34
 
35
35
  ## Project structure
36
36
 
37
- The module builder requires a special project structur. You can checkout the [module template](https://github.com/nuxt/starter/tree/module).
37
+ The module builder requires a special project structure. You can check out the [module template](https://github.com/nuxt/starter/tree/module).
38
38
 
39
39
  ### `src/module.ts`
40
40
 
@@ -0,0 +1,159 @@
1
+ import { promises, existsSync } from 'fs';
2
+ import { pathToFileURL } from 'url';
3
+ import { resolve } from 'pathe';
4
+ import { consola } from 'consola';
5
+ import { findExports } from 'mlly';
6
+ import { defineCommand } from 'citty';
7
+
8
+ const build = defineCommand({
9
+ meta: {
10
+ name: "build",
11
+ description: "Build module for distribution"
12
+ },
13
+ args: {
14
+ cwd: {
15
+ type: "string",
16
+ description: "Current working directory"
17
+ },
18
+ rootDir: {
19
+ type: "positional",
20
+ description: "Root directory",
21
+ required: false
22
+ },
23
+ outDir: {
24
+ type: "string"
25
+ },
26
+ sourcemap: {
27
+ type: "boolean"
28
+ },
29
+ stub: {
30
+ type: "boolean"
31
+ }
32
+ },
33
+ async run(context) {
34
+ const { build } = await import('unbuild');
35
+ const cwd = resolve(context.args.cwd || context.args.rootDir || ".");
36
+ const outDir = context.args.outDir || "dist";
37
+ await build(cwd, false, {
38
+ declaration: true,
39
+ sourcemap: context.args.sourcemap,
40
+ stub: context.args.stub,
41
+ outDir,
42
+ entries: [
43
+ "src/module",
44
+ { input: "src/runtime/", outDir: `${outDir}/runtime`, ext: "mjs" }
45
+ ],
46
+ rollup: {
47
+ emitCJS: false,
48
+ cjsBridge: true
49
+ },
50
+ externals: [
51
+ "@nuxt/schema",
52
+ "@nuxt/schema-edge",
53
+ "@nuxt/kit",
54
+ "@nuxt/kit-edge",
55
+ "nuxt",
56
+ "nuxt-edge",
57
+ "nuxt3",
58
+ "vue",
59
+ "vue-demi"
60
+ ],
61
+ hooks: {
62
+ async "rollup:done"(ctx) {
63
+ await writeCJSStub(ctx.options.outDir);
64
+ const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
65
+ const moduleFn = await import(pathToFileURL(moduleEntryPath).toString()).then((r) => r.default || r).catch((err) => {
66
+ consola.error(err);
67
+ consola.error("Cannot load module. Please check dist:", moduleEntryPath);
68
+ return null;
69
+ });
70
+ if (!moduleFn) {
71
+ return;
72
+ }
73
+ const moduleMeta = await moduleFn.getMeta();
74
+ if (ctx.pkg) {
75
+ if (!moduleMeta.name) {
76
+ moduleMeta.name = ctx.pkg.name;
77
+ }
78
+ if (!moduleMeta.version) {
79
+ moduleMeta.version = ctx.pkg.version;
80
+ }
81
+ }
82
+ const metaFile = resolve(ctx.options.outDir, "module.json");
83
+ await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
84
+ await writeTypes(ctx.options.outDir, moduleMeta);
85
+ }
86
+ }
87
+ });
88
+ }
89
+ });
90
+ async function writeTypes(distDir, meta) {
91
+ const dtsFile = resolve(distDir, "types.d.ts");
92
+ const dtsFileMts = resolve(distDir, "types.d.mts");
93
+ if (existsSync(dtsFile)) {
94
+ return;
95
+ }
96
+ const moduleTypesFile = resolve(distDir, "module.d.ts");
97
+ const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
98
+ const typeExports = findExports(
99
+ // Replace `export { type Foo }` with `export { Foo }`
100
+ moduleTypes.replace(
101
+ /export\s*{.*?}/sg,
102
+ (match) => match.replace(/\btype\b/g, "")
103
+ )
104
+ );
105
+ const isStub = moduleTypes.includes("export *");
106
+ const schemaShims = [];
107
+ const moduleImports = [];
108
+ const hasTypeExport = (name) => isStub || typeExports.find((exp) => exp.names.includes(name));
109
+ if (meta.configKey && hasTypeExport("ModuleOptions")) {
110
+ moduleImports.push("ModuleOptions");
111
+ schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`);
112
+ schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
113
+ }
114
+ if (hasTypeExport("ModuleHooks")) {
115
+ moduleImports.push("ModuleHooks");
116
+ schemaShims.push(" interface NuxtHooks extends ModuleHooks {}");
117
+ }
118
+ if (hasTypeExport("ModuleRuntimeConfig")) {
119
+ moduleImports.push("ModuleRuntimeConfig");
120
+ schemaShims.push(" interface RuntimeConfig extends ModuleRuntimeConfig {}");
121
+ }
122
+ if (hasTypeExport("ModulePublicRuntimeConfig")) {
123
+ moduleImports.push("ModulePublicRuntimeConfig");
124
+ schemaShims.push(" interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}");
125
+ }
126
+ const dtsContents = `
127
+ import { ${moduleImports.join(", ")} } from './module'
128
+
129
+ ${schemaShims.length ? `declare module '@nuxt/schema' {
130
+ ${schemaShims.join("\n")}
131
+ }
132
+ ` : ""}
133
+ ${schemaShims.length ? `declare module 'nuxt/schema' {
134
+ ${schemaShims.join("\n")}
135
+ }
136
+ ` : ""}
137
+
138
+ export { ${typeExports[0].names.join(", ")} } from './module'
139
+ `;
140
+ await promises.writeFile(dtsFile, dtsContents, "utf8");
141
+ if (!existsSync(dtsFileMts)) {
142
+ await promises.writeFile(dtsFileMts, dtsContents, "utf8");
143
+ }
144
+ }
145
+ async function writeCJSStub(distDir) {
146
+ const cjsStubFile = resolve(distDir, "module.cjs");
147
+ if (existsSync(cjsStubFile)) {
148
+ return;
149
+ }
150
+ const cjsStub = `module.exports = function(...args) {
151
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
152
+ }
153
+ const _meta = module.exports.meta = require('./module.json')
154
+ module.exports.getMeta = () => Promise.resolve(_meta)
155
+ `;
156
+ await promises.writeFile(cjsStubFile, cjsStub, "utf8");
157
+ }
158
+
159
+ export { build as default };
@@ -0,0 +1,46 @@
1
+ import { defineCommand } from 'citty';
2
+ import { resolve } from 'pathe';
3
+
4
+ const prepare = defineCommand({
5
+ meta: {
6
+ name: "prepare",
7
+ description: "Prepare @nuxt/module-builder environment by writing types and stubs"
8
+ },
9
+ args: {
10
+ cwd: {
11
+ type: "string",
12
+ description: "Current working directory"
13
+ },
14
+ rootDir: {
15
+ type: "positional",
16
+ description: "Root directory",
17
+ required: false
18
+ }
19
+ },
20
+ async run(context) {
21
+ const { runCommand } = await import('nuxi');
22
+ const cwd = resolve(context.args.cwd || context.args.rootDir || ".");
23
+ return runCommand("prepare", [cwd], {
24
+ overrides: {
25
+ typescript: {
26
+ builder: "shared"
27
+ },
28
+ imports: {
29
+ autoImport: false
30
+ },
31
+ modules: [
32
+ resolve(cwd, "./src/module"),
33
+ function(_options, nuxt) {
34
+ nuxt.hooks.hook("app:templates", (app) => {
35
+ for (const template of app.templates) {
36
+ template.write = true;
37
+ }
38
+ });
39
+ }
40
+ ]
41
+ }
42
+ });
43
+ }
44
+ });
45
+
46
+ export { prepare as default };
package/dist/cli.mjs CHANGED
@@ -1,27 +1,28 @@
1
1
  #!/usr/bin/env node
2
- import mri from 'mri';
3
- import { resolve } from 'pathe';
4
- import { prepareModule, buildModule } from './index.mjs';
5
- import 'fs';
6
- import 'url';
7
- import 'consola';
8
- import 'mlly';
2
+ import { defineCommand, runMain } from 'citty';
3
+ import { consola } from 'consola';
9
4
 
10
- function main() {
11
- const args = mri(process.argv.slice(2));
12
- if (args._[0] === "prepare") {
13
- return prepareModule({
14
- rootDir: resolve(args._[1] || ".")
15
- });
5
+ const name = "@nuxt/module-builder";
6
+ const version = "0.5.2";
7
+ const description = "Complete solution for building Nuxt modules";
8
+
9
+ const _rDefault = (r) => r.default || r;
10
+ const main = defineCommand({
11
+ meta: {
12
+ name,
13
+ description,
14
+ version
15
+ },
16
+ subCommands: {
17
+ prepare: () => import('./chunks/prepare.mjs').then(_rDefault),
18
+ build: () => import('./chunks/build.mjs').then(_rDefault)
19
+ },
20
+ setup(context) {
21
+ const firstArg = context.rawArgs[0];
22
+ if (!(firstArg in context.cmd.subCommands)) {
23
+ consola.warn("Please specify the `build` command explicitly. In a future version of `@nuxt/module-builder`, the implicit default build command will be removed.");
24
+ context.rawArgs.unshift("build");
25
+ }
16
26
  }
17
- return buildModule({
18
- rootDir: resolve(args._[0] || "."),
19
- outDir: args.outDir,
20
- sourcemap: args.sourcemap,
21
- stub: args.stub
22
- });
23
- }
24
- main().catch((err) => {
25
- console.error(err);
26
- process.exit(1);
27
27
  });
28
+ runMain(main);
package/dist/index.d.mts CHANGED
@@ -1,14 +1,36 @@
1
- interface BuildModuleOptions {
2
- rootDir: string;
3
- sourcemap?: boolean;
4
- stub?: boolean;
5
- outDir?: string;
6
- }
7
- declare function buildModule(opts: BuildModuleOptions): Promise<void>;
1
+ import * as citty from 'citty';
8
2
 
9
- interface PrepareModuleOptions {
10
- rootDir: string;
11
- }
12
- declare function prepareModule(options: PrepareModuleOptions): Promise<void>;
3
+ declare const _default$1: citty.CommandDef<{
4
+ cwd: {
5
+ type: "string";
6
+ description: string;
7
+ };
8
+ rootDir: {
9
+ type: "positional";
10
+ description: string;
11
+ required: false;
12
+ };
13
+ outDir: {
14
+ type: "string";
15
+ };
16
+ sourcemap: {
17
+ type: "boolean";
18
+ };
19
+ stub: {
20
+ type: "boolean";
21
+ };
22
+ }>;
13
23
 
14
- export { type BuildModuleOptions, type PrepareModuleOptions, buildModule, prepareModule };
24
+ declare const _default: citty.CommandDef<{
25
+ cwd: {
26
+ type: "string";
27
+ description: string;
28
+ };
29
+ rootDir: {
30
+ type: "positional";
31
+ description: string;
32
+ required: false;
33
+ };
34
+ }>;
35
+
36
+ export { _default$1 as build, _default as prepare };
package/dist/index.d.ts CHANGED
@@ -1,14 +1,36 @@
1
- interface BuildModuleOptions {
2
- rootDir: string;
3
- sourcemap?: boolean;
4
- stub?: boolean;
5
- outDir?: string;
6
- }
7
- declare function buildModule(opts: BuildModuleOptions): Promise<void>;
1
+ import * as citty from 'citty';
8
2
 
9
- interface PrepareModuleOptions {
10
- rootDir: string;
11
- }
12
- declare function prepareModule(options: PrepareModuleOptions): Promise<void>;
3
+ declare const _default$1: citty.CommandDef<{
4
+ cwd: {
5
+ type: "string";
6
+ description: string;
7
+ };
8
+ rootDir: {
9
+ type: "positional";
10
+ description: string;
11
+ required: false;
12
+ };
13
+ outDir: {
14
+ type: "string";
15
+ };
16
+ sourcemap: {
17
+ type: "boolean";
18
+ };
19
+ stub: {
20
+ type: "boolean";
21
+ };
22
+ }>;
13
23
 
14
- export { type BuildModuleOptions, type PrepareModuleOptions, buildModule, prepareModule };
24
+ declare const _default: citty.CommandDef<{
25
+ cwd: {
26
+ type: "string";
27
+ description: string;
28
+ };
29
+ rootDir: {
30
+ type: "positional";
31
+ description: string;
32
+ required: false;
33
+ };
34
+ }>;
35
+
36
+ export { _default$1 as build, _default as prepare };
package/dist/index.mjs CHANGED
@@ -1,155 +1,8 @@
1
- import { promises, existsSync } from 'fs';
2
- import { pathToFileURL } from 'url';
3
- import { resolve } from 'pathe';
4
- import { consola } from 'consola';
5
- import { findExports } from 'mlly';
6
-
7
- async function buildModule(opts) {
8
- const { build } = await import('unbuild');
9
- const outDir = opts.outDir || "dist";
10
- await build(opts.rootDir, false, {
11
- declaration: true,
12
- sourcemap: opts.sourcemap,
13
- stub: opts.stub,
14
- outDir,
15
- entries: [
16
- "src/module",
17
- { input: "src/runtime/", outDir: `${outDir}/runtime`, ext: "mjs" }
18
- ],
19
- rollup: {
20
- emitCJS: false,
21
- cjsBridge: true
22
- },
23
- externals: [
24
- "@nuxt/schema",
25
- "@nuxt/schema-edge",
26
- "@nuxt/kit",
27
- "@nuxt/kit-edge",
28
- "nuxt",
29
- "nuxt-edge",
30
- "nuxt3",
31
- "vue",
32
- "vue-demi"
33
- ],
34
- hooks: {
35
- async "rollup:done"(ctx) {
36
- await writeCJSStub(ctx.options.outDir);
37
- const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
38
- const moduleFn = await import(pathToFileURL(moduleEntryPath).toString()).then((r) => r.default || r).catch((err) => {
39
- consola.error(err);
40
- consola.error("Cannot load module. Please check dist:", moduleEntryPath);
41
- return null;
42
- });
43
- if (!moduleFn) {
44
- return;
45
- }
46
- const moduleMeta = await moduleFn.getMeta();
47
- if (ctx.pkg) {
48
- if (!moduleMeta.name) {
49
- moduleMeta.name = ctx.pkg.name;
50
- }
51
- if (!moduleMeta.version) {
52
- moduleMeta.version = ctx.pkg.version;
53
- }
54
- }
55
- const metaFile = resolve(ctx.options.outDir, "module.json");
56
- await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
57
- await writeTypes(ctx.options.outDir, moduleMeta);
58
- }
59
- }
60
- });
61
- }
62
- async function writeTypes(distDir, meta) {
63
- const dtsFile = resolve(distDir, "types.d.ts");
64
- const dtsFileMts = resolve(distDir, "types.d.mts");
65
- if (existsSync(dtsFile)) {
66
- return;
67
- }
68
- const moduleTypesFile = resolve(distDir, "module.d.ts");
69
- const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
70
- const typeExports = findExports(
71
- // Replace `export { type Foo }` with `export { Foo }`
72
- moduleTypes.replace(
73
- /export\s*{.*?}/sg,
74
- (match) => match.replace(/\btype\b/g, "")
75
- )
76
- );
77
- const isStub = moduleTypes.includes("export *");
78
- const schemaShims = [];
79
- const moduleImports = [];
80
- const hasTypeExport = (name) => isStub || typeExports.find((exp) => exp.names.includes(name));
81
- if (meta.configKey && hasTypeExport("ModuleOptions")) {
82
- moduleImports.push("ModuleOptions");
83
- schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`);
84
- schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
85
- }
86
- if (hasTypeExport("ModuleHooks")) {
87
- moduleImports.push("ModuleHooks");
88
- schemaShims.push(" interface NuxtHooks extends ModuleHooks {}");
89
- }
90
- if (hasTypeExport("ModuleRuntimeConfig")) {
91
- moduleImports.push("ModuleRuntimeConfig");
92
- schemaShims.push(" interface RuntimeConfig extends ModuleRuntimeConfig {}");
93
- }
94
- if (hasTypeExport("ModulePublicRuntimeConfig")) {
95
- moduleImports.push("ModulePublicRuntimeConfig");
96
- schemaShims.push(" interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}");
97
- }
98
- const dtsContents = `
99
- import { ${moduleImports.join(", ")} } from './module'
100
-
101
- ${schemaShims.length ? `declare module '@nuxt/schema' {
102
- ${schemaShims.join("\n")}
103
- }
104
- ` : ""}
105
- ${schemaShims.length ? `declare module 'nuxt/schema' {
106
- ${schemaShims.join("\n")}
107
- }
108
- ` : ""}
109
-
110
- export { ${typeExports[0].names.join(", ")} } from './module'
111
- `;
112
- await promises.writeFile(dtsFile, dtsContents, "utf8");
113
- if (!existsSync(dtsFileMts)) {
114
- await promises.writeFile(dtsFileMts, dtsContents, "utf8");
115
- }
116
- }
117
- async function writeCJSStub(distDir) {
118
- const cjsStubFile = resolve(distDir, "module.cjs");
119
- if (existsSync(cjsStubFile)) {
120
- return;
121
- }
122
- const cjsStub = `module.exports = function(...args) {
123
- return import('./module.mjs').then(m => m.default.call(this, ...args))
124
- }
125
- const _meta = module.exports.meta = require('./module.json')
126
- module.exports.getMeta = () => Promise.resolve(_meta)
127
- `;
128
- await promises.writeFile(cjsStubFile, cjsStub, "utf8");
129
- }
130
-
131
- async function prepareModule(options) {
132
- const { runCommand } = await import('nuxi');
133
- return runCommand("prepare", [options.rootDir], {
134
- overrides: {
135
- typescript: {
136
- builder: "shared"
137
- },
138
- imports: {
139
- autoImport: false
140
- },
141
- modules: [
142
- resolve(options.rootDir, "./src/module"),
143
- function(_options, nuxt) {
144
- nuxt.hooks.hook("app:templates", (app) => {
145
- for (const template of app.templates) {
146
- template.write = true;
147
- }
148
- });
149
- }
150
- ]
151
- }
152
- });
153
- }
154
-
155
- export { buildModule, prepareModule };
1
+ export { default as build } from './chunks/build.mjs';
2
+ export { default as prepare } from './chunks/prepare.mjs';
3
+ import 'fs';
4
+ import 'url';
5
+ import 'pathe';
6
+ import 'consola';
7
+ import 'mlly';
8
+ import 'citty';
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@nuxt/module-builder",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "repository": "nuxt/module-builder",
5
- "description": "Complete solution to build Nuxt Modules",
5
+ "description": "Complete solution for building Nuxt modules",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "exports": {
@@ -19,31 +19,30 @@
19
19
  "files": [
20
20
  "dist"
21
21
  ],
22
- "packageManager": "pnpm@8.7.0",
22
+ "packageManager": "pnpm@8.8.0",
23
23
  "dependencies": {
24
+ "citty": "^0.1.4",
24
25
  "consola": "^3.2.3",
25
- "mlly": "^1.4.1",
26
- "mri": "^1.2.0",
26
+ "mlly": "^1.4.2",
27
27
  "pathe": "^1.1.1",
28
28
  "unbuild": "^2.0.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@nuxt/kit": "^3.7.0",
32
- "nuxi": "^3.7.3"
31
+ "@nuxt/kit": "^3.7.4",
32
+ "nuxi": "^3.9.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@nuxt/kit": "^3.7.0",
36
- "@nuxt/schema": "^3.7.0",
35
+ "@nuxt/kit": "^3.7.4",
36
+ "@nuxt/schema": "^3.7.4",
37
37
  "@nuxtjs/eslint-config-typescript": "^12.1.0",
38
- "@types/mri": "^1.1.1",
39
- "@types/node": "^20.5.7",
40
- "@vitest/coverage-v8": "^0.34.3",
38
+ "@types/node": "^20.7.1",
39
+ "@vitest/coverage-v8": "^0.34.5",
41
40
  "changelogen": "^0.5.5",
42
- "eslint": "^8.48.0",
43
- "jiti": "^1.19.3",
44
- "nuxi": "^3.7.3",
45
- "nuxt": "^3.7.0",
46
- "vitest": "^0.34.3"
41
+ "eslint": "^8.50.0",
42
+ "jiti": "^1.20.0",
43
+ "nuxi": "^3.9.0",
44
+ "nuxt": "^3.7.4",
45
+ "vitest": "^0.34.5"
47
46
  },
48
47
  "scripts": {
49
48
  "build": "unbuild",