forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
190 lines (180 loc) · 5.59 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
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
181
182
183
184
185
186
187
188
189
190
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const generate = require('generate-file-webpack-plugin');
const fs = require('fs-extra');
const APP_ROOT = path.join(__dirname, '.');
const devVscode = !!process.env.DEV_VSCODE;
const useHashHtml = !!process.env.USE_HASH_HTML;
const isWebExtension = (manifest) => {
if (Boolean(manifest.browser)) {
return true;
}
if (Boolean(manifest.main)) {
return false;
}
// neither browser nor main
if (typeof manifest.extensionKind !== 'undefined') {
const extensionKind = Array.isArray(manifest.extensionKind) ? manifest.extensionKind : [manifest.extensionKind];
if (extensionKind.indexOf('web') >= 0) {
return true;
}
}
if (typeof manifest.contributes !== 'undefined') {
for (const id of ['debuggers', 'terminal', 'typescriptServerPlugins']) {
if (manifest.contributes.hasOwnProperty(id)) {
return false;
}
}
}
return true;
};
const getExtensionData = (absolutePath) => {
try {
const packageJSONPath = path.join(absolutePath, 'package.json');
if (!fs.existsSync(packageJSONPath)) {
return null;
}
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath).toString('utf8'));
if (!isWebExtension(packageJSON)) {
return null;
}
const children = fs.readdirSync(absolutePath);
const packageNLSPath = children.filter((child) => child === 'package.nls.json')[0];
const packageNLS = packageNLSPath
? JSON.parse(fs.readFileSync(path.join(absolutePath, packageNLSPath)).toString())
: undefined;
const readme = children.filter((child) => /^readme(\.txt|\.md|)$/i.test(child))[0];
const changelog = children.filter((child) => /^changelog(\.txt|\.md|)$/i.test(child))[0];
const extensionFolder = path.basename(absolutePath);
return {
extensionPath: extensionFolder,
packageJSON,
packageNLS,
readmePath: readme ? path.join(extensionFolder, readme) : undefined,
changelogPath: changelog ? path.join(extensionFolder, changelog) : undefined,
};
} catch {
return null;
}
};
const scanVSCodeExtensions = () => {
const extensionsPath = path.join(APP_ROOT, 'node_modules/@github1s/vscode-web/dist/extensions');
const extensionFolders = fs.existsSync(extensionsPath) ? fs.readdirSync(extensionsPath) : [];
return extensionFolders.map((item) => getExtensionData(path.join(extensionsPath, item))).filter(Boolean);
};
const scanGitHub1sExtensions = () => {
const extensions = fs.readdirSync(path.join(APP_ROOT, 'extensions'));
return extensions.map((item) => getExtensionData(path.join(APP_ROOT, 'extensions', item))).filter(Boolean);
};
const VSCODE_NODE_MODULES = [
'@vscode/iconv-lite-umd',
'@vscode/vscode-languagedetection',
'jschardet',
'tas-client-umd',
'vscode-oniguruma',
'vscode-textmate',
'xterm',
'xterm-addon-search',
'xterm-addon-unicode11',
'xterm-addon-webgl',
]
.map((x) => `vscode-web/node_modules/${x}/**`)
.map((from) => ({
from,
globOptions: {
dot: true,
// ignore: ["**/node_modules/**/node_modules/**"]
},
to({ context, absoluteFilename }) {
const relativePath = path.relative(context, absoluteFilename);
const relativeDir = path.dirname(relativePath.replace('vscode-web/node_modules/', ''));
return `static/node_modules/${relativeDir}/[name][ext]`;
},
}));
module.exports = {
mode: 'development',
entry: './resources/manifest.json',
plugins: [
new CopyPlugin({
patterns: [
{ from: 'resources/favicon*', to: '[name][ext]' },
{ from: 'resources/manifest.json', to: '[name][ext]' },
{ from: 'resources/robots.txt', to: '[name][ext]' },
{
from: 'node_modules/@github1s/vscode-web/dist/extensions/**',
to({ context, absoluteFilename }) {
const relativePath = path.relative(context, absoluteFilename);
const relativeDir = path.dirname(
relativePath.replace('node_modules/@github1s/vscode-web/dist/extensions/', '')
);
return `static/extensions/${relativeDir}/[name][ext]`;
},
},
!devVscode && {
from: 'node_modules/@github1s/vscode-web/dist/vscode/',
to({ context, absoluteFilename }) {
const relativePath = path.relative(context, absoluteFilename);
const relativeDir = path.dirname(
relativePath.replace('node_modules/@github1s/vscode-web/dist/vscode/', '')
);
return `static/vscode/${relativeDir}/[name][ext]`;
},
},
{
from: 'extensions/**/*',
to: 'static',
globOptions: {
dot: true,
ignore: ['**/node_modules/**'],
},
},
{
from: 'resources/{initialize.js,github.svg,gitlab.svg,bitbucket.svg,npm.svg}',
to: 'static/config/[name][ext]',
},
...VSCODE_NODE_MODULES,
].filter(Boolean),
}),
new HtmlWebpackPlugin({
templateParameters: {
titleScript: fs.readFileSync('resources/titleScript.js'),
},
template: useHashHtml
? 'resources/index-hash.html'
: devVscode
? 'resources/index-dev-vscode.html'
: 'resources/index.html',
inject: false,
}),
generate({
file: 'static/config/extensions.js',
content: () => {
const vscodeExtensions = devVscode ? scanVSCodeExtensions() : [];
const extensions = [...vscodeExtensions, ...scanGitHub1sExtensions()];
return `window.github1sExtensions = ${JSON.stringify(extensions)};`;
},
}),
],
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
allowedHosts: 'all',
client: {
progress: true,
},
historyApiFallback: {
rewrites: [{ from: /./, to: '/index.html' }],
},
devMiddleware: {
writeToDisk: true,
},
proxy: {
'/api/vscode-unpkg': {
target: 'http://localhost:5001',
},
},
port: 5000,
},
};