Skip to content

fix: do not handle the path that can’t be resolved #612

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

Merged
merged 1 commit into from
Dec 26, 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
37 changes: 18 additions & 19 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ const composeShimsConfig = (
};

export const composeModuleImportWarn = (request: string): string => {
return `The externalized commonjs request ${color.green(`"${request}"`)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, considering set the request and type with ${color.blue('"output.externals"')}.`;
return `The externalized commonjs request ${color.green(`"${request}"`)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, consider setting the request and type with ${color.blue('"output.externals"')}.`;
};

const composeExternalsConfig = (
Expand Down Expand Up @@ -1022,26 +1022,25 @@ const composeBundlelessExternalConfig = (
if (jsRedirectPath) {
try {
resolvedRequest = await resolver(context, resolvedRequest);
resolvedRequest = normalizeSlash(
path.relative(
path.dirname(contextInfo.issuer),
resolvedRequest,
),
);
// Requests that fall through here cannot be matched by any other externals config ahead.
// Treat all these requests as relative import of source code. Node.js won't add the
// leading './' to the relative path resolved by `path.relative`. So add manually it here.
if (resolvedRequest[0] !== '.') {
resolvedRequest = `./${resolvedRequest}`;
}
} catch (e) {
// Do nothing, fallthrough to other external matches.
logger.debug(
`Failed to resolve ${resolvedRequest} with resolver`,
// e.g. A react component library importing and using 'react' but while not defining
// it in devDependencies and peerDependencies. Preserve 'react' as-is if so.
logger.warn(
`Failed to resolve module ${color.green(`"${resolvedRequest}"`)} from ${color.green(contextInfo.issuer)}. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.`,
);
}

resolvedRequest = normalizeSlash(
path.relative(
path.dirname(contextInfo.issuer),
resolvedRequest,
),
);

// Requests that fall through here cannot be matched by any other externals config ahead.
// Treat all these requests as relative import of source code. Node.js won't add the
// leading './' to the relative path resolved by `path.relative`. So add manually it here.
if (resolvedRequest[0] !== '.') {
resolvedRequest = `./${resolvedRequest}`;
}
}

// Node.js ECMAScript module loader does no extension searching.
Expand All @@ -1050,7 +1049,7 @@ const composeBundlelessExternalConfig = (
// If data.request already have an extension, we replace it with new extension
// This may result in a change in semantics,
// user should use copy to keep origin file or use another separate entry to deal this
if (jsRedirectExtension) {
if (jsRedirectExtension && resolvedRequest.startsWith('.')) {
const ext = extname(resolvedRequest);
if (ext) {
if (JS_EXTENSIONS_PATTERN.test(resolvedRequest)) {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions tests/integration/redirect/js-not-resolve/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "redirect-js-no-resolve-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
54 changes: 54 additions & 0 deletions tests/integration/redirect/js-not-resolve/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
// 0 default
generateBundleEsmConfig({
bundle: false,
output: {
distPath: {
root: 'dist/default/esm',
},
},
}),
// 1 js.path: false
generateBundleEsmConfig({
bundle: false,
output: {
distPath: {
root: 'dist/js-path-false/esm',
},
},
redirect: {
js: {
path: false,
},
},
}),
// 2 js.extension: false
generateBundleEsmConfig({
bundle: false,
output: {
distPath: {
root: 'dist/js-extension-false/esm',
},
},
redirect: {
js: {
extension: false,
},
},
}),
],
resolve: {
alias: {
'~': './src',
},
},
source: {
entry: {
index: './src/**',
},
},
});
5 changes: 5 additions & 0 deletions tests/integration/redirect/js-not-resolve/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import lodash from 'lodash';
import bar from './bar.js';
import foo from './foo';

export default lodash.toUpper(foo + bar);
8 changes: 4 additions & 4 deletions tests/integration/redirect/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ test('redirect.js.path false', async () => {

expect(indexContent).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_fb2b582c__ from "@/bar.js";
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_ce8863d2__ from "@/foo.js";
import * as __WEBPACK_EXTERNAL_MODULE__baz_js_b1797427__ from "~/baz.js";
import * as __WEBPACK_EXTERNAL_MODULE__bar_943a8c75__ from "@/bar";
import * as __WEBPACK_EXTERNAL_MODULE__foo_a5f33889__ from "@/foo";
import * as __WEBPACK_EXTERNAL_MODULE__baz_3ce4598c__ from "~/baz";
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__.foo + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__.bar + __WEBPACK_EXTERNAL_MODULE__foo_js_ce8863d2__.foo + __WEBPACK_EXTERNAL_MODULE__bar_js_fb2b582c__.bar + __WEBPACK_EXTERNAL_MODULE__baz_js_b1797427__.baz);
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__.foo + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__.bar + __WEBPACK_EXTERNAL_MODULE__foo_a5f33889__.foo + __WEBPACK_EXTERNAL_MODULE__bar_943a8c75__.bar + __WEBPACK_EXTERNAL_MODULE__baz_3ce4598c__.baz);
export { src_rslib_entry_ as default };
"
`);
Expand Down
103 changes: 103 additions & 0 deletions tests/integration/redirect/jsNotResolved.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import path from 'node:path';
import stripAnsi from 'strip-ansi';
import { buildAndGetResults, proxyConsole, queryContent } from 'test-helper';
import { expect, test } from 'vitest';

test('redirect.js default', async () => {
const fixturePath = path.resolve(__dirname, './js-not-resolve');
const { logs } = proxyConsole();
const contents = (await buildAndGetResults({ fixturePath, lib: ['esm0'] }))
.contents;

const logStrings = logs
.map((log) => stripAnsi(log))
.filter((log) => log.startsWith('warn'))
.sort();

expect(logStrings).toMatchInlineSnapshot(
`
[
"warn Failed to resolve module "./bar.js" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
"warn Failed to resolve module "./foo" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
"warn Failed to resolve module "lodash" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
]
`,
);

const { content: indexContent } = queryContent(
contents.esm0!,
/esm\/index\.js/,
);

expect(indexContent).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__["default"] + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__["default"]);
export { src_rslib_entry_ as default };
"
`);
});

test('redirect.js.path false', async () => {
const fixturePath = path.resolve(__dirname, './js-not-resolve');
const { logs } = proxyConsole();
const contents = (await buildAndGetResults({ fixturePath, lib: ['esm1'] }))
.contents;

const logStrings = logs
.map((log) => stripAnsi(log))
.filter((log) => log.startsWith('warn'));

expect(logStrings.length).toBe(0);

const { content: indexContent } = queryContent(
contents.esm1!,
/esm\/index\.js/,
);

expect(indexContent).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__["default"] + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__["default"]);
export { src_rslib_entry_ as default };
"
`);
});

test('redirect.js.extension: false', async () => {
const fixturePath = path.resolve(__dirname, './js-not-resolve');
const { logs } = proxyConsole();
const contents = (await buildAndGetResults({ fixturePath, lib: ['esm2'] }))
.contents;

const logStrings = logs
.map((log) => stripAnsi(log))
.filter((log) => log.startsWith('warn'))
.sort();

expect(logStrings).toMatchInlineSnapshot(
`
[
"warn Failed to resolve module "./bar.js" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
"warn Failed to resolve module "./foo" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
"warn Failed to resolve module "lodash" from <ROOT>/tests/integration/redirect/js-not-resolve/src/index.js. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.",
]
`,
);

const { content: indexContent } = queryContent(
contents.esm2!,
/esm\/index\.js/,
);

expect(indexContent).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__ from "./bar.js";
import * as __WEBPACK_EXTERNAL_MODULE__foo_23da6eef__ from "./foo";
const src_rslib_entry_ = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].toUpper(__WEBPACK_EXTERNAL_MODULE__foo_23da6eef__["default"] + __WEBPACK_EXTERNAL_MODULE__bar_js_69b41beb__["default"]);
export { src_rslib_entry_ as default };
"
`);
});
9 changes: 8 additions & 1 deletion tests/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,28 @@ export async function rslibBuild({
cwd,
path,
modifyConfig,
lib,
}: {
cwd: string;
path?: string;
modifyConfig?: (config: RslibConfig) => void;
lib?: string[];
}) {
const { content: rslibConfig } = await loadConfig({
cwd,
path,
});
modifyConfig?.(rslibConfig);
process.chdir(cwd);
const rsbuildInstance = await build(rslibConfig);
const rsbuildInstance = await build(rslibConfig, { lib });
return { rsbuildInstance, rslibConfig };
}

export async function buildAndGetResults(options: {
fixturePath: string;
configPath?: string;
type: 'all';
lib?: string[];
}): Promise<{
js: BuildResult;
dts: BuildResult;
Expand All @@ -223,19 +226,23 @@ export async function buildAndGetResults(options: {
fixturePath: string;
configPath?: string;
type?: 'js' | 'dts' | 'css';
lib?: string[];
}): Promise<BuildResult>;
export async function buildAndGetResults({
fixturePath,
configPath,
type = 'js',
lib,
}: {
fixturePath: string;
configPath?: string;
type?: 'js' | 'dts' | 'css' | 'all';
lib?: string[];
}) {
const { rsbuildInstance, rslibConfig } = await rslibBuild({
cwd: fixturePath,
path: configPath,
lib,
});
const {
origin: { bundlerConfigs, rsbuildConfig },
Expand Down
Loading