import { Configuration, rspack } from '@rspack/core';
import * as path from 'path';
import * as fs from 'fs';
const distPath = path.resolve(__dirname, 'dist');
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, { recursive: true });
}
const webviewDistPath = path.resolve(__dirname, 'web-resources', 'dist');
if (fs.existsSync(webviewDistPath)) {
fs.rmSync(webviewDistPath, { recursive: true });
}
const argv = process.argv.join(' ');
const isProduction =
argv.includes('--mode production') ||
process.env.RSPACK_BUILD_MODE === 'production' ||
process.env.NODE_ENV === 'production';
const extensionConfig: Configuration = {
target: 'node',
mode: 'none',
entry: {
extension: './src/extension.ts',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2',
clean: false,
},
devtool: isProduction ? false : 'nosources-source-map',
externals: {
vscode: 'commonjs vscode',
},
resolve: {
extensions: ['.ts', '.js'],
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
},
target: 'es2020',
},
},
type: 'javascript/auto',
},
],
},
optimization: {
minimize: isProduction,
splitChunks: false,
},
performance: {
hints: isProduction ? 'warning' : false,
},
};
const webviewConfig: Configuration = {
target: 'web',
mode: isProduction ? 'production' : 'development',
entry: {
index: './web-resources/scripts/index.js',
},
output: {
path: webviewDistPath,
filename: '[name].js',
libraryTarget: 'umd',
assetModuleFilename: 'assets/[name][ext]',
clean: false,
},
devtool: isProduction ? false : 'source-map',
resolve: {
extensions: ['.ts', '.js'],
modules: [path.resolve(__dirname, 'node_modules'), path.resolve(__dirname, '../../node_modules'), 'node_modules'],
alias: {
'cherry-markdown-core$': path.resolve(__dirname, '../cherry-markdown/dist/cherry-markdown.esm.js'),
'cherry-markdown-core/dist/cherry-markdown.min.css': path.resolve(
__dirname,
'../cherry-markdown/dist/cherry-markdown.min.css'
),
},
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'ecmascript',
},
},
env: {
targets: '> 0.25%, not dead',
},
},
type: 'javascript/auto',
},
{
test: /\.css$/,
use: [rspack.CssExtractRspackPlugin.loader, 'css-loader'],
type: 'javascript/auto',
},
{
test: /\.(woff|woff2|ttf|eot)$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]',
},
},
{
test: /\.(png|jpg|gif|svg)$/,
type: 'asset/resource',
generator: {
filename: 'assets/[name][ext]',
},
},
],
},
plugins: [
new rspack.CssExtractRspackPlugin({
filename: '[name].css',
}),
],
optimization: {
minimize: isProduction,
},
};
export default [extensionConfig, webviewConfig];