Skip to content

Commit 87d2923

Browse files
committed
Use typescript for webpack
1 parent f03ec92 commit 87d2923

File tree

12 files changed

+553
-325
lines changed

12 files changed

+553
-325
lines changed

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.json" {
2+
const value: any
3+
export default value
4+
}

package-lock.json

Lines changed: 428 additions & 238 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "timer",
33
"version": "0.1.7",
44
"description": "Web timer",
5+
"homepage": "https://github.com/sheepzh/timer",
56
"scripts": {
6-
"dev": "webpack --mode=development --config=webpack/webpack.dev.js --watch",
7-
"build": "webpack --mode=production --config=webpack/webpack.prod.js",
7+
"dev": "webpack --mode=development --config=webpack/webpack.dev.ts --watch",
8+
"build": "webpack --mode=production --config=webpack/webpack.prod.ts",
89
"test": "jest",
910
"test-c": "jest --coverage"
1011
},
@@ -13,6 +14,8 @@
1314
"devDependencies": {
1415
"@types/chrome": "0.0.145",
1516
"@types/jest": "^26.0.23",
17+
"@types/node": "^15.12.2",
18+
"@types/webpack": "^5.28.0",
1619
"babel-loader": "^8.2.2",
1720
"copy-webpack-plugin": "^9.0.0",
1821
"css-loader": "^5.2.6",
@@ -24,14 +27,17 @@
2427
"style-loader": "^2.0.0",
2528
"ts-jest": "^27.0.3",
2629
"ts-loader": "^9.2.3",
30+
"ts-node": "^10.0.0",
2731
"tslib": "^2.2.0",
2832
"typescript": "^4.3.2",
2933
"url-loader": "^4.1.1",
3034
"webpack": "^5.38.1",
3135
"webpack-cli": "^4.7.2"
3236
},
3337
"dependencies": {
38+
"@types/copy-webpack-plugin": "^8.0.0",
3439
"@types/echarts": "^4.9.8",
40+
"@types/generate-json-webpack-plugin": "^0.3.3",
3541
"echarts": "^5.1.2",
3642
"element-plus": "*",
3743
"file-saver": "^2.0.5",

src/locale/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import trender, { TrenderMessage } from './components/trender'
99
import menu, { MenuMessage } from './components/menu'
1010
import setting, { SettingMessage } from './components/setting'
1111

12-
type I18nMessage = {
12+
export type I18nMessage = {
1313
lang: { name: string }
1414
app: AppMessage
1515
message: MsgMessage,
@@ -65,6 +65,5 @@ export default messages
6565

6666
// Used for export json file of chrome
6767
// @see [project-path]/webpack/plugins/generate-locale-for-chrome.js
68-
// And this can be auto injected via this plugin in the future
69-
const globalAny: any = global
68+
const globalAny = global as any
7069
globalAny.exportsToChrome = messages

src/manifest.js renamed to src/manifest.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
* @author zhy
55
* @since 0.0.1
66
*/
7-
const { version, author, homepage } = require('../package.json')
8-
// const { defaultLocale } = require('./locale/index')
9-
module.exports = {
7+
// @ts-ignore
8+
import * as packageInfo from '../package.json'
9+
const { version, author, homepage } = packageInfo as any
10+
export default {
1011
name: '__MSG_app_marketName__',
1112
description: "__MSG_app_description__",
1213
version,

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"target": "es5",
5+
"esModuleInterop": true,
56
"sourceMap": true,
7+
"resolveJsonModule": true,
68
"importHelpers": true,
79
"moduleResolution": "node"
810
},

webpack/index.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Declare for 'filemanager-webpack-plugin'
2+
declare module 'filemanager-webpack-plugin' {
3+
type Endpoint = 'onEnd'
4+
// Sequence
5+
type OperationSeq = Partial<Record<CommandName, CommandConfig>>
6+
type CommandName = 'delete' | 'copy' | 'archive' | 'move'
7+
type CommandConfig = string[] | { source: string, destination: string }[]
8+
9+
type Options = Partial<Record<Endpoint, OperationSeq[]>>
10+
11+
class FilemanagerWebpackPlugin {
12+
constructor(config: { events: Options })
13+
}
14+
export default FilemanagerWebpackPlugin
15+
}

webpack/plugins/generate-locale-for-chrome.js renamed to webpack/plugins/generate-locale-for-chrome.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const path = require('path')
2-
const fs = require('fs')
1+
import * as path from 'path'
2+
import * as fs from 'fs'
3+
import webpack from 'webpack'
34

45
// Genearate the messages used by Chrome
5-
function translate (obj, parentKey = '') {
6+
function translate(obj: any, parentKey = ''): any {
67
const result = {}
78
if (typeof obj === 'object') {
89
for (const key in obj) {
@@ -27,23 +28,25 @@ function translate (obj, parentKey = '') {
2728
/**
2829
* The plugin to generate locale message files for browser
2930
*/
30-
class GenerateLocaleForChrome {
31+
class GenerateLocaleForChrome implements webpack.WebpackPluginInstance {
32+
outputFileName: string
33+
outputFileImport: string
3134
/**
3235
* @param {*} name the file name to generate
3336
* @param {*} importFile
3437
*/
35-
constructor(name, importFile) {
38+
constructor(name: string, importFile: string) {
3639
this.outputFileName = `generate_locale_messages_for_chrome_${name}`
3740
this.outputFileImport = importFile
3841
}
39-
async apply (compiler) {
42+
async apply(compiler: webpack.Compiler) {
4043
const options = compiler.options
4144
options.entry[this.outputFileName] = { import: [this.outputFileImport] }
4245
const outputPath = options.output.path
4346
const outFilePath = path.join(outputPath, `${this.outputFileName}.js`)
4447
compiler.hooks.done.tap('GenerateLocaleForChromePlugin', () => {
4548
require(outFilePath)
46-
const messages = global.exportsToChrome
49+
const messages = (global as any).exportsToChrome
4750
for (const localeName in messages) {
4851
// .e.g
4952
// {
@@ -68,4 +71,4 @@ class GenerateLocaleForChrome {
6871
}
6972
}
7073

71-
module.exports = GenerateLocaleForChrome
74+
export default GenerateLocaleForChrome
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
const path = require('path')
2-
const GenerateJsonPlugin = require('generate-json-webpack-plugin')
3-
const GenerateLocaleForChrome = require('./plugins/generate-locale-for-chrome')
4-
const CopyWebpackPlugin = require('copy-webpack-plugin')
5-
1+
import * as path from 'path'
2+
import GenerateJsonPlugin from 'generate-json-webpack-plugin'
3+
import GenerateLocaleForChrome from './plugins/generate-locale-for-chrome'
4+
import CopyWebpackPlugin from 'copy-webpack-plugin'
5+
import * as webpack from 'webpack'
66
// Generate json files
7-
const manifest = require('../src/manifest')
8-
const generateJsonPlugins = [new GenerateJsonPlugin('manifest.json', manifest), new GenerateLocaleForChrome('locale', './src/locale')]
7+
import manifest from '../src/manifest'
8+
const generateJsonPlugins = [
9+
new GenerateJsonPlugin('manifest.json', manifest) as unknown as webpack.WebpackPluginInstance,
10+
new GenerateLocaleForChrome('locale', './src/locale')
11+
]
912

10-
const optionGenerator = (outputPath, manifestHooker) => {
13+
const optionGenerator = (outputPath: string, manifestHooker?: (config: webpack.Configuration) => void) => {
1114
manifestHooker && manifestHooker(manifest)
1215
const plugins = [
1316
...generateJsonPlugins,
@@ -18,7 +21,7 @@ const optionGenerator = (outputPath, manifestHooker) => {
1821
]
1922
})
2023
]
21-
return {
24+
const optionTemplate: webpack.Configuration = {
2225
entry: {
2326
background: './src/background',
2427
content_scripts: './src/content-script',
@@ -58,7 +61,7 @@ const optionGenerator = (outputPath, manifestHooker) => {
5861
extensions: ['.ts', ".js", '.css', '.scss', '.sass'],
5962
}
6063
}
64+
return optionTemplate
6165
}
6266

63-
64-
module.exports = optionGenerator
67+
export default optionGenerator
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const path = require('path')
2-
const GenerateJsonPlugin = require('generate-json-webpack-plugin')
3-
const FileManagerWebpackPlugin = require('filemanager-webpack-plugin')
4-
const optionGenerator = require('./webpack.common')
5-
const webpack = require('webpack')
6-
1+
import * as path from 'path'
2+
import GenerateJsonPlugin from 'generate-json-webpack-plugin'
3+
import FileManagerWebpackPlugin from 'filemanager-webpack-plugin'
4+
import optionGenerator from './webpack.common'
5+
import webpack from 'webpack'
76

87
const outputDir = path.join(__dirname, '..', 'dist_dev')
9-
let manifest
8+
let manifest: any
109

1110
const options = optionGenerator(
1211
outputDir,
@@ -18,7 +17,12 @@ const options = optionGenerator(
1817

1918
const manifestFirefoxName = 'manifest-firefox.json'
2019
// The manifest.json is different from Chrome's with add-on ID
21-
const firefoxManifestGeneratePlugin = new GenerateJsonPlugin(manifestFirefoxName, { ...manifest, browser_specific_settings: { gecko: { id: 'timer@zhy' } } })
20+
const firefoxManifestGeneratePlugin = new GenerateJsonPlugin(
21+
manifestFirefoxName,
22+
{
23+
...manifest, browser_specific_settings: { gecko: { id: 'timer@zhy' } }
24+
}
25+
) as unknown as webpack.WebpackPluginInstance
2226
options.plugins.push(firefoxManifestGeneratePlugin)
2327
const firefoxDevDir = path.join(__dirname, '..', 'firefox_dev')
2428
// Generate FireFox dev files
@@ -33,7 +37,7 @@ options.plugins.push(
3337
}
3438
]
3539
}
36-
}),
40+
}) as webpack.WebpackPluginInstance,
3741
new webpack.DefinePlugin({
3842
__VUE_OPTIONS_API__: false,
3943
__VUE_PROD_DEVTOOLS__: false

0 commit comments

Comments
 (0)