Tools: Update Joplin plugin generator to Webpack 5, TypeScript 4.8 (#6826)

pull/6843/head^2
Henry Heino 2022-09-12 02:44:40 -07:00 committed by GitHub
parent f59d29f1c5
commit ea14488dc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 32 deletions

View File

@ -2,7 +2,7 @@
"name": "<%= packageName %>", "name": "<%= packageName %>",
"version": "1.0.0", "version": "1.0.0",
"scripts": { "scripts": {
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive", "dist": "webpack --env joplin-plugin-config=buildMain && webpack --env joplin-plugin-config=buildExtraScripts && webpack --env joplin-plugin-config=createArchive",
"prepare": "npm run dist", "prepare": "npm run dist",
"update": "npm install -g generator-joplin && yo joplin --update" "update": "npm install -g generator-joplin && yo joplin --update"
}, },
@ -14,18 +14,16 @@
"publish" "publish"
], ],
"devDependencies": { "devDependencies": {
"@types/node": "^14.0.14", "@types/node": "^18.7.13",
"chalk": "^4.1.0", "chalk": "^4.1.0",
"copy-webpack-plugin": "^6.1.0", "copy-webpack-plugin": "^11.0.0",
"fs-extra": "^9.0.1", "fs-extra": "^10.1.0",
"glob": "^7.1.6", "glob": "^8.0.3",
"on-build-webpack": "^0.1.0", "tar": "^6.1.11",
"tar": "^6.0.5", "ts-loader": "^9.3.1",
"ts-loader": "^7.0.5", "typescript": "^4.8.2",
"typescript": "^3.9.3", "webpack": "^5.74.0",
"webpack": "^4.43.0", "webpack-cli": "^4.10.0",
"webpack-cli": "^3.3.11",
"yargs": "^16.2.0",
"@joplin/lib": "~2.9" "@joplin/lib": "~2.9"
} }
} }

View File

@ -11,7 +11,6 @@ const crypto = require('crypto');
const fs = require('fs-extra'); const fs = require('fs-extra');
const chalk = require('chalk'); const chalk = require('chalk');
const CopyPlugin = require('copy-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin');
const WebpackOnBuildPlugin = require('on-build-webpack');
const tar = require('tar'); const tar = require('tar');
const glob = require('glob'); const glob = require('glob');
const execSync = require('child_process').execSync; const execSync = require('child_process').execSync;
@ -35,6 +34,16 @@ const manifest = readManifest(manifestPath);
const pluginArchiveFilePath = path.resolve(publishDir, `${manifest.id}.jpl`); const pluginArchiveFilePath = path.resolve(publishDir, `${manifest.id}.jpl`);
const pluginInfoFilePath = path.resolve(publishDir, `${manifest.id}.json`); const pluginInfoFilePath = path.resolve(publishDir, `${manifest.id}.json`);
const { builtinModules } = require('node:module');
// Webpack5 doesn't polyfill by default and displays a warning when attempting to require() built-in
// node modules. Set these to false to prevent Webpack from warning about not polyfilling these modules.
// We don't need to polyfill because the plugins run in Electron's Node environment.
const moduleFallback = {};
for (const moduleName of builtinModules) {
moduleFallback[moduleName] = false;
}
function validatePackageJson() { function validatePackageJson() {
const content = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); const content = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (!content.name || content.name.indexOf('joplin-plugin-') !== 0) { if (!content.name || content.name.indexOf('joplin-plugin-') !== 0) {
@ -164,6 +173,7 @@ const pluginConfig = Object.assign({}, baseConfig, {
alias: { alias: {
api: path.resolve(__dirname, 'api'), api: path.resolve(__dirname, 'api'),
}, },
fallback: moduleFallback,
// JSON files can also be required from scripts so we include this. // JSON files can also be required from scripts so we include this.
// https://github.com/joplin/plugin-bibtex/pull/2 // https://github.com/joplin/plugin-bibtex/pull/2
extensions: ['.js', '.tsx', '.ts', '.json'], extensions: ['.js', '.tsx', '.ts', '.json'],
@ -198,6 +208,7 @@ const extraScriptConfig = Object.assign({}, baseConfig, {
alias: { alias: {
api: path.resolve(__dirname, 'api'), api: path.resolve(__dirname, 'api'),
}, },
fallback: moduleFallback,
extensions: ['.js', '.tsx', '.ts', '.json'], extensions: ['.js', '.tsx', '.ts', '.json'],
}, },
}); });
@ -205,11 +216,18 @@ const extraScriptConfig = Object.assign({}, baseConfig, {
const createArchiveConfig = { const createArchiveConfig = {
stats: 'errors-only', stats: 'errors-only',
entry: './dist/index.js', entry: './dist/index.js',
resolve: {
fallback: moduleFallback,
},
output: { output: {
filename: 'index.js', filename: 'index.js',
path: publishDir, path: publishDir,
}, },
plugins: [new WebpackOnBuildPlugin(onBuildCompleted)], plugins: [{
apply(compiler) {
compiler.hooks.done.tap('archiveOnBuildListener', onBuildCompleted);
},
}],
}; };
function resolveExtraScriptPath(name) { function resolveExtraScriptPath(name) {
@ -250,11 +268,8 @@ function buildExtraScriptConfigs(userConfig) {
return output; return output;
} }
function main(processArgv) { function main(environ) {
const yargs = require('yargs/yargs'); const configName = environ['joplin-plugin-config'];
const argv = yargs(processArgv).argv;
const configName = argv['joplin-plugin-config'];
if (!configName) throw new Error('A config file must be specified via the --joplin-plugin-config flag'); if (!configName) throw new Error('A config file must be specified via the --joplin-plugin-config flag');
// Webpack configurations run in parallel, while we need them to run in // Webpack configurations run in parallel, while we need them to run in
@ -292,19 +307,22 @@ function main(processArgv) {
return configs[configName]; return configs[configName];
} }
let exportedConfigs = [];
try { module.exports = (env) => {
exportedConfigs = main(process.argv); let exportedConfigs = [];
} catch (error) {
console.error(chalk.red(error.message));
process.exit(1);
}
if (!exportedConfigs.length) { try {
// Nothing to do - for example where there are no external scripts to exportedConfigs = main(env);
// compile. } catch (error) {
process.exit(0); console.error(error.message);
} process.exit(1);
}
module.exports = exportedConfigs; if (!exportedConfigs.length) {
// Nothing to do - for example where there are no external scripts to
// compile.
process.exit(0);
}
return exportedConfigs;
};