Skip to content

Commit

Permalink
Add: Rewrite Webpack config with TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Nov 2, 2023
1 parent 73c41c5 commit a0b71e8
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 40 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ node_modules
dist
pnpm-lock.yaml
Dockerfile
package.json
*.md

1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ node_modules
.next
pnpm-lock.yaml
Dockerfile
package.json
/README.md
/SUSHI.md
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
"license": "MIT",
"version": "2.0.0",
"scripts": {
"build-chrome": "pnpm webpack --mode production --config webpack.prod.js --env browser=chrome",
"build-chrome:dev": "pnpm webpack --mode development --config webpack.dev.js --env browser=chrome",
"watch": "pnpm webpack --mode development --watch --config webpack.dev.js",
"build": "pnpm run build-chrome",
"build:dev": "pnpm run build-chrome:dev",
"watch": "pnpm run watch-chrome",
"build-chrome": "pnpm webpack --mode production --env browser=chrome",
"build-chrome:dev": "pnpm webpack --mode development --env browser=chrome",
"watch-chrome": "pnpm webpack --mode development --watch --env browser=chrome",
"build-firefox": "pnpm webpack --mode production --env browser=firefox",
"build-firefox:dev": "pnpm webpack --mode development --env browser=firefox",
"watch-firefox": "pnpm webpack --mode development --watch --env browser=firefox",
"fmt": "prettier --write '**/*.{js,json,md,html,sh}'",
"lint": "prettier --check . && eslint . ",
"lint:fix": "pnpm fmt && eslint . --fix"
Expand Down Expand Up @@ -59,8 +65,7 @@
"tsconfig-paths-webpack-plugin": "^4.1.0",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-merge": "^5.10.0"
"webpack-cli": "^5.1.4"
},
"volta": {
"node": "20.9.0"
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prettier.config.js → prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import("prettier").Config} */
module.exports = {
export default {
tabWidth: 4,
singleQuote: false,
trailingComma: "all",
Expand Down
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
"paths": {
"@/*": ["src/*"]
}
},
"ts-node": {
"compilerOptions": {
"module": "CommonJS"
}
}
}
46 changes: 35 additions & 11 deletions webpack.common.js → webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
const CopyPlugin = require("copy-webpack-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");
const fs = require("fs");
const HtmlPlugin = require("html-webpack-plugin");
const path = require("path");
const { TsconfigPathsPlugin } = require("tsconfig-paths-webpack-plugin");
import CopyPlugin from "copy-webpack-plugin";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import ESLintPlugin from "eslint-webpack-plugin";
import fs from "fs";
import HtmlPlugin from "html-webpack-plugin";
import path from "path";
import { TsconfigPathsPlugin } from "tsconfig-paths-webpack-plugin";
import { Configuration } from "webpack";

const loadFilesInScripts = function () {
const dir = path.join(__dirname, "src/scripts");
const files = fs.readdirSync(dir);
const entry = {};
const entry = {} as {
[key: string]: string;
};
files.forEach(function (file) {
const name = file.replace(".ts", "").replace("content_", "");
entry[name] = path.join(dir, file);
});
return entry;
};

module.exports = (env) => {
console.log("env", env);
interface Env {
browser?: "chrome" | "firefox";
WEBPACK_BUILD: boolean;
WEBPACK_BUNDLE: boolean;
}

interface Argv {
mode: "development" | "production";
env: Env;
}

module.exports = (env: Env, argv: Argv): Configuration => {
console.log("Build as " + argv.mode + " mode");

if (env.browser === undefined) {
throw new Error("Please set browser");
}

return {
entry: {
popup: "./src/popup/index.tsx",
Expand Down Expand Up @@ -50,7 +69,6 @@ module.exports = (env) => {
{
test: /\.(css|sass|scss|pcss)/,
use: [
CssMinimizerPlugin.loader,
"style-loader",
{
loader: "css-loader",
Expand All @@ -68,6 +86,10 @@ module.exports = (env) => {
],
},

optimization: {
minimizer: [new CssMinimizerPlugin()],
},

plugins: [
new ESLintPlugin({ extensions: ["js", "jsx", "ts", "tsx"] }),
new CopyPlugin({
Expand Down Expand Up @@ -112,5 +134,7 @@ module.exports = (env) => {
maxEntrypointSize: 2000000,
maxAssetSize: 2000000,
},

devtool: argv.mode !== "development" ? false : "source-map",
};
};
10 changes: 0 additions & 10 deletions webpack.dev.js

This file was deleted.

8 changes: 0 additions & 8 deletions webpack.prod.js

This file was deleted.

0 comments on commit a0b71e8

Please sign in to comment.