Skip to content

[Feature]: Redirect in bundleless mode #140

@Timeless0911

Description

@Timeless0911

What problem does this feature solve?

When bundle: false, aka bundleless mode, the import and export path should be redirected to ensure that it is linked to the correct output, e.g:

  • import '. /index.less' should be rewritten to import '. /index.css'
  • import icon from '. /close.svg' should be rewritten to import icon from '... /asset/close.svg' or to import icon from '. /asset/close.svg'
  • import { a } from '@/alias' should be rewritten to import { a } from "./alias"
  • import * from './utils' should be rewritten to import * from './utils.mjs or import * from './utils.cjs

The redirect behaviour should take the autoExtension logic in Rslib as well as node/Typescript resolver logic into consider.

In the existing code logic, we already do some redirect logic of autoExtenson by default when it is a relative path

output: {
externals: [
(data: any, callback: any) => {
// Issuer is not empty string when the module is imported by another module.
// Prevent from externalizing entry modules here.
if (data.contextInfo.issuer) {
// Node.js ECMAScript module loader does no extension searching.
// Add a file extension according to autoExtension config
// when data.request is a relative path and do not have an extension.
// 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
let request = data.request;
if (request[0] === '.') {
request = extname(request)
? request.replace(/\.[^.]+$/, jsExtension)
: `${request}${jsExtension}`;
}
return callback(null, request);
}
callback();
},
],
},
};
};

And for same feature in Modern.js Module, the implement is based on AST, see https://github.com/web-infra-dev/modern.js/blob/main/packages/solutions/module-tools/src/builder/feature/redirect.ts and https://github.com/web-infra-dev/modern.js/blob/95090d23cc4109e46ad45d8cd613f0eed6204547/packages/solutions/module-tools/src/utils/dts.ts#L140-L223

What does the proposed API look like?

To implement this feature, we should both support js and DTS output:

  • redirect in js outputs of bundleless mode
    • alias
    • extensions (import/export)
    • css
    • asset path
  • redirect in DTS outputs of bundleless mode
    • alias
    • extensions

This feature should be opt-in:

export type Redirect = {
  alias?: boolean;
  style?: boolean;
  asset?: boolean;
  autoExtension?: boolean;
};

Sub-issues

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions