forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
127 lines (123 loc) · 3.95 KB
/
webpack.config.js
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
const path = require('path');
const fs = require('fs-extra');
const cp = require('child_process');
const webpack = require('webpack');
const CleanCSS = require('clean-css');
const UglifyJS = require('uglify-js');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const generate = require('generate-file-webpack-plugin');
const packUtils = require('./scripts/webpack.js');
const GIT_COMMIT_ID = cp.execSync('git rev-parse HEAD').toString().trim();
const STATIC_HASH = GIT_COMMIT_ID.padStart(7, '0').slice(0, 7);
const devVscode = !!process.env.DEV_VSCODE;
const skipMinified = { info: { minimized: true } };
const VSCODE_NODE_MODULES = [
'@vscode/iconv-lite-umd',
'@vscode/vscode-languagedetection',
'@xterm/addon-canvas',
'@xterm/addon-image',
'@xterm/addon-search',
'@xterm/addon-serialize',
'@xterm/addon-unicode11',
'@xterm/addon-webgl',
'@xterm/xterm',
'jschardet',
'tas-client-umd',
'vscode-oniguruma',
'vscode-textmate',
].map((pkg) => ({
from: `vscode-web/node_modules/${pkg}/**`,
globOptions: { dot: true },
to({ context, absoluteFilename }) {
const relativePath = path.relative(context, absoluteFilename);
const relativeDir = path.dirname(relativePath.replace('vscode-web/node_modules/', ''));
return `static-${STATIC_HASH}/node_modules/${relativeDir}/[name][ext]`;
},
...skipMinified,
}));
module.exports = (env, argv) => {
const devMode = argv.mode === 'development';
const minifyCSS = (code) => (devMode ? code : new CleanCSS().minify(code).styles);
const minifyJS = (code) => (devMode ? code : UglifyJS.minify(code).code);
return {
mode: env.mode || 'production',
entry: path.resolve(__dirname, 'src/index.ts'),
output: { filename: `static-${STATIC_HASH}/config/bootstrap.js` },
resolve: { extensions: ['.js', '.ts'] },
module: {
rules: [
{ test: /\.tsx?$/, use: 'ts-loader' },
{ test: /\.css?$/, use: ['style-loader', 'css-loader'] },
{ test: /\.svg$/, use: 'file-loader' },
],
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'public/favicon*', to: '[name][ext]' },
{ from: 'public/manifest.json', to: '[name][ext]' },
{ from: 'public/robots.txt', to: '[name][ext]' },
{
from: 'extensions',
to: `static-${STATIC_HASH}/extensions`,
globOptions: { dot: true, ignore: ['**/node_modules/**'] },
...skipMinified,
},
!devVscode && {
from: 'node_modules/@github1s/vscode-web/dist/vscode',
to: `static-${STATIC_HASH}/vscode`,
...skipMinified,
},
!devVscode && {
from: 'node_modules/@github1s/vscode-web/dist/extensions',
to: `static-${STATIC_HASH}/extensions`,
...skipMinified,
},
...VSCODE_NODE_MODULES,
].filter(Boolean),
}),
new HtmlWebpackPlugin({
inject: false,
template: 'public/index.html',
templateParameters: {
devVscode: devVscode,
staticHash: STATIC_HASH,
spinnerStyle: minifyCSS(fs.readFileSync('./public/spinner.css').toString()),
pageTitleScript: minifyJS(fs.readFileSync('./public/page-title.js').toString()),
analyticsScript: devMode ? '' : minifyJS(fs.readFileSync('./public/analytics.js').toString()),
},
}),
new webpack.DefinePlugin({
STATIC_HASH: JSON.stringify(STATIC_HASH),
GITHUB_ORIGIN: JSON.stringify(process.env.GITHUB_DOMAIN || 'https://github.com'),
GITLAB_ORIGIN: JSON.stringify(process.env.GITLAB_DOMAIN || 'https://gitlab.com'),
}),
generate({
file: `static-${STATIC_HASH}/config/extensions.js`,
content: packUtils.createExtensionsContent(devVscode),
}),
],
performance: false,
devServer: {
port: 8080,
liveReload: false,
allowedHosts: 'all',
static: {
directory: path.join(__dirname, 'dist'),
},
client: {
progress: true,
},
historyApiFallback: {
rewrites: [{ from: /./, to: '/index.html' }],
},
devMiddleware: {
writeToDisk: true,
},
proxy: {
'/api/vscode-unpkg': packUtils.createVSCodeUnpkgProxy(),
},
},
};
};