Skip to content

Commit 8e7067f

Browse files
authored
Merge pull request #171 from NicDoesCode/develop
Merge DEV release
2 parents 3b400d9 + eedcc02 commit 8e7067f

File tree

185 files changed

+32565
-10643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+32565
-10643
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ node_modules/
77
cert/
88
*.instance.*
99
.env
10-
wwwroot/docs/
10+
wwwroot/docs/
11+
wwwroot/config/

.vscode/launch.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Launch Chrome (HTTPS)",
9+
"request": "launch",
10+
"type": "chrome",
11+
"url": "https://localhost:8443",
12+
"webRoot": "${workspaceFolder}/wwwroot"
13+
},
714
{
815
"name": "Launch Firefox (HTTP)",
916
"type": "firefox",

app.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const express = require('express');
4-
const https = require('https');
1+
import path from "path";
2+
import fs from "fs";
3+
import url from 'url';
4+
import express from 'express';
5+
import https from 'https';
6+
7+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
8+
59
const app = express();
610
const portHttp = 8080;
711
const portHttps = 8443;
File renamed without changes.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import HtmlWebpackPlugin from "html-webpack-plugin";
2+
import Minimatch from 'minimatch';
3+
4+
const PLUGIN_NAME = 'HtmlWebpackExcludeScriptsPlugin';
5+
6+
export default class HtmlWebpackExcludeScriptsPlugin {
7+
8+
9+
/** @type {{skipAssets: Array<string|RegExp|Function>, excludeAssets: Array<string|RegExp|Function>}} */
10+
#config;
11+
12+
13+
/**
14+
* @param {{skipAssets: Array<string|RegExp|Function>, excludeAssets: Array<string|RegExp|Function>}} config
15+
*/
16+
constructor(config) {
17+
this.#config = config ?? [];
18+
}
19+
20+
21+
apply(compiler) {
22+
23+
if (compiler.hooks) {
24+
// webpack 4 support
25+
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
26+
27+
console.log('WP4'); // TMP
28+
if (compilation.outputName === 'index.html') {
29+
console.log(compilation); // TMP
30+
// data.plugin.userOptions.chunks.forEach(c => console.log(c));
31+
}
32+
33+
// if (compilation.hooks.htmlWebpackPluginAlterAssetTags) {
34+
if (compilation.hooks.htmlWebpackPluginAlterAssetTags) {
35+
// html webpack 3
36+
console.log('html webpack 3'); // TMP
37+
compilation.hooks.beforeAssetTagGeneration.tapAsync(PLUGIN_NAME, (data, callback) => {
38+
/** @type {Array<string|RegExp|Function>} */
39+
const filters = [
40+
...(this.#config.skipAssets || []),
41+
...(this.#config.excludeAssets || []),
42+
...(data.plugin.options.skipAssets || []),
43+
...(data.plugin.options.excludeAssets || []),
44+
];
45+
data.head = this.#skipAssets(data.head, filters);
46+
data.body = this.#skipAssets(data.body, filters);
47+
return callback(null, data);
48+
});
49+
} else if (HtmlWebpackPlugin && HtmlWebpackPlugin.getHooks) {
50+
// html-webpack 4
51+
console.log('html-webpack 4'); // TMP
52+
const hooks = HtmlWebpackPlugin.getHooks(compilation);
53+
// hooks.alterAssetTags.tapAsync(PLUGIN_NAME, (data, callback) => {
54+
hooks.alterAssetTags.tapAsync(PLUGIN_NAME, (data, callback) => {
55+
// console.log(data.outputName);
56+
if (data.outputName === 'index.html') {
57+
console.log(data); // TMP
58+
// data.plugin.userOptions.chunks.forEach(c => console.log(c));
59+
}
60+
// data.assetTags.scripts.forEach(s => console.log(s));
61+
// /** @type {Array<string|RegExp|Function>} */
62+
// const filters = [
63+
// ...(this.#config.skipAssets || []),
64+
// ...(this.#config.excludeAssets || []),
65+
// ...(data.plugin['options'].skipAssets || []),
66+
// ...(data.plugin['options'].excludeAssets || []),
67+
// ];
68+
// data.assetTags.scripts = this.#skipAssets(data.assetTags.scripts, filters);
69+
// data.assetTags.styles = this.#skipAssets(data.assetTags.styles, filters);
70+
// data.assetTags.meta = this.#skipAssets(data.assetTags.meta, filters);
71+
// return callback(null, data);
72+
});
73+
} else {
74+
throw new Error('Cannot find appropriate compilation hook');
75+
}
76+
});
77+
78+
} else {
79+
console.log('WP5'); // TMP
80+
// Hook into the html-webpack-plugin processing
81+
compiler.plugin('compilation', (compilation) => {
82+
compilation.plugin('html-webpack-plugin-alter-asset-tags', (htmlPluginData, callback) => {
83+
/** @type {Array<string|RegExp|Function>} */
84+
const filters = [
85+
...(this.#config.skipAssets || []),
86+
...(this.#config.excludeAssets || []),
87+
...(htmlPluginData.plugin.options.skipAssets || []),
88+
...(htmlPluginData.plugin.options.excludeAssets || []),
89+
];
90+
htmlPluginData.head = this.#skipAssets(htmlPluginData.head, filters);
91+
htmlPluginData.body = this.#skipAssets(htmlPluginData.body, filters);
92+
return callback(null, htmlPluginData);
93+
});
94+
});
95+
}
96+
97+
98+
}
99+
100+
/**
101+
* @param {HtmlWebpackPlugin.HtmlTagObject[]} assets
102+
* @param {Array<string|RegExp|Function>} matchers
103+
* @returns {HtmlWebpackPlugin.HtmlTagObject[]}
104+
*/
105+
#skipAssets(assets, matchers) {
106+
return assets.filter((asset) => {
107+
const assetUrl = `${(asset.attributes.src ?? asset.attributes.href ?? '')}`;
108+
console.log('assetUrl', assetUrl);
109+
const skipped = matchers.some((matcher) => {
110+
// if (!matcher) {
111+
// return false;
112+
// }
113+
// const assetUrl = `${(asset.attributes.src ?? asset.attributes.href ?? '')}`;
114+
// if (typeof matcher === 'string') {
115+
// return minimatch(assetUrl, matcher);
116+
// }
117+
// if (matcher.constructor && matcher.constructor.name === 'RegExp') {
118+
// /** @type {RegExp} */
119+
// const regexMatcher = matcher;
120+
// return !!(assetUrl.match(regexMatcher));
121+
// }
122+
// if (typeof matcher === 'function') {
123+
// const matchesCallback = matcher(asset);
124+
// return !!(matchesCallback);
125+
// }
126+
return false;
127+
});
128+
return !skipped;
129+
});
130+
}
131+
132+
}
133+
134+
/**
135+
* @typedef {object} HtmlWebpackExcludeScriptsPluginFilters
136+
*/

build/post-build.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import path from "path";
2+
import fs from "fs";
3+
import url from 'url';
4+
import dotenv from 'dotenv';
5+
6+
const __filename = url.fileURLToPath(import.meta.url);
7+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
8+
9+
// Load environment variables
10+
dotenv.config();
11+
12+
13+
function run() {
14+
makeConfig();
15+
makeGoogleAnalyticsConfig();
16+
}
17+
18+
19+
function makeConfig() {
20+
const patreonHandle = process.env[`HANDLE_PATREON`] ?? null;
21+
const kofiHandle = process.env[`HANDLE_KOFI`] ?? null;
22+
23+
const config = {
24+
patreonHandle: patreonHandle,
25+
kofiHandle: kofiHandle
26+
};
27+
const configString = JSON.stringify(config);
28+
const distDir = path.resolve(__dirname, '..', 'dist');
29+
const targetDir = path.join(distDir, 'config');
30+
if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir);
31+
const filePath = path.join(targetDir, 'config.json');
32+
fs.writeFileSync(filePath, configString);
33+
}
34+
35+
function makeGoogleAnalyticsConfig() {
36+
const useGA = (process.env[`GOOGLE_ANALYTICS_USE`] && process.env[`GOOGLE_ANALYTICS_USE`].toLowerCase() === 'true');
37+
const gaKey = process.env[`GOOGLE_ANALYTICS_KEY`] ?? null;
38+
39+
const gaConfig = {
40+
useGoogleAnalytics: (useGA && gaKey) ? true : false,
41+
key: gaKey ?? 'YOUR_KEY'
42+
};
43+
const gaString = JSON.stringify(gaConfig);
44+
const distDir = path.resolve(__dirname, '..', 'dist');
45+
const targetDir = path.join(distDir, 'config');
46+
if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir);
47+
const filePath = path.join(targetDir, 'googleAnalytics.json');
48+
fs.writeFileSync(filePath, gaString);
49+
}
50+
51+
run();

git-cleanup.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { exec } from 'node:child_process';
2+
3+
const rxGone = /:\sgone\]/;
4+
const rxInUse = /^\*\s/;
5+
const rxBranch = /^\s+([^\s]+)/;
6+
7+
async function pruneAsync() {
8+
console.log('> Pruning branches');
9+
return new Promise((resolve, reject) => {
10+
exec('git remote prune origin', (error, stdout, stderr) => {
11+
if (stderr || error) {
12+
reject('Error when pruning branches.');
13+
} else if (stdout) {
14+
resolve();
15+
}
16+
});
17+
});
18+
}
19+
20+
/**
21+
* @returns {Promise<string[]>}
22+
*/
23+
async function getBranchesNoLongerOnOriginAsync() {
24+
console.log('> Getting branches no longer on origin');
25+
return new Promise((resolve, reject) => {
26+
exec('git branch -vv', (error, stdout, stderr) => {
27+
if (stderr || error) {
28+
reject('Error when getting branches.');
29+
} else if (stdout) {
30+
const lines = stdout.replace('\r', '').split('\n')
31+
.filter((line) => rxGone.test(line))
32+
.filter((line) => !rxInUse.test(line))
33+
.filter((line) => rxBranch.test(line));
34+
resolve(lines.map((line) => rxBranch.exec(line)[1]));
35+
}
36+
});
37+
});
38+
}
39+
40+
/**
41+
* @param {string[]} arrayOfBranches
42+
*/
43+
async function deleteBranchesAsync(arrayOfBranches) {
44+
console.log('> Removing branches no longer on origin');
45+
if (arrayOfBranches.length > 0) {
46+
arrayOfBranches.forEach((branchName) => {
47+
exec(`git branch -d "${branchName}"`, (error, stdout, stderr) => {
48+
if (stderr || error) {
49+
console.error(` - Error when deleting branch: ${branchName}`, stderr);
50+
} else if (stdout) {
51+
console.log(` - Deleted: ${branchName}`);
52+
}
53+
});
54+
});
55+
} else {
56+
console.log('> No branches to remove');
57+
}
58+
}
59+
60+
async function runAsync() {
61+
await pruneAsync();
62+
const branches = await getBranchesNoLongerOnOriginAsync();
63+
await deleteBranchesAsync(branches);
64+
}
65+
66+
runAsync();

0 commit comments

Comments
 (0)