Skip to content

Commit

Permalink
refactor: migrate webpack config to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Jan 22, 2024
1 parent f4360fd commit 2434935
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 70 deletions.
5 changes: 0 additions & 5 deletions config/utils.js

This file was deleted.

3 changes: 3 additions & 0 deletions config/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path'

export const resolvePath = (dir = '') => path.join(__dirname, '..', dir)
27 changes: 10 additions & 17 deletions config/webpack.base.conf.js → config/webpack.base.conf.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const utils = require('./utils')
import type { Configuration } from 'webpack'
import CopyWebpackPlugin from 'copy-webpack-plugin'

/**
* @type {import('webpack').Configuration}
*/
module.exports = {
mode: process.env.NODE_ENV,
entry: utils.resolvePath('src/index.tsx'),
import { resolvePath } from './utils'

const baseWebpackConfig: Configuration = {
mode: process.env.NODE_ENV as any,
entry: resolvePath('src/index.tsx'),
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
},
Expand All @@ -28,7 +26,6 @@ module.exports = {
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.m?js/,
Expand All @@ -39,16 +36,10 @@ module.exports = {
],
},
plugins: [
new webpack.ProvidePlugin({
// Make a global `process` variable that points to the `process` package,
// because the `util` package expects there to be a global variable named `process`.
// Thanks to https://stackoverflow.com/a/65018686/14239942
process: 'process/browser',
}),
new CopyWebpackPlugin({
patterns: [
{
from: utils.resolvePath('public'),
from: resolvePath('public'),
globOptions: {
ignore: ['**/index.html'],
},
Expand All @@ -57,3 +48,5 @@ module.exports = {
}),
],
}

export default baseWebpackConfig
36 changes: 0 additions & 36 deletions config/webpack.dev.conf.js

This file was deleted.

38 changes: 38 additions & 0 deletions config/webpack.dev.conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import HtmlWebpackPlugin from 'html-webpack-plugin'
import type { Configuration as WebpackConfiguration } from 'webpack'
import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server'
import mergePlugin from 'webpack-merge'
import { resolvePath } from './utils'
import baseWebpackConfig from './webpack.base.conf'

interface Configuration extends WebpackConfiguration {
devServer?: WebpackDevServerConfiguration
}

const webpackConfig: Configuration = mergePlugin(baseWebpackConfig, {
mode: 'development',
// https://webpack.js.org/configuration/devtool/
devtool: 'cheap-module-source-map',
output: {
publicPath: '/',
},
plugins: [
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
template: resolvePath('public/index.html'),
inject: true,
}),
],
devServer: {
port: 3000,
open: true,
hot: true,
historyApiFallback: true,
static: { directory: resolvePath('public') },
},
performance: {
hints: false,
},
})

module.exports = webpackConfig
17 changes: 7 additions & 10 deletions config/webpack.prod.conf.js → config/webpack.prod.conf.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
const mergePlugin = require('webpack-merge')
const HtmlWebpackPlugin = require('html-webpack-plugin')
import HtmlWebpackPlugin from 'html-webpack-plugin'
import type { Configuration } from 'webpack'
import mergePlugin from 'webpack-merge'
import { resolvePath } from './utils'
import baseWebpackConfig from './webpack.base.conf'

const baseWebpackConfig = require('./webpack.base.conf')
const utils = require('./utils')

/**
* @type {import('webpack').Configuration}
*/
const webpackConfig = mergePlugin.merge(baseWebpackConfig, {
const webpackConfig: Configuration = mergePlugin(baseWebpackConfig, {
mode: 'production',
bail: true,
devtool: process.env.CI ? 'source-map' : false,
Expand All @@ -20,7 +17,7 @@ const webpackConfig = mergePlugin.merge(baseWebpackConfig, {
plugins: [
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
template: utils.resolvePath('public/index.html'),
template: resolvePath('public/index.html'),
inject: true,
minify: {
removeComments: true,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "a personal website",
"scripts": {
"start": "npm run dev",
"dev": "webpack serve --config config/webpack.dev.conf.js --mode=development",
"dev": "webpack serve --config config/webpack.dev.conf.ts --mode=development",
"fetchUserInfo": "ts-node scripts/fetchUserInfo.ts",
"build": "npm run fetchUserInfo && webpack --config config/webpack.prod.conf.js --mode=production",
"build": "npm run fetchUserInfo && webpack --config config/webpack.prod.conf.ts --mode=production",
"lint": "eslint --ext .ts,.tsx,.json .",
"lint:fix": "npm run lint -- --fix",
"clean": "rm -rf dist",
Expand Down

0 comments on commit 2434935

Please sign in to comment.