Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Dec 21, 2024
1 parent a8a82bb commit bc3cad3
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 152 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require("path");
const { Worker } = require("worker_threads");
const { Worker, workerData, parentPort } = require("worker_threads");
const Listr = require("listr");
const { BasePackagesBuilder } = require("./BasePackagesBuilder");
const { gray } = require("chalk");
const { measureDuration } = require("../../utils");
const { cli } = require("@webiny/cli");

class MultiplePackagesBuilder extends BasePackagesBuilder {
async build() {
Expand All @@ -25,44 +26,68 @@ class MultiplePackagesBuilder extends BasePackagesBuilder {
buildTasks.push({
pkg: pkg,
task: new Promise((resolve, reject) => {
const enableLogs = inputs.logs === true;

const workerData = {
options: {
env,
variant,
debug,
logs: enableLogs
logs: true
},
package: { ...pkg.paths }
};

const worker = new Worker(path.join(__dirname, "./worker.js"), {
workerData,
stderr: true,
stdout: true
});

worker.on("message", threadMessage => {
const { type, stdout, stderr, error } = JSON.parse(threadMessage);

const result = {
package: pkg,
stdout,
stderr,
error,
duration: getBuildDuration()
};

if (type === "error") {
reject(result);
return;
}

if (type === "success") {
resolve(result);
}
});
const { options, package: pckg } = workerData;
let config = require(pckg.config).default || require(pckg.config);
if (typeof config === "function") {
config = config({ options: { ...options, cwd: pckg.root }, context: cli });
}

const hasBuildCommand =
config.commands && typeof config.commands.build === "function";
if (!hasBuildCommand) {
throw new Error("Build command not found.");
}

config.commands.build(options).then(resolve).catch(reject);

// const enableLogs = inputs.logs === true;
//
// const workerData = {
// options: {
// env,
// variant,
// debug,
// logs: enableLogs
// },
// package: { ...pkg.paths }
// };
//
// const worker = new Worker(path.join(__dirname, "./worker.js"), {
// workerData,
// stderr: false,
// stdout: false
// });
//
// worker.on("message", threadMessage => {
// const { type, stdout, stderr, error } = JSON.parse(threadMessage);
//
// const result = {
// package: pkg,
// stdout,
// stderr,
// error,
// duration: getBuildDuration()
// };
//
// if (type === "error") {
// reject(result);
// return;
// }
//
// if (type === "success") {
// resolve(result);
// }
// });
})
});
}
Expand Down
72 changes: 8 additions & 64 deletions packages/project-utils/bundling/function/buildFunction.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,13 @@
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages");
const { getProjectApplication } = require("@webiny/cli/utils");
const { RspackBundler } = require("./bundlers/RspackBundler");
const { WebpackBundler } = require("./bundlers/WebpackBundler");

module.exports = async options => {
const { overrides, logs, cwd, debug } = options;
const { overrides, cwd } = options;

let projectApplication;
try {
projectApplication = getProjectApplication({ cwd });
} catch {
// No need to do anything.
}
const { featureFlags } = require("@webiny/feature-flags");
const bundler = featureFlags.rspack
? new RspackBundler({ cwd, overrides })
: new WebpackBundler({ cwd, overrides });

let webpackConfig = require("./webpack.config")({
production: !debug,
projectApplication,
...options
});

// Customize Webpack config.
if (typeof overrides.webpack === "function") {
webpackConfig = overrides.webpack(webpackConfig);
}

const webpack = require("webpack");

return new Promise(async (resolve, reject) => {
webpack(webpackConfig).run(async (err, stats) => {
let messages = {};

if (err) {
messages = formatWebpackMessages({
errors: [err.message],
warnings: []
});

const errorMessages = messages.errors.join("\n\n");
console.error(errorMessages);
return reject(new Error(errorMessages));
}

if (stats.hasErrors()) {
messages = formatWebpackMessages(
stats.toJson({
all: false,
warnings: true,
errors: true
})
);
}

if (Array.isArray(messages.errors) && messages.errors.length) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if (messages.errors.length > 1) {
messages.errors.length = 1;
}

const errorMessages = messages.errors.join("\n\n");
console.error(errorMessages);
reject(new Error(errorMessages));
return;
}

logs && console.log(`Compiled successfully.`);
resolve();
});
});
return bundler.build();
};
61 changes: 7 additions & 54 deletions packages/project-utils/bundling/function/watchFunction.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,13 @@
const { getProjectApplication } = require("@webiny/cli/utils");
const { RspackBundler } = require("./bundlers/RspackBundler");
const { WebpackBundler } = require("./bundlers/WebpackBundler");

module.exports = async options => {
if (!options) {
options = {};
}
if (!options.cwd) {
options.cwd = process.cwd();
}
const webpack = require("webpack");

const { overrides, cwd } = options;

let projectApplication;
try {
projectApplication = getProjectApplication({ cwd });
} catch {
// No need to do anything.
}

// Load base webpack config
let webpackConfig = require("./webpack.config")({
production: false,
projectApplication,
...options
});

// Customize Webpack config.
if (typeof overrides.webpack === "function") {
webpackConfig = overrides.webpack(webpackConfig);
}

return new Promise(async (resolve, reject) => {
let initialCompilation = true;
if (options.logs) {
const message = initialCompilation ? "Initial compilation started..." : "Compiling...";
console.log(message);
}

return webpack(webpackConfig).watch({}, async (err, stats) => {
if (err) {
return reject(err);
}

if (!options.logs) {
return;
}

if (stats.hasErrors()) {
console.log(stats.toString("errors-warnings"));
return;
}
const { featureFlags } = require("@webiny/feature-flags");
const bundler = featureFlags.rspack
? new RspackBundler({ cwd, overrides })
: new WebpackBundler({ cwd, overrides });

if (initialCompilation) {
initialCompilation = false;
console.log("Initial compilation completed. Watching for changes...");
}
});
});
return bundler.watch();
};
11 changes: 8 additions & 3 deletions packages/project-utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Configuration as WebpackConfig } from "webpack";
import { Configuration as RspackConfig } from "@rspack/core";

export { WebpackConfig };
export { RspackConfig };

// Build commands.
export type BuildCommand<TOptions = Record<string, any>> = (options: TOptions) => Promise<void>;
Expand All @@ -9,6 +11,10 @@ export interface BabelConfig {
[key: string]: any;
}

export interface SwcConfig {
[key: string]: any;
}

export interface DefinePluginOptions {
[key: string]: any;
}
Expand Down Expand Up @@ -48,16 +54,15 @@ interface BuildFunctionConfig {
};
define?: DefinePluginOptions;
webpack?: (config: WebpackConfig) => WebpackConfig;
rspack?: (config: RspackConfig) => RspackConfig;
babel?: (config: BabelConfig) => BabelConfig;
swc?: (config: SwcConfig) => SwcConfig;
};
}

export function createBuildFunction(options: BuildFunctionConfig): BuildCommand;
export function createWatchFunction(options: BuildFunctionConfig): BuildCommand;

export function createBuildHandler(options: BuildFunctionConfig): BuildCommand;
export function createWatchHandler(options: BuildFunctionConfig): BuildCommand;

// Build commands - packages.
interface BuildPackageConfig {
[key: string]: any;
Expand Down
2 changes: 2 additions & 0 deletions packages/project-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@babel/preset-typescript": "^7.23.3",
"@babel/runtime": "^7.24.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
"@rspack/core": "^1.1.8",
"@svgr/webpack": "^6.1.1",
"@types/webpack-env": "1.16.3",
"@webiny/aws-sdk": "0.0.0",
Expand Down Expand Up @@ -73,6 +74,7 @@
"source-map-support": "^0.5.21",
"style-loader": "3.3.1",
"terser-webpack-plugin": "5.2.5",
"ts-checker-rspack-plugin": "^1.0.3",
"ttypescript": "^1.5.12",
"typescript": "4.9.5",
"url": "0.11.0",
Expand Down
3 changes: 2 additions & 1 deletion webiny.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default {

featureFlags: {
experimentalAdminOmniSearch: true,
newWatchCommand: true
newWatchCommand: true,
rspack: true
}
};
Loading

0 comments on commit bc3cad3

Please sign in to comment.