Skip to content

Commit 21767c5

Browse files
authoredApr 8, 2022
feat: typescript rewrite (#74)
1 parent 7529bf6 commit 21767c5

23 files changed

+4508
-4546
lines changed
 

‎.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"@nuxtjs/eslint-config-typescript"
4+
]
5+
}

‎.eslintrc.js

-6
This file was deleted.

‎.github/workflows/ci.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: ci
33
on:
44
push:
55
branches:
6-
- master
6+
- main
77
pull_request:
88
branches:
9-
- master
9+
- main
1010

1111
jobs:
1212
ci:
@@ -16,15 +16,15 @@ jobs:
1616
matrix:
1717
# os: [ubuntu-latest, macos-latest, windows-latest]
1818
os: [ubuntu-latest]
19-
node: [10, 12, 14, 15]
19+
node: [12]
2020

2121
steps:
2222
- uses: actions/setup-node@v3
2323
with:
2424
node-version: ${{ matrix.node }}
2525

2626
- name: checkout
27-
uses: actions/checkout@master
27+
uses: actions/checkout@v3
2828

2929
- name: cache node_modules
3030
uses: actions/cache@v3

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ node_modules
77
.DS_Store
88
coverage
99
dist
10+
sw.*
11+
.env

‎commitlint.config.js

-5
This file was deleted.

‎husky.config.js

-7
This file was deleted.

‎jest.config.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = {
2-
collectCoverage: true,
3-
collectCoverageFrom: ['lib/**/*.js'],
4-
testEnvironment: 'node'
2+
preset: '@nuxt/test-utils',
3+
collectCoverageFrom: ['src/**']
54
}

‎lib/logger.js

-3
This file was deleted.

‎lib/module.js

-46
This file was deleted.

‎lib/utils.js

-11
This file was deleted.

‎package.json

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@
77
"contributors": [
88
"Ricardo Gobbo de Souza <ricardogobbosouza@yahoo.com.br>"
99
],
10-
"main": "lib/module.js",
10+
"main": "./dist/module.js",
11+
"types": "./dist/module.d.ts",
1112
"files": [
12-
"lib"
13+
"dist"
1314
],
1415
"scripts": {
15-
"dev": "nuxt test/fixture",
16-
"lint": "eslint --ext .js,.vue .",
17-
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
18-
"test": "yarn lint && jest"
16+
"build": "siroc build",
17+
"prepublishOnly": "yarn build",
18+
"dev": "nuxt dev test/fixture/basic",
19+
"lint": "eslint --ext .js,.ts,.vue .",
20+
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
21+
"test": "yarn lint && yarn jest"
1922
},
2023
"dependencies": {
2124
"consola": "^2.15.3",
25+
"defu": "^6.0.0",
2226
"eslint-webpack-plugin": "^2.6.0"
2327
},
2428
"devDependencies": {
25-
"@commitlint/cli": "latest",
26-
"@commitlint/config-conventional": "latest",
27-
"@nuxtjs/eslint-config": "latest",
28-
"@nuxtjs/module-test-utils": "latest",
29+
"@babel/preset-typescript": "latest",
30+
"@nuxt/test-utils": "latest",
31+
"@nuxt/types": "latest",
32+
"@nuxtjs/eslint-config-typescript": "latest",
33+
"@types/jest": "latest",
34+
"@types/node": "latest",
2935
"eslint": "latest",
30-
"husky": "latest",
3136
"jest": "latest",
32-
"nuxt-edge": "latest",
37+
"nuxt": "latest",
38+
"siroc": "latest",
3339
"standard-version": "latest"
3440
},
3541
"peerDependencies": {

‎src/module.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { resolve } from 'path'
2+
import consola from 'consola'
3+
import { defu } from 'defu'
4+
import type { Module } from '@nuxt/types'
5+
import type { Options } from 'eslint-webpack-plugin'
6+
import { name, version } from '../package.json'
7+
import { moduleExists } from './utils'
8+
9+
const logger = consola.withTag('nuxt:eslint')
10+
11+
export interface ModuleOptions extends Partial<Options>{}
12+
13+
const CONFIG_KEY = 'eslint'
14+
15+
const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
16+
const DEFAULTS: ModuleOptions = {
17+
context: this.options.srcDir,
18+
eslintPath: 'eslint',
19+
extensions: ['js', 'ts', 'vue'],
20+
cache: true,
21+
lintDirtyModulesOnly: true
22+
}
23+
24+
const options: ModuleOptions = defu(
25+
this.options[CONFIG_KEY],
26+
moduleOptions,
27+
DEFAULTS
28+
)
29+
30+
if (!moduleExists(options.eslintPath)) {
31+
logger.warn(
32+
`The dependency \`${options.eslintPath}\` not found.`,
33+
'Please run `yarn add eslint --dev` or `npm install eslint --save-dev`'
34+
)
35+
return
36+
}
37+
38+
const filesToWatch = [
39+
'.eslintrc',
40+
'.eslintrc.json',
41+
'.eslintrc.yaml',
42+
'.eslintrc.yml',
43+
'.eslintrc.js'
44+
]
45+
46+
this.options.watch = this.options.watch || []
47+
this.options.watch.push(...filesToWatch.map(file => resolve(this.options.rootDir, file)))
48+
49+
this.extendBuild((config, { isDev, isClient }) => {
50+
if (isDev && isClient) {
51+
const EslintPlugin = require('eslint-webpack-plugin')
52+
53+
config.plugins.push(new EslintPlugin(options))
54+
}
55+
})
56+
}
57+
58+
;(nuxtModule as any).meta = { name, version }
59+
60+
declare module '@nuxt/types' {
61+
interface NuxtConfig { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.14+
62+
interface Configuration { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.9 - 2.13
63+
}
64+
65+
export default nuxtModule

‎src/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function moduleExists (name) {
2+
try {
3+
return require.resolve(name)
4+
} catch (e) {
5+
return false
6+
}
7+
}

‎test/dev.test.js

-18
This file was deleted.

‎test/dev.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { setupTest, get } from '@nuxt/test-utils'
2+
3+
describe('dev', () => {
4+
setupTest({
5+
fixture: 'fixture',
6+
server: true,
7+
config: {
8+
dev: true
9+
}
10+
})
11+
12+
test('render', async () => {
13+
const { body } = await get('/')
14+
expect(body).toContain('Works!')
15+
})
16+
})

‎test/fixture/nuxt.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module.exports = {
1+
export default {
22
rootDir: __dirname,
3-
buildModules: [
4-
{ handler: require('../../') }
3+
modules: [
4+
'../../src/module.ts'
55
]
66
}

‎test/fixture/pages/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
<script>
88
export default {
9-
9+
name: 'PageIndex'
1010
}
1111
</script>

‎test/prod.test.js

-18
This file was deleted.

‎test/prod.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { setupTest, get } from '@nuxt/test-utils'
2+
3+
describe('prod', () => {
4+
setupTest({
5+
fixture: 'fixture',
6+
server: true,
7+
config: {
8+
dev: false
9+
}
10+
})
11+
12+
test('render', async () => {
13+
const { body } = await get('/')
14+
expect(body).toContain('Works!')
15+
})
16+
})

‎test/warn.test.js

-36
This file was deleted.

‎test/warn.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { setupTest } from '@nuxt/test-utils'
2+
3+
const mockReporter = {
4+
warn: jest.fn()
5+
}
6+
7+
jest.mock('consola', () => ({
8+
info: jest.fn(),
9+
success: jest.fn(),
10+
debug: jest.fn(),
11+
warn: jest.fn(),
12+
withTag: jest.fn().mockImplementation(() => mockReporter)
13+
}))
14+
15+
describe('warn', () => {
16+
setupTest({
17+
fixture: 'fixture',
18+
server: true,
19+
config: {
20+
dev: true,
21+
eslint: {
22+
eslintPath: 'foo'
23+
}
24+
}
25+
})
26+
27+
test('should warn if not found the `eslint` dependency', () => {
28+
expect(mockReporter.warn).toHaveBeenCalledWith(
29+
'The dependency `foo` not found.',
30+
'Please run `yarn add eslint --dev` or `npm install eslint --save-dev`'
31+
)
32+
})
33+
})

‎tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
6+
"resolveJsonModule": true,
7+
"strict": false,
8+
"types": [
9+
"node",
10+
"jest"
11+
]
12+
},
13+
"exclude": [
14+
"node_modules",
15+
"dist"
16+
]
17+
}

‎yarn.lock

+4,319-4,373
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.