Skip to content

Commit 3de869e

Browse files
committed
refactor: rewrite with typescript
1 parent 872dce1 commit 3de869e

38 files changed

+2583
-3959
lines changed

.eslintignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ dist
55
coverage
66

77
# Plugin
8-
lib/**/plugin.js
9-
lib/**/templates
8+
templates
109

1110
# Fixtures
1211
test/fixture/**/static

.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.

jest.config.js

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

lib/meta/meta.merge.js renamed to lib/meta.utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function mergeMeta (to, from) {
1+
export function mergeMeta (to, from) {
22
if (typeof to === 'function') {
33
// eslint-disable-next-line no-console
44
console.warn('Cannot merge meta. Avoid using head as a function!')

lib/meta/module.runtime.js

-14
This file was deleted.

lib/icon/resize.js renamed to lib/resize.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const path = require('path')
2-
const Jimp = require('jimp-compact')
3-
const { normalizeSize, sizeName } = require('../utils')
2+
const jimp = require('jimp-compact')
43

54
async function resize ({ input, distDir, sizes }) {
6-
const inputFile = await Jimp.read(input)
5+
const inputFile = await jimp.read(input)
76

87
// Icons
98
await Promise.all(sizes.map(normalizeSize).map((size) => {
@@ -21,3 +20,23 @@ resize(JSON.parse(process.argv[2])).then(() => {
2120
console.error(error) // eslint-disable-line no-console
2221
process.exit(1)
2322
})
23+
24+
// TODO: Dedup
25+
function sizeName (size) {
26+
size = normalizeSize(size)
27+
const prefix = size[2] ? (size[2] + '_') : ''
28+
return prefix + size[0] + 'x' + size[1]
29+
}
30+
31+
// TODO: Dedup
32+
function normalizeSize (size) {
33+
if (!Array.isArray(size)) {
34+
size = [size, size]
35+
}
36+
if (size.length === 1) {
37+
size = [size, size]
38+
} else if (size.length === 0) {
39+
size = 64
40+
}
41+
return size
42+
}

lib/utils/index.js

-80
This file was deleted.

lib/workbox/utils.js

-49
This file was deleted.

package.json

+16-15
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
"description": "Zero config PWA solution for Nuxt.js",
55
"repository": "nuxt-community/pwa-module",
66
"license": "MIT",
7-
"main": "lib/module.js",
7+
"main": "dist/pwa.js",
8+
"types": "./types/index.d.ts",
89
"files": [
910
"lib",
11+
"dist",
12+
"templates",
1013
"types"
1114
],
12-
"types": "./types/index.d.ts",
1315
"scripts": {
14-
"dev": "nuxt test/fixture",
15-
"lint": "eslint --ext .js,.vue .",
16+
"build": "siroc build",
17+
"dev": "nuxt-ts test/fixture",
18+
"lint": "eslint --ext .js,.vue,.ts .",
1619
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
1720
"test": "yarn lint && jest"
1821
},
@@ -23,30 +26,28 @@
2326
"hasha": "^5.2.2",
2427
"jimp-compact": "^0.16.1",
2528
"lodash.template": "^4.5.0",
29+
"serve-static": "^1.14.1",
2630
"workbox-cdn": "^5.1.4"
2731
},
2832
"devDependencies": {
33+
"@babel/preset-typescript": "^7.12.7",
34+
"@nuxt/test-utils": "^0.0.1",
2935
"@nuxt/types": "^2.14.7",
36+
"@nuxt/typescript-build": "^2.0.3",
37+
"@nuxt/typescript-runtime": "^2.0.0",
3038
"@nuxtjs/eslint-config": "latest",
39+
"@nuxtjs/eslint-config-typescript": "^5.0.0",
3140
"@nuxtjs/module-test-utils": "latest",
3241
"@types/workbox-sw": "^4.3.1",
33-
"axios": "latest",
3442
"babel-eslint": "latest",
3543
"codecov": "latest",
3644
"eslint": "latest",
37-
"eslint-config-standard": "latest",
38-
"eslint-plugin-html": "latest",
39-
"eslint-plugin-import": "latest",
40-
"eslint-plugin-jest": "latest",
41-
"eslint-plugin-node": "latest",
42-
"eslint-plugin-promise": "latest",
43-
"eslint-plugin-standard": "latest",
4445
"jest": "latest",
45-
"jsdom": "latest",
4646
"klaw-sync": "latest",
47-
"lerna": "latest",
4847
"node-fetch": "latest",
4948
"nuxt-edge": "latest",
50-
"standard-version": "latest"
49+
"siroc": "^0.4.3",
50+
"standard-version": "latest",
51+
"typescript": "^4.1.2"
5152
}
5253
}

lib/icon/module.js renamed to src/icon.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
const path = require('path')
2-
const { fork } = require('child_process')
3-
const { join } = require('path')
4-
const fs = require('fs-extra')
5-
const hasha = require('hasha')
6-
const { joinUrl, getRouteParams, sizeName, emitAsset } = require('../utils')
7-
const { version } = require('../../package.json')
8-
9-
module.exports = async function (nuxt, pwa, moduleContainer) {
1+
import { join, resolve } from 'path'
2+
import { fork } from 'child_process'
3+
import fs from 'fs-extra'
4+
import hasha from 'hasha'
5+
import type { IconOptions } from '../types/icon'
6+
import { joinUrl, getRouteParams, sizeName, emitAsset, PKG, PKG_DIR } from './utils'
7+
8+
export async function icon (nuxt, pwa, moduleContainer) {
109
const { publicPath } = getRouteParams(nuxt.options)
1110

1211
// Defaults
13-
const defaults = {
12+
const defaults: IconOptions = {
1413
sizes: [64, 120, 144, 152, 192, 384, 512],
1514

1615
iosSizes: [
@@ -38,14 +37,15 @@ module.exports = async function (nuxt, pwa, moduleContainer) {
3837

3938
publicPath,
4039

40+
// @ts-ignore
4141
_iconHash: null,
4242
_assets: null,
4343
_manifestIcons: null,
4444
_iosSplash: null
4545
}
4646

4747
// Merge options
48-
const options = {
48+
const options: IconOptions = {
4949
...defaults,
5050
...pwa.icon
5151
}
@@ -56,7 +56,7 @@ module.exports = async function (nuxt, pwa, moduleContainer) {
5656
// Disable module if no icon specified
5757
if (!options.source) {
5858
// eslint-disable-next-line no-console
59-
console.warn('[pwa] [icon] Icon not found in ' + path.resolve(nuxt.options.srcDir, nuxt.options.dir.static, options.fileName))
59+
console.warn('[pwa] [icon] Icon not found in ' + resolve(nuxt.options.srcDir, nuxt.options.dir.static, options.fileName))
6060
return
6161
}
6262

@@ -90,8 +90,8 @@ module.exports = async function (nuxt, pwa, moduleContainer) {
9090
function findIcon (nuxt, options) {
9191
const iconSearchPath = [
9292
options.source,
93-
path.resolve(nuxt.options.srcDir, nuxt.options.dir.static, options.fileName),
94-
path.resolve(nuxt.options.srcDir, nuxt.options.dir.assets, options.fileName)
93+
resolve(nuxt.options.srcDir, nuxt.options.dir.static, options.fileName),
94+
resolve(nuxt.options.srcDir, nuxt.options.dir.assets, options.fileName)
9595
].filter(p => p)
9696

9797
for (const source of iconSearchPath) {
@@ -109,8 +109,8 @@ function addPlugin (_nuxt, options, moduleContainer) {
109109

110110
if (options.plugin) {
111111
moduleContainer.addPlugin({
112-
src: path.resolve(__dirname, './plugin.js'),
113-
fileName: 'pwa/icons.js',
112+
src: resolve(PKG_DIR, 'templates/icon.plugin.js'),
113+
fileName: 'pwa/icon.plugin.js',
114114
options: {
115115
pluginName: options.pluginName,
116116
icons
@@ -173,14 +173,14 @@ function emitAssets (nuxt, options) {
173173
const resizePromise = resizeIcons(nuxt, options)
174174

175175
for (const { name, target } of options._assets) {
176-
const srcFileName = path.join(options.cacheDir, `${name}.png`)
176+
const srcFileName = join(options.cacheDir, `${name}.png`)
177177
emitAsset(nuxt, target, resizePromise.then(() => fs.readFile(srcFileName)))
178178
}
179179
}
180180

181181
async function resizeIcons (_nuxt, options) {
182182
const resizeOpts = JSON.stringify({
183-
version,
183+
version: PKG.version,
184184
input: options.source,
185185
distDir: options.cacheDir,
186186
sizes: [
@@ -189,18 +189,19 @@ async function resizeIcons (_nuxt, options) {
189189
]
190190
})
191191

192-
const integrityFile = path.join(options.cacheDir, '.' + hasha(resizeOpts).substr(0, 8))
192+
const integrityFile = join(options.cacheDir, '.' + hasha(resizeOpts).substr(0, 8))
193193

194194
if (fs.existsSync(integrityFile)) {
195195
return
196196
}
197197
await fs.remove(options.cacheDir)
198198
await fs.mkdirp(options.cacheDir)
199199

200-
await new Promise((resolve, reject) => {
201-
const child = fork(require.resolve('./resize'), [resizeOpts], { execArgv: [] })
200+
// eslint-disable-next-line promise/param-names
201+
await new Promise((_resolve, _reject) => {
202+
const child = fork(resolve(PKG_DIR, 'lib/resize.js'), [resizeOpts], { execArgv: [] })
202203
child.on('exit', (code) => {
203-
return code ? reject(code) : resolve()
204+
return code ? _reject(code) : _resolve()
204205
})
205206
})
206207

0 commit comments

Comments
 (0)