From e1cef6019b9a601eaba7d491dc181082fa23a183 Mon Sep 17 00:00:00 2001 From: Piotr Kaminski Date: Wed, 16 Sep 2020 16:20:55 -0700 Subject: [PATCH] Split out V2 and V1 result handling, and tighten up types. --- snowpack/src/build/build-pipeline.ts | 14 ++++++++++---- snowpack/src/config.ts | 2 +- snowpack/src/types/snowpack.ts | 9 ++++++--- .../transform-sourcemap/custom-transform-plugin.js | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/snowpack/src/build/build-pipeline.ts b/snowpack/src/build/build-pipeline.ts index 54fcafd2c6..859d08ad3e 100644 --- a/snowpack/src/build/build-pipeline.ts +++ b/snowpack/src/build/build-pipeline.ts @@ -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'; @@ -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) { @@ -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) { diff --git a/snowpack/src/config.ts b/snowpack/src/config.ts index ad4cd9e76a..178cf1fe99 100644 --- a/snowpack/src/config.ts +++ b/snowpack/src/config.ts @@ -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) { diff --git a/snowpack/src/types/snowpack.ts b/snowpack/src/types/snowpack.ts index 547e8e5b0e..0f270d5e5d 100644 --- a/snowpack/src/types/snowpack.ts +++ b/snowpack/src/types/snowpack.ts @@ -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 = { [P in keyof T]?: T[P] extends Array @@ -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; @@ -73,9 +76,9 @@ export interface SnowpackPlugin { output: string[]; }; /** load a file that matches resolve.input */ - load?(options: PluginLoadOptions): Promise; + load?(options: PluginLoadOptions): Promise; /** transform a file that matches resolve.input */ - transform?(options: PluginTransformOptions): Promise; + transform?(options: PluginTransformOptions): Promise; /** runs a command, unrelated to file building (e.g. TypeScript, ESLint) */ run?(options: PluginRunOptions): Promise; /** optimize the entire built application */ diff --git a/test/build/transform-sourcemap/custom-transform-plugin.js b/test/build/transform-sourcemap/custom-transform-plugin.js index a51dd603e0..39360dfe07 100644 --- a/test/build/transform-sourcemap/custom-transform-plugin.js +++ b/test/build/transform-sourcemap/custom-transform-plugin.js @@ -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() }