Skip to content

Commit 75cf5cd

Browse files
feat: typescript rewrite (#23)
1 parent 5f17c5a commit 75cf5cd

33 files changed

+1335
-852
lines changed

.eslintignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@ node_modules
33
dist
44
.nuxt
55
coverage
6-
7-
# Plugin
8-
lib/plugin.js

.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
os: [ubuntu-latest, macos-latest, windows-latest]
18-
node: [10, 12]
18+
node: [12]
1919

2020
steps:
2121
- uses: actions/setup-node@v2

.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 & 4 deletions
This file was deleted.

package.json

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,38 @@
55
"repository": "nuxt-community/netlify-files-module",
66
"license": "MIT",
77
"contributors": [
8-
{
9-
"name": "Alexander Lichter <[email protected]>"
10-
}
8+
"Alexander Lichter <[email protected]>",
9+
"Ricardo Gobbo de Souza <[email protected]>"
1110
],
12-
"main": "lib/module.js",
11+
"main": "./dist/module.js",
12+
"types": "./dist/module.d.ts",
1313
"files": [
14-
"lib"
14+
"dist"
1515
],
1616
"scripts": {
17-
"lint": "eslint --ext .js,.vue .",
18-
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
19-
"test": "yarn lint && jest"
17+
"build": "siroc build",
18+
"prepublishOnly": "yarn build",
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"
2022
},
2123
"dependencies": {
2224
"@iarna/toml": "^2.2.5",
2325
"consola": "^2.15.0",
26+
"defu": "^3.2.2",
2427
"node-html-parser": "^3.0.3"
2528
},
2629
"devDependencies": {
27-
"@commitlint/cli": "latest",
28-
"@commitlint/config-conventional": "latest",
29-
"@nuxtjs/eslint-config": "latest",
30-
"@nuxtjs/module-test-utils": "latest",
30+
"@babel/preset-typescript": "latest",
31+
"@nuxt/test-utils": "latest",
32+
"@nuxt/types": "latest",
33+
"@nuxtjs/eslint-config-typescript": "latest",
34+
"@types/jest": "latest",
35+
"@types/node": "latest",
3136
"eslint": "latest",
32-
"husky": "latest",
3337
"jest": "latest",
34-
"nuxt-edge": "latest",
38+
"nuxt": "latest",
39+
"siroc": "latest",
3540
"standard-version": "latest"
3641
},
3742
"publishConfig": {
Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
1-
const { resolve } = require('path')
2-
const { copyFileSync, existsSync, writeFileSync } = require('fs')
3-
const { parse } = require('node-html-parser')
4-
const tomlStringify = require('@iarna/toml/stringify')
5-
const logger = require('./logger')
1+
import { resolve } from 'path'
2+
import { copyFileSync, existsSync, writeFileSync } from 'fs'
3+
import type { Module } from '@nuxt/types'
4+
import { parse, HTMLElement } from 'node-html-parser'
5+
import { stringify as tomlStringify } from '@iarna/toml'
6+
import consola from 'consola'
7+
import defu from 'defu'
8+
import { name, version } from '../package.json'
9+
10+
const logger = consola.withTag('nuxt:netlify-files')
11+
12+
export type NetlifyToml = object
13+
14+
export interface ModuleOptions {
15+
copyExistingFiles?: boolean,
16+
detectForms?: boolean,
17+
existingFilesDirectory?: string,
18+
netlifyToml?: NetlifyToml | (() => NetlifyToml)
19+
}
620

21+
const CONFIG_KEY = 'netlifyFiles'
722
const HEADERS_FILE_NAME = '_headers'
823
const REDIRECTS_FILE_NAME = '_redirects'
924
const TOML_FILE_NAME = 'netlify.toml'
1025
const FILE_NAMES = [HEADERS_FILE_NAME, REDIRECTS_FILE_NAME, TOML_FILE_NAME]
1126

12-
module.exports = function (moduleOptions) {
13-
const { srcDir } = this.options
14-
const DEFAULT_OPTIONS = {
27+
const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
28+
const DEFAULTS: ModuleOptions = {
1529
copyExistingFiles: true,
1630
detectForms: false,
17-
existingFilesDirectory: srcDir,
31+
existingFilesDirectory: this.options.srcDir,
1832
netlifyToml: undefined
1933
}
20-
const options = {
21-
...DEFAULT_OPTIONS,
22-
...this.options['netlify-files'],
23-
...this.options.netlifyFiles,
24-
...moduleOptions
25-
}
2634

27-
const forms = []
35+
const options: ModuleOptions = defu(
36+
this.options['netlify-files'],
37+
this.options[CONFIG_KEY],
38+
moduleOptions,
39+
DEFAULTS
40+
)
2841

29-
if (options.detectForms) {
30-
const isNetlifyForm = (form) => {
31-
return form.getAttribute('netlify') === '' || form.getAttribute('netlify') === 'true' ||
32-
form.getAttribute('data-netlify') === '' || form.getAttribute('data-netlify') === 'true'
33-
}
42+
const forms: HTMLElement[] = []
3443

44+
if (options.detectForms) {
3545
this.nuxt.hook('generate:page', function ({ html }) {
36-
forms.push(...parse(html).querySelectorAll('form').filter(el => isNetlifyForm(el)))
46+
forms.push(...parse(html).querySelectorAll('form').filter((form) => {
47+
return form.getAttribute('netlify') === '' ||
48+
form.getAttribute('netlify') === 'true' ||
49+
form.getAttribute('data-netlify') === '' ||
50+
form.getAttribute('data-netlify') === 'true'
51+
}))
3752
})
3853
}
3954

40-
this.nuxt.hook('generate:done', () => {
55+
this.nuxt.hook('generate:done', async () => {
4156
// prevent netlify.toml from being copied when it has been generated programmatically
4257
const filesToCopy = options.netlifyToml ? FILE_NAMES.filter(file => file !== TOML_FILE_NAME) : FILE_NAMES
4358

44-
programmaticallyCreateToml.bind(this)(options)
59+
await programmaticallyCreateToml.bind(this)(options)
4560
copyExistingNetlifyFiles.bind(this)(options, filesToCopy)
4661
generatePageWithForms.bind(this)(options, forms)
4762
})
4863
}
4964

50-
async function programmaticallyCreateToml ({ netlifyToml }) {
65+
async function programmaticallyCreateToml ({ netlifyToml }: ModuleOptions) {
5166
if (!netlifyToml) {
5267
return
5368
}
@@ -63,7 +78,7 @@ async function programmaticallyCreateToml ({ netlifyToml }) {
6378
writeFileSync(destination, toml)
6479
}
6580

66-
function copyExistingNetlifyFiles ({ existingFilesDirectory, copyExistingFiles }, filesToCopy) {
81+
function copyExistingNetlifyFiles ({ existingFilesDirectory, copyExistingFiles }: ModuleOptions, filesToCopy: string[]) {
6782
if (!copyExistingFiles) {
6883
return
6984
}
@@ -82,7 +97,7 @@ function copyExistingNetlifyFiles ({ existingFilesDirectory, copyExistingFiles }
8297
})
8398
}
8499

85-
function generatePageWithForms ({ detectForms }, forms) {
100+
function generatePageWithForms ({ detectForms }: ModuleOptions, forms: HTMLElement[]) {
86101
if (!detectForms) {
87102
return
88103
}
@@ -95,4 +110,11 @@ function generatePageWithForms ({ detectForms }, forms) {
95110
}
96111
}
97112

98-
module.exports.meta = require('../package.json')
113+
;(nuxtModule as any).meta = { name, version }
114+
115+
declare module '@nuxt/types' {
116+
interface NuxtConfig { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.14+
117+
interface Configuration { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.9 - 2.13
118+
}
119+
120+
export default nuxtModule

test/basic.test.js

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

test/basic.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { existsSync } from 'fs'
2+
import { getNuxt, setupTest } from '@nuxt/test-utils'
3+
import { resolvePaths } from './utils'
4+
5+
describe('basic', () => {
6+
setupTest({
7+
generate: true,
8+
fixture: 'fixture/basic'
9+
})
10+
11+
test('it copies netlify files', () => {
12+
const { headersPath, redirectsPath, tomlPath } = resolvePaths(getNuxt())
13+
14+
expect(existsSync(headersPath)).toBe(true)
15+
expect(existsSync(redirectsPath)).toBe(true)
16+
expect(existsSync(tomlPath)).toBe(true)
17+
})
18+
})

test/disable.test.js

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

test/disable.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { existsSync } from 'fs'
2+
import { getNuxt, setupTest } from '@nuxt/test-utils'
3+
import { resolvePaths } from './utils'
4+
5+
describe('disable', () => {
6+
setupTest({
7+
generate: true,
8+
fixture: 'fixture/disable'
9+
})
10+
11+
test('should not copies netlify files', () => {
12+
const { headersPath, redirectsPath, tomlPath } = resolvePaths(getNuxt())
13+
14+
expect(existsSync(headersPath)).toBe(false)
15+
expect(existsSync(redirectsPath)).toBe(false)
16+
expect(existsSync(tomlPath)).toBe(false)
17+
})
18+
})

test/fixture/basic/nuxt.config.js

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

test/fixture/disable/nuxt.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module.exports = {
1+
export default {
22
rootDir: __dirname,
33
buildModules: [
4-
{ handler: require('../../../') }
4+
'../../../src/module.ts'
55
],
66
netlifyFiles: {
77
copyExistingFiles: false

test/fixture/forms-invalid/nuxt.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module.exports = {
1+
export default {
22
rootDir: __dirname,
33
buildModules: [
4-
{ handler: require('../../../') }
4+
'../../../src/module.ts'
55
],
66
netlifyFiles: {
77
detectForms: true

test/fixture/forms/nuxt.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module.exports = {
1+
export default {
22
rootDir: __dirname,
33
buildModules: [
4-
{ handler: require('../../../') }
4+
'../../../src/module.ts'
55
],
66
netlifyFiles: {
77
detectForms: true

test/fixture/programmatically-func/nuxt.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module.exports = {
1+
export default {
22
rootDir: __dirname,
33
buildModules: [
4-
{ handler: require('../../../') }
4+
'../../../src/module.ts'
55
],
66
netlifyFiles: {
77
netlifyToml: () => {

test/fixture/programmatically/nuxt.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module.exports = {
1+
export default {
22
rootDir: __dirname,
33
buildModules: [
4-
{ handler: require('../../../') }
4+
'../../../src/module.ts'
55
],
66
netlifyFiles: {
77
netlifyToml: {

test/fixture/warn/nuxt.config.js

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

0 commit comments

Comments
 (0)