Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Nuxt3 support #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dist/module.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(...args) {
return import('./module.mjs').then(m => m.default.call(this, ...args))
}
const _meta = module.exports.meta = require('./module.json')
module.exports.getMeta = () => Promise.resolve(_meta)
7 changes: 5 additions & 2 deletions dist/module.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as _nuxt_schema from '@nuxt/schema';

interface Options {
nested?: boolean;
alias?: string | boolean;
}
export default function stormModule(moduleOptions: Options): void;
export {};
declare const _default: _nuxt_schema.NuxtModule<Options>;

export { _default as default };
38 changes: 0 additions & 38 deletions dist/module.js

This file was deleted.

5 changes: 5 additions & 0 deletions dist/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "nuxt-storm",
"version": "1.1.1",
"configKey": "storm"
}
65 changes: 65 additions & 0 deletions dist/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useLogger, defineNuxtModule, resolvePath } from '@nuxt/kit';
import { writeFile } from 'node:fs/promises';

const name = "nuxt-storm";
const version = "1.1.1";

const logger = useLogger("nuxt-storm");
const module = defineNuxtModule({
meta: {
name,
version,
configKey: "storm"
},
defaults: {
nested: false,
alias: "@"
},
async setup(moduleOptions, nuxt) {
if (process.env.NODE_ENV === "production") {
return;
}
let count = 0;
let components;
nuxt.hook("components:extend", async (dirs) => {
components = [...dirs].map((file) => {
count++;
let filePath;
if (moduleOptions.alias) {
let alias;
if (typeof moduleOptions.alias !== "string")
alias = "@";
else
alias = moduleOptions.alias;
filePath = `${alias}/${file.filePath.slice(file.filePath.indexOf("components"))}`;
} else {
filePath = file.filePath;
}
return { name: file.pascalName, file: filePath };
});
if (moduleOptions.nested) {
logger.info(`Nested components option detected`);
}
await writeComponentsFile(components);
logger.info(`${count} components compiled for nuxt-storm`);
});
}
});
const writeComponentsFile = async (components) => {
let template = `import Vue from 'vue'

${components.map(({ name: name2, file }) => {
return `import ${name2} from '${file}'`;
}).join("\n")}

`;
template += components.map(({ name: name2 }) => {
let template2 = `Vue.component('${name2}', ${name2})`;
template2 += `
Vue.component('Lazy${name2}', ${name2})`;
return template2;
}).join("\n");
await writeFile(await resolvePath(".components.gen.js"), template);
};

export { module as default };
6 changes: 6 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import { } from './module'



export { default } from './module'
22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@
"repository": "https://github.com/fumeapp/nuxt-storm",
"license": "MIT",
"author": "Kevin Olson",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"require": "./dist/module.cjs",
"import": "./dist/module.mjs"
}
},
"main": "./dist/module.cjs",
"types": "./dist/types.d.ts",
"files": [
"dist",
"templates"
"dist"
],
"type": "module",
"main": "./dist/module.js",
"scripts": {
"build": "tsc"
"build": "nuxt-module-build"
},
"devDependencies": {
"@nuxt/types": "^2.15.4",
"@nuxt/module-builder": "^0.1.7",
"@types/node": "^15.0.1",
"siroc": "^0.9.2",
"typescript": "^4.2.4"
},
"dependencies": {
"consola": "^2.15.3"
"@nuxt/kit": "^3.0.0-rc.9"
}
}
99 changes: 62 additions & 37 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,71 @@
import { resolve } from 'path'
import consola from 'consola'
import {defineNuxtModule, resolvePath, useLogger} from "@nuxt/kit";
import {name, version} from '../package.json'
import {writeFile} from "node:fs/promises";

interface Options {
nested?: boolean
alias?: string | boolean
nested?: boolean
alias?: string | boolean
}

export default function stormModule (moduleOptions: Options) {
if (process.env.NODE_ENV === 'production') {
return
}
let count = 0
const logger = consola.withScope('nuxt:storm')

let components
this.nuxt.hook('components:extend', dirs => {
components = [...dirs].map(file => {
count++
let filePath
if (moduleOptions.alias) {
let alias
if (typeof moduleOptions.alias !== 'string') alias = '@'
else alias = moduleOptions.alias
filePath = `${alias}/${file.filePath.slice(file.filePath.indexOf('components'))}`
}
else { filePath = file.filePath }
return { name: file.pascalName, file: filePath}
})

if (moduleOptions.nested) {
logger.info(`Nested components option detected`)
}
logger.info(`${count} components compiled for nuxt-storm`)
const logger = useLogger('nuxt-storm')

export default defineNuxtModule<Options>({
meta: {
name,
version,
configKey: 'storm'
},
defaults: {
nested: false,
alias: '@'
},
async setup(moduleOptions, nuxt) {
if (process.env.NODE_ENV === 'production') {
return
}
let count = 0

let components
nuxt.hook('components:extend', async dirs => {
components = [...dirs].map(file => {
count++
let filePath
if (moduleOptions.alias) {
let alias
if (typeof moduleOptions.alias !== 'string') alias = '@'
else alias = moduleOptions.alias
filePath = `${alias}/${file.filePath.slice(file.filePath.indexOf('components'))}`
} else {
filePath = file.filePath
}
return {name: file.pascalName, file: filePath}
})

const getComponents = () => components
if (moduleOptions.nested) {
logger.info(`Nested components option detected`)
}

await writeComponentsFile(components)

logger.info(`${count} components compiled for nuxt-storm`)
});
}
})

this.addTemplate({
src: resolve(__dirname, '../templates', 'components.js'),
fileName: '../.components.gen.js',
options: { getComponents },
})
const writeComponentsFile = async components => {
let template = `import Vue from 'vue'

${components.map(({name, file}) => {
return `import ${name} from '${file}'`
}).join('\n')}

`;

});
template += components.map(({name}) => {
let template = `Vue.component('${name}', ${name})`
template += `\nVue.component('Lazy${name}', ${name})`
return template
}).join('\n')

await writeFile(await resolvePath('.components.gen.js'), template)
}
10 changes: 0 additions & 10 deletions templates/components.js

This file was deleted.

Loading