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

Do not resolve multiple times outside root files #1382

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/healthy-lizards-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@web/dev-server-rollup": patch
"@web/dev-server": patch
---

do not resolve multiple times outside root files
7 changes: 6 additions & 1 deletion packages/dev-server-rollup/src/rollupAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CustomPluginOptions, Plugin as RollupPlugin, TransformPluginContext } f
import { InputOptions } from 'rollup';
import { red, cyanBright } from 'chalk';

import { toBrowserPath, isAbsoluteFilePath } from './utils';
import { toBrowserPath, isAbsoluteFilePath, isOutsideRootDir } from './utils';
import { createRollupPluginContextAdapter } from './createRollupPluginContextAdapter';
import { createRollupPluginContexts, RollupPluginContexts } from './createRollupPluginContexts';

Expand Down Expand Up @@ -196,6 +196,11 @@ export function rollupAdapter(
return `${resolvedImportPath}`;
}

// file already resolved outsided root dir
if (isOutsideRootDir(resolvedImportPath)) {
return `${resolvedImportPath}`;
}

const normalizedPath = path.normalize(resolvedImportPath);
if (!normalizedPath.startsWith(rootDir)) {
const relativePath = path.relative(rootDir, normalizedPath);
Expand Down
4 changes: 4 additions & 0 deletions packages/dev-server-rollup/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ export function toBrowserPath(filePath: string) {
export function isAbsoluteFilePath(path: string) {
return REGEXP_ABSOLUTE.test(path);
}

export function isOutsideRootDir(path: string) {
return path.startsWith('/__wds-outside-root__/');
}

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,3 @@
import moduleA from 'module-a';

console.log(moduleA);
37 changes: 37 additions & 0 deletions packages/dev-server-rollup/test/node/plugins/node-resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import path from 'path';
import rollupNodeResolve from '@rollup/plugin-node-resolve';
import rollupCommonjs from '@rollup/plugin-commonjs';
import fetch from 'node-fetch';

import { createTestServer, fetchText, expectIncludes } from '../test-helpers';
import { fromRollup } from '../../../src/index';
import { expect } from 'chai';

const nodeResolve = fromRollup(rollupNodeResolve, {}, { throwOnUnresolvedImport: true });
const commonjs = fromRollup(rollupCommonjs);

describe('@rollup/plugin-node-resolve', () => {
it('can resolve imports', async () => {
Expand Down Expand Up @@ -79,4 +82,38 @@ describe('@rollup/plugin-node-resolve', () => {
server.stop();
}
});

it('node modules resolved outside root directory are rewritten', async () => {
const { server, host } = await createTestServer({
rootDir: path.resolve(__dirname, '..', 'fixtures', 'resolve-outside-dir', 'src'),
plugins: [nodeResolve()],
});

try {
const responseText = await fetchText(`${host}/app.js`);
expectIncludes(
responseText,
"import moduleA from '/__wds-outside-root__/1/node_modules/module-a/index.js'",
);
} finally {
server.stop();
}
});

it('node modules resolved outside root directory are rewritten with commonjs', async () => {
const { server, host } = await createTestServer({
rootDir: path.resolve(__dirname, '..', 'fixtures', 'resolve-outside-dir', 'src'),
plugins: [commonjs(), nodeResolve()],
});

try {
const responseText = await fetchText(`${host}/app.js`);
expectIncludes(
responseText,
"import moduleA from '/__wds-outside-root__/1/node_modules/module-a/index.js'",
);
} finally {
server.stop();
}
});
});