1 Star 2 Fork 2

OS-HUBU/hypertrons-crx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
webpack.config.js 5.36 KB
一键复制 编辑 原始数据 按行查看 历史
Harry 提交于 2023-05-17 15:26 +08:00 . refactor: reconstruct option management (#659)
import webpack from 'webpack';
import { fileURLToPath } from 'url';
import path, { dirname } from 'path';
import fileSystem from 'fs-extra';
import env from './utils/env.js';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const ASSET_PATH = process.env.ASSET_PATH || '/';
const alias = {
'react-dom': '@hot-loader/react-dom',
};
// load the secrets
const secretsPath = path.join(__dirname, 'secrets.' + env.NODE_ENV + '.js');
const fileExtensions = [
'jpg',
'jpeg',
'png',
'gif',
'eot',
'otf',
'svg',
'ttf',
'woff',
'woff2',
];
if (fileSystem.existsSync(secretsPath)) {
alias['secrets'] = secretsPath;
}
let options = {
mode: process.env.NODE_ENV || 'development',
entry: {
options: path.join(__dirname, 'src', 'pages', 'Options', 'index.jsx'),
popup: path.join(__dirname, 'src', 'pages', 'Popup', 'index.jsx'),
background: path.join(__dirname, 'src', 'pages', 'Background', 'index.ts'),
contentScript: path.join(
__dirname,
'src',
'pages',
'ContentScripts',
'index.ts'
),
},
// "custom" is not a standard key of webpack options
// it will be consumed by utils/server.js and must be deleted before webpack(config)
custom: {
notHMR: ['background', 'contentScript'],
enableBackgroundAutoReload: true, // always true when "enableContentScriptsAutoReload" is set true
enableContentScriptsAutoReload: true,
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'build'),
clean: true,
publicPath: ASSET_PATH,
},
module: {
rules: [
{
test: /README\.md$/,
use: [
{
loader: './utils/features-loader.cjs',
},
],
},
{
// look for .css or .scss files
test: /\.(css|scss)$/,
// in the `src` directory
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: new RegExp('.(' + fileExtensions.join('|') + ')$'),
type: 'asset/resource',
exclude: /node_modules/,
// loader: 'file-loader',
// options: {
// name: '[name].[ext]',
// },
},
{
test: /\.html$/,
loader: 'html-loader',
exclude: /node_modules/,
},
{ test: /\.(ts|tsx)$/, loader: 'ts-loader', exclude: /node_modules/ },
{
test: /\.(js|jsx)$/,
use: [
{
loader: 'source-map-loader',
},
{
loader: 'babel-loader',
},
],
exclude: /node_modules/,
},
],
},
resolve: {
alias: alias,
extensions: fileExtensions
.map((extension) => '.' + extension)
.concat(['.js', '.jsx', '.ts', '.tsx', '.css']),
},
plugins: [
new MiniCssExtractPlugin(),
new CleanWebpackPlugin({ verbose: false }),
new webpack.ProgressPlugin(),
// expose and write the allowed env vars on the compiled bundle
new webpack.EnvironmentPlugin({
NODE_ENV: env.NODE_ENV,
PORT: env.PORT,
}),
new CopyWebpackPlugin({
patterns: [
{
from: 'src/manifest.json',
to: path.join(__dirname, 'build'),
force: true,
transform: function (content, path) {
// generates the manifest file using the package.json informations
return Buffer.from(
JSON.stringify({
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString()),
})
);
},
},
],
}),
new CopyWebpackPlugin({
patterns: [
{
from: 'src/assets/img/main.png',
to: path.join(__dirname, 'build'),
force: true,
},
],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src', 'pages', 'Options', 'index.html'),
filename: 'options.html',
chunks: ['options'],
cache: false,
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src', 'pages', 'Popup', 'index.html'),
filename: 'popup.html',
chunks: ['popup'],
cache: false,
}),
// Work around for Buffer is undefined:
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
],
infrastructureLogging: {
level: 'info',
},
};
if (env.NODE_ENV === 'development') {
options.devtool = 'cheap-module-source-map';
} else {
options.optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
// avoid all constructor.name to be 't' after code compression in production mode,
// because the same constructor.name will lead to only one key in Features(a Map() obj),
// which then faild the inject2Perceptor step.
keep_fnames: true,
},
}),
],
};
}
export default options;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/os-hubu/hypertrons-crx.git
git@gitee.com:os-hubu/hypertrons-crx.git
os-hubu
hypertrons-crx
hypertrons-crx
master

搜索帮助