Skip to content

Commit 06776ee

Browse files
authored
fix(app-shell-vite-plugin): cross-platform module resolution (#4760)
Co-authored-by: Bruno Henriques <[email protected]>
1 parent ed8230b commit 06776ee

File tree

5 files changed

+49
-88
lines changed

5 files changed

+49
-88
lines changed

package-lock.json

Lines changed: 32 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app-shell-vite-plugin/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@
3737
"@rollup/plugin-terser": "^0.4.0",
3838
"@rollup/plugin-virtual": "^3.0.1",
3939
"es-module-shims": "^1.6.3",
40-
"find-up-simple": "^1.0.1",
4140
"react": "^18.2.0",
4241
"react-dom": "^18.2.0",
4342
"react-router-dom": "^6.9.0",
4443
"rollup": "^4.6.1",
4544
"ts-node": "^10.9.1",
46-
"vite-plugin-static-copy": "^2.0.0"
45+
"vite-plugin-static-copy": "^3.0.0"
4746
},
4847
"peerDependencies": {
4948
"vite": "^4.1.4 || ^5.0.4 || ^6.0.0"
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
11
import { createRequire } from "node:module";
2-
import { join } from "node:path";
3-
import { findUpSync } from "find-up-simple";
2+
import { dirname, join } from "node:path";
43

54
export const require = createRequire(import.meta.url);
65

76
/**
87
* Resolves the module entrypoint by name and normalizes slashes to be posix/unix-like forward slashes.
98
*
109
* @param moduleName The name of the module to be searched for
11-
* @returns The module path normalized
12-
*/
13-
export function resolveModule(moduleName: string) {
14-
return require.resolve(moduleName).replace(/\\+/g, "/");
15-
}
16-
17-
/** Resolves the module directory (where `package.json` is) by `moduleName` */
18-
function resolveModuleDirectory(moduleName: string) {
19-
const moduleEntrypoint = resolveModule(moduleName);
20-
const modulePackage = findUpSync("package.json", { cwd: moduleEntrypoint });
21-
return modulePackage?.slice(0, modulePackage.lastIndexOf("/")) || "";
22-
}
23-
24-
/**
25-
* This function will find out the module path using node `require.resolve` function
26-
* and add the suffix param after the folder with module name.
27-
*
28-
* @param moduleName "@module/name"
2910
* @param suffix to be added after the module path
30-
* @returns the /path/to/@module/name/<suffix>
11+
* @returns The module path normalized
3112
*/
32-
export function getModulePath(moduleName: string, suffix: string) {
33-
return join(resolveModuleDirectory(moduleName), suffix);
13+
export function resolveModule(moduleName: string, suffix?: string) {
14+
const entrypoint = require.resolve(moduleName);
15+
return suffix ? join(dirname(entrypoint), suffix) : entrypoint;
3416
}

packages/app-shell-vite-plugin/src/vite-configuration-processor-plugin.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ import {
1616
startsWithSelf,
1717
} from "./config-utils.js";
1818

19-
enum ViteCommand {
20-
BUILD = "build",
21-
SERVE = "serve",
22-
}
23-
2419
type BundleDefinition =
2520
| HvAppShellProvidersConfig
2621
| HvAppShellViewsConfig
@@ -114,7 +109,7 @@ export default function processConfiguration(
114109
},
115110
},
116111
// if serve (preview/dev) it uses the basePath. Otherwise(build), use ./
117-
base: command === ViteCommand.SERVE ? basePath : "./",
112+
base: command === "serve" ? basePath : "./",
118113
};
119114
},
120115

packages/app-shell-vite-plugin/src/vite-plugin.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getFinalModuleName,
1616
loadConfigFile,
1717
} from "./config-utils.js";
18-
import { getModulePath, resolveModule } from "./nodeModule.js";
18+
import { resolveModule } from "./nodeModule.js";
1919
import SHARED_DEPENDENCIES from "./shared-dependencies.js";
2020
import getVirtualEntrypoints from "./virtual-entrypoints.js";
2121
import processConfiguration from "./vite-configuration-processor-plugin.js";
@@ -28,10 +28,10 @@ import generateImportmap, {
2828
import injectMetadata from "./vite-metadata-plugin.js";
2929
import serveAppShellConfig from "./vite-watch-config-plugin.js";
3030

31-
enum ViteBuildMode {
32-
PRODUCTION = "production",
33-
DEVELOPMENT = "development",
34-
}
31+
const ViteBuildMode = {
32+
PRODUCTION: "production",
33+
DEVELOPMENT: "development",
34+
} as const;
3535

3636
export type ApplicationBundleType = "app" | "bundle";
3737

@@ -199,17 +199,15 @@ export function HvAppShellVitePlugin(
199199
viteStaticCopy({
200200
targets: [
201201
{
202-
src: getModulePath("es-module-shims", "dist/*"),
202+
src: resolveModule("es-module-shims", "*"),
203203
dest: "bundles",
204204
},
205205
// copy the ui kit icons' sprites to the "icons" folder
206206
{
207-
src: [
208-
getModulePath(
209-
"@hitachivantara/uikit-react-icons",
210-
"dist/sprites/*.svg",
211-
),
212-
],
207+
src: resolveModule(
208+
"@hitachivantara/uikit-react-icons",
209+
"../sprites/*.svg",
210+
),
213211
dest: "icons",
214212
},
215213
...(!devMode && buildEntryPoint

0 commit comments

Comments
 (0)