IMPR: Add ts-loader

This commit is contained in:
Tony Air 2021-09-12 22:49:47 +02:00
parent 92e8cfa5b5
commit ff5f84f500
3 changed files with 166 additions and 165 deletions

View File

@ -172,6 +172,8 @@
"terser-webpack-plugin": "^5.2.3", "terser-webpack-plugin": "^5.2.3",
"ts-invariant": "^0.9.1", "ts-invariant": "^0.9.1",
"tslib": "^2.3.1", "tslib": "^2.3.1",
"ts-loader": "^9.2.5",
"typescript": "^4.4.3",
"url": "^0.11.0", "url": "^0.11.0",
"url-loader": "^4.1.1", "url-loader": "^4.1.1",
"webpack": "^5.52.0", "webpack": "^5.52.0",

View File

@ -6,10 +6,9 @@ const conf = common.configuration;
const webpack = require('webpack'); const webpack = require('webpack');
const { const {
merge merge,
} = require('webpack-merge'); } = require('webpack-merge');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -30,107 +29,107 @@ const PORT = process.env.PORT || conf.PORT;
let plugins = common.plugins; let plugins = common.plugins;
if (COMPRESS) { if (COMPRESS) {
plugins.push(require('autoprefixer')); plugins.push(require('autoprefixer'));
/*plugins.push( /*plugins.push(
new ImageSpritePlugin({ new ImageSpritePlugin({
exclude: /exclude|original|default-|icons|sprite|svg|logo|favicon/, exclude: /exclude|original|default-|icons|sprite|svg|logo|favicon/,
commentOrigin: false, commentOrigin: false,
compress: COMPRESS, compress: COMPRESS,
extensions: ['png'], extensions: ['png'],
indent: '', indent: '',
log: true, log: true,
//outputPath: path.join(__dirname, conf.APPDIR, conf.DIST), //outputPath: path.join(__dirname, conf.APPDIR, conf.DIST),
outputFilename: 'img/sprite-[hash].png', outputFilename: 'img/sprite-[hash].png',
padding: 0, padding: 0,
}), }),
);*/ );*/
} }
const indexPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'index.html'); const indexPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'index.html');
if (fs.existsSync(indexPath)) { if (fs.existsSync(indexPath)) {
plugins.push( plugins.push(
new HtmlWebpackPlugin({ new HtmlWebpackPlugin({
publicPath: '', publicPath: '',
template: path.join(conf.APPDIR, conf.SRC, 'index.html'), template: path.join(conf.APPDIR, conf.SRC, 'index.html'),
templateParameters: { templateParameters: {
NODE_ENV: NODE_ENV, NODE_ENV: NODE_ENV,
GRAPHQL_URL: conf['GRAPHQL_URL'], GRAPHQL_URL: conf['GRAPHQL_URL'],
STATIC_URL: conf['STATIC_URL'], STATIC_URL: conf['STATIC_URL'],
REACT_SCRIPTS: NODE_ENV === 'production' ? REACT_SCRIPTS: NODE_ENV === 'production' ?
'<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>' : '<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>', '<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>' : '<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>',
}, },
xhtml: true, xhtml: true,
}), }),
); );
} }
const faviconPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'favicon.png'); const faviconPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'favicon.png');
if (fs.existsSync(faviconPath)) { if (fs.existsSync(faviconPath)) {
plugins.push( plugins.push(
new FaviconsWebpackPlugin({ new FaviconsWebpackPlugin({
title: 'Webpack App', title: 'Webpack App',
logo: faviconPath, logo: faviconPath,
prefix: '/icons/', prefix: '/icons/',
emitStats: false, emitStats: false,
persistentCache: true, persistentCache: true,
inject: false, inject: false,
statsFilename: path.join( statsFilename: path.join(
conf.APPDIR, conf.APPDIR,
conf.DIST, conf.DIST,
'icons', 'icons',
'iconstats.json', 'iconstats.json',
), ),
icons: { icons: {
android: true, android: true,
appleIcon: true, appleIcon: true,
appleStartup: true, appleStartup: true,
coast: true, coast: true,
favicons: true, favicons: true,
firefox: true, firefox: true,
opengraph: true, opengraph: true,
twitter: true, twitter: true,
yandex: true, yandex: true,
windows: true, windows: true,
}, },
}), }),
); );
} }
// add themes favicons // add themes favicons
common.themes.forEach((theme) => { common.themes.forEach((theme) => {
const faviconPath = path.join(__dirname, theme, conf.SRC, 'favicon.png'); const faviconPath = path.join(__dirname, theme, conf.SRC, 'favicon.png');
if (fs.existsSync(faviconPath)) { if (fs.existsSync(faviconPath)) {
plugins.push( plugins.push(
new FaviconsWebpackPlugin({ new FaviconsWebpackPlugin({
title: 'Webpack App', title: 'Webpack App',
logo: faviconPath, logo: faviconPath,
prefix: '/' + theme + '-icons/', prefix: '/' + theme + '-icons/',
emitStats: false, emitStats: false,
persistentCache: true, persistentCache: true,
inject: false, inject: false,
statsFilename: path.join( statsFilename: path.join(
conf.APPDIR, conf.APPDIR,
conf.DIST, conf.DIST,
theme + '-icons', theme + '-icons',
'iconstats.json', 'iconstats.json',
), ),
icons: { icons: {
android: true, android: true,
appleIcon: true, appleIcon: true,
appleStartup: true, appleStartup: true,
coast: true, coast: true,
favicons: true, favicons: true,
firefox: true, firefox: true,
opengraph: true, opengraph: true,
twitter: true, twitter: true,
yandex: true, yandex: true,
windows: true, windows: true,
}, },
}), }),
); );
} }
}); });
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer') const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin; .BundleAnalyzerPlugin;
@ -138,14 +137,14 @@ plugins.push(
new BundleAnalyzerPlugin({ new BundleAnalyzerPlugin({
analyzerMode: 'static', analyzerMode: 'static',
openAnalyzer: false, openAnalyzer: false,
}), }),
); );
const cfg = merge(common.webpack, { const cfg = merge(common.webpack, {
mode: NODE_ENV, mode: NODE_ENV,
cache: { cache: {
type: 'filesystem', type: 'filesystem',
}, },
recordsPath: path.join(__dirname, conf.APPDIR, conf.DIST, 'records.json'), recordsPath: path.join(__dirname, conf.APPDIR, conf.DIST, 'records.json'),
optimization: { optimization: {
//removeAvailableModules: false, //removeAvailableModules: false,
@ -153,7 +152,7 @@ const cfg = merge(common.webpack, {
splitChunks: { splitChunks: {
name: 'vendor', name: 'vendor',
minChunks: 2, minChunks: 2,
}, },
concatenateModules: true, //ModuleConcatenationPlugin concatenateModules: true, //ModuleConcatenationPlugin
minimizer: [ minimizer: [
new TerserPlugin({ new TerserPlugin({
@ -166,7 +165,7 @@ const cfg = merge(common.webpack, {
// sections only apply transformations that are ecma 5 safe // sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234 // https://github.com/facebook/create-react-app/pull/4234
ecma: 8, ecma: 8,
}, },
compress: { compress: {
ecma: 5, ecma: 5,
warnings: false, warnings: false,
@ -175,7 +174,7 @@ const cfg = merge(common.webpack, {
// Pending further investigation: // Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011 // https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false, comparisons: false,
}, },
keep_fnames: true, keep_fnames: true,
keep_classnames: true, keep_classnames: true,
@ -184,19 +183,19 @@ const cfg = merge(common.webpack, {
keep_fnames: true, keep_fnames: true,
keep_classnames: true, keep_classnames: true,
reserved: ['$', 'jQuery', 'jquery'], reserved: ['$', 'jQuery', 'jquery'],
}, },
output: { output: {
ecma: 5, ecma: 5,
comments: false, comments: false,
// Turned on because emoji and regex is not minified properly using default // Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488 // https://github.com/facebook/create-react-app/issues/2488
ascii_only: true, ascii_only: true,
}, },
}, },
// Use multi-process parallel running to improve the build speed // Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1 // Default number of concurrent runs: os.cpus().length - 1
parallel: true, parallel: true,
}), }),
new CssMinimizerPlugin({ new CssMinimizerPlugin({
parallel: true, parallel: true,
minimizerOptions: [{ minimizerOptions: [{
@ -204,8 +203,8 @@ const cfg = merge(common.webpack, {
'default', 'default',
{ {
discardComments: { discardComments: {
removeAll: true removeAll: true,
}, },
zindex: true, zindex: true,
cssDeclarationSorter: true, cssDeclarationSorter: true,
reduceIdents: false, reduceIdents: false,
@ -215,25 +214,25 @@ const cfg = merge(common.webpack, {
discardUnused: true, discardUnused: true,
discardOverridden: true, discardOverridden: true,
discardDuplicates: true, discardDuplicates: true,
}, },
], ],
}, ], },],
minify: [ minify: [
CssMinimizerPlugin.cssnanoMinify, CssMinimizerPlugin.cssnanoMinify,
//CssMinimizerPlugin.cleanCssMinify, //CssMinimizerPlugin.cleanCssMinify,
] ],
}), }),
], ],
}, },
output: { output: {
publicPath: path.join(conf.APPDIR, conf.DIST), publicPath: path.join(conf.APPDIR, conf.DIST),
path: path.join(__dirname, conf.APPDIR, conf.DIST), path: path.join(__dirname, conf.APPDIR, conf.DIST),
filename: path.join('js', '[name].js'), filename: path.join('js', '[name].js'),
}, },
module: { module: {
rules: [{ rules: [{ test: /\.tsx?$/, loader: 'ts-loader' }, {
test: /\.jsx?$/, test: /\.jsx?$/,
//exclude: /node_modules/, //exclude: /node_modules/,
use: { use: {
@ -247,48 +246,48 @@ const cfg = merge(common.webpack, {
plugins: [ plugins: [
'@babel/plugin-proposal-class-properties', '@babel/plugin-proposal-class-properties',
], ],
}, },
], //Preset used for env setup ], //Preset used for env setup
plugins: [ plugins: [
['@babel/transform-react-jsx'] ['@babel/transform-react-jsx'],
], ],
cacheDirectory: true, cacheDirectory: true,
cacheCompression: true, cacheCompression: true,
}, },
}, },
}, },
{ {
test: /\.s?css$/, test: /\.s?css$/,
use: [{ use: [{
loader: MiniCssExtractPlugin.loader, loader: MiniCssExtractPlugin.loader,
}, },
{ {
loader: 'css-loader', loader: 'css-loader',
options: { options: {
sourceMap: true, sourceMap: true,
}, },
}, },
{ {
loader: 'resolve-url-loader', loader: 'resolve-url-loader',
}, },
{ {
loader: 'sass-loader', loader: 'sass-loader',
options: { options: {
sourceMap: true, sourceMap: true,
}, },
}, ], },],
}, },
{ {
test: /fontawesome([^.]+).(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, test: /fontawesome([^.]+).(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
type: "asset/resource", type: 'asset/resource',
}, },
{ {
test: /\.(ttf|otf|eot|woff(2)?)$/, test: /\.(ttf|otf|eot|woff(2)?)$/,
type: "asset/resource", type: 'asset/resource',
}, },
{ {
test: /\.(png|webp|jpg|jpeg|gif|svg)$/, test: /\.(png|webp|jpg|jpeg|gif|svg)$/,
type: "javascript/auto", type: 'javascript/auto',
use: [{ use: [{
loader: 'img-optimize-loader', loader: 'img-optimize-loader',
options: { options: {
@ -303,33 +302,33 @@ const cfg = merge(common.webpack, {
// loseless compression for png // loseless compression for png
optipng: { optipng: {
optimizationLevel: 4, optimizationLevel: 4,
}, },
// lossy compression for png. This will generate smaller file than optipng. // lossy compression for png. This will generate smaller file than optipng.
pngquant: { pngquant: {
quality: [0.2, 0.8], quality: [0.2, 0.8],
}, },
// Compression for svg. // Compression for svg.
svgo: true, svgo: true,
// Compression for gif. // Compression for gif.
gifsicle: { gifsicle: {
optimizationLevel: 3, optimizationLevel: 3,
}, },
// Compression for jpg. // Compression for jpg.
mozjpeg: { mozjpeg: {
progressive: true, progressive: true,
quality: 60, quality: 60,
}, },
}, },
inline: { inline: {
limit: 1, limit: 1,
}, },
}, },
}, ], },],
}, ], },],
}, },
plugins: plugins, plugins: plugins,
}); });
console.log(cfg); console.log(cfg);
module.exports = cfg; module.exports = cfg;

View File

@ -10,7 +10,7 @@ const fs = require('fs');
//const autoprefixer = require('autoprefixer'); //const autoprefixer = require('autoprefixer');
const webpack = require('webpack'); const webpack = require('webpack');
const { const {
merge merge,
} = require('webpack-merge'); } = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin');
@ -23,27 +23,27 @@ let plugins = common.plugins;
const indexPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'index.html'); const indexPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'index.html');
if (fs.existsSync(indexPath)) { if (fs.existsSync(indexPath)) {
plugins.push( plugins.push(
new HtmlWebpackPlugin({ new HtmlWebpackPlugin({
publicPath: '', publicPath: '',
template: path.join(conf.APPDIR, conf.SRC, 'index.html'), template: path.join(conf.APPDIR, conf.SRC, 'index.html'),
templateParameters: { templateParameters: {
NODE_ENV: 'development', NODE_ENV: 'development',
GRAPHQL_URL: conf['GRAPHQL_URL'], GRAPHQL_URL: conf['GRAPHQL_URL'],
STATIC_URL: conf['STATIC_URL'], STATIC_URL: conf['STATIC_URL'],
REACT_SCRIPTS: '<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>', REACT_SCRIPTS: '<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>',
}, },
}), }),
); );
} }
const config = merge(common.webpack, { const config = merge(common.webpack, {
entry: { entry: {
/*hot: [ /*hot: [
'react-hot-loader/patch', 'react-hot-loader/patch',
'webpack-dev-server/?https://' + conf.HOSTNAME + ':' + conf.PORT, 'webpack-dev-server/?https://' + conf.HOSTNAME + ':' + conf.PORT,
'webpack/hot/only-dev-server', 'webpack/hot/only-dev-server',
],*/ ],*/
}, },
output: { output: {
@ -51,10 +51,10 @@ const config = merge(common.webpack, {
filename: '[name].js', filename: '[name].js',
// necessary for HMR to know where to load the hot update chunks // necessary for HMR to know where to load the hot update chunks
publicPath: `http${conf['HTTPS'] ? 's' : ''}://${conf['HOSTNAME']}:${conf.PORT}/`, publicPath: `http${conf['HTTPS'] ? 's' : ''}://${conf['HOSTNAME']}:${conf.PORT}/`,
}, },
module: { module: {
rules: [{ rules: [{ test: /\.tsx?$/, loader: 'ts-loader' }, {
test: /\.jsx?$/, test: /\.jsx?$/,
//exclude: /node_modules/, //exclude: /node_modules/,
use: { use: {
@ -77,42 +77,42 @@ const config = merge(common.webpack, {
], ],
cacheDirectory: true, cacheDirectory: true,
cacheCompression: true,*/ cacheCompression: true,*/
}, },
}, },
}, },
{ {
test: /\.s?css$/, test: /\.s?css$/,
use: [{ use: [{
loader: MiniCssExtractPlugin.loader, loader: MiniCssExtractPlugin.loader,
}, },
{ {
loader: 'css-loader', loader: 'css-loader',
options: { options: {
sourceMap: true, sourceMap: true,
}, },
}, },
{ {
loader: 'resolve-url-loader', loader: 'resolve-url-loader',
}, },
{ {
loader: 'sass-loader', loader: 'sass-loader',
options: { options: {
sourceMap: true, sourceMap: true,
}, },
}, ], },],
}, },
{ {
test: /fontawesome([^.]+).(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, test: /fontawesome([^.]+).(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
type: "asset/resource", type: 'asset/resource',
}, },
{ {
test: /\.(gif|png|jpg|jpeg|ttf|otf|eot|svg|webp|woff(2)?)$/, test: /\.(gif|png|jpg|jpeg|ttf|otf|eot|svg|webp|woff(2)?)$/,
type: "asset/resource", type: 'asset/resource',
}, { }, {
test: /\.(png|webp|jpg|jpeg|gif|svg)$/, test: /\.(png|webp|jpg|jpeg|gif|svg)$/,
type: "asset/resource", type: 'asset/resource',
}, ], },],
}, },
plugins: plugins, plugins: plugins,
mode: 'development', mode: 'development',
@ -130,8 +130,8 @@ const config = merge(common.webpack, {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Referrer-Policy': 'unsafe-url', 'Referrer-Policy': 'unsafe-url',
'service-worker-allowed': '/', 'service-worker-allowed': '/',
}, },
}, },
}); });
module.exports = config; module.exports = config;