Add webpack configuration for webview

This commit adds a new webpack configuration for the webview part of the project. It includes the necessary rules for transpiling and bundling the TypeScript and React code, as well as the CSS and image files. The new configuration is added to the existing configuration for the extension. The devtool option is changed to 'source-map' for better debugging experience.
This commit is contained in:
Rankin Zheng 2023-05-05 23:08:48 +08:00
parent 6f998ef956
commit f87525da78

View File

@ -12,17 +12,16 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
/** @type WebpackConfig */ /** @type WebpackConfig */
const extensionConfig = { const extensionConfig = {
name: 'vscode extension',
target: 'node', // VS Code extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/ target: 'node', // VS Code extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/
mode: 'none', // this leaves the source code as close as possible to the original (when packaging we set this to 'production') mode: 'none', // this leaves the source code as close as possible to the original (when packaging we set this to 'production')
entry: { entry: './src/extension.ts', // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
extension: './src/extension.ts', // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
index: './src/views/index.tsx'
},
output: { output: {
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/ // the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'dist'), path: path.resolve(__dirname, 'dist'),
filename: '[name].js', filename: 'extension.js',
libraryTarget: 'commonjs2' libraryTarget: 'commonjs2'
}, },
externals: { externals: {
@ -31,6 +30,51 @@ const extensionConfig = {
}, },
resolve: { resolve: {
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader // support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
extensions: ['.ts', '.json']
},
module: {
rules: [
{
test: /\.ts?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
'@babel/preset-typescript'
]
}
},
{
loader: 'ts-loader'
}
]
},
]
},
devtool: 'nosources-source-map',
infrastructureLogging: {
level: "log", // enables logging required for problem matchers
},
plugins: []
};
/** @type WebpackConfig */
const webviewConfig = {
name: 'webview',
target: 'web',
mode: 'development',
entry: './src/views/index.tsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'] extensions: ['.ts', '.tsx', '.js', '.json']
}, },
module: { module: {
@ -56,8 +100,18 @@ const extensionConfig = {
}, },
{ {
test: /\.jsx?$/, test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/, exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
'@babel/preset-react',
],
]
},
}]
}, },
{ {
test: /\.css$/i, test: /\.css$/i,
@ -94,9 +148,9 @@ const extensionConfig = {
} }
] ]
}, },
devtool: 'nosources-source-map', devtool: 'source-map',
infrastructureLogging: { infrastructureLogging: {
level: "log", // enables logging required for problem matchers level: "log",
}, },
plugins: [ plugins: [
// generate an HTML file that includes the extension's JavaScript file // generate an HTML file that includes the extension's JavaScript file
@ -109,12 +163,9 @@ const extensionConfig = {
patterns: [ patterns: [
{ from: 'assets', to: 'assets' }, { from: 'assets', to: 'assets' },
], ],
}),
// define global variables
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development')
}) })
] ]
}; };
module.exports = [extensionConfig];
module.exports = [extensionConfig, webviewConfig];