fix(webpack): publicPath and rebaseRootRelative (#20992)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->
 can not configure webpack publicPath with NX option.
## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
 make the publicPath work with postcssCliResources.
## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

---------

Co-authored-by: Colum Ferry <cferry09@gmail.com>
This commit is contained in:
Edward Wang 2024-05-09 19:39:29 +08:00 committed by GitHub
parent 8f705e31e2
commit 8f25ade650
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 33 additions and 1 deletions

View File

@ -375,6 +375,14 @@
"type": "string",
"description": "Path to the babel configuration file of your project. If not provided, Nx will default to the .babelrc file at the root of your project. See https://babeljs.io/docs/en/config-files",
"x-completion-type": "file"
},
"publicPath": {
"type": "string",
"description": "Set a public path for assets resources with absolute paths."
},
"rebaseRootRelative": {
"type": "boolean",
"description": "Whether to rebase absolute path for assets in postcss cli resources."
}
},
"required": [],

View File

@ -84,6 +84,8 @@ export interface WebpackExecutorOptions {
stylePreprocessorOptions?: any;
styles?: Array<ExtraEntryPointClass | string>;
subresourceIntegrity?: boolean;
publicPath?: string;
rebaseRootRelative?: boolean;
}
export interface NormalizedWebpackExecutorOptions

View File

@ -299,6 +299,14 @@
"type": "string",
"description": "Path to the babel configuration file of your project. If not provided, Nx will default to the .babelrc file at the root of your project. See https://babeljs.io/docs/en/config-files",
"x-completion-type": "file"
},
"publicPath": {
"type": "string",
"description": "Set a public path for assets resources with absolute paths."
},
"rebaseRootRelative": {
"type": "boolean",
"description": "Whether to rebase absolute path for assets in postcss cli resources."
}
},
"required": [],

View File

@ -128,6 +128,8 @@ function postcssOptionsCreator(
deployUrl: options.deployUrl,
loader,
filename: `[name]${hashFormat.file}.[ext]`,
publicPath: options.publicPath,
rebaseRootRelative: options.rebaseRootRelative,
}),
autoprefixer(),
]),

View File

@ -210,6 +210,14 @@ export interface NxAppWebpackPluginOptions {
* Watch for file changes.
*/
watch?: boolean;
/**
* Set a public path for assets resources with absolute paths.
*/
publicPath?: string;
/**
* Whether to rebase absolute path for assets in postcss cli resources.
*/
rebaseRootRelative?: boolean;
}
export interface NormalizedNxAppWebpackPluginOptions

View File

@ -22,6 +22,7 @@ export interface PostcssCliResourcesOptions {
rebaseRootRelative?: boolean;
filename: string;
loader: LoaderContext<unknown>;
publicPath: string;
}
async function resolve(
@ -46,6 +47,7 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) {
rebaseRootRelative = false,
filename,
loader,
publicPath = '',
} = options;
const dedupeSlashes = (url: string) => url.replace(/\/\/+/g, '/');
const process = async (
@ -85,7 +87,9 @@ export function PostcssCliResources(options: PostcssCliResourcesOptions) {
dedupeSlashes(`/${deployUrl}/${inputUrl}`);
} else {
// Join together base-href, deploy-url and the original URL.
outputUrl = dedupeSlashes(`/${baseHref}/${deployUrl}/${inputUrl}`);
outputUrl = dedupeSlashes(
`/${baseHref}/${deployUrl}/${publicPath}/${inputUrl}`
);
}
resourceCache.set(cacheKey, outputUrl);
return outputUrl;