Skip to content

Commit 9297742

Browse files
committed
Update tsconfig, webpack & pretierrc config
1 parent 637c1b8 commit 9297742

File tree

3 files changed

+147
-46
lines changed

3 files changed

+147
-46
lines changed

.prettierrc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"printWidth": 100,
3-
"semi": true,
4-
"singleQuote": true,
5-
"jsxSingleQuote": true,
6-
"tabWidth": 2,
7-
"bracketSpacing": true,
8-
"trailingComma": "none"
9-
}
2+
"printWidth": 100,
3+
"semi": true,
4+
"singleQuote": true,
5+
"jsxSingleQuote": true,
6+
"tabWidth": 2,
7+
"bracketSpacing": true,
8+
"trailingComma": "all"
9+
}

tsconfig.json

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
11
{
22
"compilerOptions": {
3-
"outDir": "./src/extension/build/bundles/",
4-
"module": "es6",
5-
// "noImplicitAny": true,
3+
/* Basic Options */
4+
//---------------------LANGUAGE AND ENVIRONEMNT-----------------------------
5+
// Specifies the ECMAScript version that the TypeScript compiler should target. This allow the transpiled file being used in older browser.
66
"target": "es5",
7+
// Specifies the syntax for writing React components in TypeScript
78
"jsx": "react",
8-
"removeComments": true,
9-
"allowJs": true,
10-
"allowSyntheticDefaultImports": true,
11-
"typeRoots": [
12-
"./node_modules/@types"
13-
],
9+
10+
// --------------------------MODULES----------------------------------------
11+
// Specifies the module format for the compiled TypeScript files
12+
"module": "es6",
13+
// Specifies the folder where TypeScript should look for type definition files.
14+
"typeRoots": ["./node_modules/@types"],
15+
// Specifies which global types to include in the project.
16+
// Specifies type package names to be included without being referenced in a source file.
1417
"types": ["chrome", "jest", "node"],
15-
"esModuleInterop": true,
18+
19+
// ---------------------------------TYPE CHECKING---------------------------
20+
// "strict": true, /* Enable all strict type-checking options. */
21+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
22+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
23+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
24+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
25+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
26+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
27+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
28+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
29+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
30+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
31+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
32+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
33+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
34+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
35+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
36+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
37+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
38+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
39+
40+
// -------------------------------EMIT--------------------------------------
41+
// Specifies TypeScript compiler to remove comments from the compiled JavaScript code.
42+
"removeComments": true,
43+
44+
// --------------------------JAVASCRIPT SUPPORT-----------------------------
45+
// Specifies TypeScript compiler to allow JavaScript files to be included in the compilation process. => good to use if want to incrementally convert javascript to typescript files
46+
"allowJs": true,
47+
48+
// Specifies the module resolution strategy to use.
1649
"moduleResolution": "node",
50+
51+
// Enables TypeScript to import JSON files as modules.
1752
"resolveJsonModule": true,
53+
54+
//------------------------INTEROP CONSTRAINTS------------------------------
55+
// Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility.
56+
"esModuleInterop": true,
57+
// Specifies TypeScript compiler to allow importing modules with a default export from modules
58+
// some third party modules may still use
59+
"allowSyntheticDefaultImports": true,
60+
// Transiple each file as a seprate module => expect all files to have import/export statement
61+
// "isolatedModules": true,
62+
// Ensures that file names are consistently cased.
1863
"forceConsistentCasingInFileNames": true,
64+
65+
// -----------------------------COMPLETENESS--------------------------------
66+
// Skip type checking of type declartion files (.d.ts files) that are included with TypeScript. If we trust the type definition of our imported file, turn this flag on => reduce compiling time.
67+
"skipLibCheck": true
1968
},
69+
70+
// Specifies which files should be included in the compilation.
2071
"include": ["./src/app", "./src/backend", "./src/extension", "./jest-setup.ts", "./global.d.ts"],
72+
// Specifies which files should be excluded from the compilation.
2173
"exclude": ["./src/app/__tests__", "./src/backend/__tests__", "node_modules"],
74+
75+
// Specifies options for TypeDoc, a documentation generator for TypeScript. In this case, it tells TypeDoc to generate documentation for the project in "file" mode, and to output the documentation to a "docs" folder.
2276
"typeDocOptions": {
2377
"mode": "file",
2478
"out": "docs"
25-
},
26-
}
79+
}
80+
}

webpack.config.js

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
const path = require('path');
3-
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader'); // enable hot reloading while developing a chrome extension
3+
4+
/** ChromeExtensionReloader plugin is a tool for hot-reloading code in a Chrome extension during development.
5+
* It works by injecting a script into the extension that listens for file changes and automatically reloads the extension when a file is modified.
6+
*/
7+
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader');
8+
49
// const TypedocWebpackPlugin = require('typedoc-webpack-plugin');
510

611
const config = {
712
// use a "multi-main entry" to inject multiple dependent files together
813
// and graph their dependencies into one "chunk"
914
entry: {
10-
// app: './src/app/index.js',
1115
app: './src/app/index.tsx',
1216
background: './src/extension/background.js',
1317
content: './src/extension/contentScript.ts',
14-
backend: './src/backend/index.ts'
18+
backend: './src/backend/index.ts',
1519
},
20+
/**
21+
* Bundle each entry point into one "chunk" & store it in the extension/build/bundles folder
22+
* When load the unpacked extension in the chrome extension store, the src/extension/build folder is selected => load all bundles (app, backend, background & content script)
23+
*/
1624
output: {
1725
path: path.resolve(__dirname, 'src/extension/build/bundles'),
18-
filename: '[name].bundle.js'
26+
filename: '[name].bundle.js',
1927
},
28+
2029
node: {
2130
net: 'empty',
22-
tls: 'empty'
31+
tls: 'empty',
2332
},
2433
module: {
34+
/** The order of rules array is bottom to top.
35+
* In your rules array, the order is:
36+
* 1 .css and .scss files will be evaluated first will use the style-loader and css-loader, as well as the sass-loader (only applicable for .scss files).
37+
* 2 .tsx and .ts files will be evaluated third and will use the ts-loader.
38+
* 3 .jsx and .js files will be evaluated last and will use the babel-loader to transpile them into code that is compatible with older browsers.
39+
*/
2540
rules: [
41+
/**
42+
* For all files ending .js or .jsx, except those in node_modules
43+
* => transpile them into code that is compatible with older browser using babel-loader
44+
*/
2645
{
2746
test: /\.jsx?/,
2847
exclude: /(node_modules)/,
2948
resolve: {
30-
extensions: ['.js', '.jsx']
49+
extensions: ['.js', '.jsx'],
3150
},
3251
use: {
3352
loader: 'babel-loader',
@@ -38,35 +57,46 @@ const config = {
3857
{
3958
useBuiltIns: 'entry',
4059
corejs: 3,
41-
debug: true
42-
}
60+
debug: true,
61+
},
4362
],
4463

4564
'@babel/preset-react',
4665
{
47-
plugins: ['@babel/plugin-proposal-class-properties']
48-
}
49-
]
50-
}
51-
}
66+
plugins: ['@babel/plugin-proposal-class-properties'],
67+
},
68+
],
69+
},
70+
},
5271
},
72+
/**
73+
* For all files ending in .ts or .tsx, except those in node_modules
74+
* => transpile typescript files into javascript file.
75+
*/
5376
{
5477
test: /\.tsx?$/,
5578
use: 'ts-loader',
5679
exclude: /node_modules/,
5780
resolve: {
58-
extensions: ['.tsx', '.ts', '.js']
59-
}
81+
extensions: ['.tsx', '.ts', '.js'],
82+
},
6083
},
84+
/**
85+
* For all files ending in .scss or .css files
86+
* Since sass-loader will only works with .scss & .sass files, for any .css file, webpack will skip sass-loader and use css-loader, then style-loader.
87+
*/
6188
{
62-
test: /\.scss$/,
63-
use: ['style-loader', 'css-loader', 'sass-loader']
89+
test: /\.s?css$/,
90+
use: [
91+
// Creates `style` nodes from JS strings
92+
'style-loader',
93+
// Translates CSS into CommonJS
94+
'css-loader',
95+
// Compiles Sass to CSS
96+
'sass-loader',
97+
],
6498
},
65-
{
66-
test: /\.css$/,
67-
use: ['style-loader', 'css-loader']
68-
}
69-
]
99+
],
70100
},
71101
plugins: [
72102
// new TypedocWebpackPlugin({
@@ -76,19 +106,36 @@ const config = {
76106
// includeDeclarations: false,
77107
// ignoreCompilerErrors: true,
78108
// }),
79-
]
109+
],
110+
111+
// Add `.ts` and `.tsx` as a resolvable extension.
112+
resolve: {
113+
extensions: ['.ts', '.tsx', '.js'],
114+
},
80115
};
81116

82117
module.exports = (env, argv) => {
118+
/**
119+
* env stands for "environment" and is an object that contains environment-specific configuration properties.
120+
* argv stands for "argument vector" and is an object that contains the arguments passed to Webpack via the command line interface.
121+
* argv.mode is an argument that is parsed by the Webpack CLI to specify the build mode.
122+
* For example, running webpack --mode=production will set argv.mode to 'production'.
123+
*/
83124
if (argv.mode === 'development') {
125+
/**
126+
* "cheap-module-source-map" is a type of source map in webpack.
127+
* A source map is a file that maps the source code to the compiled code, making it easier to debug and trace issues in the original source code.
128+
* devtool is option to control if & how source maps are generated
129+
* https://webpack.js.org/configuration/devtool/#root
130+
*/
84131
config.devtool = 'cheap-module-source-map';
85132
config.plugins.push(
86133
new ChromeExtensionReloader({
87134
entries: {
88135
contentScript: ['app', 'content'],
89-
background: ['background']
90-
}
91-
})
136+
background: ['background'],
137+
},
138+
}),
92139
);
93140
} else {
94141
config.mode = 'production';

0 commit comments

Comments
 (0)