Skip to content

Commit

Permalink
feat: option parse pages (#95)
Browse files Browse the repository at this point in the history
* feat: option `parsePages`

* chore: update lock file

* test: update

* ci: update branch
  • Loading branch information
ricardogobbosouza authored Mar 12, 2021
1 parent 73b2ee3 commit 4ebf7e6
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 515 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,16 @@ Name of you route file.

Can access the default router.

### `parsePages`

- Type: `Boolean`
- Default: `'keepDefaultRouter'`

Can disable/enable the `pages/` directory into Nuxt.

## Usage

This module disable the `pages/` directory into Nuxt and will use a `router.js` file at your `srcDir` directory:
This module, by default, disable the `pages/` directory into Nuxt and will use a `router.js` file at your `srcDir` directory:

```bash
components/
Expand Down
37 changes: 22 additions & 15 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ModuleOptions {
path: string;
fileName: string;
keepDefaultRouter?: boolean;
parsePages?: boolean;
}

const CONFIG_KEY = 'routerModule'
Expand All @@ -29,6 +30,10 @@ const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
DEFAULTS
)

if (typeof options.parsePages === 'undefined') {
options.parsePages = options.keepDefaultRouter
}

const routerFilePath = resolve(options.path, options.fileName)

// Check if router file path is defined
Expand All @@ -48,33 +53,35 @@ const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
})

// Disable parsing `pages/`
if (!options.keepDefaultRouter) {
return this.nuxt.hook('build:before', () => {
if (!options.parsePages) {
this.nuxt.hook('build:before', () => {
this.nuxt.options.build.createRoutes = () => {
return []
}
})
}

// Put default router as .nuxt/defaultRouter.js
let defaultRouter: string
if (options.keepDefaultRouter) {
// Put default router as .nuxt/defaultRouter.js
let defaultRouter: string

try {
defaultRouter = require.resolve('@nuxt/vue-app/template/router')
} catch (err) {
/* istanbul ignore next */
try {
defaultRouter = require.resolve('@nuxt/vue-app-edge/template/router')
defaultRouter = require.resolve('@nuxt/vue-app/template/router')
} catch (err) {
/* istanbul ignore next */
defaultRouter = require.resolve('nuxt/lib/app/router')
try {
defaultRouter = require.resolve('@nuxt/vue-app-edge/template/router')
} catch (err) {
/* istanbul ignore next */
defaultRouter = require.resolve('nuxt/lib/app/router')
}
}
}

this.addTemplate({
fileName: 'defaultRouter.js',
src: defaultRouter
})
this.addTemplate({
fileName: 'defaultRouter.js',
src: defaultRouter
})
}
}

;(nuxtModule as any).meta = { name, version }
Expand Down
17 changes: 17 additions & 0 deletions test/disable-parse-pages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { setupTest, get } from '@nuxt/test-utils'

describe('disable-parse-pages', () => {
setupTest({
server: true,
fixture: 'fixture/disable-parse-pages'
})

test('render', async () => {
const { body } = await get('/')
expect(body).toContain('Hello server')
})

test('disable pages', async () => {
await expect(get('/foo')).rejects.toThrow('Not Found')
})
})
16 changes: 3 additions & 13 deletions test/fail.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { setupTest } from '@nuxt/test-utils'
import { setupTest, mockConsola } from '@nuxt/test-utils'

const mockReporter = {
warn: jest.fn()
}

jest.mock('consola', () => ({
info: jest.fn(),
success: jest.fn(),
debug: jest.fn(),
warn: jest.fn(),
withTag: jest.fn().mockImplementation(() => mockReporter)
}))
const logger = mockConsola()

describe('fail', () => {
setupTest({
Expand All @@ -19,6 +9,6 @@ describe('fail', () => {
})

test('should warn if not found the router file', () => {
expect(mockReporter.warn).toHaveBeenCalledWith(expect.stringMatching(/^No `(.*)` file found in `(.*)`.$/))
expect(logger.warn).toHaveBeenCalledWith(expect.stringMatching(/^No `(.*)` file found in `(.*)`.$/))
})
})
4 changes: 2 additions & 2 deletions test/fixture/custom-file-path/components/my-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

<script>
export default {
asyncData ({ isClient }) {
asyncData () {
return {
name: (isClient ? 'client' : 'server')
name: process.client ? 'client' : 'server'
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/fixture/disable-parse-pages/components/my-page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<h1>Hello {{ name }}</h1>
</template>

<script>
export default {
asyncData () {
return {
name: process.client ? 'client' : 'server'
}
}
}
</script>
10 changes: 10 additions & 0 deletions test/fixture/disable-parse-pages/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
rootDir: __dirname,
buildModules: [
'../../../src/module.ts'
],
routerModule: {
keepDefaultRouter: true,
parsePages: false
}
}
3 changes: 3 additions & 0 deletions test/fixture/disable-parse-pages/pages/foo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<h1>foo</h1>
</template>
16 changes: 16 additions & 0 deletions test/fixture/disable-parse-pages/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Router from 'vue-router'
import MyPage from '~/components/my-page'

export function createRouter (ssrContext, createDefaultRouter, routerOptions) {
const options = routerOptions || createDefaultRouter(ssrContext).options

return new Router({
...options,
routes: [
{
path: '/',
component: MyPage
}
]
})
}
4 changes: 2 additions & 2 deletions test/fixture/ok/components/my-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

<script>
export default {
asyncData ({ isClient }) {
asyncData () {
return {
name: (isClient ? 'client' : 'server')
name: process.client ? 'client' : 'server'
}
}
}
Expand Down
Loading

0 comments on commit 4ebf7e6

Please sign in to comment.