Skip to content

Commit

Permalink
Webpack conversion complete!
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunomiac committed Feb 27, 2024
1 parent 9182d72 commit 074ac7c
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 84 deletions.
15 changes: 15 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2020",
"jsx": "react",
"allowImportingTsExtensions": true,
"strictNullChecks": true,
"strictFunctionTypes": true
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
348 changes: 323 additions & 25 deletions module/blades.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"description": "An implementation of Kult: Divinity Lost (4th Edition), based on the original Kult 4E Foundry system by Tom LaPorta.",
"scripts": {
"webpack:watch": "webpack --config webpack.dev.js --watch",
"webpack:watch": "webpack serve --config webpack.dev.js",
"foundry:serve": "node 'D:/LTSC Programs/FoundryVTT/Foundry Virtual Tabletop/resources/app/main.js' --world=city-of-knives --dataPath='D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/FoundryDevData' --hotReload"
},
"type": "module",
Expand Down
178 changes: 124 additions & 54 deletions run-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import webpackProdConfigPromise from "./webpack.prod.js";

// Determine the mode based on the command-line argument
const mode = process.argv.includes("--prod") ? "production" : "development";
// Determine the action based on the command-line argument
const action = process.argv.find((arg) => arg.startsWith("--action="))?.split("=")[1];

// Flag to track if the build process is running for the first time
let isFirstBuild = true;

// Function to asynchronously select and load the appropriate Webpack configuration
async function selectConfig() {
Expand All @@ -22,84 +27,149 @@ async function runWebpackBuild() {
const config = await selectConfig();
return new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err) {
console.error("[runWebpackBuild] Webpack build error:", err);
if (err || stats.hasErrors()) {
console.error("[runWebpackBuild] Webpack build error:", err || stats.toString());
reject(err);
return;
}
console.log("[runWebpackBuild] Webpack build stats:", stats.toString({ colors: true })); // Output build stats
resolve();
});
});
}

// Function to start the Foundry server using Node's child_process.exec
function startFoundryServer() {
return new Promise((resolve, reject) => {
const serverProcess = exec("node \"D:/LTSC Programs/FoundryVTT/Foundry Virtual Tabletop/resources/app/main.js\" --world=city-of-knives --dataPath=\"D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/FoundryDevData\" --hotReload",
(error, stdout, stderr) => {
if (error) {
console.error(`[startFoundryServer] exec error: ${error}`);
reject(error);
return;
}
// Log any output received directly from the exec call
if (stdout) console.log(`[startFoundryServer] stdout: ${stdout}`);
if (stderr) console.error(`[startFoundryServer] stderr: ${stderr}`);
});

serverProcess.stdout.on("data", (data) => {
console.log(data);
if (data.includes("Server started and listening")) {
console.log("[startFoundryServer] RESOLVING!");
resolve();
}
});
// After a successful build in development mode, start the Foundry server if the action is 'build'
if (mode === "development" && action === "build" && isFirstBuild) {
// Toggle 'isFirstBuild' to ensure server doesn't run on every recompile.
isFirstBuild = false;

// Handle any errors that occur during the process execution
serverProcess.on("error", (error) => {
console.error(`[startFoundryServer] exec error: ${error}`);
reject(error);
// Construct the PowerShell command to start the server in a new window
const serverStartCommand = "powershell -Command \"Start-Process powershell -ArgumentList '-NoExit', '-Command', 'node run-webpack.js --action=server'\"";

// Execute the command
exec(serverStartCommand, (error, stdout, stderr) => {
if (error) {
console.error(`[runWebpackBuild] Failed to start Foundry server: ${error}`);
reject(error);
return;
}
if (stdout) console.log(`[runWebpackBuild] Foundry server stdout: ${stdout}`);
if (stderr) console.error(`[runWebpackBuild] Foundry server stderr: ${stderr}`);
});
}
resolve(); // Resolve immediately after logging stats or attempting to start the server
});
});
}

// function startFoundryServer() {
// return new Promise((resolve, reject) => {
// // Construct the PowerShell command to start the server in a new window
// const serverStartCommand = `
// powershell -Command "Start-Process powershell -ArgumentList '-NoExit', '-Command', {
// [System.Media.SystemSounds]::Asterisk.Play();

// node 'D:/LTSC Programs/FoundryVTT/Foundry Virtual Tabletop/resources/app/main.js' --world=city-of-knives --dataPath='D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/FoundryDevData' --hotReload | ForEach-Object {
// \$_;
// if (\$_ -match 'Server started and listening') {
// Start-Process 'chrome.exe' -ArgumentList @(
// '--start-maximized',
// '--remote-debugging-port=9222',
// '--auto-open-devtools-for-tabs',
// '--user-data-dir=\"D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/ChromeDevProfile\"',
// 'http://localhost:30000',
// 'http://localhost:30000'
// ) -WindowStyle Maximized
// }
// }
// }" -WindowStyle Hidden`;

// Function to open Chrome in debug mode
function openChromeDebugSession() {
exec("cmd /c start \"\" \"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --remote-debugging-port=9222 --auto-open-devtools-for-tabs --user-data-dir='C:/temp/chrome_dev_test' http://localhost:30000",
(error, stdout, stderr) => {
if (error) {
console.error("[openChromeDebugSession] exec error:", error);
} else {
console.log("[openChromeDebugSession] stdout:", stdout);
console.error("[openChromeDebugSession] stderr:", stderr);
}
});
// exec("cmd /c start chrome --start-maximized --remote-debugging-port=9222 --auto-open-devtools-for-tabs --user-data-dir='C:/temp/chrome_dev_test' http://localhost:30000 http://localhost:30000",
// (error, stdout, stderr) => {
// // Execute the command
// exec(serverStartCommand, (error, stdout, stderr) => {
// if (error) {
// console.error("[openChromeDebugSession] exec error:", error);
// console.error(`[startFoundryServer] exec error: ${error}`);
// reject(error);
// return;
// }
// console.log("[openChromeDebugSession] stdout:", stdout);
// console.error("[openChromeDebugSession] stderr:", stderr);
// if (stdout) console.log(`[startFoundryServer] stdout: ${stdout}`);
// if (stderr) console.error(`[startFoundryServer] stderr: ${stderr}`);
// resolve(); // Resolve the promise when the command executes successfully
// });
// });
// }

// Function to start the Foundry server directly using Node's child_process.exec
// function startFoundryServer() {
// return new Promise((resolve, reject) => {
// // Construct the PowerShell command to start the server in a new window
// // Replace the paths and arguments as necessary to match your original PowerShell script
// const serverStartCommand = `
// Start-Process powershell -ArgumentList '-NoExit', '-Command', {
// [System.Media.SystemSounds]::Asterisk.Play();

// node 'D:/LTSC Programs/FoundryVTT/Foundry Virtual Tabletop/resources/app/main.js' --world=city-of-knives --dataPath='D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/FoundryDevData' --hotReload | ForEach-Object {
// $_;
// if ($_ -match 'Server started and listening') {
// Start-Process 'chrome.exe' -ArgumentList @(
// '--start-maximized',
// '--remote-debugging-port=9222',
// '--auto-open-devtools-for-tabs',
// '--user-data-dir="D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/ChromeDevProfile"',
// 'http://localhost:30000',
// 'http://localhost:30000'
// ) -WindowStyle Maximized
// }
// }
// } -WindowStyle Minimized;
// `;

// // const serverStartCommand = `powershell -Command "Start-Process powershell -ArgumentList '-NoExit', '-Command', &{node 'D:/LTSC Programs/FoundryVTT/Foundry Virtual Tabletop/resources/app/main.js' --world=city-of-knives --dataPath='D:/Users/Ryan/Documents/Projects/.CODING/FoundryVTT/FoundryDevData' --hotReload}"`;

// // Execute the command
// exec(serverStartCommand, (error, stdout, stderr) => {
// if (error) {
// console.error(`[startFoundryServer] exec error: ${error}`);
// reject(error);
// return;
// }
// // Log any output received directly from the exec call
// if (stdout) console.log(`[startFoundryServer] stdout: ${stdout}`);
// if (stderr) console.error(`[startFoundryServer] stderr: ${stderr}`);
// resolve(); // Resolve the promise when the command executes successfully
// });
// });
// }

// Function to start the Foundry server using Node's child_process.exec
function startFoundryServer() {
return new Promise((resolve, reject) => {
// Use a relative path to refer to the PowerShell script in the same directory
const serverStartCommand = "powershell -File \".\\start-foundry-server.ps1\"";

// Execute the command
exec(serverStartCommand, { windowsHide: true }, (error, stdout, stderr) => {
if (error) {
console.error(`[startFoundryServer] exec error: ${error}`);
reject(error);
return;
}
// Log any output received directly from the exec call
if (stdout) console.log(`[startFoundryServer] stdout: ${stdout}`);
if (stderr) console.error(`[startFoundryServer] stderr: ${stderr}`);
resolve(); // Resolve the promise when the command executes successfully
});
});
}

// Orchestrating the sequence
async function runProcess() {
try {
await runWebpackBuild();
if (mode === "development") {
// Only run these in development mode
// Run the build process regardless of the action, if specified to do so
if (action === "build" || !action) {
await runWebpackBuild();
}
// Start the server only if in development mode and the action is 'server'
if (mode === "development" && (action === "server" || !action)) {
await startFoundryServer();
console.log("[runProcess] Awaited Foundry Server, Calling openChromeDebugSession()");
openChromeDebugSession();
}
} catch(error) {
console.error("[runProcess] An error occurred during the development process:", error);
console.error("[runProcess] An error occurred:", error);
}
}

Expand Down
1 change: 1 addition & 0 deletions ts/BladesItem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
import C, {BladesItemType, BladesNoticeType, Tag, Factor} from "./core/constants";
import U from "./core/utilities";
import {BladesActor, BladesCrew, BladesPC} from "./documents/BladesActorProxy";
Expand Down
3 changes: 0 additions & 3 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ export default {
entry: "./ts/blades.ts", // Adjust this path to your main JavaScript file
resolve: {
extensions: [".ts", ".js"]
},
externals: {
"gsap/all": "gsap"
}
};
12 changes: 12 additions & 0 deletions webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,21 @@ export default merge(common, {
]
},

resolve: {
// Aliases are used to point imports to specific file paths, allowing for simpler import statements
alias: {
// Alias "gsap/all" to the absolute path of the gsap library provided by FoundryVTT
// This allows Webpack to resolve the "gsap/all" import to the correct file during development
"gsap/all": "D:/LTSC Programs/FoundryVTT/Foundry Virtual Tabletop/resources/app/public/scripts/greensock/esm/all.js"
}
},

// Configure source maps for better error debugging. Allows you to see the original source code during debugging
devtool: "eval-source-map",

// Enable watch mode
watch: true,

// Configuration for the webpack-dev-server, which provides live reloading
devServer: {
contentBase: ".", // Directory to serve content from
Expand Down
8 changes: 7 additions & 1 deletion webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function loadPlugins() {

async function createWebpackProductionConfig() {
const plugins = await loadPlugins();
merge(common, {
return merge(common, {
// Set the mode to 'production'. This enables optimizations like minification and tree shaking out of the box.
mode: "production",

Expand Down Expand Up @@ -100,6 +100,12 @@ async function createWebpackProductionConfig() {
]
},

externals: {
// Configure Webpack to treat "gsap/all" as an external module
// This prevents Webpack from attempting to bundle it, allowing FoundryVTT to handle the import at runtime
"gsap/all": "commonjs gsap/all"
},

// Configuration for plugins used in the production build.
plugins,

Expand Down

0 comments on commit 074ac7c

Please sign in to comment.