Skip to content

Commit a476850

Browse files
authored
Fix CSS Modules + baseUrl (#3515)
1 parent 28884b5 commit a476850

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

snowpack/src/build/build-import-proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ async function generateCssModuleImportProxy({
244244
hmr: boolean;
245245
config: SnowpackConfig;
246246
}) {
247+
const reqUrl = url.replace(new RegExp(`^${config.buildOptions.baseUrl}`), '/'); // note: in build, buildOptions.baseUrl gets prepended. Remove that for looking up CSS Module code
247248
return `
248249
export let code = ${JSON.stringify(code)};
249-
let json = ${cssModuleJSON(url)};
250+
let json = ${cssModuleJSON(reqUrl)};
250251
export default json;
251252
${
252253
hmr

test/snowpack/cssModules/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,18 @@ describe('cssModules', () => {
162162
expect(result['_dist_/App.module.css']).not.toContain(`.App {`);
163163
expect(result['_dist_/App.module.css.json']).toBeDefined();
164164
});
165+
166+
it('Works with buildOptions.baseUrl set', async () => {
167+
const result = await testFixture({
168+
...files(),
169+
'snowpack.config.js': dedent`
170+
module.exports = {
171+
buildOptions: {
172+
baseUrl: '/subdir/'
173+
}
174+
};
175+
`,
176+
});
177+
expect(result['src/App.module.css.proxy.js']).toContain(`let json = {"App":"_App_`);
178+
});
165179
});

0 commit comments

Comments
 (0)