@nuxt/module-builder 0.3.1 → 0.4.0
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 +2 -3
- package/dist/cli.mjs +33 -5
- package/dist/index.mjs +120 -6
- package/package.json +17 -10
- package/dist/shared/module-builder.0966342c.mjs +0 -120
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ We recommend to checkout the [Nuxt modules author guide](https://nuxt.com/docs/g
|
|
|
21
21
|
## Requirements
|
|
22
22
|
|
|
23
23
|
For a user to use a module generated from module-builder, it's recommended they have:
|
|
24
|
-
- Node.js >=
|
|
24
|
+
- Node.js >= 16.x. _Latest Node LTS preferred_
|
|
25
25
|
- Nuxt 3 or Nuxt Bridge. _Nuxt 2 is functional but not advised_
|
|
26
26
|
|
|
27
27
|
## Quick start
|
|
@@ -133,7 +133,7 @@ Module builder generates dist files in `dist/` directory:
|
|
|
133
133
|
## 💻 Development
|
|
134
134
|
|
|
135
135
|
- Clone repository
|
|
136
|
-
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
136
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
137
137
|
- Install dependencies using `pnpm install`
|
|
138
138
|
- Try building [example module](./example) using `pnpm example:build`
|
|
139
139
|
|
|
@@ -151,4 +151,3 @@ Module builder generates dist files in `dist/` directory:
|
|
|
151
151
|
|
|
152
152
|
[license-src]: https://img.shields.io/github/license/nuxt/module-builder.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
153
153
|
[license-href]: https://github.com/nuxt/module-builder/blob/main/LICENSE
|
|
154
|
-
|
package/dist/cli.mjs
CHANGED
|
@@ -1,17 +1,45 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import mri from 'mri';
|
|
3
3
|
import { resolve } from 'pathe';
|
|
4
|
-
import {
|
|
4
|
+
import { buildModule } from './index.mjs';
|
|
5
5
|
import 'fs';
|
|
6
6
|
import 'url';
|
|
7
7
|
import 'consola';
|
|
8
8
|
import 'mlly';
|
|
9
9
|
|
|
10
|
-
async function
|
|
10
|
+
async function prepareModule(options) {
|
|
11
|
+
const { runCommand } = await import('nuxi');
|
|
12
|
+
return runCommand("prepare", [options.rootDir], {
|
|
13
|
+
overrides: {
|
|
14
|
+
typescript: {
|
|
15
|
+
builder: "shared"
|
|
16
|
+
},
|
|
17
|
+
imports: {
|
|
18
|
+
autoImport: false
|
|
19
|
+
},
|
|
20
|
+
modules: [
|
|
21
|
+
resolve(options.rootDir, "./src/module"),
|
|
22
|
+
function(_options, nuxt) {
|
|
23
|
+
nuxt.hooks.hook("app:templates", (app) => {
|
|
24
|
+
for (const template of app.templates) {
|
|
25
|
+
template.write = true;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function main() {
|
|
11
35
|
const args = mri(process.argv.slice(2));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
36
|
+
if (args._[0] === "prepare") {
|
|
37
|
+
return prepareModule({
|
|
38
|
+
rootDir: resolve(args._[1] || ".")
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return buildModule({
|
|
42
|
+
rootDir: resolve(args._[0] || "."),
|
|
15
43
|
outDir: args.outDir,
|
|
16
44
|
stub: args.stub
|
|
17
45
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
import '
|
|
6
|
-
|
|
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
|
+
stub: opts.stub,
|
|
13
|
+
outDir,
|
|
14
|
+
entries: [
|
|
15
|
+
"src/module",
|
|
16
|
+
{ input: "src/runtime/", outDir: `${outDir}/runtime`, ext: "mjs" }
|
|
17
|
+
],
|
|
18
|
+
rollup: {
|
|
19
|
+
emitCJS: false,
|
|
20
|
+
cjsBridge: true
|
|
21
|
+
},
|
|
22
|
+
externals: [
|
|
23
|
+
"@nuxt/schema",
|
|
24
|
+
"@nuxt/schema-edge",
|
|
25
|
+
"@nuxt/kit",
|
|
26
|
+
"@nuxt/kit-edge",
|
|
27
|
+
"nuxt",
|
|
28
|
+
"nuxt-edge",
|
|
29
|
+
"nuxt3",
|
|
30
|
+
"vue",
|
|
31
|
+
"vue-demi"
|
|
32
|
+
],
|
|
33
|
+
hooks: {
|
|
34
|
+
async "rollup:done"(ctx) {
|
|
35
|
+
await writeCJSStub(ctx.options.outDir);
|
|
36
|
+
const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
|
|
37
|
+
const moduleFn = await import(pathToFileURL(moduleEntryPath).toString()).then((r) => r.default || r).catch((err) => {
|
|
38
|
+
consola.error(err);
|
|
39
|
+
consola.error("Cannot load module. Please check dist:", moduleEntryPath);
|
|
40
|
+
return null;
|
|
41
|
+
});
|
|
42
|
+
if (!moduleFn) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const moduleMeta = await moduleFn.getMeta();
|
|
46
|
+
if (ctx.pkg) {
|
|
47
|
+
if (!moduleMeta.name) {
|
|
48
|
+
moduleMeta.name = ctx.pkg.name;
|
|
49
|
+
}
|
|
50
|
+
if (!moduleMeta.version) {
|
|
51
|
+
moduleMeta.version = ctx.pkg.version;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const metaFile = resolve(ctx.options.outDir, "module.json");
|
|
55
|
+
await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
|
|
56
|
+
await writeTypes(ctx.options.outDir, moduleMeta);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function writeTypes(distDir, meta) {
|
|
62
|
+
const dtsFile = resolve(distDir, "types.d.ts");
|
|
63
|
+
if (existsSync(dtsFile)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const moduleTypesFile = resolve(distDir, "module.d.ts");
|
|
67
|
+
const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
|
|
68
|
+
const typeExports = findExports(moduleTypes);
|
|
69
|
+
const isStub = moduleTypes.includes("export *");
|
|
70
|
+
const schemaShims = [];
|
|
71
|
+
const moduleImports = [];
|
|
72
|
+
const hasTypeExport = (name) => isStub || typeExports.find((exp) => exp.names.includes(name));
|
|
73
|
+
if (meta.configKey && hasTypeExport("ModuleOptions")) {
|
|
74
|
+
moduleImports.push("ModuleOptions");
|
|
75
|
+
schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`);
|
|
76
|
+
schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
|
|
77
|
+
}
|
|
78
|
+
if (hasTypeExport("ModuleHooks")) {
|
|
79
|
+
moduleImports.push("ModuleHooks");
|
|
80
|
+
schemaShims.push(" interface NuxtHooks extends ModuleHooks {}");
|
|
81
|
+
}
|
|
82
|
+
if (hasTypeExport("ModuleRuntimeConfig")) {
|
|
83
|
+
moduleImports.push("ModuleRuntimeConfig");
|
|
84
|
+
schemaShims.push(" interface RuntimeConfig extends ModuleRuntimeConfig {}");
|
|
85
|
+
}
|
|
86
|
+
if (hasTypeExport("ModulePublicRuntimeConfig")) {
|
|
87
|
+
moduleImports.push("ModulePublicRuntimeConfig");
|
|
88
|
+
schemaShims.push(" interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}");
|
|
89
|
+
}
|
|
90
|
+
const dtsContents = `
|
|
91
|
+
import { ${moduleImports.join(", ")} } from './module'
|
|
92
|
+
|
|
93
|
+
${schemaShims.length ? `declare module '@nuxt/schema' {
|
|
94
|
+
${schemaShims.join("\n")}
|
|
95
|
+
}
|
|
96
|
+
` : ""}
|
|
97
|
+
${schemaShims.length ? `declare module 'nuxt/schema' {
|
|
98
|
+
${schemaShims.join("\n")}
|
|
99
|
+
}
|
|
100
|
+
` : ""}
|
|
101
|
+
|
|
102
|
+
export { ${typeExports[0].names.join(", ")} } from './module'
|
|
103
|
+
`;
|
|
104
|
+
await promises.writeFile(dtsFile, dtsContents, "utf8");
|
|
105
|
+
}
|
|
106
|
+
async function writeCJSStub(distDir) {
|
|
107
|
+
const cjsStubFile = resolve(distDir, "module.cjs");
|
|
108
|
+
if (existsSync(cjsStubFile)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const cjsStub = `module.exports = function(...args) {
|
|
112
|
+
return import('./module.mjs').then(m => m.default.call(this, ...args))
|
|
113
|
+
}
|
|
114
|
+
const _meta = module.exports.meta = require('./module.json')
|
|
115
|
+
module.exports.getMeta = () => Promise.resolve(_meta)
|
|
116
|
+
`;
|
|
117
|
+
await promises.writeFile(cjsStubFile, cjsStub, "utf8");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { buildModule };
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/module-builder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"repository": "nuxt/module-builder",
|
|
4
5
|
"description": "Complete solution to build Nuxt Modules",
|
|
5
6
|
"license": "MIT",
|
|
6
7
|
"type": "module",
|
|
@@ -22,29 +23,35 @@
|
|
|
22
23
|
"build": "unbuild",
|
|
23
24
|
"example:build": "pnpm nuxt-module-build ./example",
|
|
24
25
|
"lint": "eslint --ext .ts,.js,.mjs .",
|
|
25
|
-
"nuxt-module-build": "jiti ./src/cli.ts",
|
|
26
|
+
"nuxt-module-build": "JITI_ESM_RESOLVE=1 jiti ./src/cli.ts",
|
|
26
27
|
"prepack": "pnpm build",
|
|
27
28
|
"release": "pnpm test && standard-version && git push --follow-tags && npm publish",
|
|
28
29
|
"test": "pnpm vitest"
|
|
29
30
|
},
|
|
31
|
+
"packageManager": "pnpm@8.5.1",
|
|
30
32
|
"dependencies": {
|
|
31
33
|
"consola": "^3.1.0",
|
|
32
|
-
"mlly": "^1.
|
|
34
|
+
"mlly": "^1.3.0",
|
|
33
35
|
"mri": "^1.2.0",
|
|
34
36
|
"pathe": "^1.1.0",
|
|
35
37
|
"unbuild": "^1.2.1"
|
|
36
38
|
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@nuxt/kit": "^3.5.0",
|
|
41
|
+
"nuxi": "^3.5.0"
|
|
42
|
+
},
|
|
37
43
|
"devDependencies": {
|
|
38
|
-
"@nuxt/kit": "^3.
|
|
39
|
-
"@nuxt/schema": "^3.
|
|
44
|
+
"@nuxt/kit": "^3.5.1",
|
|
45
|
+
"@nuxt/schema": "^3.5.1",
|
|
40
46
|
"@nuxtjs/eslint-config-typescript": "^12.0.0",
|
|
41
47
|
"@types/mri": "^1.1.1",
|
|
42
|
-
"@types/node": "^
|
|
43
|
-
"@vitest/coverage-c8": "^0.
|
|
44
|
-
"eslint": "^8.
|
|
48
|
+
"@types/node": "^20.1.3",
|
|
49
|
+
"@vitest/coverage-c8": "^0.31.1",
|
|
50
|
+
"eslint": "^8.41.0",
|
|
45
51
|
"jiti": "^1.18.2",
|
|
46
|
-
"
|
|
52
|
+
"nuxi": "^3.5.1",
|
|
53
|
+
"nuxt": "^3.5.1",
|
|
47
54
|
"standard-version": "^9.5.0",
|
|
48
|
-
"vitest": "^0.
|
|
55
|
+
"vitest": "^0.31.1"
|
|
49
56
|
}
|
|
50
57
|
}
|
|
@@ -1,120 +0,0 @@
|
|
|
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
|
-
stub: opts.stub,
|
|
13
|
-
outDir,
|
|
14
|
-
entries: [
|
|
15
|
-
"src/module",
|
|
16
|
-
{ input: "src/runtime/", outDir: `${outDir}/runtime`, ext: "mjs" }
|
|
17
|
-
],
|
|
18
|
-
rollup: {
|
|
19
|
-
emitCJS: false,
|
|
20
|
-
cjsBridge: true
|
|
21
|
-
},
|
|
22
|
-
externals: [
|
|
23
|
-
"@nuxt/schema",
|
|
24
|
-
"@nuxt/schema-edge",
|
|
25
|
-
"@nuxt/kit",
|
|
26
|
-
"@nuxt/kit-edge",
|
|
27
|
-
"nuxt",
|
|
28
|
-
"nuxt-edge",
|
|
29
|
-
"nuxt3",
|
|
30
|
-
"vue",
|
|
31
|
-
"vue-demi"
|
|
32
|
-
],
|
|
33
|
-
hooks: {
|
|
34
|
-
async "rollup:done"(ctx) {
|
|
35
|
-
await writeCJSStub(ctx.options.outDir);
|
|
36
|
-
const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
|
|
37
|
-
const moduleFn = await import(pathToFileURL(moduleEntryPath).toString()).then((r) => r.default || r).catch((err) => {
|
|
38
|
-
consola.error(err);
|
|
39
|
-
consola.error("Cannot load module. Please check dist:", moduleEntryPath);
|
|
40
|
-
return null;
|
|
41
|
-
});
|
|
42
|
-
if (!moduleFn) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const moduleMeta = await moduleFn.getMeta();
|
|
46
|
-
if (ctx.pkg) {
|
|
47
|
-
if (!moduleMeta.name) {
|
|
48
|
-
moduleMeta.name = ctx.pkg.name;
|
|
49
|
-
}
|
|
50
|
-
if (!moduleMeta.version) {
|
|
51
|
-
moduleMeta.version = ctx.pkg.version;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const metaFile = resolve(ctx.options.outDir, "module.json");
|
|
55
|
-
await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
|
|
56
|
-
await writeTypes(ctx.options.outDir, moduleMeta);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
async function writeTypes(distDir, meta) {
|
|
62
|
-
const dtsFile = resolve(distDir, "types.d.ts");
|
|
63
|
-
if (existsSync(dtsFile)) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const moduleTypesFile = resolve(distDir, "module.d.ts");
|
|
67
|
-
const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
|
|
68
|
-
const typeExports = findExports(moduleTypes);
|
|
69
|
-
const isStub = moduleTypes.includes("export *");
|
|
70
|
-
const schemaShims = [];
|
|
71
|
-
const moduleImports = [];
|
|
72
|
-
const hasTypeExport = (name) => isStub || typeExports.find((exp) => exp.names.includes(name));
|
|
73
|
-
if (meta.configKey && hasTypeExport("ModuleOptions")) {
|
|
74
|
-
moduleImports.push("ModuleOptions");
|
|
75
|
-
schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`);
|
|
76
|
-
schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
|
|
77
|
-
}
|
|
78
|
-
if (hasTypeExport("ModuleHooks")) {
|
|
79
|
-
moduleImports.push("ModuleHooks");
|
|
80
|
-
schemaShims.push(" interface NuxtHooks extends ModuleHooks {}");
|
|
81
|
-
}
|
|
82
|
-
if (hasTypeExport("ModuleRuntimeConfig")) {
|
|
83
|
-
moduleImports.push("ModuleRuntimeConfig");
|
|
84
|
-
schemaShims.push(" interface RuntimeConfig extends ModuleRuntimeConfig {}");
|
|
85
|
-
}
|
|
86
|
-
if (hasTypeExport("ModulePublicRuntimeConfig")) {
|
|
87
|
-
moduleImports.push("ModulePublicRuntimeConfig");
|
|
88
|
-
schemaShims.push(" interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}");
|
|
89
|
-
}
|
|
90
|
-
const dtsContents = `
|
|
91
|
-
import { ${moduleImports.join(", ")} } from './module'
|
|
92
|
-
|
|
93
|
-
${schemaShims.length ? `declare module '@nuxt/schema' {
|
|
94
|
-
${schemaShims.join("\n")}
|
|
95
|
-
}
|
|
96
|
-
` : ""}
|
|
97
|
-
${schemaShims.length ? `declare module 'nuxt/schema' {
|
|
98
|
-
${schemaShims.join("\n")}
|
|
99
|
-
}
|
|
100
|
-
` : ""}
|
|
101
|
-
|
|
102
|
-
export { ${typeExports[0].names.join(", ")} } from './module'
|
|
103
|
-
`;
|
|
104
|
-
await promises.writeFile(dtsFile, dtsContents, "utf8");
|
|
105
|
-
}
|
|
106
|
-
async function writeCJSStub(distDir) {
|
|
107
|
-
const cjsStubFile = resolve(distDir, "module.cjs");
|
|
108
|
-
if (existsSync(cjsStubFile)) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
const cjsStub = `module.exports = function(...args) {
|
|
112
|
-
return import('./module.mjs').then(m => m.default.call(this, ...args))
|
|
113
|
-
}
|
|
114
|
-
const _meta = module.exports.meta = require('./module.json')
|
|
115
|
-
module.exports.getMeta = () => Promise.resolve(_meta)
|
|
116
|
-
`;
|
|
117
|
-
await promises.writeFile(cjsStubFile, cjsStub, "utf8");
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export { buildModule as b };
|