Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions Bundling With Rspack #4461

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
};
40 changes: 0 additions & 40 deletions packages/project-utils/bundling/function/buildHandler.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class BaseFunctionBundler {
bundlerConfig = {};

build() {
throw new Error("Method not implemented.");
}

watch() {
throw new Error("Method not implemented.");
}

setBundlerConfig(config) {
if (typeof config === 'function') {
this.bundlerConfig = config(this.bundlerConfig);
}

this.bundlerConfig = config;
}

getBundlerConfig() {
return this.bundlerConfig
}
}

module.exports = { BaseFunctionBundler };
87 changes: 87 additions & 0 deletions packages/project-utils/bundling/function/bundlers/RspackBundler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages");
const { BaseFunctionBundler } = require("./BaseFunctionBundler");

class RspackBundler extends BaseFunctionBundler {
constructor({ cwd, overrides }) {
super();
this.cwd = cwd;
this.overrides = overrides;
}

build() {
return new Promise(async (resolve, reject) => {
const bundlerConfig = require("./rspack/rspack.config.js")({
cwd: this.cwd,
overrides: this.overrides,
production: true
});

require("@rspack/core")(bundlerConfig, 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;
}

console.log(`Compiled successfully.`);
resolve();
});
});
}

watch() {
return new Promise(async (resolve, reject) => {
console.log("Compiling...");

const bundlerConfig = require("./rspack/rspack.config.js")({
cwd: this.cwd,
overrides: this.overrides,
production: false
});

return require("@rspack/core")(bundlerConfig).watch({}, async (err, stats) => {
if (err) {
return reject(err);
}

if (!stats.hasErrors()) {
console.log("Compiled successfully.");
} else {
console.log(stats.toString("errors-warnings"));
}
});
});
}
}

module.exports = {RspackBundler};
Loading
Loading