Skip to content

Commit 810831c

Browse files
authored
fix: Vite production build errors with transitive dependencies (#15413)
Fixes #15395 Modifies the Vite stats.json plugin so that only direct dependencies are considered in `npmModules`, assuming that transitive dependency versions are not relevant. In addition, includes integration testing setup up with a transitive dependency for Vite, in both development and production modes.
1 parent 84a7d37 commit 810831c

File tree

17 files changed

+108
-31
lines changed

17 files changed

+108
-31
lines changed

flow-server/src/main/resources/vite.generated.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const generatedFlowImportsFolder = path.resolve(__dirname, settings.generatedFlo
3131
const themeResourceFolder = path.resolve(__dirname, settings.themeResourceFolder);
3232
const projectPackageJsonFile = path.resolve(__dirname, 'package.json');
3333
const statsFile = path.resolve(statsFolder, 'stats.json');
34+
const nodeModulesFolder = path.resolve(__dirname, 'node_modules');
3435

3536
const projectStaticAssetsFolders = [
3637
path.resolve(__dirname, 'src', 'main', 'resources', 'META-INF', 'resources'),
@@ -171,10 +172,11 @@ function statsExtracterPlugin(): PluginOption {
171172
enforce: 'post',
172173
async writeBundle(options: OutputOptions, bundle: { [fileName: string]: AssetInfo | ChunkInfo }) {
173174
const modules = Object.values(bundle).flatMap((b) => (b.modules ? Object.keys(b.modules) : []));
174-
const nodeModulesFolders = modules.filter((id) => id.includes('node_modules'));
175+
const nodeModulesFolders = modules
176+
.filter((id) => id.startsWith(nodeModulesFolder))
177+
.map(id => id.substring(nodeModulesFolder.length + 1));
175178
const npmModules = nodeModulesFolders
176179
.map((id) => id.replace(/\\/g, '/'))
177-
.map((id) => id.replace(/.*node_modules./, ''))
178180
.map((id) => {
179181
const parts = id.split('/');
180182
if (id.startsWith('@')) {
@@ -224,7 +226,7 @@ function vaadinBundlesPlugin(): PluginOption {
224226

225227
const disabledMessage = 'Vaadin component dependency bundles are disabled.';
226228

227-
const modulesDirectory = path.resolve(__dirname, 'node_modules').replace(/\\/g, '/');
229+
const modulesDirectory = nodeModulesFolder.replace(/\\/g, '/');
228230

229231
let vaadinBundleJson: BundleJson;
230232

@@ -476,7 +478,7 @@ let spaMiddlewareForceRemoved = false;
476478
const allowedFrontendFolders = [
477479
frontendFolder,
478480
path.resolve(generatedFlowImportsFolder), // Contains only generated-flow-imports
479-
path.resolve(__dirname, 'node_modules')
481+
nodeModulesFolder
480482
];
481483

482484
function setHmrPortToServerPort(): PluginOption {
@@ -649,7 +651,7 @@ export const overrideVaadinConfig = (customConfig: UserConfigFn) => {
649651
return defineConfig((env) => mergeConfig(vaadinConfig(env), customConfig(env)));
650652
};
651653
function getVersion(module: string):string {
652-
const packageJson = `node_modules/${module}/package.json`;
654+
const packageJson = path.resolve(nodeModulesFolder, module, 'package.json');
653655
return JSON.parse(readFileSync(packageJson, {encoding: 'utf-8'})).version;
654656
}
655657

flow-tests/test-frontend/vite-basics/package-outside-npm/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

flow-tests/test-frontend/vite-basics/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"construct-style-sheets-polyfill": "3.1.0",
2020
"copy-to-clipboard": "^3.3.1",
2121
"lit": "2.4.1",
22-
"package-outside-npm": "file:package-outside-npm",
23-
"package2-outside-npm": "./package2-outside-npm"
22+
"package-outside-npm": "file:../vite-test-assets/packages/package-outside-npm",
23+
"package2-outside-npm": "../vite-test-assets/packages/package2-outside-npm"
2424
},
2525
"devDependencies": {
2626
"@rollup/plugin-replace": "3.1.0",
@@ -31,9 +31,9 @@
3131
"rollup-plugin-brotli": "3.1.0",
3232
"strip-css-comments": "5.0.0",
3333
"transform-ast": "2.4.4",
34-
"typescript": "4.7.4",
35-
"vite": "v3.2.3",
36-
"vite-plugin-checker": "0.4.9",
34+
"typescript": "4.9.3",
35+
"vite": "v4.0.0-alpha.6",
36+
"vite-plugin-checker": "0.5.1",
3737
"workbox-build": "6.5.0",
3838
"workbox-core": "6.5.0",
3939
"workbox-precaching": "6.5.0"
@@ -77,13 +77,13 @@
7777
"rollup-plugin-brotli": "3.1.0",
7878
"strip-css-comments": "5.0.0",
7979
"transform-ast": "2.4.4",
80-
"typescript": "4.7.4",
81-
"vite": "v3.2.3",
82-
"vite-plugin-checker": "0.4.9",
80+
"typescript": "4.9.3",
81+
"vite": "v4.0.0-alpha.6",
82+
"vite-plugin-checker": "0.5.1",
8383
"workbox-build": "6.5.0",
8484
"workbox-core": "6.5.0",
8585
"workbox-precaching": "6.5.0"
8686
},
87-
"hash": "e754f39771a40db99825f9a1ac5f1b0410805614ed8d41076b74eb77a6693bd2"
87+
"hash": "1dcb4cd20949590f5059908b48b415dc7a387bf0044f84e8eaf230d0fc54535a"
8888
}
8989
}

flow-tests/test-frontend/vite-basics/package2-outside-npm/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

flow-tests/test-frontend/vite-production/package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"@vaadin/vaadin-text-field": "23.3.0-alpha2",
1515
"@vaadin/vaadin-themable-mixin": "23.3.0-alpha2",
1616
"construct-style-sheets-polyfill": "3.1.0",
17-
"lit": "2.4.1"
17+
"lit": "2.4.1",
18+
"package-outside-npm": "file:../vite-test-assets/packages/package-outside-npm",
19+
"package2-outside-npm": "../vite-test-assets/packages/package2-outside-npm"
1820
},
1921
"devDependencies": {
2022
"@rollup/plugin-replace": "3.1.0",
@@ -25,9 +27,9 @@
2527
"rollup-plugin-brotli": "3.1.0",
2628
"strip-css-comments": "5.0.0",
2729
"transform-ast": "2.4.4",
28-
"typescript": "4.7.4",
29-
"vite": "v3.2.3",
30-
"vite-plugin-checker": "0.4.9",
30+
"typescript": "4.9.3",
31+
"vite": "v4.0.0-alpha.6",
32+
"vite-plugin-checker": "0.5.1",
3133
"workbox-build": "6.5.0",
3234
"workbox-core": "6.5.0",
3335
"workbox-precaching": "6.5.0"
@@ -53,13 +55,13 @@
5355
"rollup-plugin-brotli": "3.1.0",
5456
"strip-css-comments": "5.0.0",
5557
"transform-ast": "2.4.4",
56-
"typescript": "4.7.4",
57-
"vite": "v3.2.3",
58-
"vite-plugin-checker": "0.4.9",
58+
"typescript": "4.9.3",
59+
"vite": "v4.0.0-alpha.6",
60+
"vite-plugin-checker": "0.5.1",
5961
"workbox-build": "6.5.0",
6062
"workbox-core": "6.5.0",
6163
"workbox-precaching": "6.5.0"
6264
},
63-
"hash": "b61eb98027a28e8ca115264e78543abc1bf08c08c109f8c8cedbd17990281ab7"
65+
"hash": "5f6b4e932fbbcc4dda9663ca11044b8bd2cfcdd282faee58f07a2d27a95f51d1"
6466
}
6567
}

flow-tests/test-frontend/vite-production/src/main/java/com/vaadin/viteapp/views/empty/MainView.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
@Route("")
1414
@JsModule("@testscope/button")
1515
@JsModule("@testscope/map")
16+
@JsModule("package-outside-npm/index.js")
17+
@JsModule("package2-outside-npm/index.js")
1618
@JsModule("./lit-invalid-imports.ts")
1719
@CssImport("./image.css")
1820
public class MainView extends Div {
1921

2022
public static final String PLANT = "plant";
2123
public static final String HIDEPLANT = "hideplant";
24+
public static final String OUTSIDE = "outsideButton";
25+
public static final String OUTSIDE_RESULT = "outsideResult";
2226

2327
public MainView() {
2428
Image img = new Image("themes/vite-production/images/plant.png",
@@ -38,6 +42,17 @@ public MainView() {
3842
setSizeFull();
3943
getStyle().set("text-align", "center");
4044

45+
NativeButton checkOutsideJs = new NativeButton("Check outside JS",
46+
e -> {
47+
getElement().executeJs(OUTSIDE_RESULT
48+
+ ".innerText = window.packageOutsideNpm() + ' - ' + window.package2OutsideNpm();");
49+
});
50+
checkOutsideJs.setId(OUTSIDE);
51+
add(checkOutsideJs);
52+
Paragraph outsideStatus = new Paragraph();
53+
outsideStatus.setId(OUTSIDE_RESULT);
54+
add(outsideStatus);
55+
4156
add(new HtmlComponent("testscope-button"));
4257
add(new HtmlComponent("testscope-map"));
4358
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vaadin.viteapp;
2+
3+
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
4+
import com.vaadin.flow.component.html.testbench.ParagraphElement;
5+
import com.vaadin.flow.testutil.ChromeBrowserTest;
6+
import com.vaadin.viteapp.views.empty.MainView;
7+
import io.github.bonigarcia.wdm.WebDriverManager;
8+
import org.junit.Assert;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
public class ExternalPackageIT extends ChromeBrowserTest {
13+
14+
@BeforeClass
15+
public static void driver() {
16+
WebDriverManager.chromedriver().setup();
17+
}
18+
19+
@Test
20+
public void packageOutsideNpmWorks() {
21+
getDriver().get(getRootURL());
22+
waitForDevServer();
23+
$(NativeButtonElement.class).id(MainView.OUTSIDE).click();
24+
Assert.assertEquals("It works - It works", $(ParagraphElement.class)
25+
.id(MainView.OUTSIDE_RESULT).getText());
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { packageTransitive } from 'package-transitive';
2+
3+
window.packageOutsideNpm = packageTransitive;

flow-tests/test-frontend/vite-test-assets/packages/package-outside-npm/node_modules/package-transitive

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)