From 8d689a76d854e444e620e76c3158f8ee0f5cc5ba Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Wed, 31 Jul 2019 12:04:10 -0500 Subject: [PATCH] Parcel 2: CoffeeScript sourcemaps (#3282) * Add sourcemaps to CoffeeScript transformer * update sourcemap to use rawsourcemap output --- .../transformers/coffeescript/package.json | 2 ++ .../src/CoffeeScriptTransformer.js | 25 ++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/transformers/coffeescript/package.json b/packages/transformers/coffeescript/package.json index ab8bd38452b..465aed2a380 100644 --- a/packages/transformers/coffeescript/package.json +++ b/packages/transformers/coffeescript/package.json @@ -13,6 +13,8 @@ }, "dependencies": { "@parcel/plugin": "^2.0.0-alpha.0", + "@parcel/source-map": "^2.0.0-alpha.0", + "@parcel/utils": "^2.0.0-alpha.0", "coffeescript": "^2.0.3", "nullthrows": "^1.1.1", "semver": "^5.4.1" diff --git a/packages/transformers/coffeescript/src/CoffeeScriptTransformer.js b/packages/transformers/coffeescript/src/CoffeeScriptTransformer.js index 75a3786d99e..5156aac6edf 100644 --- a/packages/transformers/coffeescript/src/CoffeeScriptTransformer.js +++ b/packages/transformers/coffeescript/src/CoffeeScriptTransformer.js @@ -1,11 +1,30 @@ +// @flow import {Transformer} from '@parcel/plugin'; +import SourceMap from '@parcel/source-map'; import coffee from 'coffeescript'; +import {relativeUrl} from '@parcel/utils'; export default new Transformer({ - async transform({asset}) { + async transform({asset, options}) { + let sourceFileName: string = relativeUrl( + options.projectRoot, + asset.filePath + ); + asset.type = 'js'; - let output = coffee.compile(await asset.getCode()); - asset.setCode(output); + let output = coffee.compile(await asset.getCode(), { + filename: sourceFileName, + sourceMap: options.sourceMaps + }); + + // return from compile is based on sourceMaps option + if (options.sourceMaps) { + asset.setCode(output.js); + asset.setMap(SourceMap.fromRawSourceMap(output.v3SourceMap)); + } else { + asset.setCode(output); + } + return [asset]; } });