Skip to content

Commit

Permalink
Clean up empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Oct 14, 2020
1 parent 63d574e commit 2576c5d
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions snowpack/src/build/build-pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import path from 'path';
import {validatePluginLoadResult} from '../config';
import {logger} from '../logger';
import {SnowpackBuildMap, SnowpackConfig, SnowpackPlugin, PluginTransformResult} from '../types/snowpack';
import {
SnowpackBuildMap,
SnowpackConfig,
SnowpackPlugin,
PluginTransformResult,
} from '../types/snowpack';
import {getExt, readFile, replaceExt} from '../util';
import {SourceMapConsumer, SourceMapGenerator, RawSourceMap} from 'source-map';

Expand Down Expand Up @@ -84,6 +89,9 @@ async function runPipelineLoadStep(

// if source maps disabled, don’t return any
if (!sourceMaps) result[ext].map = undefined;

// clean up empty files
if (!result[ext].code) delete result[ext];
});
return result;
}
Expand All @@ -101,10 +109,14 @@ async function runPipelineLoadStep(
};
}

async function composeSourceMaps(id: string, base: string | RawSourceMap, derived: string | RawSourceMap) : Promise<string> {
async function composeSourceMaps(
id: string,
base: string | RawSourceMap,
derived: string | RawSourceMap,
): Promise<string> {
const [baseMap, transformedMap] = await Promise.all([
new SourceMapConsumer(base),
new SourceMapConsumer(derived)
new SourceMapConsumer(derived),
]);
try {
const generator = SourceMapGenerator.fromSourceMap(transformedMap);
Expand Down Expand Up @@ -159,22 +171,31 @@ async function runPipelineTransformStep(
// V2 API, simple string variant
output[destExt].code = result;
output[destExt].map = undefined;
} else if (result && typeof result === 'object' && (result as PluginTransformResult).contents) {
} else if (
result &&
typeof result === 'object' &&
(result as PluginTransformResult).contents
) {
// V2 API, structured result variant
output[destExt].code = (result as PluginTransformResult).contents;
const map = (result as PluginTransformResult).map;
let outputMap: string | undefined = undefined;
if (map && sourceMaps) { // if source maps disabled, don’t return any
if (map && sourceMaps) {
// if source maps disabled, don’t return any
if (output[destExt].map) {
outputMap = await composeSourceMaps(filePath, output[destExt].map!, map);
} else {
outputMap = typeof map === 'object' ? JSON.stringify(map) : map;
}
}
output[destExt].map = outputMap;
} else if (result && typeof result === 'object' && (result as unknown as {result: string}).result) {
} else if (
result &&
typeof result === 'object' &&
((result as unknown) as {result: string}).result
) {
// V1 API, deprecated
output[destExt].code = (result as unknown as {result: string}).result;
output[destExt].code = ((result as unknown) as {result: string}).result;
output[destExt].map = undefined;
}
}
Expand Down

0 comments on commit 2576c5d

Please sign in to comment.