-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add support for nuxt 3/nuxt bridge (#52)
* refactor: add support for nuxt 3/nuxt bridge * test: disabled for now * docs: typo * refactor: update module * test: enable * chore: remove .ts * chore: update script * ci: update * refactor: use config file to write rules * feat: add `rules` option * refactor: use `nitro.prerender` * refactor: remove robots hooks * test: update * chore: update test command * test: update
- Loading branch information
1 parent
ce0c78c
commit d950072
Showing
53 changed files
with
3,988 additions
and
9,471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ coverage | |
dist | ||
sw.* | ||
.env | ||
.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,32 +9,40 @@ | |
"Robert Reinhard", | ||
"William DA SILVA <[email protected]>" | ||
], | ||
"main": "./dist/module.js", | ||
"types": "./dist/module.d.ts", | ||
"type": "module", | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"require": "./dist/module.cjs", | ||
"import": "./dist/module.mjs" | ||
} | ||
}, | ||
"main": "./dist/module.cjs", | ||
"types": "./dist/types.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "siroc build", | ||
"prepublishOnly": "yarn build", | ||
"dev": "nuxt dev test/fixture/basic", | ||
"lint": "eslint --ext .js,.ts,.vue .", | ||
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish", | ||
"test": "yarn lint && yarn jest" | ||
"build": "nuxt-module-build", | ||
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground", | ||
"dev": "nuxt dev playground", | ||
"lint": "eslint --ext .js,.ts,.vue", | ||
"prepack": "yarn build", | ||
"release": "yarn test && standard-version && git push --follow-tags && npm publish", | ||
"test": "yarn lint && nuxi test --coverage" | ||
}, | ||
"dependencies": { | ||
"@nuxt/kit": "^3.0.0-rc.6" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-typescript": "latest", | ||
"@nuxt/test-utils": "latest", | ||
"@nuxt/types": "latest", | ||
"@nuxt/module-builder": "latest", | ||
"@nuxt/test-utils": "^3.0.0-rc.6", | ||
"@nuxtjs/eslint-config-typescript": "latest", | ||
"@types/jest": "latest", | ||
"@types/node": "latest", | ||
"del": "latest", | ||
"c8": "latest", | ||
"eslint": "latest", | ||
"jest": "latest", | ||
"nuxt": "latest", | ||
"siroc": "latest", | ||
"standard-version": "latest" | ||
"nuxt": "^3.0.0-rc.6", | ||
"standard-version": "latest", | ||
"vitest": "latest" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<NuxtWelcome /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
import { defineNuxtConfig } from 'nuxt' | ||
import RobotsModule from '..' | ||
|
||
export default defineNuxtConfig({ | ||
modules: [ | ||
RobotsModule | ||
] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "@nuxtjs/robots-playground" | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,60 @@ | ||
import type { Module } from '@nuxt/types' | ||
import { existsSync } from 'node:fs' | ||
import { defineNuxtModule, addServerHandler, createResolver, useLogger, isNuxt2, findPath, addTemplate } from '@nuxt/kit' | ||
import { name, version } from '../package.json' | ||
import { build } from './build' | ||
import { generate } from './generate' | ||
import { middleware } from './middleware' | ||
import { Rule } from './types' | ||
|
||
const CONFIG_KEY = 'robots' | ||
|
||
export type { Rule } | ||
|
||
export type ModuleOptions = Rule | Rule[] | (() => Rule | Rule[]) | ||
|
||
async function getOptions (moduleOptions: ModuleOptions): Promise<Rule[]> { | ||
if (typeof moduleOptions === 'function') { | ||
moduleOptions = await moduleOptions.call(this) | ||
} | ||
|
||
if (Array.isArray(moduleOptions)) { | ||
return moduleOptions | ||
} | ||
|
||
let { robots } = this.options | ||
|
||
if (typeof robots === 'function') { | ||
robots = await robots.call(this) | ||
} | ||
|
||
if (Array.isArray(robots)) { | ||
return robots | ||
} | ||
|
||
return [{ | ||
UserAgent: '*', | ||
Disallow: '', | ||
...robots, | ||
...moduleOptions | ||
}] | ||
} | ||
|
||
const nuxtModule: Module<ModuleOptions> = async function (moduleOptions) { | ||
const options: Rule[] = await getOptions.call(this, moduleOptions) | ||
|
||
build.bind(this)() | ||
generate.bind(this)(options) | ||
middleware.bind(this)(options) | ||
export type ModuleOptions = { | ||
configPath: string, | ||
rules: Rule | Rule[] | ||
} | ||
|
||
;(nuxtModule as any).meta = { name, version } | ||
|
||
declare module '@nuxt/types' { | ||
interface NuxtConfig { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.14+ | ||
interface Configuration { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.9 - 2.13 | ||
} | ||
|
||
export default nuxtModule | ||
const ROBOTS_FILENAME = 'robots.txt' | ||
const logger = useLogger('nuxt:robots') | ||
|
||
export default defineNuxtModule<ModuleOptions>({ | ||
meta: { | ||
name, | ||
version, | ||
configKey: 'robots' | ||
}, | ||
defaults: { | ||
configPath: 'robots.config', | ||
rules: { | ||
UserAgent: '*', | ||
Disallow: '' | ||
} | ||
}, | ||
async setup (options, nuxt) { | ||
const { resolve } = createResolver(import.meta.url) | ||
|
||
const staticFilePath = resolve( | ||
nuxt.options.srcDir, | ||
isNuxt2() ? nuxt.options.dir.static : nuxt.options.dir.public, | ||
ROBOTS_FILENAME | ||
) | ||
|
||
if (existsSync(staticFilePath)) { | ||
logger.warn('To use `' + name + '` module, please remove public `robots.txt`') | ||
return | ||
} | ||
|
||
nuxt.options.alias['#robots-config'] = await findPath(options.configPath) ?? resolve('./robots.config') | ||
nuxt.options.alias['#robots-rules'] = addTemplate({ | ||
filename: 'robots-rules.mjs', | ||
write: true, | ||
getContents: () => `export const rules = ${JSON.stringify(options.rules, null, 2)}` | ||
}).dst | ||
|
||
nuxt.hook('nitro:build:before', (nitro) => { | ||
nitro.options.prerender.routes.push(`/${ROBOTS_FILENAME}`) | ||
}) | ||
|
||
const runtimeDir = resolve('./runtime') | ||
nuxt.options.build.transpile.push(runtimeDir) | ||
|
||
addServerHandler({ | ||
route: `/${ROBOTS_FILENAME}`, | ||
handler: resolve(runtimeDir, 'server/middleware') | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { rules } from '#robots-rules' | ||
|
||
export default rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineHandler } from 'h3' | ||
import { getRules, render } from '../../utils' | ||
import config from '#robots-config' | ||
|
||
export default defineHandler(async ({ req, res }) => { | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.end(render(await getRules(config, req))) | ||
}) |
Oops, something went wrong.