Skip to content

Commit 8552a4a

Browse files
committed
fix(dev-server-rollup): dedupe imports from outside root
1 parent 6fcd4e1 commit 8552a4a

File tree

7 files changed

+64
-4
lines changed

7 files changed

+64
-4
lines changed

.changeset/witty-ways-occur.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/dev-server-rollup': patch
3+
---
4+
5+
dedupe imports from outside root

packages/dev-server-rollup/src/rollupAdapter.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ export function rollupAdapter(
289289

290290
// append a path separator to rootDir so we are actually testing containment
291291
// of the normalized path within the rootDir folder
292-
const checkRootDir = rootDir.endsWith(path.sep) ? rootDir : rootDir + path.sep;
292+
const normalizedRootDir = rootDir.endsWith(path.sep) ? rootDir : rootDir + path.sep;
293293

294-
if (!normalizedPath.startsWith(checkRootDir)) {
294+
if (!normalizedPath.startsWith(normalizedRootDir)) {
295295
const relativePath = path.relative(rootDir, normalizedPath);
296296
const dirUp = `..${path.sep}`;
297297
const lastDirUpIndex = relativePath.lastIndexOf(dirUp) + 3;
@@ -317,8 +317,17 @@ export function rollupAdapter(
317317
const importPath = toBrowserPath(relativePath.substring(lastDirUpIndex));
318318
resolvedImportPath = `/__wds-outside-root__/${dirUpStrings.length - 1}/${importPath}`;
319319
} else {
320-
const resolveRelativeTo = path.dirname(filePath);
321-
const relativeImportFilePath = path.relative(resolveRelativeTo, resolvedImportPath);
320+
let relativeImportFilePath = '';
321+
322+
if (context.url.match(OUTSIDE_ROOT_REGEXP)) {
323+
const pathInsideRootDir = `/${normalizedPath.replace(normalizedRootDir, '')}`;
324+
const resolveRelativeTo = path.dirname(context.url);
325+
relativeImportFilePath = path.relative(resolveRelativeTo, pathInsideRootDir);
326+
} else {
327+
const resolveRelativeTo = path.dirname(filePath);
328+
relativeImportFilePath = path.relative(resolveRelativeTo, resolvedImportPath);
329+
}
330+
322331
resolvedImportPath = `./${toBrowserPath(relativeImportFilePath)}`;
323332
}
324333

packages/dev-server-rollup/test/node/fixtures/monorepo-import-inside-from-outside/node_modules/storybook/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dev-server-rollup/test/node/fixtures/monorepo-import-inside-from-outside/node_modules/storybook/package.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'react';

packages/dev-server-rollup/test/node/fixtures/monorepo-import-inside-from-outside/src/packages/subpackage/node_modules/.prebundled_modules/react.mjs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dev-server-rollup/test/node/unit.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,46 @@ describe('@web/dev-server-rollup', () => {
104104
});
105105
});
106106

107+
it('files inside root directory imported by files inside or outside are resolved to be deduped in the browser', async () => {
108+
const rootDir = path.resolve(
109+
__dirname,
110+
'fixtures',
111+
'monorepo-import-inside-from-outside',
112+
'src',
113+
'packages',
114+
'subpackage',
115+
);
116+
const { server, host } = await createTestServer({
117+
rootDir,
118+
plugins: [
119+
fromRollup(() => {
120+
return {
121+
name: 'prebundle-modules',
122+
async resolveId(source) {
123+
if (source === 'react') {
124+
return path.join(rootDir, 'node_modules', '.prebundled_modules', 'react.mjs');
125+
}
126+
},
127+
};
128+
})(),
129+
],
130+
});
131+
132+
try {
133+
const responseTextInside = await fetchText(`${host}/app.js`);
134+
expectIncludes(responseTextInside, "import './node_modules/.prebundled_modules/react.mjs'");
135+
const responseTextOutside = await fetchText(
136+
`${host}/__wds-outside-root__/3/node_modules/storybook/index.js`,
137+
);
138+
expectIncludes(
139+
responseTextOutside,
140+
"import './../../../../node_modules/.prebundled_modules/react.mjs'",
141+
);
142+
} finally {
143+
server.stop();
144+
}
145+
});
146+
107147
describe('load', () => {
108148
it('can serve files', async () => {
109149
const plugin: RollupPlugin = {

0 commit comments

Comments
 (0)