@nuxt/module-builder 0.1.2 → 0.1.6

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
@@ -14,9 +14,17 @@
14
14
  - Auto generated types and shims for `@nuxt/schema`
15
15
 
16
16
 
17
+ ## Quick start
18
+
19
+ You can quickly get started with pre-configured [module starter](https://github.com/nuxt/starter/tree/module):
20
+
21
+ ```bash
22
+ $ npx nuxi init -t "nuxt/starter#module" my-module
23
+ ```
24
+
17
25
  ## Project structure
18
26
 
19
- Using module builder, requires a special project setup. You can check a full example [here](./example).
27
+ Using module builder, requires a special project setup. You can check an example [here](./example).
20
28
 
21
29
  ### `src/module.ts`
22
30
 
@@ -24,6 +32,8 @@ This is the entrypoint for module definition.
24
32
 
25
33
  A default export using `defineNuxtModule` and `ModuleOptions` type export is expected.
26
34
 
35
+ You could also optionally export `ModuleHooks` to annotate any custom hooks module uses.
36
+
27
37
  ```js [src/module.ts]
28
38
  import { defineNuxtModule } from '@nuxt/kit'
29
39
 
@@ -31,6 +41,10 @@ export interface ModuleOptions {
31
41
  apiKey: string
32
42
  }
33
43
 
44
+ export interface ModuleHooks {
45
+ 'my-module:init': any
46
+ }
47
+
34
48
  export default defineNuxtModule<ModuleOptions>({
35
49
  meta: {
36
50
  name: 'my-module',
@@ -85,6 +99,20 @@ A minimum `package.json` should look like this:
85
99
  }
86
100
  ```
87
101
 
102
+ ## Dist files
103
+
104
+ Module builder generates dist files in `dist/` directory:
105
+
106
+ - `module.mjs`: Module entrypoint build from `src/module`
107
+ - `module.json`: Module meta extracted from `module.mjs` + `package.json`
108
+ - `module.cjs`: ESM proxy to allow require module in CommonJS context
109
+ - `types.d.ts`: Exported types in addition to shims for `nuxt.config` auto completion.
110
+ - `runtime/*`: Individually transformed files using [unjs/mkdist](https://github.com/unjs/mkdist)
111
+ - Javascript and `.ts` files will be transformed to `.mjs` with extracted types on `.d.ts` file with same name
112
+ - `.vue` files will be transformed with extracted `.d.ts` file
113
+ - Other files will be copied as as
114
+
115
+
88
116
 
89
117
  ## 💻 Development
90
118
 
@@ -1,11 +1,14 @@
1
- import { existsSync, promises } from 'fs';
1
+ import { promises, existsSync } from 'fs';
2
+ import { pathToFileURL } from 'url';
2
3
  import { resolve } from 'pathe';
3
4
  import consola from 'consola';
5
+ import { findExports } from 'mlly';
4
6
 
5
- async function buildModule(rootDir) {
7
+ async function buildModule(opts) {
6
8
  const { build } = await import('unbuild');
7
- await build(rootDir, false, {
9
+ await build(opts.rootDir, false, {
8
10
  declaration: true,
11
+ stub: opts.stub,
9
12
  entries: [
10
13
  "src/module",
11
14
  { input: "src/runtime/", outDir: "dist/runtime", ext: "mjs" }
@@ -24,7 +27,7 @@ async function buildModule(rootDir) {
24
27
  async "rollup:done"(ctx) {
25
28
  await writeCJSStub(ctx.options.outDir);
26
29
  const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
27
- const moduleFn = await import(moduleEntryPath).then((r) => r.default || r).catch((err) => {
30
+ const moduleFn = await import(pathToFileURL(moduleEntryPath).toString()).then((r) => r.default || r).catch((err) => {
28
31
  consola.error(err);
29
32
  consola.error("Cannot load module. Please check dist:", moduleEntryPath);
30
33
  return null;
@@ -33,6 +36,16 @@ async function buildModule(rootDir) {
33
36
  return;
34
37
  }
35
38
  const moduleMeta = await moduleFn.getMeta();
39
+ if (ctx.pkg) {
40
+ if (!moduleMeta.name) {
41
+ moduleMeta.name = ctx.pkg.name;
42
+ }
43
+ if (!moduleMeta.version) {
44
+ moduleMeta.version = ctx.pkg.version;
45
+ }
46
+ }
47
+ const metaFile = resolve(ctx.options.outDir, "module.json");
48
+ await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
36
49
  await writeTypes(ctx.options.outDir, moduleMeta);
37
50
  }
38
51
  }
@@ -43,18 +56,32 @@ async function writeTypes(distDir, meta) {
43
56
  if (existsSync(dtsFile)) {
44
57
  return;
45
58
  }
59
+ const moduleTypesFile = resolve(distDir, "module.d.ts");
60
+ const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
61
+ const typeExports = findExports(moduleTypes);
62
+ const isStub = moduleTypes.includes("export *");
63
+ const hasModuleOptions = isStub || typeExports.find((exp) => exp.names.includes("ModuleOptions"));
64
+ const hasModuleHooks = isStub || typeExports.find((exp) => exp.names.includes("ModuleHooks"));
46
65
  const schemaShims = [];
47
- if (meta.configKey) {
48
- schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`, ` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
66
+ const moduleImports = [];
67
+ if (meta.configKey && hasModuleOptions) {
68
+ moduleImports.push("ModuleOptions");
69
+ schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`);
70
+ schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
71
+ }
72
+ if (hasModuleHooks) {
73
+ moduleImports.push("ModuleHooks");
74
+ schemaShims.push(" interface NuxtHooks extends ModuleHooks {}");
49
75
  }
50
76
  const dtsContents = `
51
- import type { ModuleOptions } from './module'
77
+ import { ${moduleImports.join(", ")} } from './module'
52
78
 
53
79
  ${schemaShims.length ? `declare module '@nuxt/schema' {
54
80
  ${schemaShims.join("\n")}
55
81
  }
56
82
  ` : ""}
57
- export * from './module'
83
+
84
+ export { default } from './module'
58
85
  `;
59
86
  await promises.writeFile(dtsFile, dtsContents, "utf8");
60
87
  }
@@ -66,7 +93,8 @@ async function writeCJSStub(distDir) {
66
93
  const cjsStub = `module.exports = function(...args) {
67
94
  return import('./module.mjs').then(m => m.default.call(this, ...args))
68
95
  }
69
- module.exports.meta = require('../package.json')
96
+ const _meta = module.exports.meta = require('./module.json')
97
+ module.exports.getMeta = () => Promise.resolve(_meta)
70
98
  `;
71
99
  await promises.writeFile(cjsStubFile, cjsStub, "utf8");
72
100
  }
package/dist/cli.mjs CHANGED
@@ -1,14 +1,19 @@
1
- #!/bin/env node
1
+ #!/usr/bin/env node
2
2
  import mri from 'mri';
3
3
  import { resolve } from 'pathe';
4
4
  import { b as buildModule } from './chunks/build.mjs';
5
5
  import 'fs';
6
+ import 'url';
6
7
  import 'consola';
8
+ import 'mlly';
7
9
 
8
10
  async function main() {
9
11
  const args = mri(process.argv.slice(2));
10
12
  const rootDir = resolve(args._[0] || ".");
11
- await buildModule(rootDir);
13
+ await buildModule({
14
+ rootDir,
15
+ stub: args.stub
16
+ });
12
17
  }
13
18
  main().catch((err) => {
14
19
  console.error(err);
package/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
- declare function buildModule(rootDir: string): Promise<void>;
1
+ interface BuildModuleOptions {
2
+ rootDir: string;
3
+ stub?: boolean;
4
+ }
5
+ declare function buildModule(opts: BuildModuleOptions): Promise<void>;
2
6
 
3
- export { buildModule };
7
+ export { BuildModuleOptions, buildModule };
package/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  export { b as buildModule } from './chunks/build.mjs';
2
2
  import 'fs';
3
+ import 'url';
3
4
  import 'pathe';
4
5
  import 'consola';
6
+ import 'mlly';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/module-builder",
3
- "version": "0.1.2",
3
+ "version": "0.1.6",
4
4
  "description": "Complete solution to build Nuxt Modules",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -28,6 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "consola": "^2.15.3",
31
+ "mlly": "^0.3.19",
31
32
  "mri": "^1.2.0",
32
33
  "pathe": "^0.2.0",
33
34
  "unbuild": "^0.6.7"
@@ -35,10 +36,10 @@
35
36
  "devDependencies": {
36
37
  "@nuxt/kit": "npm:@nuxt/kit-edge@latest",
37
38
  "@nuxt/schema": "npm:@nuxt/schema-edge@latest",
38
- "@nuxtjs/eslint-config-typescript": "^8.0.0",
39
- "@types/mri": "^1.1.1",
40
- "eslint": "^8.5.0",
39
+ "@nuxtjs/eslint-config-typescript": "latest",
40
+ "@types/mri": "latest",
41
+ "eslint": "latest",
41
42
  "jiti": "^1.12.9",
42
- "standard-version": "^9.3.2"
43
+ "standard-version": "latest"
43
44
  }
44
45
  }
package/CHANGELOG.md DELETED
@@ -1,23 +0,0 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
-
5
- ### [0.1.2](https://github.com/nuxt/module-builder/compare/v0.1.1...v0.1.2) (2021-12-23)
6
-
7
-
8
- ### Bug Fixes
9
-
10
- * **pkg:** only include dist ([d2ff7e4](https://github.com/nuxt/module-builder/commit/d2ff7e4168363dd4e8a93dc9c909e6f6e07765ff))
11
-
12
- ### 0.1.1 (2021-12-23)
13
-
14
-
15
- ### Features
16
-
17
- * poc for schema types generation ([4991907](https://github.com/nuxt/module-builder/commit/49919078806b95e8d9bde64b708cb430a3bb09cf))
18
-
19
-
20
- ### Bug Fixes
21
-
22
- * emit cjs stub before validation ([0154bc5](https://github.com/nuxt/module-builder/commit/0154bc51db2246217287c0fba4d9be42955fa306))
23
- * improve types generation ([68c02e6](https://github.com/nuxt/module-builder/commit/68c02e60ab4f504c6b7d078c7e8e5fb13cb81e37))