diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index cddd2b02e..b7a990eb9 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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'], @@ -939,7 +945,7 @@ const composeEntryConfig = async ( return { entryConfig: { source: { - entry: appendEntryQuery(entries), + entry: appendEntryQuery(resolveEntryPath(entries, root)), }, }, lcp: null, @@ -1044,6 +1050,7 @@ const composeBundlelessExternalConfig = ( redirect: Redirect, cssModulesAuto: CssLoaderOptionsAuto, bundle: boolean, + rootPath: string, ): { config: EnvironmentConfig; resolvedJsRedirect?: DeepRequired; @@ -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), ); @@ -1359,6 +1369,7 @@ async function composeLibRsbuildConfig( redirect, cssModulesAuto, bundle, + rootPath, ); const { config: targetConfig, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9e9fe079c..8ff80fc23 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -555,6 +555,14 @@ importers: tests/integration/bundle-false/js-extension: {} + tests/integration/bundle-false/monorepo/importee: {} + + tests/integration/bundle-false/monorepo/importer: + dependencies: + bundle-false-monorepo-importee-test: + specifier: workspace:* + version: link:../importee + tests/integration/bundle-false/single-file: {} tests/integration/bundle-false/svgr: diff --git a/tests/assets/logo.svg b/tests/integration/asset/limit/assets/logo.svg similarity index 100% rename from tests/assets/logo.svg rename to tests/integration/asset/limit/assets/logo.svg diff --git a/tests/integration/asset/limit/src/index.js b/tests/integration/asset/limit/src/index.js index a6afa8809..76ad10053 100644 --- a/tests/integration/asset/limit/src/index.js +++ b/tests/integration/asset/limit/src/index.js @@ -1,3 +1,3 @@ -import logo from '../../../../assets/logo.svg'; +import logo from '../assets/logo.svg'; export default logo; diff --git a/tests/assets/image.png b/tests/integration/asset/name/assets/image.png similarity index 100% rename from tests/assets/image.png rename to tests/integration/asset/name/assets/image.png diff --git a/tests/integration/asset/name/src/index.jsx b/tests/integration/asset/name/src/index.jsx index ce7aaaa9d..d78a7a874 100644 --- a/tests/integration/asset/name/src/index.jsx +++ b/tests/integration/asset/name/src/index.jsx @@ -1,4 +1,4 @@ -import a from '../../../../assets/image.png'; +import a from '../assets/image.png'; export default () => { return ; diff --git a/tests/integration/asset/path/assets/image.png b/tests/integration/asset/path/assets/image.png new file mode 100644 index 000000000..a53ecc092 Binary files /dev/null and b/tests/integration/asset/path/assets/image.png differ diff --git a/tests/integration/asset/path/src/index.js b/tests/integration/asset/path/src/index.js index 63613bce5..206c64540 100644 --- a/tests/integration/asset/path/src/index.js +++ b/tests/integration/asset/path/src/index.js @@ -1,3 +1,3 @@ -import a from '../../../../assets/image.png'; +import a from '../assets/image.png'; export default a; diff --git a/tests/integration/asset/public-path/assets/logo.svg b/tests/integration/asset/public-path/assets/logo.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/tests/integration/asset/public-path/assets/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/integration/asset/public-path/src/index.js b/tests/integration/asset/public-path/src/index.js index a6afa8809..76ad10053 100644 --- a/tests/integration/asset/public-path/src/index.js +++ b/tests/integration/asset/public-path/src/index.js @@ -1,3 +1,3 @@ -import logo from '../../../../assets/logo.svg'; +import logo from '../assets/logo.svg'; export default logo; diff --git a/tests/integration/asset/svgr/assets/logo.svg b/tests/integration/asset/svgr/assets/logo.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/tests/integration/asset/svgr/assets/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/integration/asset/svgr/src/index.js b/tests/integration/asset/svgr/src/index.js index 0b3a78b3c..b6aaa208b 100644 --- a/tests/integration/asset/svgr/src/index.js +++ b/tests/integration/asset/svgr/src/index.js @@ -1,3 +1,3 @@ -import logo from '../../../../assets/logo.svg?react'; +import logo from '../assets/logo.svg?react'; console.log(logo); diff --git a/tests/integration/asset/svgr/src/namedExport.js b/tests/integration/asset/svgr/src/namedExport.js index 5ab1c97c9..0241abbd6 100644 --- a/tests/integration/asset/svgr/src/namedExport.js +++ b/tests/integration/asset/svgr/src/namedExport.js @@ -1,3 +1,3 @@ -import { ReactComponent } from '../../../../assets/logo.svg'; +import { ReactComponent } from '../assets/logo.svg'; console.log(ReactComponent); diff --git a/tests/integration/bundle-false/asset/assets/image.png b/tests/integration/bundle-false/asset/assets/image.png new file mode 100644 index 000000000..a53ecc092 Binary files /dev/null and b/tests/integration/bundle-false/asset/assets/image.png differ diff --git a/tests/integration/bundle-false/asset/assets/logo.svg b/tests/integration/bundle-false/asset/assets/logo.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/tests/integration/bundle-false/asset/assets/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/integration/bundle-false/asset/src/index.js b/tests/integration/bundle-false/asset/src/index.js index a5d00849e..2e3671336 100644 --- a/tests/integration/bundle-false/asset/src/index.js +++ b/tests/integration/bundle-false/asset/src/index.js @@ -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); diff --git a/tests/integration/bundle-false/index.test.ts b/tests/integration/bundle-false/index.test.ts index c3c9b0d9c..c46d47115 100644 --- a/tests/integration/bundle-false/index.test.ts +++ b/tests/integration/bundle-false/index.test.ts @@ -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 }); diff --git a/tests/integration/bundle-false/monorepo/importee/index.js b/tests/integration/bundle-false/monorepo/importee/index.js new file mode 100644 index 000000000..7a112c942 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importee/index.js @@ -0,0 +1 @@ +export default 'importee'; diff --git a/tests/integration/bundle-false/monorepo/importee/package.json b/tests/integration/bundle-false/monorepo/importee/package.json new file mode 100644 index 000000000..d8e7c0017 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importee/package.json @@ -0,0 +1,7 @@ +{ + "name": "bundle-false-monorepo-importee-test", + "version": "1.0.0", + "private": true, + "type": "module", + "main": "./index.js" +} diff --git a/tests/integration/bundle-false/monorepo/importee/tsconfig.json b/tests/integration/bundle-false/monorepo/importee/tsconfig.json new file mode 100644 index 000000000..888d3e460 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importee/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "baseUrl": "./" + }, + "include": ["src"] +} diff --git a/tests/integration/bundle-false/monorepo/importer/package.json b/tests/integration/bundle-false/monorepo/importer/package.json new file mode 100644 index 000000000..a7b2c6ce6 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importer/package.json @@ -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:*" + } +} diff --git a/tests/integration/bundle-false/monorepo/importer/rslib.config.ts b/tests/integration/bundle-false/monorepo/importer/rslib.config.ts new file mode 100644 index 000000000..6a10170f6 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importer/rslib.config.ts @@ -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/**'], + }, + }, +}); diff --git a/tests/integration/bundle-false/monorepo/importer/src/index.js b/tests/integration/bundle-false/monorepo/importer/src/index.js new file mode 100644 index 000000000..98e1cbda4 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importer/src/index.js @@ -0,0 +1,3 @@ +import importee from 'bundle-false-monorepo-importee-test'; + +export default importee; diff --git a/tests/integration/bundle-false/monorepo/importer/tsconfig.json b/tests/integration/bundle-false/monorepo/importer/tsconfig.json new file mode 100644 index 000000000..888d3e460 --- /dev/null +++ b/tests/integration/bundle-false/monorepo/importer/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "baseUrl": "./" + }, + "include": ["src"] +} diff --git a/tests/integration/bundle-false/svgr/assets/logo.svg b/tests/integration/bundle-false/svgr/assets/logo.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/tests/integration/bundle-false/svgr/assets/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/integration/bundle-false/svgr/src/index.jsx b/tests/integration/bundle-false/svgr/src/index.jsx index 9d8974f03..bbb3d6502 100644 --- a/tests/integration/bundle-false/svgr/src/index.jsx +++ b/tests/integration/bundle-false/svgr/src/index.jsx @@ -1,3 +1,3 @@ -import Logo from '../../../../assets/logo.svg?react'; +import Logo from '../assets/logo.svg?react'; export default Logo; diff --git a/tests/integration/style/css-modules/bundle-false-auto/rslib.config.ts b/tests/integration/style/css-modules/bundle-false-auto/rslib.config.ts index 020b3f5c8..f9cbf3721 100644 --- a/tests/integration/style/css-modules/bundle-false-auto/rslib.config.ts +++ b/tests/integration/style/css-modules/bundle-false-auto/rslib.config.ts @@ -9,7 +9,7 @@ export default defineConfig({ ], source: { entry: { - index: ['../__fixtures__/basic/src/**', '!../__fixtures__/**/*.d.ts'], + index: ['../__fixtures__/basic/src/**'], }, }, plugins: [ diff --git a/tests/integration/style/less/bundle-false/rslib.config.ts b/tests/integration/style/less/bundle-false/rslib.config.ts index ac9f4baa0..e5697df60 100644 --- a/tests/integration/style/less/bundle-false/rslib.config.ts +++ b/tests/integration/style/less/bundle-false/rslib.config.ts @@ -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: [ diff --git a/tests/integration/style/less/bundle-import/rslib.config.ts b/tests/integration/style/less/bundle-import/rslib.config.ts index fb6cb7826..a5c9635e4 100644 --- a/tests/integration/style/less/bundle-import/rslib.config.ts +++ b/tests/integration/style/less/bundle-import/rslib.config.ts @@ -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: { diff --git a/tests/integration/style/less/bundle/rslib.config.ts b/tests/integration/style/less/bundle/rslib.config.ts index d7b7991c5..3cfabaf26 100644 --- a/tests/integration/style/less/bundle/rslib.config.ts +++ b/tests/integration/style/less/bundle/rslib.config.ts @@ -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: { diff --git a/tests/integration/style/sass/bundle-false/rslib.config.ts b/tests/integration/style/sass/bundle-false/rslib.config.ts index 7ff820973..6cd56ea34 100644 --- a/tests/integration/style/sass/bundle-false/rslib.config.ts +++ b/tests/integration/style/sass/bundle-false/rslib.config.ts @@ -1,20 +1,33 @@ +import { resolve } from 'node:path'; import { pluginSass } from '@rsbuild/plugin-sass'; 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'), + }, + }, + }), ], source: { entry: { - index: [ - '../__fixtures__/src/**/*.scss', - '../__fixtures__/foundation/logo.svg', - ], + index: ['./src/**/*.scss', './foundation/logo.svg'], }, }, + root: resolve(__dirname, '../__fixtures__'), plugins: [ pluginSass({ sassLoaderOptions: {