Skip to content

Commit

Permalink
feat: typescript rewrite (#94)
Browse files Browse the repository at this point in the history
* feat: typescript rewrite

* chore: change minimum supported node version to 12

* chore: istanbul ignore

* fix: ignore istanbul
  • Loading branch information
ricardogobbosouza authored Mar 4, 2021
1 parent bab21a7 commit 0c6a034
Show file tree
Hide file tree
Showing 25 changed files with 1,528 additions and 702 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ dist
coverage

# Plugin
lib/plugin.js
templates/plugin.js
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@nuxtjs/eslint-config-typescript"
]
}
6 changes: 0 additions & 6 deletions .eslintrc.js

This file was deleted.

1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE.md

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
# os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest]
node: [10]
node: [12]

steps:
- uses: actions/setup-node@v2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ node_modules
.DS_Store
coverage
dist
sw.*
.env
5 changes: 2 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
collectCoverage: true,
collectCoverageFrom: ['lib/**/*.js', '!lib/plugin.js'],
testEnvironment: 'node'
preset: '@nuxt/test-utils',
collectCoverageFrom: ['src/**']
}
3 changes: 0 additions & 3 deletions lib/logger.js

This file was deleted.

31 changes: 20 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,35 @@
"Sebastien Chopin <[email protected]>",
"Ricardo Gobbo de Souza <[email protected]>"
],
"main": "lib/module.js",
"main": "./dist/module.js",
"types": "./dist/module.d.ts",
"files": [
"lib"
"dist"
],
"scripts": {
"dev": "nuxt test/fixture/ok",
"lint": "eslint --ext .js,.vue .",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
"build": "siroc build",
"prepublishOnly": "yarn build",
"dev": "nuxt dev test/fixture/ok",
"lint": "eslint --ext .js,.ts,.vue .",
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && yarn jest"
},
"dependencies": {
"consola": "^2.15.3"
"consola": "^2.15.3",
"defu": "^3.2.2"
},
"devDependencies": {
"@nuxtjs/eslint-config": "latest",
"@nuxtjs/module-test-utils": "latest",
"@babel/preset-typescript": "latest",
"@nuxt/test-utils": "latest",
"@nuxt/types": "latest",
"@nuxtjs/eslint-config-typescript": "latest",
"@types/jest": "latest",
"@types/node": "latest",
"del": "latest",
"eslint": "latest",
"husky": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"nuxt": "latest",
"siroc": "latest",
"standard-version": "latest"
},
"publishConfig": {
Expand Down
49 changes: 37 additions & 12 deletions lib/module.js → src/module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
const { relative, resolve } = require('path')
const { existsSync } = require('fs')
const logger = require('./logger')
import { relative, resolve } from 'path'
import { existsSync } from 'fs'
import type { Module } from '@nuxt/types'
import consola from 'consola'
import defu from 'defu'
import { name, version } from '../package.json'

module.exports = function (moduleOptions) {
const options = {
const logger = consola.withTag('nuxt:router')

export interface ModuleOptions {
path: string;
fileName: string;
keepDefaultRouter?: boolean;
}

const CONFIG_KEY = 'routerModule'

const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
const DEFAULTS: ModuleOptions = {
path: this.options.srcDir,
fileName: 'router.js',
keepDefaultRouter: false,
...this.options['router-module'],
...this.options.routerModule,
...moduleOptions
keepDefaultRouter: false
}

const options: ModuleOptions = defu(
this.options['router-module'],
this.options[CONFIG_KEY],
moduleOptions,
DEFAULTS
)

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

// Check if router file path is defined
Expand All @@ -22,7 +39,7 @@ module.exports = function (moduleOptions) {

// Add plugin to import router file path as the main template for routing
this.addPlugin({
src: resolve(__dirname, 'plugin.js'),
src: resolve(__dirname, '../templates/plugin.js'),
fileName: 'router.js',
options: {
routerFilePath: relative(this.options.buildDir, routerFilePath).replace(/\/+|\\+/g, '/'),
Expand All @@ -40,11 +57,12 @@ module.exports = function (moduleOptions) {
}

// Put default router as .nuxt/defaultRouter.js
let defaultRouter
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')
} catch (err) {
Expand All @@ -59,4 +77,11 @@ module.exports = function (moduleOptions) {
})
}

module.exports.meta = require('../package.json')
;(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
File renamed without changes.
18 changes: 0 additions & 18 deletions test/custom-file-path.test.js

This file was deleted.

13 changes: 13 additions & 0 deletions test/custom-file-path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { setupTest, get } from '@nuxt/test-utils'

describe('custom-file-path', () => {
setupTest({
server: true,
fixture: 'fixture/custom-file-path'
})

test('render', async () => {
const { body } = await get('/')
expect(body).toContain('Hello server')
})
})
24 changes: 0 additions & 24 deletions test/fail.test.js

This file was deleted.

24 changes: 24 additions & 0 deletions test/fail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { setupTest } 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)
}))

describe('fail', () => {
setupTest({
build: true,
fixture: 'fixture/fail'
})

test('should warn if not found the router file', () => {
expect(mockReporter.warn).toHaveBeenCalledWith(expect.stringMatching(/^No `(.*)` file found in `(.*)`.$/))
})
})
4 changes: 2 additions & 2 deletions test/fixture/custom-file-path/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { join } = require('path')

module.exports = {
export default {
rootDir: __dirname,
buildModules: [
{ handler: require('../../../') }
'../../../src/module.ts'
],
routerModule: {
path: join(__dirname, 'routes'),
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/fail/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
export default {
rootDir: __dirname,
buildModules: [
{ handler: require('../../../') }
'../../../src/module.ts'
]
}
4 changes: 2 additions & 2 deletions test/fixture/keep-default-router/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
export default {
rootDir: __dirname,
buildModules: [
{ handler: require('../../../') }
'../../../src/module.ts'
],
routerModule: {
keepDefaultRouter: true
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/ok/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
export default {
rootDir: __dirname,
buildModules: [
{ handler: require('../../../') }
'../../../src/module.ts'
]
}
18 changes: 0 additions & 18 deletions test/keep-default-router.test.js

This file was deleted.

13 changes: 13 additions & 0 deletions test/keep-default-router.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { setupTest, get } from '@nuxt/test-utils'

describe('keep-default-router', () => {
setupTest({
server: true,
fixture: 'fixture/keep-default-router'
})

test('render', async () => {
const { body } = await get('/some/foo')
expect(body).toContain('Hello page')
})
})
18 changes: 0 additions & 18 deletions test/ok.test.js

This file was deleted.

13 changes: 13 additions & 0 deletions test/ok.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { setupTest, get } from '@nuxt/test-utils'

describe('ok', () => {
setupTest({
server: true,
fixture: 'fixture/ok'
})

test('render', async () => {
const { body } = await get('/')
expect(body).toContain('Hello server')
})
})
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"strict": false,
"types": [
"node",
"jest"
]
},
"exclude": [
"node_modules",
"dist"
]
}
Loading

0 comments on commit 0c6a034

Please sign in to comment.