@nuxt/module-builder 0.1.3 → 0.1.7
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 +15 -1
- package/dist/chunks/build.mjs +28 -8
- package/dist/cli.mjs +5 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.mjs +1 -0
- package/package.json +3 -2
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
|
|
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',
|
package/dist/chunks/build.mjs
CHANGED
|
@@ -2,11 +2,13 @@ import { promises, existsSync } from 'fs';
|
|
|
2
2
|
import { pathToFileURL } from 'url';
|
|
3
3
|
import { resolve } from 'pathe';
|
|
4
4
|
import consola from 'consola';
|
|
5
|
+
import { findExports } from 'mlly';
|
|
5
6
|
|
|
6
|
-
async function buildModule(
|
|
7
|
+
async function buildModule(opts) {
|
|
7
8
|
const { build } = await import('unbuild');
|
|
8
|
-
await build(rootDir, false, {
|
|
9
|
+
await build(opts.rootDir, false, {
|
|
9
10
|
declaration: true,
|
|
11
|
+
stub: opts.stub,
|
|
10
12
|
entries: [
|
|
11
13
|
"src/module",
|
|
12
14
|
{ input: "src/runtime/", outDir: "dist/runtime", ext: "mjs" }
|
|
@@ -19,7 +21,11 @@ async function buildModule(rootDir) {
|
|
|
19
21
|
"@nuxt/schema",
|
|
20
22
|
"@nuxt/schema-edge",
|
|
21
23
|
"@nuxt/kit",
|
|
22
|
-
"@nuxt/kit-edge"
|
|
24
|
+
"@nuxt/kit-edge",
|
|
25
|
+
"nuxt",
|
|
26
|
+
"nuxt-edge",
|
|
27
|
+
"nuxt3",
|
|
28
|
+
"vue"
|
|
23
29
|
],
|
|
24
30
|
hooks: {
|
|
25
31
|
async "rollup:done"(ctx) {
|
|
@@ -54,18 +60,32 @@ async function writeTypes(distDir, meta) {
|
|
|
54
60
|
if (existsSync(dtsFile)) {
|
|
55
61
|
return;
|
|
56
62
|
}
|
|
63
|
+
const moduleTypesFile = resolve(distDir, "module.d.ts");
|
|
64
|
+
const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
|
|
65
|
+
const typeExports = findExports(moduleTypes);
|
|
66
|
+
const isStub = moduleTypes.includes("export *");
|
|
67
|
+
const hasModuleOptions = isStub || typeExports.find((exp) => exp.names.includes("ModuleOptions"));
|
|
68
|
+
const hasModuleHooks = isStub || typeExports.find((exp) => exp.names.includes("ModuleHooks"));
|
|
57
69
|
const schemaShims = [];
|
|
58
|
-
|
|
59
|
-
|
|
70
|
+
const moduleImports = [];
|
|
71
|
+
if (meta.configKey && hasModuleOptions) {
|
|
72
|
+
moduleImports.push("ModuleOptions");
|
|
73
|
+
schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`);
|
|
74
|
+
schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
|
|
75
|
+
}
|
|
76
|
+
if (hasModuleHooks) {
|
|
77
|
+
moduleImports.push("ModuleHooks");
|
|
78
|
+
schemaShims.push(" interface NuxtHooks extends ModuleHooks {}");
|
|
60
79
|
}
|
|
61
80
|
const dtsContents = `
|
|
62
|
-
import
|
|
81
|
+
import { ${moduleImports.join(", ")} } from './module'
|
|
63
82
|
|
|
64
83
|
${schemaShims.length ? `declare module '@nuxt/schema' {
|
|
65
84
|
${schemaShims.join("\n")}
|
|
66
85
|
}
|
|
67
86
|
` : ""}
|
|
68
|
-
|
|
87
|
+
|
|
88
|
+
export { default } from './module'
|
|
69
89
|
`;
|
|
70
90
|
await promises.writeFile(dtsFile, dtsContents, "utf8");
|
|
71
91
|
}
|
|
@@ -77,7 +97,7 @@ async function writeCJSStub(distDir) {
|
|
|
77
97
|
const cjsStub = `module.exports = function(...args) {
|
|
78
98
|
return import('./module.mjs').then(m => m.default.call(this, ...args))
|
|
79
99
|
}
|
|
80
|
-
const _meta = module.exports.meta = require('./
|
|
100
|
+
const _meta = module.exports.meta = require('./module.json')
|
|
81
101
|
module.exports.getMeta = () => Promise.resolve(_meta)
|
|
82
102
|
`;
|
|
83
103
|
await promises.writeFile(cjsStubFile, cjsStub, "utf8");
|
package/dist/cli.mjs
CHANGED
|
@@ -5,11 +5,15 @@ import { b as buildModule } from './chunks/build.mjs';
|
|
|
5
5
|
import 'fs';
|
|
6
6
|
import 'url';
|
|
7
7
|
import 'consola';
|
|
8
|
+
import 'mlly';
|
|
8
9
|
|
|
9
10
|
async function main() {
|
|
10
11
|
const args = mri(process.argv.slice(2));
|
|
11
12
|
const rootDir = resolve(args._[0] || ".");
|
|
12
|
-
await buildModule(
|
|
13
|
+
await buildModule({
|
|
14
|
+
rootDir,
|
|
15
|
+
stub: args.stub
|
|
16
|
+
});
|
|
13
17
|
}
|
|
14
18
|
main().catch((err) => {
|
|
15
19
|
console.error(err);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/module-builder",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
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"
|
|
@@ -38,7 +39,7 @@
|
|
|
38
39
|
"@nuxtjs/eslint-config-typescript": "latest",
|
|
39
40
|
"@types/mri": "latest",
|
|
40
41
|
"eslint": "latest",
|
|
41
|
-
"jiti": "
|
|
42
|
+
"jiti": "^1.12.9",
|
|
42
43
|
"standard-version": "latest"
|
|
43
44
|
}
|
|
44
45
|
}
|