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
[Feature] Add preserveModulesRoot config option #3786
Changes from 1 commit
ab49e9a
bb3603a
45c7516
b8e5e5a
c6628e5
4b629a6
ab4eb7b
181397a
bb16a35
e536077
1f6d7a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ import { | |
isDefaultAProperty, | ||
namespaceInteropHelpersByInteropType | ||
} from './utils/interopHelpers'; | ||
import { basename, dirname, extname, isAbsolute, normalize, resolve } from './utils/path'; | ||
import { basename, dirname, extname, isAbsolute, join, normalize, resolve } from './utils/path'; | ||
import { PluginDriver } from './utils/PluginDriver'; | ||
import relativeId, { getAliasName } from './utils/relativeId'; | ||
import renderChunk from './utils/renderChunk'; | ||
|
@@ -441,20 +441,30 @@ export default class Chunk { | |
? '[name].js' | ||
: '[name][extname].js' | ||
: options.entryFileNames; | ||
path = relative( | ||
preserveModulesRelativeDir, | ||
`${dirname(sanitizedId)}/${renderNamePattern( | ||
pattern, | ||
'output.entryFileNames', | ||
{ | ||
ext: () => extension.substr(1), | ||
extname: () => extension, | ||
format: () => options.format as string, | ||
name: () => this.getChunkName() | ||
}, | ||
this.getChunkInfo.bind(this) | ||
)}` | ||
let currentDir = dirname(sanitizedId); | ||
const fileName = renderNamePattern( | ||
pattern, | ||
'output.entryFileNames', | ||
{ | ||
ext: () => extension.substr(1), | ||
extname: () => extension, | ||
format: () => options.format as string, | ||
name: () => this.getChunkName() | ||
}, | ||
this.getChunkInfo.bind(this) | ||
); | ||
if (options.moduleRootDir) { | ||
const moduleRootDir = resolve(options.moduleRootDir); | ||
const moduleRootDirRegExp = new RegExp(`^${moduleRootDir}`); | ||
if (currentDir.match(moduleRootDirRegExp)) { | ||
currentDir = join( | ||
preserveModulesRelativeDir, | ||
currentDir.replace(moduleRootDirRegExp, '') | ||
); | ||
} | ||
} | ||
const currentPath = `${currentDir}/${fileName}`; | ||
path = relative(preserveModulesRelativeDir, currentPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking, is this really all necessary? Wouldn't this be the same: if (currentDir.startsWith(preserveModulesRoot)) {
path = currentDir.slice(preserveModulesRoot.length).replace(/^\//, '')
} else {
path = relative(preserveModulesRelativeDir, `${currentDir}/${fileName}`);
} or am I overlooking something here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In practice, this will probably will be the case, at least for right now. Edit: I think I figured this out -- this was close to what we needed |
||
} else { | ||
path = `_virtual/${basename(sanitizedId)}`; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -575,6 +575,7 @@ export interface OutputOptions { | |
intro?: string | (() => string | Promise<string>); | ||
manualChunks?: ManualChunksOption; | ||
minifyInternalExports?: boolean; | ||
moduleRootDir?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the option only applies to the preserveModules case, I would suggest to change the name to reflect this like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I like that name better |
||
name?: string; | ||
namespaceToStringTag?: boolean; | ||
noConflict?: boolean; | ||
|
@@ -620,6 +621,7 @@ export interface NormalizedOutputOptions { | |
intro: () => string | Promise<string>; | ||
manualChunks: ManualChunksOption; | ||
minifyInternalExports: boolean; | ||
moduleRootDir: string | undefined; | ||
name: string | undefined; | ||
namespaceToStringTag: boolean; | ||
noConflict: boolean; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not quite happy about doing it this way. Just putting the string into a regular expression has some side-effects like
.
matching any character that people may not be aware of. If we want to have the ability to have regular expressions, then I would prefer if we change the signature tostring | RegExp
. If it is a string, we check via.startsWith
and slice the id, if it is a regular expression we replace. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review! Will start looking into these changes. With this one, I wasn't planning on using regex as an input option, so I think
.startsWith
is definitely preferable