Skip to content

Commit

Permalink
fix: only treat files within context as source file
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jan 13, 2025
1 parent 1de43b3 commit 5fe32e5
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 30 deletions.
17 changes: 14 additions & 3 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,12 @@ const traverseEntryQuery = (
export const appendEntryQuery = (entries: RsbuildConfigEntry): RsbuildEntry =>
traverseEntryQuery(entries, (item) => `${item}?${RSLIB_ENTRY_QUERY}`);

export const resolveEntryPath = (
entries: RsbuildConfigEntry,
root: string,
): RsbuildEntry =>
traverseEntryQuery(entries, (item) => path.resolve(root, item));

const composeEntryConfig = async (
rawEntry: RsbuildConfigEntry,
bundle: LibConfig['bundle'],
Expand Down Expand Up @@ -939,7 +945,7 @@ const composeEntryConfig = async (
return {
entryConfig: {
source: {
entry: appendEntryQuery(entries),
entry: appendEntryQuery(resolveEntryPath(entries, root)),
},
},
lcp: null,
Expand Down Expand Up @@ -1044,6 +1050,7 @@ const composeBundlelessExternalConfig = (
redirect: Redirect,
cssModulesAuto: CssLoaderOptionsAuto,
bundle: boolean,
rootPath: string,
): {
config: EnvironmentConfig;
resolvedJsRedirect?: DeepRequired<JsRedirect>;
Expand Down Expand Up @@ -1083,9 +1090,12 @@ const composeBundlelessExternalConfig = (
let resolvedRequest = request;
// use resolver to resolve the request
resolvedRequest = await resolver!(context!, resolvedRequest);
const isSubpath = resolvedRequest.startsWith(
rootPath + path.sep,
);

// only handle the request that is not in node_modules
if (!resolvedRequest.includes('node_modules')) {
// only handle the request that within the root path
if (isSubpath) {
resolvedRequest = normalizeSlash(
path.relative(path.dirname(issuer), resolvedRequest),
);
Expand Down Expand Up @@ -1359,6 +1369,7 @@ async function composeLibRsbuildConfig(
redirect,
cssModulesAuto,
bundle,
rootPath,
);
const {
config: targetConfig,
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

File renamed without changes
2 changes: 1 addition & 1 deletion tests/integration/asset/limit/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import logo from '../../../../assets/logo.svg';
import logo from '../assets/logo.svg';

export default logo;
File renamed without changes
2 changes: 1 addition & 1 deletion tests/integration/asset/name/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import a from '../../../../assets/image.png';
import a from '../assets/image.png';

export default () => {
return <img src={a} alt="" />;
Expand Down
Binary file added tests/integration/asset/path/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/integration/asset/path/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import a from '../../../../assets/image.png';
import a from '../assets/image.png';

export default a;
7 changes: 7 additions & 0 deletions tests/integration/asset/public-path/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/integration/asset/public-path/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import logo from '../../../../assets/logo.svg';
import logo from '../assets/logo.svg';

export default logo;
7 changes: 7 additions & 0 deletions tests/integration/asset/svgr/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/integration/asset/svgr/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import logo from '../../../../assets/logo.svg?react';
import logo from '../assets/logo.svg?react';

console.log(logo);
2 changes: 1 addition & 1 deletion tests/integration/asset/svgr/src/namedExport.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ReactComponent } from '../../../../assets/logo.svg';
import { ReactComponent } from '../assets/logo.svg';

console.log(ReactComponent);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/integration/bundle-false/asset/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/integration/bundle-false/asset/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import image from '../../../../assets/image.png';
import logoURL from '../../../../assets/logo.svg';
import image from '../assets/image.png';
import logoURL from '../assets/logo.svg';

console.log(logoURL);
console.log(image);
15 changes: 15 additions & 0 deletions tests/integration/bundle-false/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ test('basic', async () => {
`);
});

test('monorepo', async () => {
const fixturePath = join(__dirname, 'monorepo/importer');
const { contents } = await buildAndGetResults({ fixturePath });
expect(
queryContent(contents.esm, 'index.js', {
basename: true,
}).content,
).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_bundle_false_monorepo_importee_test_35ca595f__ from "bundle-false-monorepo-importee-test";
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_bundle_false_monorepo_importee_test_35ca595f__["default"];
export { src_rslib_entry_ as default };
"
`);
});

test('single file', async () => {
const fixturePath = join(__dirname, 'single-file');
const { files } = await buildAndGetResults({ fixturePath });
Expand Down
1 change: 1 addition & 0 deletions tests/integration/bundle-false/monorepo/importee/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'importee';
7 changes: 7 additions & 0 deletions tests/integration/bundle-false/monorepo/importee/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "bundle-false-monorepo-importee-test",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "./index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
9 changes: 9 additions & 0 deletions tests/integration/bundle-false/monorepo/importer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "bundle-false-monorepo-importer-test",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"bundle-false-monorepo-importee-test": "workspace:*"
}
}
18 changes: 18 additions & 0 deletions tests/integration/bundle-false/monorepo/importer/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: false,
}),
generateBundleCjsConfig({
bundle: false,
}),
],
source: {
entry: {
index: ['./src/**'],
},
},
});
3 changes: 3 additions & 0 deletions tests/integration/bundle-false/monorepo/importer/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import importee from 'bundle-false-monorepo-importee-test';

export default importee;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
7 changes: 7 additions & 0 deletions tests/integration/bundle-false/svgr/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/integration/bundle-false/svgr/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Logo from '../../../../assets/logo.svg?react';
import Logo from '../assets/logo.svg?react';

export default Logo;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineConfig({
],
source: {
entry: {
index: ['../__fixtures__/basic/src/**', '!../__fixtures__/**/*.d.ts'],
index: ['../__fixtures__/basic/src/**'],
},
},
plugins: [
Expand Down
27 changes: 20 additions & 7 deletions tests/integration/style/less/bundle-false/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import { resolve } from 'node:path';
import { pluginLess } from '@rsbuild/plugin-less';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({ bundle: false }),
generateBundleCjsConfig({ bundle: false }),
generateBundleEsmConfig({
bundle: false,
output: {
distPath: {
root: resolve(__dirname, 'dist/esm'),
},
},
}),
generateBundleCjsConfig({
bundle: false,
output: {
distPath: {
root: resolve(__dirname, 'dist/cjs'),
},
},
}),
],
root: resolve(__dirname, '../__fixtures__/basic'),
source: {
entry: {
index: ['../__fixtures__/basic/src/**', '!../__fixtures__/**/*.d.ts'],
index: ['./src/**'],
},
},
resolve: {
alias: {
'~': require('node:path').resolve(
__dirname,
'../__fixtures__/basic/src/nest',
),
'~': resolve(__dirname, '../__fixtures__/basic/src/nest'),
},
},
plugins: [
Expand Down
21 changes: 19 additions & 2 deletions tests/integration/style/less/bundle-import/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { resolve } from 'node:path';
import { pluginLess } from '@rsbuild/plugin-less';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [generateBundleEsmConfig(), generateBundleCjsConfig()],
lib: [
generateBundleEsmConfig({
output: {
distPath: {
root: resolve(__dirname, 'dist/esm'),
},
},
}),
generateBundleCjsConfig({
output: {
distPath: {
root: resolve(__dirname, 'dist/cjs'),
},
},
}),
],
root: resolve(__dirname, '../__fixtures__/import'),
source: {
entry: {
index: ['../__fixtures__/import/src/index.ts'],
index: './src/index.ts',
},
},
output: {
Expand Down
21 changes: 19 additions & 2 deletions tests/integration/style/less/bundle/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { resolve } from 'node:path';
import { pluginLess } from '@rsbuild/plugin-less';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [generateBundleEsmConfig(), generateBundleCjsConfig()],
lib: [
generateBundleEsmConfig({
output: {
distPath: {
root: resolve(__dirname, 'dist/esm'),
},
},
}),
generateBundleCjsConfig({
output: {
distPath: {
root: resolve(__dirname, 'dist/cjs'),
},
},
}),
],
root: resolve(__dirname, '../__fixtures__/basic'),
source: {
entry: {
index: ['../__fixtures__/basic/src/index.less'],
index: ['./src/index.less'],
},
},
resolve: {
Expand Down
Loading

0 comments on commit 5fe32e5

Please sign in to comment.