forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.js
More file actions
94 lines (84 loc) · 3.07 KB
/
webpack.js
File metadata and controls
94 lines (84 loc) · 3.07 KB
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
const Url = require('url');
const path = require('path');
const fs = require('fs-extra');
const APP_ROOT = path.join(__dirname, '..');
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 createExtensionsContent = (devVscode) => {
const vscodeExtensions = devVscode ? scanVSCodeExtensions() : [];
const extensions = [...vscodeExtensions, ...scanGitHub1sExtensions()];
return `window.github1sExtensions = ${JSON.stringify(extensions)};`;
};
const createVSCodeUnpkgProxy = () => ({
changeOrigin: true,
target: 'vscode-unpkg.net',
pathRewrite: (path) => path.replace(/^\/api\/vscode-unpkg\//, '/'),
router: (req) => {
const PATH_REGEXP = /^\/api\/vscode-unpkg\/([^/]+)\/(.*)/;
const matches = Url.parse(req.url).pathname.match(PATH_REGEXP);
return `https://${matches?.[1]}.vscode-unpkg.net`.toLowerCase();
},
});
module.exports = {
createVSCodeUnpkgProxy,
createExtensionsContent,
};