Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev-server-rollup): dedupe imports from outside root #2723

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-ways-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/dev-server-rollup': patch
---

dedupe imports from outside root
17 changes: 13 additions & 4 deletions packages/dev-server-rollup/src/rollupAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ export function rollupAdapter(

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

if (!normalizedPath.startsWith(checkRootDir)) {
if (!normalizedPath.startsWith(normalizedRootDir)) {
const relativePath = path.relative(rootDir, normalizedPath);
const dirUp = `..${path.sep}`;
const lastDirUpIndex = relativePath.lastIndexOf(dirUp) + 3;
Expand All @@ -317,8 +317,17 @@ export function rollupAdapter(
const importPath = toBrowserPath(relativePath.substring(lastDirUpIndex));
resolvedImportPath = `/__wds-outside-root__/${dirUpStrings.length - 1}/${importPath}`;
} else {
const resolveRelativeTo = path.dirname(filePath);
const relativeImportFilePath = path.relative(resolveRelativeTo, resolvedImportPath);
let relativeImportFilePath = '';

if (context.url.match(OUTSIDE_ROOT_REGEXP)) {
const pathInsideRootDir = `/${normalizedPath.replace(normalizedRootDir, '')}`;
const resolveRelativeTo = path.dirname(context.url);
relativeImportFilePath = path.relative(resolveRelativeTo, pathInsideRootDir);
} else {
const resolveRelativeTo = path.dirname(filePath);
relativeImportFilePath = path.relative(resolveRelativeTo, resolvedImportPath);
}

resolvedImportPath = `./${toBrowserPath(relativeImportFilePath)}`;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'react';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions packages/dev-server-rollup/test/node/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,46 @@ describe('@web/dev-server-rollup', () => {
});
});

it('files inside root directory imported by files inside or outside are resolved to be deduped in the browser', async () => {
const rootDir = path.resolve(
__dirname,
'fixtures',
'monorepo-import-inside-from-outside',
'src',
'packages',
'subpackage',
);
const { server, host } = await createTestServer({
rootDir,
plugins: [
fromRollup(() => {
return {
name: 'prebundle-modules',
async resolveId(source) {
if (source === 'react') {
return path.join(rootDir, 'node_modules', '.prebundled_modules', 'react.mjs');
}
},
};
})(),
],
});

try {
const responseTextInside = await fetchText(`${host}/app.js`);
expectIncludes(responseTextInside, "import './node_modules/.prebundled_modules/react.mjs'");
const responseTextOutside = await fetchText(
`${host}/__wds-outside-root__/3/node_modules/storybook/index.js`,
);
expectIncludes(
responseTextOutside,
"import './../../../../node_modules/.prebundled_modules/react.mjs'",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without my fix this would wrongly be

import './../../src/packages/subpackage/node_modules/.prebundled_modules/react.mjs'

causing a duplicate in the browser

);
} finally {
server.stop();
}
});

describe('load', () => {
it('can serve files', async () => {
const plugin: RollupPlugin = {
Expand Down
Loading