Skip to content

Commit

Permalink
Use webpack to replace the shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
xcv58 committed May 29, 2022
1 parent a5748f8 commit fa5122c
Show file tree
Hide file tree
Showing 4 changed files with 1,915 additions and 14 deletions.
1 change: 1 addition & 0 deletions empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('test');
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"chokidar": "^3.5.1",
"copy-webpack-plugin": "^11.0.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-jsdoc": "^31.6.1",
"eslint-plugin-prettier": "^3.3.1",
"fs-extra": "^9.1.0",
"generate-file-webpack-plugin": "^1.0.1",
"http-proxy": "^1.18.1",
"husky": "^5.0.9",
"lint-staged": "^10.5.4",
Expand All @@ -24,6 +26,9 @@
"serve-handler": "^6.1.3",
"start-server-and-test": "^1.12.0",
"typescript": "^4.1.3",
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.9.0",
"yalc": "^1.0.0-pre.50"
},
"scripts": {
Expand All @@ -41,9 +46,10 @@
"watch": "rm -rf dist && run-p watch:*",
"watch-with-vscode": "DEV_VSCODE=true run-p watch watch-vscode",
"watch-vscode": "cd vscode-web && yarn watch",
"watch:setup": "./scripts/watch-setup.sh",
"watch:dist": "cd scripts/watch && node watch-dist.js",
"watch:webpack": "DEV_VSCODE=true webpack --config webpack.config.js --watch",
"watch:github1s-extension": "cd extensions/github1s && yarn dev",
"1watch:setup": "./scripts/watch-setup.sh",
"1watch:dist": "cd scripts/watch && node watch-dist.js",
"serve": "node ./scripts/serve-dist.js",
"format": "prettier --write .",
"eslint": "eslint --ext .ts --ext .js .",
Expand Down
184 changes: 184 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const generate = require('generate-file-webpack-plugin');
const fs = require('fs-extra');

const APP_ROOT = path.join(__dirname, '.');

const devMode = !!process.env.DEV_VSCODE;

// `window.github1sExtensions = ${JSON.stringify(extensions)};`
// vscode-web/lib/vscode/build/lib/extensions.ts
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: './empty.js',
output: {
filename: '[name].js',
},
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: devMode ? 'resources/index-dev-vscode.html' : 'resources/index.html',
from: devMode ? 'resources/index.html' : 'resources/index.html',
to: 'index[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]`;
},
},
devMode && {
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),
}),
generate({
file: 'static/config/extensions.js',
content: () => {
// const vscodeExtensions = devMode ? scanVSCodeExtensions() : [];
// const extensions = [...vscodeExtensions, ...scanGitHub1sExtensions()];
const extensions = [...scanGitHub1sExtensions()];
return `window.github1sExtensions = ${JSON.stringify(extensions)};`;
},
}),
],
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
allowedHosts: 'all',
client: {
progress: true,
},
historyApiFallback: {
index: 'index.html',
},
proxy: {
context: () => true,
target: 'http://localhost:1234',
},
port: 5000,
},
};
Loading

0 comments on commit fa5122c

Please sign in to comment.