@webiny/build-tools 0.0.0-unstable.0d717d18dd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/bundling/admin/createBuildAdmin.js +18 -0
  4. package/bundling/admin/createRsbuildConfig.js +174 -0
  5. package/bundling/admin/createWatchAdmin.js +15 -0
  6. package/bundling/admin/index.js +3 -0
  7. package/bundling/function/createBuildFunction.js +18 -0
  8. package/bundling/function/createRsbuildConfig.js +126 -0
  9. package/bundling/function/createWatchFunction.js +15 -0
  10. package/bundling/function/index.js +3 -0
  11. package/bundling/importValidatorPlugin.js +93 -0
  12. package/bundling/printBuildStats.js +46 -0
  13. package/index.d.ts +69 -0
  14. package/index.js +3 -0
  15. package/package.json +82 -0
  16. package/packages/buildPackage/copyToDist.js +11 -0
  17. package/packages/buildPackage/rslibCompile.js +65 -0
  18. package/packages/buildPackage/tsAliasReplacer.js +125 -0
  19. package/packages/buildPackage/tsCompile.js +80 -0
  20. package/packages/buildPackage/typescript/getTscBinaryPath.js +47 -0
  21. package/packages/buildPackage/typescript/readTsConfig.js +8 -0
  22. package/packages/buildPackage/typescript/runTsc.js +15 -0
  23. package/packages/buildPackage/typescript/writeTempTsConfig.js +10 -0
  24. package/packages/buildPackage/validateEsmImports.js +91 -0
  25. package/packages/buildPackage.js +93 -0
  26. package/packages/createBuildPackage.js +7 -0
  27. package/packages/createWatchPackage.js +7 -0
  28. package/packages/index.js +4 -0
  29. package/packages/watchPackage.js +39 -0
  30. package/traverseLoaders.js +14 -0
  31. package/utils/PackageJson.backup.ts +45 -0
  32. package/utils/PackageJson.d.ts +33 -0
  33. package/utils/PackageJson.js +44 -0
  34. package/utils.js +33 -0
  35. package/workspaces/index.js +15 -0
  36. package/workspaces/linkWorkspaces.js +100 -0
@@ -0,0 +1,39 @@
1
+ import fs from "node:fs";
2
+ import { join } from "node:path";
3
+
4
+ export default async options => {
5
+ const { cwd } = options;
6
+
7
+ // Invalidate the build-cache freshness marker (see scripts/buildPackages
8
+ // distBuildHash.ts, ".webiny-build-hash"). Watch writes into dist without
9
+ // updating that marker, so its contents no longer match the last full
10
+ // build — a later `yarn build` must restore/rebuild instead of trusting the
11
+ // marker and skipping the cache→dist copy.
12
+ fs.rmSync(join(cwd, "dist", ".webiny-build-hash"), { force: true });
13
+
14
+ // Must be a dynamic import — see rslibCompile.js for the reason.
15
+ const [{ createRslib }, { pluginSvgr }] = await Promise.all([
16
+ import("@rslib/core"),
17
+ import("@rsbuild/plugin-svgr")
18
+ ]);
19
+
20
+ const rslib = await createRslib({
21
+ cwd,
22
+ config: {
23
+ lib: [{ format: "esm", bundle: false }],
24
+ source: {
25
+ entry: ["./src/**/*.{ts,tsx,js,jsx}"],
26
+ alias: { "~": "./src" }
27
+ },
28
+ output: {
29
+ target: "web",
30
+ distPath: { root: "./dist" },
31
+ cleanDistPath: false,
32
+ sourceMap: { js: "source-map" }
33
+ },
34
+ plugins: [pluginSvgr({ mixedImport: true, svgrOptions: { exportType: "named" } })]
35
+ }
36
+ });
37
+
38
+ await rslib.build({ watch: true });
39
+ };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A utility to recursively traverse loaders and execute the "onLoader" callback.
3
+ */
4
+ export const traverseLoaders = (loaders, onLoader) => {
5
+ for (const loader of loaders) {
6
+ if (loader.oneOf) {
7
+ traverseLoaders(loader.oneOf, onLoader);
8
+ } else if (loader.use) {
9
+ traverseLoaders(loader.use, onLoader);
10
+ } else {
11
+ onLoader(loader);
12
+ }
13
+ }
14
+ };
@@ -0,0 +1,45 @@
1
+ // We'll use this class once the package is converted to TS!
2
+ import { loadJsonFileSync } from "load-json-file";
3
+ import { findUp } from "find-up";
4
+
5
+ export class PackageJson {
6
+ private readonly filePath: string;
7
+ private readonly json: Record<string, any>;
8
+
9
+ static fromFile(filePath: string) {
10
+ return new PackageJson(filePath, loadJsonFileSync(filePath));
11
+ }
12
+
13
+ static async findClosest(fromPath: string) {
14
+ const closestPackageJson = await findUp("package.json", { cwd: fromPath });
15
+ if (!closestPackageJson) {
16
+ throw Error(`Failed to find ${fromPath}`);
17
+ }
18
+ return PackageJson.fromFile(closestPackageJson);
19
+ }
20
+
21
+ static async fromPackage(packageName: string, cwd?: string) {
22
+ const jsonPath = await findUp(`node_modules/${packageName}/package.json`, {
23
+ cwd: cwd ?? process.cwd()
24
+ });
25
+
26
+ if (!jsonPath) {
27
+ throw Error(`Failed to find package ${packageName}`);
28
+ }
29
+
30
+ return PackageJson.fromFile(jsonPath);
31
+ }
32
+
33
+ private constructor(filePath: string, json: Record<string, any>) {
34
+ this.filePath = filePath;
35
+ this.json = json;
36
+ }
37
+
38
+ getLocation() {
39
+ return this.filePath;
40
+ }
41
+
42
+ getJson() {
43
+ return this.json;
44
+ }
45
+ }
@@ -0,0 +1,33 @@
1
+ export declare class PackageJson {
2
+ private readonly filePath: string;
3
+ private readonly json: Record<string, any>;
4
+
5
+ private constructor(filePath: string, json: Record<string, any>);
6
+
7
+ /**
8
+ * Load a PackageJson instance from a given file path.
9
+ */
10
+ static fromFile(filePath: string): PackageJson;
11
+
12
+ /**
13
+ * Find the closest package.json starting from the given path.
14
+ * Throws if no package.json is found.
15
+ */
16
+ static findClosest(fromPath: string): Promise<PackageJson>;
17
+
18
+ /**
19
+ * Load a PackageJson instance from a package in node_modules.
20
+ * Throws if the package.json cannot be found.
21
+ */
22
+ static fromPackage(packageName: string, cwd?: string): Promise<PackageJson>;
23
+
24
+ /**
25
+ * Get the absolute path to this package.json file.
26
+ */
27
+ getLocation(): string;
28
+
29
+ /**
30
+ * Get the raw JSON contents of the package.json file.
31
+ */
32
+ getJson(): Record<string, any>;
33
+ }
@@ -0,0 +1,44 @@
1
+ import { loadJsonFileSync } from "load-json-file";
2
+ import { findUp } from "find-up";
3
+
4
+ export class PackageJson {
5
+ filePath;
6
+ json;
7
+
8
+ static fromFile(filePath) {
9
+ return new PackageJson(filePath, loadJsonFileSync(filePath));
10
+ }
11
+
12
+ static async findClosest(fromPath) {
13
+ const closestPackageJson = await findUp("package.json", { cwd: fromPath });
14
+ if (!closestPackageJson) {
15
+ throw Error(`Failed to find ${fromPath}`);
16
+ }
17
+ return PackageJson.fromFile(closestPackageJson);
18
+ }
19
+
20
+ static async fromPackage(packageName, cwd) {
21
+ const jsonPath = await findUp(`node_modules/${packageName}/package.json`, {
22
+ cwd: cwd ?? process.cwd()
23
+ });
24
+
25
+ if (!jsonPath) {
26
+ throw Error(`Failed to find package ${packageName}`);
27
+ }
28
+
29
+ return PackageJson.fromFile(jsonPath);
30
+ }
31
+
32
+ constructor(filePath, json) {
33
+ this.filePath = filePath;
34
+ this.json = json;
35
+ }
36
+
37
+ getLocation() {
38
+ return this.filePath;
39
+ }
40
+
41
+ getJson() {
42
+ return this.json;
43
+ }
44
+ }
package/utils.js ADDED
@@ -0,0 +1,33 @@
1
+ import merge from "lodash/merge.js";
2
+
3
+ /**
4
+ * Prepares the options object, sent to build and watch functions.
5
+ * @param config
6
+ * @param options
7
+ * @returns {Promise<{overrides}|*>}
8
+ */
9
+ export const prepareOptions = ({ config, options }) => {
10
+ const mergedOptions = merge({}, config, options);
11
+
12
+ // If it doesn't exist, ensure `overrides` is an empty object.
13
+ if (!mergedOptions.overrides) {
14
+ mergedOptions.overrides = {};
15
+ }
16
+
17
+ // We want to have debug logs disabled by default.
18
+ mergedOptions.debug = mergedOptions.debug === true;
19
+
20
+ return mergedOptions;
21
+ };
22
+
23
+ /**
24
+ * Calculates time difference between the initial `getDuration`
25
+ * invocation and the invocation of the returned callback function.
26
+ * @returns {function(): string}
27
+ */
28
+ export const getDuration = () => {
29
+ const start = new Date();
30
+ return () => {
31
+ return (new Date() - start) / 1000;
32
+ };
33
+ };
@@ -0,0 +1,15 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { listWorkspaces } from "@webiny/stdlib/node";
4
+ export { linkWorkspaces } from "./linkWorkspaces";
5
+
6
+ const hasPackageJson = p => fs.existsSync(p + "/package.json");
7
+
8
+ export const allWorkspaces = () => {
9
+ return listWorkspaces()
10
+ .map(pkg => {
11
+ return pkg.path;
12
+ })
13
+ .filter(hasPackageJson)
14
+ .map(pkg => pkg.replace(/\//g, path.sep));
15
+ };
@@ -0,0 +1,100 @@
1
+ /**
2
+ * This tool will re-link monorepo packages to one of the following directories (by priority):
3
+ * - {package}/package.json -> webiny.publishFrom
4
+ * - package root directory
5
+ */
6
+
7
+ import "tsx";
8
+ import { listWorkspaces } from "@webiny/stdlib/node";
9
+ import path from "path";
10
+ import get from "lodash/get.js";
11
+ import fs from "fs-extra";
12
+ import * as rimraf from "rimraf";
13
+
14
+ async function symlink(src, dest) {
15
+ if (process.platform !== "win32") {
16
+ // use relative paths otherwise which will be retained if the directory is moved
17
+ src = path.relative(path.dirname(dest), src);
18
+ // When path.relative returns an empty string for the current directory, we should instead use
19
+ // '.', which is a valid fs.symlink target.
20
+ src = src || ".";
21
+ }
22
+
23
+ try {
24
+ const stats = await fs.lstat(dest);
25
+ if (stats.isSymbolicLink()) {
26
+ const resolved = dest;
27
+ if (resolved === src) {
28
+ return;
29
+ }
30
+ }
31
+ } catch (err) {
32
+ if (err.code !== "ENOENT") {
33
+ throw err;
34
+ }
35
+ }
36
+ // We use rimraf for unlink which never throws an ENOENT on missing target
37
+ rimraf.sync(dest);
38
+
39
+ if (process.platform === "win32") {
40
+ // use directory junctions if possible on win32, this requires absolute paths
41
+ await fs.symlink(src, dest, "junction");
42
+ } else {
43
+ await fs.symlink(src, dest);
44
+ }
45
+ }
46
+
47
+ const defaults = {
48
+ whitelist: [],
49
+ blacklist: []
50
+ };
51
+
52
+ export const linkWorkspaces = async ({ whitelist, blacklist } = defaults) => {
53
+ console.log(`Linking project workspaces...`);
54
+ const { PackageJson } = await import("../utils/PackageJson.js");
55
+
56
+ whitelist = (whitelist || []).map(p => path.resolve(p));
57
+ blacklist = (blacklist || []).map(p => path.resolve(p));
58
+ // Filter packages to only those in the whitelisted folders
59
+
60
+ const packages = listWorkspaces({
61
+ cwd: process.cwd()
62
+ })
63
+ .map(pkg => {
64
+ return pkg.path.replace(/\//g, path.sep);
65
+ })
66
+ .filter(pkg => {
67
+ const isBlacklisted = blacklist.some(b => pkg.startsWith(b));
68
+ if (isBlacklisted) {
69
+ return false;
70
+ } else if (whitelist.length === 0) {
71
+ return true;
72
+ }
73
+ return whitelist.some(w => pkg.startsWith(w));
74
+ });
75
+
76
+ for (let i = 0; i < packages.length; i++) {
77
+ const packageJson = path.resolve(packages[i], "package.json");
78
+ if (!fs.existsSync(packageJson)) {
79
+ continue;
80
+ }
81
+
82
+ const pkgJson = await PackageJson.fromFile(packageJson);
83
+ const pkg = pkgJson.getJson();
84
+
85
+ const targetDirectory = get(pkg, "webiny.publishFrom");
86
+ const link = path.resolve("node_modules", pkg.name);
87
+ const target = path.resolve(packages[i], targetDirectory || ".");
88
+
89
+ if (!fs.existsSync(target)) {
90
+ fs.mkdirpSync(target);
91
+ }
92
+
93
+ try {
94
+ await fs.mkdirp(path.dirname(link));
95
+ await symlink(target, link);
96
+ } catch (err) {
97
+ console.log(`Failed ${pkg.name}: ${err.message}`);
98
+ }
99
+ }
100
+ };