-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.mjs
180 lines (168 loc) · 4.62 KB
/
webpack.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//@ts-check
/** @typedef {import('webpack').Configuration} WebpackConfig **/
import CopyPlugin from 'copy-webpack-plugin';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; skipLint?: boolean } | undefined } env
* @param {{ mode: 'production' | 'development' | 'none' | undefined }} argv
* @returns { WebpackConfig[] }
*/
export default function (env, argv) {
const mode = argv.mode || 'none';
env = {
analyzeBundle: false,
analyzeDeps: false,
esbuild: true,
skipLint: false,
...env,
};
return [getExtensionConfig('node', mode, env), getWebviewsConfig(mode, env)];
}
/**
* @param { 'node' | 'webworker' } target
* @param { 'production' | 'development' | 'none' } mode
* @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; skipLint?: boolean }} env
* @returns { WebpackConfig }
*/
function getExtensionConfig(target = 'node', mode = 'none', env) {
const tsConfigPath = path.join(__dirname, 'tsconfig.node.json');
/** @type WebpackConfig */
const extensionConfig = {
name: `extension:${target}`,
target: target,
mode: mode,
devtool: mode === 'production' ? false : 'source-map',
entry: './src/extension.ts',
output: {
chunkFilename: '[name].js',
filename: 'extension.js',
libraryTarget: 'commonjs2',
path: path.resolve(__dirname, 'dist'),
},
externals: {
vscode: 'commonjs vscode',
},
resolve: {
alias: {
'@env': path.join(__dirname, 'src', 'env', 'node'),
},
mainFields: ['module', 'main'],
extensions: ['.ts', '.js', '.json'],
},
module: {
rules: [
{
test: /\.ts$/,
include: path.join(__dirname, 'src'),
exclude: /\.d\.ts$/,
use: [
{
loader: 'ts-loader',
options: {
configFile: tsConfigPath,
},
},
],
},
],
},
infrastructureLogging:
mode === 'production'
? undefined
: {
level: 'log', // enables logging required for problem matchers
},
};
return extensionConfig;
}
/**
* @param { 'production' | 'development' | 'none' } mode
* @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; skipLint?: boolean }} env
* @returns { WebpackConfig }
*/
function getWebviewsConfig(mode = 'none', env) {
const basePath = path.join(__dirname, 'src', 'webviews', 'apps');
const tsConfigPath = path.join(basePath, 'tsconfig.json');
/** @type WebpackConfig['plugins'] | any */
const plugins = [
new CopyPlugin({
patterns: [
{
from: path.posix.join(
__dirname.replace(/\\/g, '/'),
'node_modules',
'@vscode',
'codicons',
'dist',
'codicon.ttf',
),
to: path.posix.join(__dirname.replace(/\\/g, '/'), 'dist', 'webviews'),
},
{
from: path.posix.join(basePath.replace(/\\/g, '/'), '*.css'),
to: path.posix.join(__dirname.replace(/\\/g, '/'), 'dist', 'webviews'),
},
],
}),
];
/** @type WebpackConfig */
const webviewsConfig = {
name: 'webviews',
target: 'web',
mode: mode,
devtool: mode === 'production' ? false : 'source-map',
context: basePath,
entry: {
todos: './todos/index.ts',
},
output: {
chunkFilename: '[name].js',
filename: '[name].js',
libraryTarget: 'module',
path: path.join(__dirname, 'dist', 'webviews'),
publicPath: '#{root}/dist/webviews/',
},
experiments: {
outputModule: true,
},
resolve: {
alias: {
'@env': path.resolve(__dirname, 'src', 'env', 'browser'),
},
modules: [basePath, 'node_modules'],
extensions: ['.ts', '.js', '.json'],
},
module: {
rules: [
{
test: /\.m?js/,
resolve: { fullySpecified: false },
},
{
test: /\.ts$/,
include: path.join(__dirname, 'src'),
exclude: /\.d\.ts$/,
use: [
{
loader: 'ts-loader',
options: {
configFile: tsConfigPath,
},
},
],
},
],
},
plugins,
infrastructureLogging:
mode === 'production'
? undefined
: {
level: 'log', // enables logging required for problem matchers
},
};
return webviewsConfig;
}