Skip to content

Commit

Permalink
improved build process
Browse files Browse the repository at this point in the history
  • Loading branch information
jancajthaml authored Aug 11, 2024
1 parent 4fc7d56 commit 465388b
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 253 deletions.
204 changes: 99 additions & 105 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lastui/rocker",
"version": "0.20.19",
"version": "0.20.20",
"license": "Apache-2.0",
"author": "jan.cajthaml@gmail.com",
"homepage": "https://github.com/lastui/rocker#readme",
Expand Down Expand Up @@ -98,7 +98,6 @@
"weak-napi": "2.0.2",
"webpack": "5.93.0",
"webpack-dev-server": "5.0.4",
"webpack-stats-plugin": "1.1.3",
"whatwg-fetch": "3.6.20",
"yargs": "17.7.2"
},
Expand Down
69 changes: 3 additions & 66 deletions webpack/config/module/production.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const path = require("path");
const webpack = require("webpack");
const { StatsWriterPlugin } = require("webpack-stats-plugin");

const dependenciesDlls = require("@lastui/dependencies");

const babel = require("../../../babel");
const RegisterModuleInjectBuildId = require("../../../babel/plugins/RegisterModuleInjectBuildId");
const ModuleLocalesPlugin = require("../../plugins/ModuleLocalesPlugin");
const NormalizedModuleIdPlugin = require("../../plugins/NormalizedModuleIdPlugin");
const SoftwareBillOfMaterialsPlugin = require("../../plugins/SoftwareBillOfMaterialsPlugin");
const settings = require("../../settings");

const config = {
Expand Down Expand Up @@ -167,70 +166,8 @@ config.plugins.push(
context: process.env.INIT_CWD,
}),
new NormalizedModuleIdPlugin(),
new StatsWriterPlugin({
filename: path.join("..", "reports", "sbom.json"),
stats: {
assets: false,
dependentModules: true,
},
async transform(data) {
const shared = require("@lastui/dependencies/sbom.json");

const lockfile = require(
path.resolve(require.resolve("@lastui/dependencies"), "..", "..", "..", "..", "package-lock.json"),
);

const candidates = [];

for (const chunk of data.chunks) {
for (const entry of chunk.modules) {
if (entry.moduleType === "asset/inline") {
if (!entry.id.startsWith("./node_modules/")) {
continue;
}
if (candidates.indexOf(entry.id) === -1) {
candidates.push(entry.id);
}
} else {
for (const reason of entry.reasons) {
if (!reason.resolvedModuleId) {
continue;
}
if (!reason.resolvedModuleId.startsWith("./node_modules/")) {
continue;
}
if (candidates.indexOf(reason.resolvedModuleId) === -1) {
candidates.push(reason.resolvedModuleId);
}
}
}
}
}

const report = {};

for (const candidate of candidates) {
const parts = candidate.substring(15).split("/");
const item = candidate[15] === "@" ? parts[0] + "/" + parts[1] : parts[0];

if (item in shared) {
continue;
}

report[item] = "*";
}

for (const item in report) {
const entry = lockfile.packages["node_modules/" + item];
if (!entry) {
delete report[item];
} else {
report[item] = entry.version;
}
}

return JSON.stringify(report, null, 2);
},
new SoftwareBillOfMaterialsPlugin({
filename: (entrypoint) => `sbom-module-${entrypoint}.json`,
}),
);

Expand Down
82 changes: 4 additions & 78 deletions webpack/config/spa/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const HTMLWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
const webpack = require("webpack");
const { StatsWriterPlugin } = require("webpack-stats-plugin");

const dependenciesDlls = require("@lastui/dependencies");

const babel = require("../../../babel");
const NormalizedModuleIdPlugin = require("../../plugins/NormalizedModuleIdPlugin");
const SoftwareBillOfMaterialsPlugin = require("../../plugins/SoftwareBillOfMaterialsPlugin");
const settings = require("../../settings");

const config = {
Expand Down Expand Up @@ -181,7 +181,7 @@ config.plugins.push(
patterns: [
{
from: path.resolve(process.env.INIT_CWD, "static"),
to: path.resolve(process.env.INIT_CWD, "build", "spa"),
to: path.resolve(__dirname, "..", "..", "..", "..", "..", "..", "build", "spa"),
async filter(resourcePath) {
return !resourcePath.endsWith("index.html");
},
Expand Down Expand Up @@ -218,82 +218,8 @@ config.plugins.push(
},
]),
new NormalizedModuleIdPlugin(),
new StatsWriterPlugin({
filename: path.join("..", "reports", "sbom.json"),
stats: {
assets: true,
dependentModules: true,
},
async transform(data) {
const shared = require("@lastui/dependencies/sbom.json");

const lockfile = require(
path.resolve(require.resolve("@lastui/dependencies"), "..", "..", "..", "..", "package-lock.json"),
);

const candidates = [];

for (const asset of data.assets) {
if (!asset.info.sourceFilename) {
continue;
}
if (!asset.info.sourceFilename.startsWith("node_modules/")) {
continue;
}
if (candidates.indexOf("./" + asset.info.sourceFilename) === -1) {
candidates.push("./" + asset.info.sourceFilename);
}
}

for (const chunk of data.chunks) {
for (const entry of chunk.modules) {
if (entry.moduleType === "asset/inline") {
if (!entry.id.startsWith("./node_modules/")) {
continue;
}
if (candidates.indexOf(entry.id) === -1) {
candidates.push(entry.id);
}
} else {
for (const reason of entry.reasons) {
if (!reason.resolvedModuleId) {
continue;
}
if (!reason.resolvedModuleId.startsWith("./node_modules/")) {
continue;
}
if (candidates.indexOf(reason.resolvedModuleId) === -1) {
candidates.push(reason.resolvedModuleId);
}
}
}
}
}

const report = {};

for (const candidate of candidates) {
const parts = candidate.substring(15).split("/");
const item = candidate[15] === "@" ? parts[0] + "/" + parts[1] : parts[0];

if (item in shared) {
continue;
}

report[item] = "*";
}

for (const item in report) {
const entry = lockfile.packages["node_modules/" + item];
if (!entry) {
delete report[item];
} else {
report[item] = entry.version;
}
}

return JSON.stringify(report, null, 2);
},
new SoftwareBillOfMaterialsPlugin({
filename: (_entrypoint) => "sbom-spa.json",
}),
);

Expand Down
4 changes: 3 additions & 1 deletion webpack/internal/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const webpack = require("webpack");

const settings = require("../settings");

console.log(process.env);

module.exports = {
bail: true,
output: {
Expand All @@ -12,7 +14,7 @@ module.exports = {
chunkLoadingGlobal: "lastuiJsonp",
chunkLoading: "jsonp",
wasmLoading: false,
path: path.resolve(process.env.INIT_CWD, "build"),
path: path.resolve(__dirname, "..", "..", "..", "..", "..", "build"),
publicPath: settings.PROJECT_NAMESPACE,
globalObject: "this",
environment: {
Expand Down
8 changes: 7 additions & 1 deletion webpack/plugins/ModuleLocalesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ class ModuleLocalesPlugin {
});
});
compilation.fileDependencies.add(inputPath);
compilation.emitAsset(outputPath, new compiler.webpack.sources.RawSource(content));

const asset = compilation.getAsset(outputPath);
if (asset) {
compilation.updateAsset(outputPath, new compiler.webpack.sources.RawSource(content));
} else {
compilation.emitAsset(outputPath, new compiler.webpack.sources.RawSource(content));
}
} catch (error) {
console.error("Failed to process locale asset", inputPath, "with error", error);
}
Expand Down
115 changes: 115 additions & 0 deletions webpack/plugins/SoftwareBillOfMaterialsPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const path = require("path");

const DEFAULT_FILENAME = (_entrypoint) => "sbom.json";

class SoftwareBillOfMaterialsPlugin {
constructor(options = {}) {
this.options = {};
this.options.filename = options.filename ?? DEFAULT_FILENAME;
}

apply(compiler) {
compiler.hooks.thisCompilation.tap(SoftwareBillOfMaterialsPlugin.name, (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name: SoftwareBillOfMaterialsPlugin.name,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
async () => {
const stats = compilation.getStats().toJson({
assets: true,
dependentModules: true,
});

const shared = require("@lastui/dependencies/sbom.json");

const lockfile = require(
path.resolve(require.resolve("@lastui/dependencies"), "..", "..", "..", "..", "package-lock.json"),
);

for (const entrypoint in stats.namedChunkGroups) {
const candidates = [];

for (const asset of stats.assets) {
if (!asset.info.sourceFilename) {
continue;
}
if (!asset.info.sourceFilename.startsWith("node_modules/")) {
continue;
}
if (candidates.indexOf("./" + asset.info.sourceFilename) === -1) {
candidates.push("./" + asset.info.sourceFilename);
}
}

for (const chunk of stats.chunks) {
if (chunk.id !== entrypoint) {
continue;
}

for (const entry of chunk.modules) {
if (entry.moduleType === "asset/inline") {
if (!entry.id.startsWith("./node_modules/")) {
continue;
}
if (candidates.indexOf(entry.id) === -1) {
candidates.push(entry.id);
}
} else {
for (const reason of entry.reasons) {
if (!reason.resolvedModuleId) {
continue;
}
if (!reason.resolvedModuleId.startsWith("./node_modules/")) {
continue;
}
if (candidates.indexOf(reason.resolvedModuleId) === -1) {
candidates.push(reason.resolvedModuleId);
}
}
}
}
}

const report = {};

for (const candidate of candidates) {
const parts = candidate.substring(15).split("/");
const item = candidate[15] === "@" ? parts[0] + "/" + parts[1] : parts[0];

if (item in shared) {
continue;
}

report[item] = "*";
}

for (const item in report) {
const entry = lockfile.packages["node_modules/" + item];
if (!entry) {
delete report[item];
} else {
report[item] = entry.version;
}
}

const outputPath = path.join("..", "reports", this.options.filename(entrypoint));

const content = Buffer.from(JSON.stringify(report, null, 2), "utf-8");

const asset = compilation.getAsset(outputPath);
if (asset) {
compilation.updateAsset(outputPath, new compiler.webpack.sources.RawSource(content));
} else {
compilation.emitAsset(outputPath, new compiler.webpack.sources.RawSource(content));
}
}
},
);
});
}
}

SoftwareBillOfMaterialsPlugin.name = "SoftwareBillOfMaterialsPlugin";

module.exports = SoftwareBillOfMaterialsPlugin;

0 comments on commit 465388b

Please sign in to comment.