Skip to content
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

fix(web): fix styles vendorChunk in webpack 5 #6822

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -95,3 +95,10 @@ export interface WebpackConfigOptions<T = BuildOptions> {
tsConfigPath: string;
supportES2015: boolean;
}

// Stub types from webpack@5
// TODO: import from 'webpack' in Nx 13 (see browser.ts)
export type Module = any;
export type ModuleGraph = any;
export type ChunkGraph = any;
export type Chunk = any;
Expand Up @@ -5,8 +5,17 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// TODO: import from 'webpack' in Nx 13
// import type { Module, ModuleGraph, ChunkGraph, Chunk } from 'webpack';
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import { WebpackConfigOptions, BuildOptions } from '../build-options';
import {
WebpackConfigOptions,
BuildOptions,
Module,
ModuleGraph,
ChunkGraph,
Chunk,
} from '../build-options';
import {
getSourceMapDevTool,
isPolyfillsEntry,
Expand Down Expand Up @@ -120,30 +129,32 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
}
: {}),
vendors: false,
// TODO(jack): Support both 4 and 5
vendor: !!buildOptions.vendorChunk && {
name: 'vendor',
chunks: isWebpack5 ? (chunk) => chunk.name === 'main' : 'initial',
enforce: true,
test: isWebpack5
? /[\\/]node_modules[\\/]/
: (
module: { nameForCondition?: Function },
chunks: Array<{ name: string }>
) => {
const moduleName = module.nameForCondition
? module.nameForCondition()
: '';
test: (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test logic is the same, the only difference for isWebpack5 is the chunks condition. If preferred, we can revert the commit 283fcc2 to have separate function for isWebpack5.

module: Module,
// `CacheGroupsContext`, but it's not exported from webpack types
context: { moduleGraph: ModuleGraph; chunkGraph: ChunkGraph }
) => {
const moduleName = module.nameForCondition
? module.nameForCondition()
: '';
// TODO(jack): Remove in Nx 13
const chunks = isWebpack5
? context.chunkGraph.getModuleChunks(module)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going for the same condition as the webpack 4.
I'm not very familiar with the webpack API, so I am not sure if there is a better way to achieve this. I found that there's the chunksFilter config option, but it's called only with the chunk value, so I'm not sure if it would work.

: (context as unknown as Chunk[]);

return (
/[\\/]node_modules[\\/]/.test(moduleName) &&
!chunks.some(
({ name }) =>
isPolyfillsEntry(name) ||
globalStylesBundleNames.includes(name)
)
);
},
return (
/[\\/]node_modules[\\/]/.test(moduleName) &&
!chunks.some(
({ name }) =>
isPolyfillsEntry(name) ||
globalStylesBundleNames.includes(name)
)
);
},
},
},
},
Expand Down