@webiny/build-tools 0.0.0-unstable.6f45466a1d → 0.0.0-unstable.7be00a75a9
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.
- package/bundling/admin/createBuildAdmin.js +2 -1
- package/bundling/admin/createRsbuildConfig.js +43 -11
- package/bundling/admin/createWatchAdmin.js +2 -1
- package/bundling/function/createBuildFunction.js +3 -2
- package/bundling/function/createRsbuildConfig.js +19 -4
- package/bundling/function/createWatchFunction.js +3 -2
- package/bundling/importValidatorPlugin.js +1 -1
- package/bundling/printBuildStats.js +3 -1
- package/index.d.ts +0 -13
- package/index.js +1 -6
- package/package.json +31 -45
- package/packages/buildPackage/copyToDist.js +2 -2
- package/packages/buildPackage/rslibCompile.js +63 -0
- package/packages/buildPackage/tsAliasReplacer.js +93 -7
- package/packages/buildPackage/tsCompile.js +42 -12
- package/packages/buildPackage.js +61 -8
- package/packages/index.js +0 -2
- package/packages/watchPackage.js +24 -112
- package/utils/PackageJson.backup.ts +3 -4
- package/utils/PackageJson.js +3 -3
- package/workspaces/linkWorkspaces.js +2 -13
- package/.babelrc.js +0 -1
- package/.eslintrc.cjs +0 -6
- package/packages/buildPackage/babelCompile.js +0 -103
- package/packages/createBabelConfigForNode.js +0 -19
- package/packages/createBabelConfigForReact.js +0 -20
- package/packages/createSwcConfigForNode.js +0 -45
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRsbuild } from "@rsbuild/core";
|
|
2
1
|
import { createRsbuildConfig } from "./createRsbuildConfig.js";
|
|
3
2
|
import { printBuildStats } from "../printBuildStats.js";
|
|
4
3
|
|
|
@@ -7,6 +6,8 @@ export const createBuildAdmin =
|
|
|
7
6
|
async ({ cwd }) => {
|
|
8
7
|
process.env.NODE_ENV = "production";
|
|
9
8
|
|
|
9
|
+
// Must be a dynamic import — see rslibCompile.js for the reason.
|
|
10
|
+
const { createRsbuild } = await import("@rsbuild/core");
|
|
10
11
|
const rsbuildConfig = createRsbuildConfig({ cwd });
|
|
11
12
|
|
|
12
13
|
const rsbuild = await createRsbuild({ rsbuildConfig });
|
|
@@ -19,12 +19,6 @@ export const createRsbuildConfig = ({ cwd }) => {
|
|
|
19
19
|
},
|
|
20
20
|
define: envVars
|
|
21
21
|
},
|
|
22
|
-
resolve: {
|
|
23
|
-
alias: {
|
|
24
|
-
// This is a temporary fix, until we sort out the `react-butterfiles` dependency.
|
|
25
|
-
"react-butterfiles": "@webiny/app/react-butterfiles"
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
22
|
output: { distPath: { root: paths.admin.outputFolder } },
|
|
29
23
|
mode,
|
|
30
24
|
dev: { hmr: true },
|
|
@@ -33,14 +27,25 @@ export const createRsbuildConfig = ({ cwd }) => {
|
|
|
33
27
|
},
|
|
34
28
|
tools: {
|
|
35
29
|
postcss: (_, { addPlugins }) => {
|
|
36
|
-
addPlugins(
|
|
30
|
+
addPlugins([
|
|
31
|
+
createInjectTailwindSourcePlugin(
|
|
32
|
+
path.join(paths.projectRootFolder, "extensions")
|
|
33
|
+
),
|
|
37
34
|
tailwindcss({
|
|
38
35
|
base: getTailwindBasePath(paths.projectRootFolder)
|
|
39
|
-
})
|
|
40
|
-
|
|
36
|
+
}),
|
|
37
|
+
createStripTailwindSourceLeftoverPlugin()
|
|
38
|
+
]);
|
|
39
|
+
},
|
|
40
|
+
rspack: {
|
|
41
|
+
watchOptions: {
|
|
42
|
+
// Wait for dependency builds to finish before triggering a recompilation.
|
|
43
|
+
aggregateTimeout: 500,
|
|
44
|
+
ignored: ["**/node_modules/**", "**/.git/**"]
|
|
45
|
+
}
|
|
41
46
|
}
|
|
42
47
|
},
|
|
43
|
-
server: { port: 3001 },
|
|
48
|
+
server: { port: process.env.PORT || 3001, host: "0.0.0.0" },
|
|
44
49
|
html: {
|
|
45
50
|
template: paths.projectRootFolder + "/public/index.html"
|
|
46
51
|
},
|
|
@@ -52,7 +57,9 @@ export const createRsbuildConfig = ({ cwd }) => {
|
|
|
52
57
|
async: mode === "development"
|
|
53
58
|
}
|
|
54
59
|
}),
|
|
55
|
-
pluginReact(
|
|
60
|
+
pluginReact({
|
|
61
|
+
splitChunks: false
|
|
62
|
+
}),
|
|
56
63
|
pluginSass(),
|
|
57
64
|
pluginSvgr({
|
|
58
65
|
mixedImport: true,
|
|
@@ -102,6 +109,31 @@ const getTailwindBasePath = projectRootFolderPath => {
|
|
|
102
109
|
return path.join(projectRootFolderPath, "node_modules", "@webiny");
|
|
103
110
|
};
|
|
104
111
|
|
|
112
|
+
/*
|
|
113
|
+
Injects an `@source` directive into the Tailwind CSS AST at build time, pointing to the
|
|
114
|
+
given absolute path. https://tailwindcss.com/docs/functions-and-directives#source-directive
|
|
115
|
+
*/
|
|
116
|
+
const createInjectTailwindSourcePlugin = sourcePath => ({
|
|
117
|
+
postcssPlugin: "inject-tailwind-source",
|
|
118
|
+
Once(root) {
|
|
119
|
+
root.prepend(`@source "${sourcePath}";`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
/*
|
|
124
|
+
Removes any leftover `@source` at-rule from the output. Tailwind v4 strips the
|
|
125
|
+
directive from files it processes, but `@tailwindcss/postcss` only processes
|
|
126
|
+
files containing one of its trigger at-rules; for other files (e.g., pre-bundled
|
|
127
|
+
component CSS imported through JS), the injected directive would otherwise
|
|
128
|
+
survive into the production bundle, exposing the absolute build-machine path.
|
|
129
|
+
*/
|
|
130
|
+
const createStripTailwindSourceLeftoverPlugin = () => ({
|
|
131
|
+
postcssPlugin: "strip-tailwind-source-leftover",
|
|
132
|
+
Once(root) {
|
|
133
|
+
root.walkAtRules("source", node => node.remove());
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
105
137
|
const getEnvVars = () => {
|
|
106
138
|
const raw = Object.keys(process.env)
|
|
107
139
|
.filter(key => {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRsbuild } from "@rsbuild/core";
|
|
2
1
|
import { createRsbuildConfig } from "./createRsbuildConfig.js";
|
|
3
2
|
|
|
4
3
|
export const createWatchAdmin =
|
|
@@ -6,6 +5,8 @@ export const createWatchAdmin =
|
|
|
6
5
|
async ({ cwd }) => {
|
|
7
6
|
process.env.NODE_ENV = "development";
|
|
8
7
|
|
|
8
|
+
// Must be a dynamic import — see rslibCompile.js for the reason.
|
|
9
|
+
const { createRsbuild } = await import("@rsbuild/core");
|
|
9
10
|
const rsbuildConfig = createRsbuildConfig({ cwd });
|
|
10
11
|
|
|
11
12
|
const rsbuild = await createRsbuild({ rsbuildConfig });
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRsbuild } from "@rsbuild/core";
|
|
2
1
|
import { createRsbuildConfig } from "./createRsbuildConfig.js";
|
|
3
2
|
import { printBuildStats } from "../printBuildStats.js";
|
|
4
3
|
|
|
@@ -7,7 +6,9 @@ export const createBuildFunction =
|
|
|
7
6
|
async ({ cwd }) => {
|
|
8
7
|
process.env.NODE_ENV = "production";
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
// Must be a dynamic import — see rslibCompile.js for the reason.
|
|
10
|
+
const { createRsbuild } = await import("@rsbuild/core");
|
|
11
|
+
const rsbuildConfig = await createRsbuildConfig({ cwd });
|
|
11
12
|
|
|
12
13
|
const rsbuild = await createRsbuild({ rsbuildConfig });
|
|
13
14
|
|
|
@@ -1,19 +1,29 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import rspack from "@rspack/core";
|
|
3
2
|
import { pluginTypeCheck } from "@rsbuild/plugin-type-check";
|
|
4
3
|
import { createImportValidatorPlugin } from "../importValidatorPlugin.js";
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
const DEFAULT_WEBINY_INFRA_API_MAX_BUNDLE_SIZE = 4_718_592; // 4.5 MB
|
|
6
|
+
|
|
7
|
+
export const createRsbuildConfig = async ({ cwd }) => {
|
|
8
|
+
// Must be a dynamic import — see rslibCompile.js for the reason.
|
|
9
|
+
const { default: rspack } = await import("@rspack/core");
|
|
7
10
|
const paths = getPaths(cwd);
|
|
8
11
|
const mode = getMode();
|
|
12
|
+
const isDebugEnabled = process.env.DEBUG === "true";
|
|
13
|
+
|
|
14
|
+
// Configurable via WEBINY_INFRA_API_MAX_BUNDLE_SIZE (bytes).
|
|
15
|
+
const maxBundleSize =
|
|
16
|
+
parseInt(process.env.WEBINY_INFRA_API_MAX_BUNDLE_SIZE) ||
|
|
17
|
+
DEFAULT_WEBINY_INFRA_API_MAX_BUNDLE_SIZE;
|
|
9
18
|
|
|
10
19
|
return /** @type {import("@rsbuild/core").RsbuildConfig} */ ({
|
|
11
20
|
source: { entry: { index: paths.fn.entryFile } },
|
|
12
21
|
output: {
|
|
13
22
|
module: true,
|
|
14
23
|
target: "node",
|
|
24
|
+
minify: true,
|
|
15
25
|
sourceMap: {
|
|
16
|
-
js:
|
|
26
|
+
js: isDebugEnabled ? "source-map" : false
|
|
17
27
|
},
|
|
18
28
|
filename: {
|
|
19
29
|
js: pathData => {
|
|
@@ -30,7 +40,12 @@ export const createRsbuildConfig = ({ cwd }) => {
|
|
|
30
40
|
},
|
|
31
41
|
tools: {
|
|
32
42
|
rspack: {
|
|
33
|
-
|
|
43
|
+
performance: {
|
|
44
|
+
hints: "error",
|
|
45
|
+
maxEntrypointSize: maxBundleSize,
|
|
46
|
+
maxAssetSize: maxBundleSize
|
|
47
|
+
},
|
|
48
|
+
externals: [/^@aws-sdk/, /^aws-sdk$/, /^sharp$/],
|
|
34
49
|
plugins: [
|
|
35
50
|
// This is necessary to enable JSDOM usage in Lambda.
|
|
36
51
|
// https://rspack.dev/plugins/webpack/ignore-plugin
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRsbuild } from "@rsbuild/core";
|
|
2
1
|
import { createRsbuildConfig } from "./createRsbuildConfig.js";
|
|
3
2
|
|
|
4
3
|
export const createWatchFunction =
|
|
@@ -6,7 +5,9 @@ export const createWatchFunction =
|
|
|
6
5
|
async ({ cwd }) => {
|
|
7
6
|
process.env.NODE_ENV = "development";
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
// Must be a dynamic import — see rslibCompile.js for the reason.
|
|
9
|
+
const { createRsbuild } = await import("@rsbuild/core");
|
|
10
|
+
const rsbuildConfig = await createRsbuildConfig({ cwd });
|
|
10
11
|
|
|
11
12
|
const rsbuild = await createRsbuild({ rsbuildConfig });
|
|
12
13
|
|
|
@@ -5,7 +5,7 @@ const cyan = "\x1b[36m";
|
|
|
5
5
|
const reset = "\x1b[0m";
|
|
6
6
|
const bold = "\x1b[1m";
|
|
7
7
|
|
|
8
|
-
const whitelist = ["@webiny/cognito", "@webiny/auth0", "@webiny/okta"];
|
|
8
|
+
const whitelist = ["@webiny/cognito", "@webiny/auth0", "@webiny/okta", "@webiny/plugins"];
|
|
9
9
|
|
|
10
10
|
export const createImportValidatorPlugin = () => {
|
|
11
11
|
return {
|
|
@@ -4,7 +4,9 @@ import chalk from "chalk";
|
|
|
4
4
|
export const printBuildStats =
|
|
5
5
|
({ cwd, label = "build", extensions = [".js", ".mjs", ".css"] }) =>
|
|
6
6
|
({ stats }) => {
|
|
7
|
-
if (!stats)
|
|
7
|
+
if (!stats) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
8
10
|
|
|
9
11
|
const statsJson = stats.toJson({ assets: true, children: false });
|
|
10
12
|
const assets = statsJson.assets || [];
|
package/index.d.ts
CHANGED
|
@@ -5,10 +5,6 @@ export { RspackConfig };
|
|
|
5
5
|
// Build commands.
|
|
6
6
|
export type BuildCommand<TOptions = Record<string, any>> = (options: TOptions) => Promise<void>;
|
|
7
7
|
|
|
8
|
-
export interface BabelConfig {
|
|
9
|
-
[key: string]: any;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
8
|
export interface SwcConfig {
|
|
13
9
|
[key: string]: any;
|
|
14
10
|
}
|
|
@@ -20,7 +16,6 @@ export interface DefinePluginOptions {
|
|
|
20
16
|
export interface BuildAppConfigOverrides {
|
|
21
17
|
entry?: string;
|
|
22
18
|
openBrowser?: boolean;
|
|
23
|
-
babel?: (config: BabelConfig) => BabelConfig;
|
|
24
19
|
}
|
|
25
20
|
// Build commands - apps.
|
|
26
21
|
export interface BuildAppConfig {
|
|
@@ -51,7 +46,6 @@ interface BuildFunctionConfig {
|
|
|
51
46
|
};
|
|
52
47
|
define?: DefinePluginOptions;
|
|
53
48
|
rspack?: (config: RspackConfig) => RspackConfig;
|
|
54
|
-
babel?: (config: BabelConfig) => BabelConfig;
|
|
55
49
|
swc?: (config: SwcConfig) => SwcConfig;
|
|
56
50
|
};
|
|
57
51
|
}
|
|
@@ -71,12 +65,5 @@ interface BuildPackageConfig {
|
|
|
71
65
|
};
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
interface BabelConfigParams {
|
|
75
|
-
path: string;
|
|
76
|
-
esm?: boolean;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
68
|
export function createBuildPackage(options: BuildPackageConfig): BuildCommand;
|
|
80
69
|
export function createWatchPackage(options: BuildPackageConfig): BuildCommand;
|
|
81
|
-
export function createBabelConfigForNode(options: BabelConfigParams): BabelConfig;
|
|
82
|
-
export function createBabelConfigForReact(options: BabelConfigParams): BabelConfig;
|
package/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
export { createWatchAdmin, createBuildAdmin } from "./bundling/admin/index.js";
|
|
2
2
|
export { createBuildFunction, createWatchFunction } from "./bundling/function/index.js";
|
|
3
|
-
export {
|
|
4
|
-
createWatchPackage,
|
|
5
|
-
createBuildPackage,
|
|
6
|
-
createBabelConfigForNode,
|
|
7
|
-
createBabelConfigForReact
|
|
8
|
-
} from "./packages/index.js";
|
|
3
|
+
export { createWatchPackage, createBuildPackage } from "./packages/index.js";
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/build-tools",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.7be00a75a9",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./index.js",
|
|
7
|
+
"./*": "./*"
|
|
8
|
+
},
|
|
6
9
|
"description": "A collection of utilities for Webiny project.",
|
|
7
10
|
"repository": {
|
|
8
11
|
"type": "git",
|
|
@@ -14,43 +17,34 @@
|
|
|
14
17
|
"Adrian Smijulj <adrian@webiny.com>"
|
|
15
18
|
],
|
|
16
19
|
"dependencies": {
|
|
17
|
-
"@
|
|
18
|
-
"@
|
|
19
|
-
"@
|
|
20
|
-
"@
|
|
21
|
-
"@
|
|
22
|
-
"@
|
|
23
|
-
"@
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"@svgr/webpack": "6.5.1",
|
|
29
|
-
"@swc/plugin-emotion": "11.5.0",
|
|
30
|
-
"@tailwindcss/postcss": "4.1.18",
|
|
31
|
-
"@types/webpack-env": "1.18.8",
|
|
32
|
-
"chalk": "4.1.2",
|
|
33
|
-
"chokidar": "4.0.3",
|
|
34
|
-
"css-loader": "7.1.3",
|
|
35
|
-
"eslint": "9.39.2",
|
|
20
|
+
"@rsbuild/core": "2.0.6",
|
|
21
|
+
"@rsbuild/plugin-react": "2.0.0",
|
|
22
|
+
"@rsbuild/plugin-sass": "1.5.2",
|
|
23
|
+
"@rsbuild/plugin-svgr": "2.0.2",
|
|
24
|
+
"@rsbuild/plugin-type-check": "1.3.4",
|
|
25
|
+
"@rslib/core": "0.21.5",
|
|
26
|
+
"@rspack/core": "2.0.3",
|
|
27
|
+
"@swc/plugin-emotion": "14.9.0",
|
|
28
|
+
"@tailwindcss/postcss": "4.3.0",
|
|
29
|
+
"chalk": "5.6.2",
|
|
30
|
+
"css-loader": "7.1.4",
|
|
36
31
|
"fast-glob": "3.3.3",
|
|
37
|
-
"find-up": "
|
|
38
|
-
"fs-extra": "11.3.
|
|
32
|
+
"find-up": "8.0.0",
|
|
33
|
+
"fs-extra": "11.3.5",
|
|
39
34
|
"get-yarn-workspaces": "1.0.2",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
35
|
+
"load-json-file": "7.0.1",
|
|
36
|
+
"lodash": "4.18.1",
|
|
37
|
+
"postcss-loader": "8.2.1",
|
|
43
38
|
"raw-loader": "4.0.2",
|
|
44
|
-
"react-dom": "18.
|
|
45
|
-
"react-refresh": "0.
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"sass": "
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"ts-morph": "27.0.2",
|
|
39
|
+
"react-dom": "18.3.1",
|
|
40
|
+
"react-refresh": "0.18.0",
|
|
41
|
+
"rimraf": "6.1.3",
|
|
42
|
+
"sass": "1.99.0",
|
|
43
|
+
"sass-loader": "17.0.0",
|
|
44
|
+
"style-loader": "4.0.0",
|
|
45
|
+
"ts-morph": "28.0.0",
|
|
52
46
|
"tsx": "4.21.0",
|
|
53
|
-
"typescript": "
|
|
47
|
+
"typescript": "6.0.3",
|
|
54
48
|
"url-loader": "4.1.1",
|
|
55
49
|
"utf-8-validate": "6.0.6"
|
|
56
50
|
},
|
|
@@ -63,19 +57,12 @@
|
|
|
63
57
|
"!!raw-loader!"
|
|
64
58
|
],
|
|
65
59
|
"dependencies": [
|
|
66
|
-
"@babel/preset-react",
|
|
67
|
-
"@svgr/webpack",
|
|
68
|
-
"@babel/preset-env",
|
|
69
|
-
"@babel/preset-typescript",
|
|
70
60
|
"@swc/plugin-emotion",
|
|
71
|
-
"@types/webpack-env",
|
|
72
61
|
"postcss-loader",
|
|
73
62
|
"raw-loader",
|
|
74
63
|
"react-refresh",
|
|
75
64
|
"style-loader",
|
|
76
65
|
"typescript",
|
|
77
|
-
"eslint",
|
|
78
|
-
"process",
|
|
79
66
|
"sass-loader",
|
|
80
67
|
"sass",
|
|
81
68
|
"bufferutil",
|
|
@@ -86,8 +73,7 @@
|
|
|
86
73
|
}
|
|
87
74
|
},
|
|
88
75
|
"publishConfig": {
|
|
89
|
-
"access": "public"
|
|
90
|
-
"directory": "."
|
|
76
|
+
"access": "public"
|
|
91
77
|
},
|
|
92
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "b8aec8a1be3f25c3b428b357fe1e352c7cbff9ae"
|
|
93
79
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import { join } from "path";
|
|
3
3
|
|
|
4
|
-
export const copyToDist = (path, { cwd, logs }) => {
|
|
4
|
+
export const copyToDist = (path, { cwd, logs, outputDir }) => {
|
|
5
5
|
const from = join(cwd, path);
|
|
6
|
-
const to = join(cwd, "dist", path);
|
|
6
|
+
const to = join(outputDir || join(cwd, "dist"), path);
|
|
7
7
|
if (fs.existsSync(from)) {
|
|
8
8
|
fs.copyFileSync(from, to);
|
|
9
9
|
logs !== false && console.log(`Copied ${path}.`);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { dirname } from "path";
|
|
4
|
+
import glob from "fast-glob";
|
|
5
|
+
|
|
6
|
+
const COMPILE_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx"];
|
|
7
|
+
const SKIP_EXTENSIONS = [".d.ts"];
|
|
8
|
+
|
|
9
|
+
export const rslibCompile = async ({ cwd }) => {
|
|
10
|
+
// Copy non-compilable files (assets, json, graphql, etc.) as-is.
|
|
11
|
+
const pattern = path.join(cwd, "src/**/*.*").replace(/\\/g, "/");
|
|
12
|
+
const allFiles = glob.sync(pattern, { onlyFiles: true, dot: true });
|
|
13
|
+
|
|
14
|
+
for (const file of allFiles) {
|
|
15
|
+
const shouldCompile = COMPILE_EXTENSIONS.some(ext => file.endsWith(ext));
|
|
16
|
+
const shouldSkip = SKIP_EXTENSIONS.some(ext => file.endsWith(ext));
|
|
17
|
+
|
|
18
|
+
if (!shouldCompile || shouldSkip) {
|
|
19
|
+
const destPath = file.replace(path.join(cwd, "src"), path.join(cwd, "dist"));
|
|
20
|
+
fs.mkdirSync(dirname(destPath), { recursive: true });
|
|
21
|
+
fs.copyFileSync(file, destPath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Must be a dynamic import. @rslib/core statically loads rspack's native
|
|
26
|
+
// binding. Workers that also run createBuildAdmin load a different rspack
|
|
27
|
+
// version via rsbuild, so two native binaries end up in the same process
|
|
28
|
+
// and cause a SIGSEGV. Deferring the import keeps each rspack isolated to
|
|
29
|
+
// the workers that actually need it.
|
|
30
|
+
const [{ createRslib }, { pluginSvgr }] = await Promise.all([
|
|
31
|
+
import("@rslib/core"),
|
|
32
|
+
import("@rsbuild/plugin-svgr")
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const rslib = await createRslib({
|
|
36
|
+
cwd,
|
|
37
|
+
config: {
|
|
38
|
+
lib: [{ format: "esm", bundle: false }],
|
|
39
|
+
source: {
|
|
40
|
+
// SVGs included so rslib runs them through pluginSvgr and emits
|
|
41
|
+
// a .js React-component module alongside the copied .svg asset.
|
|
42
|
+
entry: ["./src/**/*.{ts,tsx,js,jsx,svg}"],
|
|
43
|
+
alias: { "~": "./src" }
|
|
44
|
+
},
|
|
45
|
+
output: {
|
|
46
|
+
target: "web",
|
|
47
|
+
distPath: { root: "./dist" },
|
|
48
|
+
cleanDistPath: false,
|
|
49
|
+
sourceMap: { js: "source-map" },
|
|
50
|
+
// mixedImport emits each SVG as a static asset for the default URL
|
|
51
|
+
// export. Without a hash, two files named the same (e.g. fullscreen.svg
|
|
52
|
+
// in different dirs) collide at static/svg/fullscreen.svg.
|
|
53
|
+
// loader-utils [ext] has no leading dot, so "[name].[hash][ext]" produces
|
|
54
|
+
// "foo.hashsvg". Use an explicit ".svg" suffix instead.
|
|
55
|
+
filename: { svg: "[name].[contenthash:8].svg" }
|
|
56
|
+
},
|
|
57
|
+
plugins: [pluginSvgr({ mixedImport: true, svgrOptions: { exportType: "named" } })]
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const { close } = await rslib.build();
|
|
62
|
+
await close();
|
|
63
|
+
};
|
|
@@ -1,25 +1,38 @@
|
|
|
1
|
-
import { dirname, relative } from "path";
|
|
1
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
2
2
|
import fg from "fast-glob";
|
|
3
|
-
import fs from "fs";
|
|
3
|
+
import fs from "node:fs";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param distDir {String}
|
|
8
|
+
* @param cwd {String}
|
|
9
|
+
* @param debug {Boolean}
|
|
10
|
+
* @returns {Promise<void>}
|
|
11
|
+
*/
|
|
5
12
|
export const replaceTscAliases = async ({ distDir, cwd, debug = false }) => {
|
|
6
13
|
const dtsFiles = await fg("**/*.d.ts", {
|
|
7
14
|
cwd: distDir,
|
|
8
15
|
absolute: true
|
|
9
16
|
});
|
|
10
17
|
|
|
18
|
+
// Build a map of absolute src paths → package names, by scanning sibling packages.
|
|
19
|
+
// This is used to rewrite cross-package src paths (e.g. "../../api/src") back to
|
|
20
|
+
// proper package names (e.g. "@webiny/api") in generated .d.ts files.
|
|
21
|
+
const srcToPackageName = buildSrcToPackageNameMap(cwd);
|
|
22
|
+
|
|
11
23
|
for (const dtsFile of dtsFiles) {
|
|
12
|
-
let content = fs.readFileSync(dtsFile, "utf8");
|
|
24
|
+
let content = fs.readFileSync(dtsFile, "utf8").toString();
|
|
13
25
|
let modified = false;
|
|
14
26
|
|
|
15
27
|
// Replace all imports/exports with ~/ alias
|
|
16
28
|
content = content.replace(
|
|
17
29
|
/(from\s+["'])~\/([^"']+)(["'])/g,
|
|
18
|
-
(
|
|
30
|
+
(_, prefix, importPath, suffix) => {
|
|
19
31
|
modified = true;
|
|
20
32
|
// Calculate relative path from current file to dist root
|
|
21
|
-
const fileDir = dirname(dtsFile);
|
|
22
|
-
const
|
|
33
|
+
const fileDir = dirname(dtsFile).replace(/\\/g, "/");
|
|
34
|
+
const normalizedDistDir = distDir.replace(/\\/g, "/");
|
|
35
|
+
const relativePath = relative(fileDir, normalizedDistDir).replace(/\\/g, "/");
|
|
23
36
|
const finalPath = relativePath
|
|
24
37
|
? `${relativePath}/${importPath}`
|
|
25
38
|
: `./${importPath}`;
|
|
@@ -29,11 +42,84 @@ export const replaceTscAliases = async ({ distDir, cwd, debug = false }) => {
|
|
|
29
42
|
}
|
|
30
43
|
);
|
|
31
44
|
|
|
45
|
+
// Replace cross-package src paths (e.g. "../../api/src/types") with proper package
|
|
46
|
+
// names (e.g. "@webiny/api/types"). TypeScript inlines these relative paths when
|
|
47
|
+
// project references are used and dependencies have paths pointing to src/.
|
|
48
|
+
if (srcToPackageName.size > 0) {
|
|
49
|
+
content = content.replace(
|
|
50
|
+
/["'](\.\.[^"']*\/src(?:\/[^"']*)?)['"]/g,
|
|
51
|
+
(match, relPath) => {
|
|
52
|
+
const quote = match[0];
|
|
53
|
+
const absPath = resolve(dirname(dtsFile), relPath);
|
|
54
|
+
const packageName = resolveToPackageName(absPath, srcToPackageName);
|
|
55
|
+
if (packageName) {
|
|
56
|
+
modified = true;
|
|
57
|
+
return `${quote}${packageName}${quote}`;
|
|
58
|
+
}
|
|
59
|
+
return match;
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
32
64
|
if (modified) {
|
|
33
65
|
fs.writeFileSync(dtsFile, content, "utf8");
|
|
34
66
|
if (debug) {
|
|
35
|
-
console.log(`Resolved
|
|
67
|
+
console.log(`Resolved aliases in: ${relative(cwd, dtsFile)}`);
|
|
36
68
|
}
|
|
37
69
|
}
|
|
38
70
|
}
|
|
39
71
|
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Scans sibling packages to build a map of absolute src directory paths → package names.
|
|
75
|
+
* @param {string} cwd - The current package directory.
|
|
76
|
+
* @returns {Map<string, string>}
|
|
77
|
+
*/
|
|
78
|
+
function buildSrcToPackageNameMap(cwd) {
|
|
79
|
+
const map = new Map();
|
|
80
|
+
// Go up to the packages/ directory (cwd is packages/xxx, parent is packages/)
|
|
81
|
+
const packagesDir = resolve(cwd, "..");
|
|
82
|
+
if (!fs.existsSync(packagesDir)) {
|
|
83
|
+
return map;
|
|
84
|
+
}
|
|
85
|
+
let entries;
|
|
86
|
+
try {
|
|
87
|
+
entries = fs.readdirSync(packagesDir);
|
|
88
|
+
} catch {
|
|
89
|
+
return map;
|
|
90
|
+
}
|
|
91
|
+
for (const entry of entries) {
|
|
92
|
+
const pkgDir = join(packagesDir, entry);
|
|
93
|
+
const pkgJsonPath = join(pkgDir, "package.json");
|
|
94
|
+
if (!fs.existsSync(pkgJsonPath)) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
99
|
+
if (pkgJson.name) {
|
|
100
|
+
const srcDir = join(pkgDir, "src");
|
|
101
|
+
map.set(srcDir, pkgJson.name);
|
|
102
|
+
}
|
|
103
|
+
} catch {
|
|
104
|
+
// ignore
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return map;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Given an absolute path that points into a package's src directory, returns the
|
|
112
|
+
* corresponding package name + sub-path (e.g. "@webiny/api/types").
|
|
113
|
+
* @param {string} absPath - Absolute path resolved from the relative import.
|
|
114
|
+
* @param {Map<string, string>} srcToPackageName
|
|
115
|
+
* @returns {string|null}
|
|
116
|
+
*/
|
|
117
|
+
function resolveToPackageName(absPath, srcToPackageName) {
|
|
118
|
+
for (const [srcDir, packageName] of srcToPackageName) {
|
|
119
|
+
if (absPath === srcDir || absPath.startsWith(srcDir + "/")) {
|
|
120
|
+
const subPath = absPath.slice(srcDir.length).replace(/^\//, "");
|
|
121
|
+
return subPath ? `${packageName}/${subPath}` : packageName;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
@@ -3,8 +3,11 @@ import ts from "typescript";
|
|
|
3
3
|
import merge from "lodash/merge.js";
|
|
4
4
|
import { replaceTscAliases } from "./tsAliasReplacer.js";
|
|
5
5
|
|
|
6
|
-
export const tsCompile = async ({ cwd = "", overrides, debug }) => {
|
|
7
|
-
|
|
6
|
+
export const tsCompile = async ({ cwd = "", overrides, debug, outputDir, checkOnly = false }) => {
|
|
7
|
+
// Normalize path separators to forward slashes for consistent behavior on Windows.
|
|
8
|
+
const normalizedCwd = cwd.replace(/\\/g, "/");
|
|
9
|
+
|
|
10
|
+
const tsConfigPath = join(normalizedCwd, "tsconfig.build.json");
|
|
8
11
|
|
|
9
12
|
let { config: readTsConfig } = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
|
|
10
13
|
|
|
@@ -20,13 +23,22 @@ export const tsCompile = async ({ cwd = "", overrides, debug }) => {
|
|
|
20
23
|
console.log(readTsConfig);
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
|
-
|
|
26
|
+
if (outputDir) {
|
|
27
|
+
readTsConfig.compilerOptions = readTsConfig.compilerOptions || {};
|
|
28
|
+
readTsConfig.compilerOptions.outDir = outputDir;
|
|
29
|
+
readTsConfig.compilerOptions.declarationDir = outputDir;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const parsedJsonConfigFile = ts.parseJsonConfigFileContent(readTsConfig, ts.sys, normalizedCwd);
|
|
24
33
|
|
|
25
34
|
const { projectReferences, options, fileNames, errors } = parsedJsonConfigFile;
|
|
26
35
|
|
|
27
|
-
// Exclude .d.ts files from TypeScript compilation
|
|
28
36
|
const filteredFileNames = fileNames.filter(fileName => !fileName.endsWith(".d.ts"));
|
|
29
37
|
|
|
38
|
+
if (checkOnly) {
|
|
39
|
+
options.noEmit = true;
|
|
40
|
+
}
|
|
41
|
+
|
|
30
42
|
const program = ts.createProgram({
|
|
31
43
|
projectReferences,
|
|
32
44
|
options,
|
|
@@ -34,12 +46,31 @@ export const tsCompile = async ({ cwd = "", overrides, debug }) => {
|
|
|
34
46
|
configFileParsingDiagnostics: errors
|
|
35
47
|
});
|
|
36
48
|
|
|
49
|
+
if (checkOnly) {
|
|
50
|
+
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(errors);
|
|
51
|
+
|
|
52
|
+
if (allDiagnostics.length) {
|
|
53
|
+
const formatHost = {
|
|
54
|
+
getCanonicalFileName: path => path,
|
|
55
|
+
getCurrentDirectory: () => normalizedCwd,
|
|
56
|
+
getNewLine: () => ts.sys.newLine
|
|
57
|
+
};
|
|
58
|
+
const message = ts.formatDiagnostics(allDiagnostics, formatHost);
|
|
59
|
+
if (message) {
|
|
60
|
+
throw { message };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
37
66
|
const { diagnostics, emitSkipped } = program.emit(
|
|
38
67
|
undefined, // targetSourceFile
|
|
39
|
-
(fileName, data, writeByteOrderMark
|
|
40
|
-
// Only emit files within the current package directory
|
|
41
|
-
|
|
42
|
-
|
|
68
|
+
(fileName, data, writeByteOrderMark) => {
|
|
69
|
+
// Only emit files within the current package directory.
|
|
70
|
+
// Normalize path separators to handle Windows backslashes vs forward slashes.
|
|
71
|
+
const normalizedFileName = fileName.replace(/\\/g, "/");
|
|
72
|
+
const relativePath = normalizedFileName.replace(normalizedCwd, "");
|
|
73
|
+
if (normalizedFileName.startsWith(normalizedCwd) && !relativePath.includes("../")) {
|
|
43
74
|
ts.sys.writeFile(fileName, data, writeByteOrderMark);
|
|
44
75
|
}
|
|
45
76
|
}
|
|
@@ -50,7 +81,7 @@ export const tsCompile = async ({ cwd = "", overrides, debug }) => {
|
|
|
50
81
|
if (allDiagnostics.length) {
|
|
51
82
|
const formatHost = {
|
|
52
83
|
getCanonicalFileName: path => path,
|
|
53
|
-
getCurrentDirectory: () =>
|
|
84
|
+
getCurrentDirectory: () => normalizedCwd,
|
|
54
85
|
getNewLine: () => ts.sys.newLine
|
|
55
86
|
};
|
|
56
87
|
const message = ts.formatDiagnostics(allDiagnostics, formatHost);
|
|
@@ -63,7 +94,6 @@ export const tsCompile = async ({ cwd = "", overrides, debug }) => {
|
|
|
63
94
|
throw { message: "TypeScript compilation failed." };
|
|
64
95
|
}
|
|
65
96
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
await replaceTscAliases({ distDir, cwd, debug });
|
|
97
|
+
const distDir = outputDir || options.outDir || join(normalizedCwd, "dist");
|
|
98
|
+
await replaceTscAliases({ distDir, cwd: normalizedCwd, debug });
|
|
69
99
|
};
|
package/packages/buildPackage.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as rimraf from "rimraf";
|
|
2
2
|
import { join } from "path";
|
|
3
|
-
import
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { rslibCompile } from "./buildPackage/rslibCompile.js";
|
|
4
5
|
import { tsCompile } from "./buildPackage/tsCompile.js";
|
|
5
6
|
import { copyToDist } from "./buildPackage/copyToDist.js";
|
|
6
7
|
import { validateEsmImports } from "./buildPackage/validateEsmImports.js";
|
|
@@ -12,8 +13,7 @@ export default async options => {
|
|
|
12
13
|
options.cwd = "";
|
|
13
14
|
}
|
|
14
15
|
const { cwd = "" } = options;
|
|
15
|
-
|
|
16
|
-
rimraf.sync(join(cwd, "./dist"));
|
|
16
|
+
|
|
17
17
|
rimraf.sync(join(cwd, "*.tsbuildinfo"), { glob: true });
|
|
18
18
|
|
|
19
19
|
options.logs !== false && console.log("Building...");
|
|
@@ -26,15 +26,68 @@ export default async options => {
|
|
|
26
26
|
// Validate ESM imports before compiling
|
|
27
27
|
await validateEsmImports({ cwd, logs: options.logs });
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
await tsCompile(options);
|
|
29
|
+
const distDir = join(cwd, "dist");
|
|
31
30
|
|
|
32
|
-
options.
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
if (options.safeReplace) {
|
|
32
|
+
await buildWithSafeReplace(options, distDir);
|
|
33
|
+
} else {
|
|
34
|
+
await buildDirect(options, distDir);
|
|
35
|
+
}
|
|
35
36
|
|
|
36
37
|
const duration = (new Date() - start) / 1000;
|
|
37
38
|
options.logs !== false && console.log(`Done! Build finished in ${duration + "s"}.`);
|
|
38
39
|
|
|
39
40
|
return { duration };
|
|
40
41
|
};
|
|
42
|
+
|
|
43
|
+
async function buildDirect(options, distDir) {
|
|
44
|
+
// Clear dist/ contents without removing the directory itself.
|
|
45
|
+
// node_modules/@webiny/<pkg> symlinks directly to dist/, so deleting
|
|
46
|
+
// the directory would leave a dangling symlink and break Rspack resolution.
|
|
47
|
+
if (fs.existsSync(distDir)) {
|
|
48
|
+
for (const entry of fs.readdirSync(distDir)) {
|
|
49
|
+
fs.rmSync(join(distDir, entry), { recursive: true, force: true });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
await rslibCompile(options);
|
|
54
|
+
await tsCompile(options);
|
|
55
|
+
|
|
56
|
+
options.logs !== false && console.log("Copying meta files...");
|
|
57
|
+
copyToDist("package.json", options);
|
|
58
|
+
copyToDist("LICENSE", options);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function buildWithSafeReplace(options, distDir) {
|
|
62
|
+
const stagingDir = distDir + "._build";
|
|
63
|
+
|
|
64
|
+
// Clean up any leftover staging dir from a previous failed build.
|
|
65
|
+
if (fs.existsSync(stagingDir)) {
|
|
66
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
67
|
+
}
|
|
68
|
+
fs.mkdirSync(stagingDir, { recursive: true });
|
|
69
|
+
|
|
70
|
+
const stagingOptions = { ...options, outputDir: stagingDir };
|
|
71
|
+
|
|
72
|
+
await babelCompile(stagingOptions);
|
|
73
|
+
await tsCompile(stagingOptions);
|
|
74
|
+
|
|
75
|
+
stagingOptions.logs !== false && console.log("Copying meta files...");
|
|
76
|
+
copyToDist("package.json", stagingOptions);
|
|
77
|
+
copyToDist("LICENSE", stagingOptions);
|
|
78
|
+
|
|
79
|
+
// Fast-swap: clear dist contents and move staged files in.
|
|
80
|
+
if (fs.existsSync(distDir)) {
|
|
81
|
+
for (const entry of fs.readdirSync(distDir)) {
|
|
82
|
+
fs.rmSync(join(distDir, entry), { recursive: true, force: true });
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
fs.mkdirSync(distDir, { recursive: true });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
for (const entry of fs.readdirSync(stagingDir)) {
|
|
89
|
+
fs.renameSync(join(stagingDir, entry), join(distDir, entry));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
93
|
+
}
|
package/packages/index.js
CHANGED
|
@@ -2,5 +2,3 @@ export { default as watchPackage } from "./watchPackage.js";
|
|
|
2
2
|
export { default as createWatchPackage } from "./createWatchPackage.js";
|
|
3
3
|
export { default as buildPackage } from "./buildPackage.js";
|
|
4
4
|
export { default as createBuildPackage } from "./createBuildPackage.js";
|
|
5
|
-
export { default as createBabelConfigForNode } from "./createBabelConfigForNode.js";
|
|
6
|
-
export { default as createBabelConfigForReact } from "./createBabelConfigForReact.js";
|
package/packages/watchPackage.js
CHANGED
|
@@ -1,117 +1,29 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import fs from "fs/promises";
|
|
3
|
-
import { transformFileAsync } from "@babel/core";
|
|
4
|
-
import chokidar from "chokidar";
|
|
5
|
-
import baseFs from "node:fs";
|
|
6
|
-
|
|
7
|
-
let compilationQueue = [];
|
|
8
|
-
let debounceTimer = null;
|
|
9
|
-
const DEBOUNCE_DELAY = 1000; // 1 second
|
|
10
|
-
|
|
11
|
-
const flushCompilationQueue = () => {
|
|
12
|
-
if (compilationQueue.length > 0) {
|
|
13
|
-
if (compilationQueue.length === 1) {
|
|
14
|
-
console.log(`Successfully compiled ${compilationQueue[0]}.`);
|
|
15
|
-
} else {
|
|
16
|
-
console.log(`Successfully compiled ${compilationQueue.length} files.`);
|
|
17
|
-
}
|
|
18
|
-
compilationQueue = [];
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const logCompilation = inputPathRelative => {
|
|
23
|
-
compilationQueue.push(inputPathRelative);
|
|
24
|
-
|
|
25
|
-
clearTimeout(debounceTimer);
|
|
26
|
-
debounceTimer = setTimeout(() => {
|
|
27
|
-
flushCompilationQueue();
|
|
28
|
-
}, DEBOUNCE_DELAY);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const getBabelFile = cwd => {
|
|
32
|
-
const babelJs = path.join(cwd, ".babelrc.js");
|
|
33
|
-
const babelCjs = path.join(cwd, ".babelrc.cjs");
|
|
34
|
-
|
|
35
|
-
if (baseFs.existsSync(babelJs)) {
|
|
36
|
-
return babelJs;
|
|
37
|
-
} else if (baseFs.existsSync(babelCjs)) {
|
|
38
|
-
return babelCjs;
|
|
39
|
-
}
|
|
40
|
-
throw new Error("No Babel configuration file found (.babelrc.js or .babelrc.cjs).");
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const compileFile = async (cwd, inputPath, outputPath) => {
|
|
44
|
-
const inputPathRelative = path.relative(cwd, inputPath);
|
|
45
|
-
|
|
46
|
-
const configFile = getBabelFile(cwd);
|
|
47
|
-
|
|
48
|
-
const result = await transformFileAsync(inputPath, {
|
|
49
|
-
configFile: configFile,
|
|
50
|
-
sourceMaps: true
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
if (!result || !result.code) {
|
|
54
|
-
throw new Error(`Failed to compile: ${inputPath}`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Write compiled file
|
|
58
|
-
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
|
59
|
-
await fs.writeFile(outputPath, result.code);
|
|
60
|
-
|
|
61
|
-
// Write source map
|
|
62
|
-
if (result.map) {
|
|
63
|
-
await fs.writeFile(`${outputPath}.map`, JSON.stringify(result.map));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
logCompilation(inputPathRelative);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const srcToDist = filePath =>
|
|
70
|
-
path.join(
|
|
71
|
-
filePath
|
|
72
|
-
.replace(`${path.sep}src${path.sep}`, `${path.sep}dist${path.sep}`)
|
|
73
|
-
.replace(/\.(ts|tsx)$/, ".js")
|
|
74
|
-
);
|
|
75
|
-
|
|
76
1
|
export default async options => {
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
watcher.on("change", async srcPath => {
|
|
103
|
-
if (!isTsFile(srcPath)) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const distPath = srcToDist(srcPath);
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
await compileFile(options.cwd, srcPath, distPath);
|
|
111
|
-
} catch (err) {
|
|
112
|
-
console.error("Error compiling:", err);
|
|
2
|
+
const { cwd } = options;
|
|
3
|
+
|
|
4
|
+
// Must be a dynamic import — see rslibCompile.js for the reason.
|
|
5
|
+
const [{ createRslib }, { pluginSvgr }] = await Promise.all([
|
|
6
|
+
import("@rslib/core"),
|
|
7
|
+
import("@rsbuild/plugin-svgr")
|
|
8
|
+
]);
|
|
9
|
+
|
|
10
|
+
const rslib = await createRslib({
|
|
11
|
+
cwd,
|
|
12
|
+
config: {
|
|
13
|
+
lib: [{ format: "esm", bundle: false }],
|
|
14
|
+
source: {
|
|
15
|
+
entry: ["./src/**/*.{ts,tsx,js,jsx}"],
|
|
16
|
+
alias: { "~": "./src" }
|
|
17
|
+
},
|
|
18
|
+
output: {
|
|
19
|
+
target: "web",
|
|
20
|
+
distPath: { root: "./dist" },
|
|
21
|
+
cleanDistPath: false,
|
|
22
|
+
sourceMap: { js: "source-map" }
|
|
23
|
+
},
|
|
24
|
+
plugins: [pluginSvgr({ mixedImport: true, svgrOptions: { exportType: "named" } })]
|
|
113
25
|
}
|
|
114
26
|
});
|
|
115
27
|
|
|
116
|
-
|
|
28
|
+
await rslib.build({ watch: true });
|
|
117
29
|
};
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
// We'll use this class once the package is converted to TS!
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import findUp from "find-up";
|
|
2
|
+
import { loadJsonFileSync } from "load-json-file";
|
|
3
|
+
import { findUp } from "find-up";
|
|
5
4
|
|
|
6
5
|
export class PackageJson {
|
|
7
6
|
private readonly filePath: string;
|
|
8
7
|
private readonly json: Record<string, any>;
|
|
9
8
|
|
|
10
9
|
static fromFile(filePath: string) {
|
|
11
|
-
return new PackageJson(filePath,
|
|
10
|
+
return new PackageJson(filePath, loadJsonFileSync(filePath));
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
static async findClosest(fromPath: string) {
|
package/utils/PackageJson.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import findUp from "find-up";
|
|
1
|
+
import { loadJsonFileSync } from "load-json-file";
|
|
2
|
+
import { findUp } from "find-up";
|
|
3
3
|
|
|
4
4
|
export class PackageJson {
|
|
5
5
|
filePath;
|
|
6
6
|
json;
|
|
7
7
|
|
|
8
8
|
static fromFile(filePath) {
|
|
9
|
-
return new PackageJson(filePath,
|
|
9
|
+
return new PackageJson(filePath, loadJsonFileSync(filePath));
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
static async findClosest(fromPath) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This tool will re-link monorepo packages to one of the following directories (by priority):
|
|
3
|
-
* - {package}/package.json ->
|
|
4
|
-
* - lerna.json -> command.publish.contents
|
|
3
|
+
* - {package}/package.json -> webiny.publishFrom
|
|
5
4
|
* - package root directory
|
|
6
5
|
*/
|
|
7
6
|
|
|
@@ -11,7 +10,6 @@ import path from "path";
|
|
|
11
10
|
import get from "lodash/get.js";
|
|
12
11
|
import fs from "fs-extra";
|
|
13
12
|
import * as rimraf from "rimraf";
|
|
14
|
-
import readJsonSync from "read-json-sync";
|
|
15
13
|
|
|
16
14
|
async function symlink(src, dest) {
|
|
17
15
|
if (process.platform !== "win32") {
|
|
@@ -53,13 +51,11 @@ const defaults = {
|
|
|
53
51
|
|
|
54
52
|
export const linkWorkspaces = async ({ whitelist, blacklist } = defaults) => {
|
|
55
53
|
console.log(`Linking project workspaces...`);
|
|
56
|
-
//eslint-disable-next-line import/dynamic-import-chunkname
|
|
57
54
|
const { PackageJson } = await import("../utils/PackageJson.js");
|
|
58
55
|
|
|
59
56
|
whitelist = (whitelist || []).map(p => path.resolve(p));
|
|
60
57
|
blacklist = (blacklist || []).map(p => path.resolve(p));
|
|
61
58
|
// Filter packages to only those in the whitelisted folders
|
|
62
|
-
//eslint-disable-next-line import/dynamic-import-chunkname
|
|
63
59
|
const getYarnWorkspaces = await import("get-yarn-workspaces").then(m => m.default ?? m);
|
|
64
60
|
const packages = getYarnWorkspaces(process.cwd())
|
|
65
61
|
.map(pkg => pkg.replace(/\//g, path.sep))
|
|
@@ -73,9 +69,6 @@ export const linkWorkspaces = async ({ whitelist, blacklist } = defaults) => {
|
|
|
73
69
|
return whitelist.some(w => pkg.startsWith(w));
|
|
74
70
|
});
|
|
75
71
|
|
|
76
|
-
const lernaJson = path.resolve("lerna.json");
|
|
77
|
-
const lerna = fs.existsSync(lernaJson) ? readJsonSync(lernaJson) : null;
|
|
78
|
-
|
|
79
72
|
for (let i = 0; i < packages.length; i++) {
|
|
80
73
|
const packageJson = path.resolve(packages[i], "package.json");
|
|
81
74
|
if (!fs.existsSync(packageJson)) {
|
|
@@ -85,11 +78,7 @@ export const linkWorkspaces = async ({ whitelist, blacklist } = defaults) => {
|
|
|
85
78
|
const pkgJson = await PackageJson.fromFile(packageJson);
|
|
86
79
|
const pkg = pkgJson.getJson();
|
|
87
80
|
|
|
88
|
-
|
|
89
|
-
if (!targetDirectory && lerna) {
|
|
90
|
-
targetDirectory = get(lerna, "command.publish.contents");
|
|
91
|
-
}
|
|
92
|
-
|
|
81
|
+
const targetDirectory = get(pkg, "webiny.publishFrom");
|
|
93
82
|
const link = path.resolve("node_modules", pkg.name);
|
|
94
83
|
const target = path.resolve(packages[i], targetDirectory || ".");
|
|
95
84
|
|
package/.babelrc.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require("./packages").createBabelConfigForNode({ path: __dirname });
|
package/.eslintrc.cjs
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import { dirname, extname, join, parse, relative } from "path";
|
|
3
|
-
import * as babel from "@babel/core";
|
|
4
|
-
import glob from "fast-glob";
|
|
5
|
-
|
|
6
|
-
const BABEL_COMPILE_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx"];
|
|
7
|
-
const BABEL_SKIP_EXTENSIONS = [".d.ts"];
|
|
8
|
-
|
|
9
|
-
const withSourceMapUrl = (file, code) => {
|
|
10
|
-
const { name } = parse(file);
|
|
11
|
-
return [code, "", `//# sourceMappingURL=${name}.js.map`].join("\n");
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const getDistCopyFilePath = ({ file, cwd }) => {
|
|
15
|
-
const relativeDir = relative(cwd, file);
|
|
16
|
-
return join(cwd, relativeDir.replace("src", "dist"));
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const getDistFilePaths = ({ file, cwd }) => {
|
|
20
|
-
const { dir, name } = parse(file);
|
|
21
|
-
|
|
22
|
-
const relativeDir = relative(cwd, dir);
|
|
23
|
-
|
|
24
|
-
const code = join(cwd, relativeDir.replace("src", "dist"), name + ".js");
|
|
25
|
-
const map = join(cwd, relativeDir.replace("src", "dist"), name + ".js.map");
|
|
26
|
-
return { code, map };
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const babelCompile = async ({ cwd }) => {
|
|
30
|
-
// We're passing "*.*" just because we want to copy all files that cannot be compiled.
|
|
31
|
-
// We want to have the same behaviour that the Babel CLI's "--copy-files" flag provides.
|
|
32
|
-
const pattern = join(cwd, "src/**/*.*").replace(/\\/g, "/");
|
|
33
|
-
const files = glob.sync(pattern, {
|
|
34
|
-
onlyFiles: true,
|
|
35
|
-
dot: true
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const compilations = [];
|
|
39
|
-
const copies = [];
|
|
40
|
-
|
|
41
|
-
for (let i = 0; i < files.length; i++) {
|
|
42
|
-
const file = files[i];
|
|
43
|
-
if (
|
|
44
|
-
fileEndsWith(file, BABEL_COMPILE_EXTENSIONS) &&
|
|
45
|
-
!fileEndsWith(file, BABEL_SKIP_EXTENSIONS)
|
|
46
|
-
) {
|
|
47
|
-
compilations.push(
|
|
48
|
-
babel
|
|
49
|
-
.transformFileAsync(file, {
|
|
50
|
-
cwd,
|
|
51
|
-
sourceMaps: true
|
|
52
|
-
})
|
|
53
|
-
.then(results => [file, results])
|
|
54
|
-
);
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// All other files should be copied as-is.
|
|
59
|
-
copies.push(copyFile(file, cwd));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// At this point, just wait for compilations to be completed, so we can proceed with writing the files ASAP.
|
|
63
|
-
await Promise.all(compilations);
|
|
64
|
-
|
|
65
|
-
const writes = [];
|
|
66
|
-
for (let i = 0; i < compilations.length; i++) {
|
|
67
|
-
const [file, result] = await compilations[i];
|
|
68
|
-
const { code, map } = result;
|
|
69
|
-
|
|
70
|
-
const paths = getDistFilePaths({ file, cwd });
|
|
71
|
-
fs.mkdirSync(dirname(paths.code), { recursive: true });
|
|
72
|
-
|
|
73
|
-
// Save the compiled JS file.
|
|
74
|
-
writes.push(fs.promises.writeFile(paths.code, withSourceMapUrl(file, code), "utf8"));
|
|
75
|
-
|
|
76
|
-
// Save source maps file.
|
|
77
|
-
const mapJson = JSON.stringify(map);
|
|
78
|
-
writes.push(fs.promises.writeFile(paths.map, mapJson, "utf8"));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Wait until all files have been written to disk.
|
|
82
|
-
return Promise.all([...writes, ...copies]);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
function copyFile(file, cwd) {
|
|
86
|
-
return new Promise((resolve, reject) => {
|
|
87
|
-
try {
|
|
88
|
-
const destPath = getDistCopyFilePath({ file, cwd });
|
|
89
|
-
if (!fs.existsSync(dirname(destPath))) {
|
|
90
|
-
fs.mkdirSync(dirname(destPath), { recursive: true });
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
fs.copyFileSync(file, destPath);
|
|
94
|
-
resolve();
|
|
95
|
-
} catch (e) {
|
|
96
|
-
reject(e);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function fileEndsWith(file, extensions) {
|
|
102
|
-
return extensions.some(ext => file.endsWith(ext));
|
|
103
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export default ({ path }) => {
|
|
2
|
-
return {
|
|
3
|
-
presets: [
|
|
4
|
-
["@babel/preset-react", { useBuiltIns: true }],
|
|
5
|
-
["@babel/preset-typescript", { isTSX: true, allExtensions: true }]
|
|
6
|
-
],
|
|
7
|
-
plugins: [
|
|
8
|
-
[
|
|
9
|
-
"babel-plugin-module-resolver",
|
|
10
|
-
{
|
|
11
|
-
cwd: path,
|
|
12
|
-
alias: {
|
|
13
|
-
"~": "./src"
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
]
|
|
17
|
-
]
|
|
18
|
-
};
|
|
19
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export default ({ path }) => ({
|
|
2
|
-
presets: [
|
|
3
|
-
["@babel/preset-react", { useBuiltIns: true }],
|
|
4
|
-
["@babel/preset-typescript", { isTSX: true, allExtensions: true }]
|
|
5
|
-
],
|
|
6
|
-
plugins: [
|
|
7
|
-
"babel-plugin-macros",
|
|
8
|
-
"@babel/plugin-proposal-throw-expressions",
|
|
9
|
-
["@emotion/babel-plugin", { autoLabel: "dev-only" }],
|
|
10
|
-
[
|
|
11
|
-
"babel-plugin-module-resolver",
|
|
12
|
-
{
|
|
13
|
-
cwd: path,
|
|
14
|
-
alias: {
|
|
15
|
-
"~": "./src"
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
]
|
|
19
|
-
]
|
|
20
|
-
});
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import readJsonSync from "read-json-sync";
|
|
2
|
-
|
|
3
|
-
export default ({ path, esm }) => {
|
|
4
|
-
return {
|
|
5
|
-
presets: [
|
|
6
|
-
[
|
|
7
|
-
"@babel/preset-env",
|
|
8
|
-
{
|
|
9
|
-
targets: {
|
|
10
|
-
node: "18"
|
|
11
|
-
},
|
|
12
|
-
modules: esm ? false : "auto",
|
|
13
|
-
exclude: [
|
|
14
|
-
"transform-typeof-symbol",
|
|
15
|
-
"@babel/plugin-proposal-optional-chaining",
|
|
16
|
-
"@babel/plugin-proposal-nullish-coalescing-operator",
|
|
17
|
-
"@babel/plugin-proposal-class-properties",
|
|
18
|
-
"@babel/plugin-transform-async-to-generator",
|
|
19
|
-
"@babel/plugin-transform-regenerator",
|
|
20
|
-
"@babel/plugin-proposal-dynamic-import"
|
|
21
|
-
]
|
|
22
|
-
}
|
|
23
|
-
],
|
|
24
|
-
"@babel/preset-typescript"
|
|
25
|
-
],
|
|
26
|
-
plugins: [
|
|
27
|
-
[
|
|
28
|
-
"@babel/plugin-transform-runtime",
|
|
29
|
-
{
|
|
30
|
-
useESModules: !!esm,
|
|
31
|
-
version: readJsonSync(require.resolve("@babel/runtime/package.json")).version
|
|
32
|
-
}
|
|
33
|
-
],
|
|
34
|
-
[
|
|
35
|
-
"babel-plugin-module-resolver",
|
|
36
|
-
{
|
|
37
|
-
cwd: path,
|
|
38
|
-
alias: {
|
|
39
|
-
"~": "./src"
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
]
|
|
43
|
-
]
|
|
44
|
-
};
|
|
45
|
-
};
|