Skip to content

Commit

Permalink
fix: proxy file when dev (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonBall authored and FredKSchott committed Aug 27, 2020
1 parent d9a2d65 commit e532198
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,16 +489,17 @@ export async function command(commandOptions: CommandOptions) {
* return a JS representation of that CSS. This is handled in the wrap step.
*/
async function wrapResponse(
code: string,
code: string | Buffer,
{
hasCssResource,
sourceMap,
sourceMappingURL,
}: {hasCssResource: boolean; sourceMap?: string; sourceMappingURL: string},
) {
// transform special requests
const stringCode = code as string;
if (isRoute) {
code = wrapHtmlResponse({code: code, hmr: isHmr, config, mode: 'development'});
code = wrapHtmlResponse({code: stringCode, hmr: isHmr, config, mode: 'development'});
} else if (isProxyModule) {
responseFileExt = '.js';
} else if (isSourceMap && sourceMap) {
Expand All @@ -509,14 +510,14 @@ export async function command(commandOptions: CommandOptions) {
// transform other files
switch (responseFileExt) {
case '.css': {
if (sourceMap) code = cssSourceMappingURL(code, sourceMappingURL);
if (sourceMap) code = cssSourceMappingURL(stringCode, sourceMappingURL);
break;
}
case '.js': {
if (isProxyModule) {
code = await wrapImportProxy({url: reqPath, code, hmr: isHmr, config});
} else {
code = wrapImportMeta({code, env: true, hmr: isHmr, config});
code = wrapImportMeta({code: stringCode, env: true, hmr: isHmr, config});
}

if (hasCssResource)
Expand Down Expand Up @@ -626,7 +627,21 @@ If Snowpack is having trouble detecting the import, add ${colors.bold(
}
// Wrap the response.
const {code, map} = output[requestedFileExt];
if (typeof code !== 'string') return code; // return binary files as-is
if (typeof code !== 'string' && !isProxyModule) return code; // return binary files as-is
let finalResponse = code;

// Resolve imports.
if (
requestedFileExt === '.js' ||
requestedFileExt === '.html' ||
requestedFileExt === '.css'
) {
finalResponse = await resolveResponseImports(
fileLoc,
requestedFileExt,
finalResponse as string,
);
}

const hasAttachedCss = requestedFileExt === '.js' && !!output['.css'];
let wrappedResponse = await wrapResponse(code, {
Expand Down

0 comments on commit e532198

Please sign in to comment.