Skip to content

Commit

Permalink
fix: new webpack config doesn't work on windows (#4030)
Browse files Browse the repository at this point in the history
  • Loading branch information
YUCLing authored Sep 28, 2024
1 parent 29bb477 commit c0d3d97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 11 additions & 1 deletion js-packages/webpack-config/src/RegisterAsyncChunksPlugin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const ConcatenatedModule = require('webpack/lib/optimize/ConcatenatedModule');
class RegisterAsyncChunksPlugin {
static registry = {};

processUrlPath(urlPath) {
if (path.sep == "\\") {
// separator on windows is "\", this will cause escape issues when used in url path.
return urlPath.replace(/\\/g, '/');
}
return urlPath;
}

apply(compiler) {
compiler.hooks.thisCompilation.tap('RegisterAsyncChunksPlugin', (compilation) => {
let alreadyOptimized = false;
Expand Down Expand Up @@ -63,6 +71,8 @@ class RegisterAsyncChunksPlugin {
// Each line that has a webpackChunkName comment.
[...module._source._value.matchAll(/.*\/\* webpackChunkName: .* \*\/.*/gm)].forEach(([match]) => {
[...match.matchAll(/(.*?) webpackChunkName: '([^']*)'.*? \*\/ '([^']+)'.*?/gm)].forEach(([match, _, urlPath, importPath]) => {
urlPath = this.processUrlPath(urlPath);

// Import path is relative to module.resource, so we need to resolve it
const importPathResolved = path.resolve(path.dirname(module.resource), importPath);
const thisComposerJson = require(path.resolve(process.cwd(), '../composer.json'));
Expand Down Expand Up @@ -118,7 +128,7 @@ class RegisterAsyncChunksPlugin {

// The path right after the src/ directory, without the extension.
const regPathSep = `\\${path.sep}`;
const urlPath = module.resource.replace(new RegExp(`.*${regPathSep}src${regPathSep}([^.]+)\..+`), '$1');
const urlPath = this.processUrlPath(module.resource.replace(new RegExp(`.*${regPathSep}src${regPathSep}([^.]+)\..+`), '$1'));

if (!registrableModulesUrlPaths.has(urlPath)) {
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
Expand Down
7 changes: 6 additions & 1 deletion js-packages/webpack-config/src/autoChunkNameLoader.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ module.exports = function autoChunkNameLoader(source) {
let chunkPath = relativePathToImport;

if (absolutePathToImport.includes('src')) {
chunkPath = absolutePathToImport.split('src/')[1];
chunkPath = absolutePathToImport.split(`src${path.sep}`)[1];
}

if (path.sep == "\\") {
// separator on windows is '\', the resolver only works with '/'.
chunkPath = chunkPath.replace(/\\/g, '/');
}

const webpackCommentOptions = {
Expand Down

0 comments on commit c0d3d97

Please sign in to comment.