@chialab/estransform
Advanced tools
Comparing version 0.9.3 to 0.9.6
@@ -5,2 +5,5 @@ import { promises } from 'fs'; | ||
import esbuild from 'esbuild'; | ||
import { Parser as AcornParser } from 'acorn'; | ||
import jsx from 'acorn-jsx'; | ||
import { simple as walk } from 'acorn-walk'; | ||
@@ -10,3 +13,38 @@ const { readFile } = promises; | ||
const Parser = AcornParser.extend(/** @type {*} */ (jsx())); | ||
export { walk }; | ||
/** | ||
* @param {string} contents | ||
*/ | ||
export function parse(contents) { | ||
return Parser.parse(contents, { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
locations: true, | ||
}); | ||
} | ||
/** | ||
* @param {string} code | ||
* @param {number} line | ||
* @param {number} column | ||
*/ | ||
export function getOffsetFromLocation(code, line, column) { | ||
let offest = 0; | ||
const lines = code.split('\n'); | ||
for (let i = 0; i < line; i++) { | ||
if (i === line - 1) { | ||
offest += column; | ||
return offest; | ||
} | ||
offest += lines[i].length + 1; | ||
} | ||
return -1; | ||
} | ||
/** | ||
* @typedef {Object} SourceMap | ||
@@ -107,4 +145,4 @@ * @property {number} [version] | ||
* @typedef {Object} TransformResult | ||
* @property {string} code | ||
* @property {SourceMap|null} map | ||
* @property {string} [code] | ||
* @property {SourceMap|null} [map] | ||
* @property {import('esbuild').Loader} [loader] | ||
@@ -115,3 +153,3 @@ * @property {string} [target] | ||
/** | ||
* @typedef {(magicCode: MagicString, code: string, options: TransformOptions) => Promise<TransformResult|void>|TransformResult|void} TransformCallack | ||
* @typedef {(data: { ast: acorn.Node, magicCode: MagicString, code: string }, options: TransformOptions) => Promise<TransformResult|void>|TransformResult|void} TransformCallack | ||
*/ | ||
@@ -126,6 +164,27 @@ | ||
export async function transform(contents, options, callback) { | ||
const magicCode = new MagicString(contents); | ||
return await callback(magicCode, contents, options) || { | ||
code: magicCode.toString(), | ||
map: options.sourcemap ? parseSourcemap( | ||
/** | ||
* @type {MagicString|undefined} | ||
*/ | ||
let magicCode; | ||
/** | ||
* @type {acorn.Node|undefined} | ||
*/ | ||
let ast; | ||
return await callback({ | ||
code: contents, | ||
get ast() { | ||
if (!ast) { | ||
ast = parse(contents); | ||
} | ||
return ast; | ||
}, | ||
get magicCode() { | ||
if (!magicCode) { | ||
magicCode = new MagicString(contents); | ||
} | ||
return magicCode; | ||
}, | ||
}, options) || { | ||
code: magicCode ? magicCode.toString() : undefined, | ||
map: options.sourcemap && magicCode ? parseSourcemap( | ||
magicCode.generateMap({ | ||
@@ -136,3 +195,3 @@ hires: true, | ||
}).toString() | ||
) : null, | ||
) : undefined, | ||
}; | ||
@@ -159,4 +218,4 @@ } | ||
export function createTypeScriptTransform(config = {}) { | ||
return async function transpileTypescript(magicCode, contents, options) { | ||
const { code, map } = await esbuild.transform(contents, { | ||
return async function transpileTypescript({ code }, options) { | ||
const { code: finalCode, map } = await esbuild.transform(code, { | ||
tsconfigRaw: {}, | ||
@@ -173,3 +232,3 @@ sourcemap: true, | ||
return { | ||
code, | ||
code: finalCode, | ||
map: parseSourcemap(map), | ||
@@ -233,5 +292,7 @@ target: TARGETS.es2020, | ||
pipeline.code = code; | ||
if (pipeline.sourceMaps && map && code !== pipeline.code) { | ||
pipeline.sourceMaps.push(map); | ||
if (code) { | ||
pipeline.code = code; | ||
if (pipeline.sourceMaps && map && code !== pipeline.code) { | ||
pipeline.sourceMaps.push(map); | ||
} | ||
} | ||
@@ -238,0 +299,0 @@ |
{ | ||
"name": "@chialab/estransform", | ||
"type": "module", | ||
"version": "0.9.3", | ||
"version": "0.9.6", | ||
"description": "Execute multiple transformations on JavaScript sources with full sourcemaps support.", | ||
@@ -37,2 +37,5 @@ "main": "lib/index.js", | ||
"@parcel/source-map": "^2.0.0-rc.4", | ||
"acorn": "^8.4.1", | ||
"acorn-jsx": "^5.3.2", | ||
"acorn-walk": "^8.1.1", | ||
"esbuild": "^0.12.15", | ||
@@ -44,3 +47,3 @@ "magic-string": "^0.25.7" | ||
}, | ||
"gitHead": "e8a7a1b5c22f75c14a2df8a5121240839f7bdac3" | ||
"gitHead": "0b11811e7def6b64c6870dd8e099e64e1f151444" | ||
} |
/** | ||
* @param {string} contents | ||
*/ | ||
export function parse(contents: string): import("acorn").Node; | ||
/** | ||
* @param {string} code | ||
* @param {number} line | ||
* @param {number} column | ||
*/ | ||
export function getOffsetFromLocation(code: string, line: number, column: number): number; | ||
/** | ||
* @typedef {Object} SourceMap | ||
@@ -41,4 +51,4 @@ * @property {number} [version] | ||
* @typedef {Object} TransformResult | ||
* @property {string} code | ||
* @property {SourceMap|null} map | ||
* @property {string} [code] | ||
* @property {SourceMap|null} [map] | ||
* @property {import('esbuild').Loader} [loader] | ||
@@ -48,3 +58,3 @@ * @property {string} [target] | ||
/** | ||
* @typedef {(magicCode: MagicString, code: string, options: TransformOptions) => Promise<TransformResult|void>|TransformResult|void} TransformCallack | ||
* @typedef {(data: { ast: acorn.Node, magicCode: MagicString, code: string }, options: TransformOptions) => Promise<TransformResult|void>|TransformResult|void} TransformCallack | ||
*/ | ||
@@ -97,2 +107,3 @@ /** | ||
export function finalize(pipeline: Pipeline, { source, sourcemap, sourcesContent }: TransformOptions): Promise<TransformResult>; | ||
export { walk }; | ||
export namespace TARGETS { | ||
@@ -133,8 +144,12 @@ const unknown: string; | ||
export type TransformResult = { | ||
code: string; | ||
map: SourceMap | null; | ||
code?: string | undefined; | ||
map?: SourceMap | null | undefined; | ||
loader?: esbuild.Loader | undefined; | ||
target?: string | undefined; | ||
}; | ||
export type TransformCallack = (magicCode: MagicString, code: string, options: TransformOptions) => Promise<TransformResult | void> | TransformResult | void; | ||
export type TransformCallack = (data: { | ||
ast: acorn.Node; | ||
magicCode: MagicString; | ||
code: string; | ||
}, options: TransformOptions) => Promise<TransformResult | void> | TransformResult | void; | ||
export type Pipeline = { | ||
@@ -148,2 +163,3 @@ contents: string; | ||
import esbuild from "esbuild"; | ||
import { simple as walk } from "acorn-walk"; | ||
import MagicString from "magic-string"; |
16789
470
6
18340
+ Addedacorn@^8.4.1
+ Addedacorn-jsx@^5.3.2
+ Addedacorn-walk@^8.1.1
+ Addedacorn@8.14.0(transitive)
+ Addedacorn-jsx@5.3.2(transitive)
+ Addedacorn-walk@8.3.4(transitive)