Skip to content

Commit

Permalink
fix(dev-server-rollup): dedupe imports from outside root
Browse files Browse the repository at this point in the history
  • Loading branch information
bashmish committed May 3, 2024
1 parent 6fcd4e1 commit 8552a4a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
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'",
);
} finally {
server.stop();
}
});

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

0 comments on commit 8552a4a

Please sign in to comment.