@nuxt/module-builder 0.1.1 → 0.1.5

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('./meta.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.1",
3
+ "version": "0.1.5",
4
4
  "description": "Complete solution to build Nuxt Modules",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,6 +14,9 @@
14
14
  "nuxt-build-module": "./dist/cli.mjs",
15
15
  "nuxt-module-build": "./dist/cli.mjs"
16
16
  },
17
+ "files": [
18
+ "dist"
19
+ ],
17
20
  "scripts": {
18
21
  "build": "unbuild",
19
22
  "example:build": "yarn nuxt-module-build ./example",
@@ -25,6 +28,7 @@
25
28
  },
26
29
  "dependencies": {
27
30
  "consola": "^2.15.3",
31
+ "mlly": "^0.3.19",
28
32
  "mri": "^1.2.0",
29
33
  "pathe": "^0.2.0",
30
34
  "unbuild": "^0.6.7"
@@ -32,10 +36,10 @@
32
36
  "devDependencies": {
33
37
  "@nuxt/kit": "npm:@nuxt/kit-edge@latest",
34
38
  "@nuxt/schema": "npm:@nuxt/schema-edge@latest",
35
- "@nuxtjs/eslint-config-typescript": "^8.0.0",
36
- "@types/mri": "^1.1.1",
37
- "eslint": "^8.5.0",
39
+ "@nuxtjs/eslint-config-typescript": "latest",
40
+ "@types/mri": "latest",
41
+ "eslint": "latest",
38
42
  "jiti": "^1.12.9",
39
- "standard-version": "^9.3.2"
43
+ "standard-version": "latest"
40
44
  }
41
45
  }
package/.editorconfig DELETED
@@ -1,15 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- end_of_line = lf
5
- insert_final_newline = true
6
- trim_trailing_whitespace = true
7
- charset = utf-8
8
-
9
- [*.js]
10
- indent_style = space
11
- indent_size = 2
12
-
13
- [{package.json,*.yml,*.cjson}]
14
- indent_style = space
15
- indent_size = 2
package/.eslintignore DELETED
@@ -1,3 +0,0 @@
1
- dist
2
- .nuxt
3
- .output
package/.eslintrc DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": [
3
- "@nuxtjs/eslint-config-typescript"
4
- ]
5
- }
@@ -1,23 +0,0 @@
1
- name: ci
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
- pull_request:
8
- branches:
9
- - main
10
-
11
- jobs:
12
- ci:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - uses: actions/checkout@v2
16
- - uses: actions/setup-node@v2
17
- with:
18
- node-version: 16
19
- cache: 'yarn'
20
- - run: yarn install
21
- - run: yarn lint
22
- - run: yarn build
23
- - run: yarn example:build
package/CHANGELOG.md DELETED
@@ -1,16 +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.1 (2021-12-23)
6
-
7
-
8
- ### Features
9
-
10
- * poc for schema types generation ([4991907](https://github.com/nuxt/module-builder/commit/49919078806b95e8d9bde64b708cb430a3bb09cf))
11
-
12
-
13
- ### Bug Fixes
14
-
15
- * emit cjs stub before validation ([0154bc5](https://github.com/nuxt/module-builder/commit/0154bc51db2246217287c0fba4d9be42955fa306))
16
- * improve types generation ([68c02e6](https://github.com/nuxt/module-builder/commit/68c02e60ab4f504c6b7d078c7e8e5fb13cb81e37))
package/build.config.ts DELETED
@@ -1,9 +0,0 @@
1
- import { defineBuildConfig } from 'unbuild'
2
-
3
- export default defineBuildConfig({
4
- declaration: true,
5
- entries: [
6
- './src/cli',
7
- './src/index'
8
- ]
9
- })
@@ -1,4 +0,0 @@
1
- module.exports = function(...args) {
2
- return import('./module.mjs').then(m => m.default.call(this, ...args))
3
- }
4
- module.exports.meta = require('../package.json')
@@ -1,8 +0,0 @@
1
- import * as _nuxt_schema from '@nuxt/schema';
2
-
3
- interface ModuleOptions {
4
- apiKey: string;
5
- }
6
- declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
7
-
8
- export { ModuleOptions, _default as default };
@@ -1,15 +0,0 @@
1
- import { defineNuxtModule } from '@nuxt/kit';
2
-
3
- const module = defineNuxtModule({
4
- meta: {
5
- name: "my-module",
6
- configKey2: "myModule"
7
- },
8
- defaults: {
9
- apiKey: null
10
- },
11
- setup(_options, _nuxt) {
12
- }
13
- });
14
-
15
- export { module as default };
@@ -1,9 +0,0 @@
1
-
2
- import type { ModuleOptions } from './module'
3
-
4
- declare module '@nuxt/schema' {
5
- interface NuxtConfig { ['my-module']?: Partial<ModuleOptions> }
6
- interface NuxtOptions { ['my-module']?: ModuleOptions }
7
- }
8
-
9
- export * from './module'
@@ -1,3 +0,0 @@
1
- declare const myModuleUtils: () => void;
2
-
3
- export { myModuleUtils };
@@ -1,4 +0,0 @@
1
- const myModuleUtils = () => {
2
- };
3
-
4
- export { myModuleUtils };
@@ -1,32 +0,0 @@
1
- {
2
- "name": "my-module",
3
- "license": "MIT",
4
- "version": "1.0.0",
5
- "exports": {
6
- ".": {
7
- "import": "./dist/module.mjs",
8
- "require": "./dist/module.cjs"
9
- },
10
- "./utils": "./dist/utils.mjs"
11
- },
12
- "main": "./dist/module.cjs",
13
- "types": "./dist/types.d.ts",
14
- "files": [
15
- "dist"
16
- ],
17
- "scripts": {
18
- "prepack": "npm run build"
19
- },
20
- "dependencies": {
21
- "@nuxt/kit": "npm:@nuxt/kit-edge@latest"
22
- },
23
- "devDependencies": {
24
- "@nuxt/module-builder": "latest",
25
- "nuxt3": "latest"
26
- },
27
- "build": {
28
- "entries": [
29
- "./src/utils"
30
- ]
31
- }
32
- }
package/renovate.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": [
3
- "@nuxtjs"
4
- ],
5
- "baseBranches": [
6
- "main"
7
- ],
8
- "lockFileMaintenance": {
9
- "enabled": true
10
- }
11
- }
package/src/build.ts DELETED
@@ -1,84 +0,0 @@
1
- import { existsSync, promises as fsp } from 'fs'
2
- import { resolve } from 'pathe'
3
- import consola from 'consola'
4
- import type { ModuleMeta } from '@nuxt/schema'
5
-
6
- export async function buildModule (rootDir: string) {
7
- const { build } = await import('unbuild')
8
-
9
- await build(rootDir, false, {
10
- declaration: true,
11
- entries: [
12
- 'src/module',
13
- { input: 'src/runtime/', outDir: 'dist/runtime', ext: 'mjs' }
14
- ],
15
- rollup: {
16
- emitCJS: false,
17
- cjsBridge: true
18
- },
19
- externals: [
20
- '@nuxt/schema',
21
- '@nuxt/schema-edge',
22
- '@nuxt/kit',
23
- '@nuxt/kit-edge'
24
- ],
25
- hooks: {
26
- async 'rollup:done' (ctx) {
27
- // Generate CommonJS stup
28
- await writeCJSStub(ctx.options.outDir)
29
-
30
- // Load module meta
31
- const moduleEntryPath = resolve(ctx.options.outDir, 'module.mjs')
32
- const moduleFn = await import(moduleEntryPath).then(r => r.default || r).catch((err) => {
33
- consola.error(err)
34
- consola.error('Cannot load module. Please check dist:', moduleEntryPath)
35
- return null
36
- })
37
- if (!moduleFn) {
38
- return
39
- }
40
- const moduleMeta = await moduleFn.getMeta()
41
-
42
- // Generate types
43
- await writeTypes(ctx.options.outDir, moduleMeta)
44
- }
45
- }
46
- })
47
- }
48
-
49
- async function writeTypes (distDir: string, meta: ModuleMeta) {
50
- const dtsFile = resolve(distDir, 'types.d.ts')
51
- if (existsSync(dtsFile)) {
52
- return
53
- }
54
-
55
- const schemaShims = []
56
- if (meta.configKey) {
57
- schemaShims.push(
58
- ` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`,
59
- ` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`
60
- )
61
- }
62
-
63
- const dtsContents = `
64
- import type { ModuleOptions } from './module'
65
-
66
- ${schemaShims.length ? `declare module '@nuxt/schema' {\n${schemaShims.join('\n')}\n}\n` : ''}
67
- export * from './module'
68
- `
69
-
70
- await fsp.writeFile(dtsFile, dtsContents, 'utf8')
71
- }
72
-
73
- async function writeCJSStub (distDir: string) {
74
- const cjsStubFile = resolve(distDir, 'module.cjs')
75
- if (existsSync(cjsStubFile)) {
76
- return
77
- }
78
- const cjsStub = `module.exports = function(...args) {
79
- return import('./module.mjs').then(m => m.default.call(this, ...args))
80
- }
81
- module.exports.meta = require('../package.json')
82
- `
83
- await fsp.writeFile(cjsStubFile, cjsStub, 'utf8')
84
- }
package/src/cli.ts DELETED
@@ -1,17 +0,0 @@
1
- #!/bin/env node
2
-
3
- /* eslint-disable no-console */
4
- import mri from 'mri'
5
- import { resolve } from 'pathe'
6
- import { buildModule } from './build'
7
-
8
- async function main () {
9
- const args = mri(process.argv.slice(2))
10
- const rootDir = resolve(args._[0] || '.')
11
- await buildModule(rootDir)
12
- }
13
-
14
- main().catch((err) => {
15
- console.error(err)
16
- process.exit(1)
17
- })
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './build'
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": ".",
4
- "target": "ESNext",
5
- "module": "ESNext",
6
- "moduleResolution": "Node",
7
- "esModuleInterop": true,
8
- "declaration": true,
9
- "types": [
10
- "node"
11
- ]
12
- },
13
- "include": [
14
- "src"
15
- ]
16
- }