@nuxt/module-builder 0.0.0 โ 0.1.1
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/.github/workflows/ci.yml +1 -1
- package/CHANGELOG.md +16 -0
- package/README.md +88 -9
- package/build.config.ts +0 -3
- package/dist/chunks/build.mjs +38 -4
- package/dist/cli.mjs +1 -0
- package/dist/index.mjs +1 -0
- package/example/dist/module.cjs +4 -0
- package/example/dist/module.d.ts +8 -0
- package/example/dist/module.mjs +15 -0
- package/example/dist/types.d.ts +9 -0
- package/example/dist/utils.d.ts +3 -0
- package/example/dist/utils.mjs +4 -0
- package/example/package.json +32 -0
- package/package.json +10 -6
- package/src/build.ts +48 -4
- package/playground/package.json +0 -17
- package/playground/src/custom.ts +0 -1
- package/playground/src/module.ts +0 -5
- package/playground/src/runtime/plugin.ts +0 -4
package/.github/workflows/ci.yml
CHANGED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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/README.md
CHANGED
|
@@ -1,18 +1,97 @@
|
|
|
1
|
-
# Nuxt Module Builder
|
|
1
|
+
# ๐ฆ Nuxt Module Builder
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Complete solution to build Nuxt Modules.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**๐งช This tool is still under heavy development for Nuxt3. Feedback is more than welcome!**
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- Use `npx nuxt-build-module` to build module
|
|
7
|
+
## Features
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
- Unified build with [unjs/unbuild](https://github.com/unjs/unbuild)
|
|
10
|
+
- Compatible with Nuxt 3 and Nuxt Kit
|
|
11
|
+
- Automated build config using last module spec
|
|
12
|
+
- Typescript and ESM support
|
|
13
|
+
- Auto generated CommonJS stubs
|
|
14
|
+
- Auto generated types and shims for `@nuxt/schema`
|
|
11
15
|
|
|
12
|
-
## Development
|
|
13
16
|
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
## Project structure
|
|
18
|
+
|
|
19
|
+
Using module builder, requires a special project setup. You can check a full example [here](./example).
|
|
20
|
+
|
|
21
|
+
### `src/module.ts`
|
|
22
|
+
|
|
23
|
+
This is the entrypoint for module definition.
|
|
24
|
+
|
|
25
|
+
A default export using `defineNuxtModule` and `ModuleOptions` type export is expected.
|
|
26
|
+
|
|
27
|
+
```js [src/module.ts]
|
|
28
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
29
|
+
|
|
30
|
+
export interface ModuleOptions {
|
|
31
|
+
apiKey: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default defineNuxtModule<ModuleOptions>({
|
|
35
|
+
meta: {
|
|
36
|
+
name: 'my-module',
|
|
37
|
+
configKey: 'myModule'
|
|
38
|
+
},
|
|
39
|
+
defaults: {
|
|
40
|
+
apiKey: 'test'
|
|
41
|
+
},
|
|
42
|
+
async setup (moduleOptions, nuxt) {
|
|
43
|
+
// Write module logic in setup function
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### `src/runtime/`
|
|
49
|
+
|
|
50
|
+
Any runtime file and code that we need to provide by module including plugins, composables and server api, should be in this directory.
|
|
51
|
+
|
|
52
|
+
Each file will be transformed individually using [unjs/mkdist](https://github.com/unjs/mkdist) to `dist/runtime/`.
|
|
53
|
+
|
|
54
|
+
<!-- TODO: Docs about how to address runtime from within setup -->
|
|
55
|
+
|
|
56
|
+
### `package.json`:
|
|
57
|
+
|
|
58
|
+
A minimum `package.json` should look like this:
|
|
59
|
+
|
|
60
|
+
```json [package.json]
|
|
61
|
+
{
|
|
62
|
+
"name": "my-module",
|
|
63
|
+
"license": "MIT",
|
|
64
|
+
"version": "1.0.0",
|
|
65
|
+
"exports": {
|
|
66
|
+
".": {
|
|
67
|
+
"import": "./dist/module.mjs",
|
|
68
|
+
"require": "./dist/module.cjs"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"main": "./dist/module.cjs",
|
|
72
|
+
"types": "./dist/types.d.ts",
|
|
73
|
+
"files": [
|
|
74
|
+
"dist"
|
|
75
|
+
],
|
|
76
|
+
"scripts": {
|
|
77
|
+
"prepack": "nuxt-module-build"
|
|
78
|
+
},
|
|
79
|
+
"dependencies": {
|
|
80
|
+
"@nuxt/kit": "npm:@nuxt/kit-edge@latest"
|
|
81
|
+
},
|
|
82
|
+
"devDependencies": {
|
|
83
|
+
"@nuxt/module-builder": "latest"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## ๐ป Development
|
|
90
|
+
|
|
91
|
+
- Clone repository
|
|
92
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
|
|
93
|
+
- Install dependencies using `yarn install`
|
|
94
|
+
- Try building [example module](./example) using `yarn example:build`
|
|
16
95
|
|
|
17
96
|
## License
|
|
18
97
|
|
package/build.config.ts
CHANGED
package/dist/chunks/build.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { promises } from 'fs';
|
|
1
|
+
import { existsSync, promises } from 'fs';
|
|
2
2
|
import { resolve } from 'pathe';
|
|
3
|
+
import consola from 'consola';
|
|
3
4
|
|
|
4
5
|
async function buildModule(rootDir) {
|
|
5
6
|
const { build } = await import('unbuild');
|
|
@@ -20,16 +21,49 @@ async function buildModule(rootDir) {
|
|
|
20
21
|
"@nuxt/kit-edge"
|
|
21
22
|
],
|
|
22
23
|
hooks: {
|
|
23
|
-
async "
|
|
24
|
+
async "rollup:done"(ctx) {
|
|
24
25
|
await writeCJSStub(ctx.options.outDir);
|
|
26
|
+
const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
|
|
27
|
+
const moduleFn = await import(moduleEntryPath).then((r) => r.default || r).catch((err) => {
|
|
28
|
+
consola.error(err);
|
|
29
|
+
consola.error("Cannot load module. Please check dist:", moduleEntryPath);
|
|
30
|
+
return null;
|
|
31
|
+
});
|
|
32
|
+
if (!moduleFn) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const moduleMeta = await moduleFn.getMeta();
|
|
36
|
+
await writeTypes(ctx.options.outDir, moduleMeta);
|
|
25
37
|
}
|
|
26
38
|
}
|
|
27
39
|
});
|
|
28
40
|
}
|
|
41
|
+
async function writeTypes(distDir, meta) {
|
|
42
|
+
const dtsFile = resolve(distDir, "types.d.ts");
|
|
43
|
+
if (existsSync(dtsFile)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const schemaShims = [];
|
|
47
|
+
if (meta.configKey) {
|
|
48
|
+
schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`, ` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`);
|
|
49
|
+
}
|
|
50
|
+
const dtsContents = `
|
|
51
|
+
import type { ModuleOptions } from './module'
|
|
52
|
+
|
|
53
|
+
${schemaShims.length ? `declare module '@nuxt/schema' {
|
|
54
|
+
${schemaShims.join("\n")}
|
|
55
|
+
}
|
|
56
|
+
` : ""}
|
|
57
|
+
export * from './module'
|
|
58
|
+
`;
|
|
59
|
+
await promises.writeFile(dtsFile, dtsContents, "utf8");
|
|
60
|
+
}
|
|
29
61
|
async function writeCJSStub(distDir) {
|
|
30
62
|
const cjsStubFile = resolve(distDir, "module.cjs");
|
|
31
|
-
|
|
32
|
-
|
|
63
|
+
if (existsSync(cjsStubFile)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const cjsStub = `module.exports = function(...args) {
|
|
33
67
|
return import('./module.mjs').then(m => m.default.call(this, ...args))
|
|
34
68
|
}
|
|
35
69
|
module.exports.meta = require('../package.json')
|
package/dist/cli.mjs
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
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 };
|
|
@@ -0,0 +1,32 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/module-builder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Complete solution to build Nuxt Modules",
|
|
4
5
|
"license": "MIT",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"exports": {
|
|
@@ -10,27 +11,30 @@
|
|
|
10
11
|
},
|
|
11
12
|
"types": "./dist/index.d.ts",
|
|
12
13
|
"bin": {
|
|
13
|
-
"nuxt-build-module": "./dist/cli.mjs"
|
|
14
|
+
"nuxt-build-module": "./dist/cli.mjs",
|
|
15
|
+
"nuxt-module-build": "./dist/cli.mjs"
|
|
14
16
|
},
|
|
15
17
|
"scripts": {
|
|
16
18
|
"build": "unbuild",
|
|
17
|
-
"
|
|
19
|
+
"example:build": "yarn nuxt-module-build ./example",
|
|
18
20
|
"lint": "eslint --ext .ts,.js,.mjs .",
|
|
21
|
+
"nuxt-module-build": "jiti ./src/cli.ts",
|
|
19
22
|
"prepack": "yarn build",
|
|
20
|
-
"play": "yarn cli ./playground",
|
|
21
23
|
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
|
|
22
24
|
"test": "yarn lint"
|
|
23
25
|
},
|
|
24
26
|
"dependencies": {
|
|
27
|
+
"consola": "^2.15.3",
|
|
25
28
|
"mri": "^1.2.0",
|
|
26
29
|
"pathe": "^0.2.0",
|
|
27
|
-
"unbuild": "^0.6.
|
|
30
|
+
"unbuild": "^0.6.7"
|
|
28
31
|
},
|
|
29
32
|
"devDependencies": {
|
|
30
33
|
"@nuxt/kit": "npm:@nuxt/kit-edge@latest",
|
|
34
|
+
"@nuxt/schema": "npm:@nuxt/schema-edge@latest",
|
|
31
35
|
"@nuxtjs/eslint-config-typescript": "^8.0.0",
|
|
32
36
|
"@types/mri": "^1.1.1",
|
|
33
|
-
"eslint": "^8.
|
|
37
|
+
"eslint": "^8.5.0",
|
|
34
38
|
"jiti": "^1.12.9",
|
|
35
39
|
"standard-version": "^9.3.2"
|
|
36
40
|
}
|
package/src/build.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { promises as fsp } from 'fs'
|
|
1
|
+
import { existsSync, promises as fsp } from 'fs'
|
|
2
2
|
import { resolve } from 'pathe'
|
|
3
|
+
import consola from 'consola'
|
|
4
|
+
import type { ModuleMeta } from '@nuxt/schema'
|
|
3
5
|
|
|
4
6
|
export async function buildModule (rootDir: string) {
|
|
5
7
|
const { build } = await import('unbuild')
|
|
@@ -21,17 +23,59 @@ export async function buildModule (rootDir: string) {
|
|
|
21
23
|
'@nuxt/kit-edge'
|
|
22
24
|
],
|
|
23
25
|
hooks: {
|
|
24
|
-
async '
|
|
26
|
+
async 'rollup:done' (ctx) {
|
|
27
|
+
// Generate CommonJS stup
|
|
25
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)
|
|
26
44
|
}
|
|
27
45
|
}
|
|
28
46
|
})
|
|
29
47
|
}
|
|
30
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
|
+
|
|
31
73
|
async function writeCJSStub (distDir: string) {
|
|
32
74
|
const cjsStubFile = resolve(distDir, 'module.cjs')
|
|
33
|
-
|
|
34
|
-
|
|
75
|
+
if (existsSync(cjsStubFile)) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
const cjsStub = `module.exports = function(...args) {
|
|
35
79
|
return import('./module.mjs').then(m => m.default.call(this, ...args))
|
|
36
80
|
}
|
|
37
81
|
module.exports.meta = require('../package.json')
|
package/playground/package.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"private": true,
|
|
3
|
-
"name": "mymodule",
|
|
4
|
-
"main": "./dist/module.cjs",
|
|
5
|
-
"types": "./dist/module.d.ts",
|
|
6
|
-
"build": {
|
|
7
|
-
"entries": [
|
|
8
|
-
"./src/custom"
|
|
9
|
-
]
|
|
10
|
-
},
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"require": "./dist/module.cjs",
|
|
14
|
-
"import": "./dist/module.mjs"
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
package/playground/src/custom.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
// Custom entry
|
package/playground/src/module.ts
DELETED