Skip to content

Commit 21767c5

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

23 files changed

+4508
-4546
lines changed

.eslintrc

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

.eslintrc.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 5 deletions
This file was deleted.

husky.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

jest.config.js

Lines changed: 2 additions & 3 deletions
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

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/module.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

lib/utils.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

package.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@
77
"contributors": [
88
"Ricardo Gobbo de Souza <[email protected]>"
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

Lines changed: 65 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/dev.test.ts

Lines changed: 16 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/prod.test.ts

Lines changed: 16 additions & 0 deletions
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

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)