Skip to content

Commit 7a63af8

Browse files
authored
webpack-plugin: Skip child compile when within the child compiler (#181)
1 parent a5c4583 commit 7a63af8

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/webpack-plugin': patch
3+
---
4+
5+
Fix issue where CSS can be duplicated in some scenarios

packages/webpack-plugin/src/compiler.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ export class ChildCompiler {
3636
);
3737
}
3838

39-
async getCompiledSource(loader: LoaderContext, request: string) {
39+
async getCompiledSource(loader: LoaderContext) {
4040
const cacheId = loader.resourcePath;
4141
let compilationPromise = this.cache.get(cacheId);
4242

4343
if (!compilationPromise) {
4444
log('No cached compilation. Compiling: %s', cacheId);
45-
compilationPromise = compileVanillaSource(
46-
loader,
47-
request,
48-
this.externals,
49-
);
45+
compilationPromise = compileVanillaSource(loader, this.externals);
5046

5147
this.cache.set(cacheId, compilationPromise);
5248
} else {
@@ -86,7 +82,6 @@ function getRootCompilation(loader: LoaderContext) {
8682

8783
function compileVanillaSource(
8884
loader: LoaderContext,
89-
request: string,
9085
externals: Externals | undefined,
9186
): Promise<CompilationResult> {
9287
return new Promise((resolve, reject) => {
@@ -127,15 +122,15 @@ function compileVanillaSource(
127122
library: {
128123
type: 'commonjs2',
129124
},
130-
import: [`!!${request}`],
125+
import: [loader.resourcePath],
131126
},
132127
});
133128
} else {
134129
// Webpack 4 code. Remove once support is removed
135130
const { LibraryTemplatePlugin, SingleEntryPlugin } = require('webpack');
136131

137132
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
138-
new SingleEntryPlugin(loader.context, `!!${request}`).apply(
133+
new SingleEntryPlugin(loader.context, loader.resourcePath).apply(
139134
childCompiler,
140135
);
141136
}

packages/webpack-plugin/src/loader.ts

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function (this: LoaderContext, source: string) {
1919
return source;
2020
}
2121

22-
export async function pitch(this: LoaderContext, remainingRequest: string) {
22+
export function pitch(this: LoaderContext) {
2323
this.cacheable(true);
2424
const { childCompiler, outputCss } = loaderUtils.getOptions(
2525
this,
@@ -33,49 +33,44 @@ export async function pitch(this: LoaderContext, remainingRequest: string) {
3333

3434
const isChildCompiler = childCompiler.isChildCompiler(compiler.name);
3535

36-
if (
37-
isChildCompiler &&
38-
compiler.options.output.filename === this.resourcePath
39-
) {
36+
if (isChildCompiler) {
4037
log(
41-
'Skip vanilla-extract loader as we are already within a child compiler for this file',
38+
'Skip vanilla-extract loader as we are already within a child compiler for %s',
39+
compiler.options.output.filename,
4240
);
4341
return;
4442
}
4543

46-
const callback = this.async();
44+
log('Loading file');
4745

48-
try {
49-
const { source } = await childCompiler.getCompiledSource(
50-
this,
51-
remainingRequest,
52-
);
46+
const callback = this.async();
5347

54-
if (isChildCompiler) {
55-
// If within a vanilla-extract child compiler then only compile source, don't eval and assign CSS
56-
return callback(null, source);
57-
}
58-
59-
const result = processVanillaFile({
60-
source,
61-
outputCss,
62-
filePath: this.resourcePath,
63-
serializeVirtualCssPath: ({ fileName, base64Source }) => {
64-
const virtualResourceLoader = `${require.resolve(
65-
'virtual-resource-loader',
66-
)}?${JSON.stringify({ source: base64Source })}`;
67-
68-
const request = loaderUtils.stringifyRequest(
69-
this,
70-
`${fileName}!=!${virtualResourceLoader}!@vanilla-extract/webpack-plugin/extracted`,
71-
);
72-
73-
return `import ${request}`;
74-
},
48+
childCompiler
49+
.getCompiledSource(this)
50+
.then(({ source }) => {
51+
const result = processVanillaFile({
52+
source,
53+
outputCss,
54+
filePath: this.resourcePath,
55+
serializeVirtualCssPath: ({ fileName, base64Source }) => {
56+
const virtualResourceLoader = `${require.resolve(
57+
'virtual-resource-loader',
58+
)}?${JSON.stringify({ source: base64Source })}`;
59+
60+
const request = loaderUtils.stringifyRequest(
61+
this,
62+
`${fileName}!=!${virtualResourceLoader}!@vanilla-extract/webpack-plugin/extracted`,
63+
);
64+
65+
return `import ${request}`;
66+
},
67+
});
68+
69+
log('Completed successfully');
70+
71+
callback(null, result);
72+
})
73+
.catch((e) => {
74+
callback(e);
7575
});
76-
77-
callback(null, result);
78-
} catch (e) {
79-
callback(e);
80-
}
8176
}

0 commit comments

Comments
 (0)