Skip to content

Commit

Permalink
Split out V2 and V1 result handling, and tighten up types.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkaminski committed Sep 26, 2020
1 parent ac0aac3 commit e1cef60
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
14 changes: 10 additions & 4 deletions snowpack/src/build/build-pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import {validatePluginLoadResult} from '../config';
import {logger} from '../logger';
import {SnowpackBuildMap, SnowpackConfig, SnowpackPlugin} 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 @@ -156,11 +156,13 @@ async function runPipelineTransformStep(
});
logger.debug(`✔ transform() success [${debugPath}]`, {name: step.name});
if (typeof result === 'string' || Buffer.isBuffer(result)) {
// V2 API, simple string variant
output[destExt].code = result;
output[destExt].map = undefined;
} else if (result && typeof result === 'object' && (result as {result: string}).result) {
output[destExt].code = (result as {result: string}).result;
let map = (result as {map: string | RawSourceMap}).map;
} 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 (output[destExt].map) {
Expand All @@ -170,6 +172,10 @@ async function runPipelineTransformStep(
}
}
output[destExt].map = outputMap;
} 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].map = undefined;
}
}
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ function validatePlugin(plugin: SnowpackPlugin) {

export function validatePluginLoadResult(
plugin: SnowpackPlugin,
result: PluginLoadResult | void | undefined | null,
result: PluginLoadResult | string | void | undefined | null,
) {
const pluginName = plugin.name;
if (!result) {
Expand Down
9 changes: 6 additions & 3 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type HttpProxy from 'http-proxy';
import type * as http from 'http';
import type {InstallOptions} from 'esinstall';
import type {RawSourceMap} from 'source-map';

export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
Expand Down Expand Up @@ -51,7 +52,9 @@ export interface PluginRunOptions {
}

/** map of extensions -> code (e.g. { ".js": "[code]", ".css": "[code]" }) */
export type PluginLoadResult = string | SnowpackBuildMap;
export type PluginLoadResult = SnowpackBuildMap;

export type PluginTransformResult = {contents: string, map: string | RawSourceMap};

export interface PluginOptimizeOptions {
buildDirectory: string;
Expand All @@ -73,9 +76,9 @@ export interface SnowpackPlugin {
output: string[];
};
/** load a file that matches resolve.input */
load?(options: PluginLoadOptions): Promise<PluginLoadResult | null | undefined | void>;
load?(options: PluginLoadOptions): Promise<PluginLoadResult | string | null | undefined | void>;
/** transform a file that matches resolve.input */
transform?(options: PluginTransformOptions): Promise<string | null | undefined | void>;
transform?(options: PluginTransformOptions): Promise<PluginTransformResult | string | null | undefined | void>;
/** runs a command, unrelated to file building (e.g. TypeScript, ESLint) */
run?(options: PluginRunOptions): Promise<unknown>;
/** optimize the entire built application */
Expand Down
2 changes: 1 addition & 1 deletion test/build/transform-sourcemap/custom-transform-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function () {
ms.appendLeft(contents.indexOf('console.log'), `console.log('transformed');\n`);
const map = ms.generateMap({source: id, hires: false, includeContent: true});
return {
result: ms.toString(),
contents: ms.toString(),
// Try returning both object and string map formats.
map: fileExt === '.js' ? map : map.toString()
}
Expand Down

0 comments on commit e1cef60

Please sign in to comment.