From 4c3fd64b0f560fc53ef5c1279c0f5d0d3ed4dbb3 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Thu, 24 Sep 2020 15:54:47 -0400 Subject: [PATCH 1/3] saving a helpful debug utility I have recreated this a couple times locally and I think it's worth keeping around. --- packages/core/tests/babel-debug-util.ts | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/core/tests/babel-debug-util.ts diff --git a/packages/core/tests/babel-debug-util.ts b/packages/core/tests/babel-debug-util.ts new file mode 100644 index 000000000..a6eb7ed4f --- /dev/null +++ b/packages/core/tests/babel-debug-util.ts @@ -0,0 +1,36 @@ +import { transform, TransformOptions } from '@babel/core'; +import { readJSONSync, readFileSync } from 'fs-extra'; +import { join } from 'path'; +import { argv } from 'process'; + +async function run(appDir: string, fileLocalPath: string) { + let pkg = readJSONSync(join(appDir, 'package.json')); + if (!pkg['ember-addon'].babel.isParallelSafe) { + throw new Error( + `can't use this babel config, it's not parallel safe so we can't load it outside its original prorcess` + ); + } + let config = (await import(join(appDir, pkg['ember-addon'].babel.filename))).default; + let filename = join(appDir, fileLocalPath); + let src = readFileSync(filename, 'utf8'); + process.stdout.write(transform(src, Object.assign({ filename }, config) as TransformOptions)!.code!); +} + +if (argv.length < 4) { + console.log( + ` + Usage: + node babel-debug-util.js [pathToAppOutputDir] [localPathToFile] + + Given an app that has been prepared by Embroider (the stage2 output) + and the local path to a JS file within that app, run the app's babel + config on that file and print the results. + ` + ); + process.exit(-1); +} + +run(process.argv[2], process.argv[3]).catch(err => { + console.log(err); + process.exit(-1); +}); From ea1248944455fbe234ec1c8d2d4708242a212c5c Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Thu, 24 Sep 2020 17:16:21 -0400 Subject: [PATCH 2/3] make it usable as a library too --- packages/core/tests/babel-debug-util.ts | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/core/tests/babel-debug-util.ts b/packages/core/tests/babel-debug-util.ts index a6eb7ed4f..77a363a7f 100644 --- a/packages/core/tests/babel-debug-util.ts +++ b/packages/core/tests/babel-debug-util.ts @@ -3,7 +3,7 @@ import { readJSONSync, readFileSync } from 'fs-extra'; import { join } from 'path'; import { argv } from 'process'; -async function run(appDir: string, fileLocalPath: string) { +export async function transpile(appDir: string, fileLocalPath: string): Promise { let pkg = readJSONSync(join(appDir, 'package.json')); if (!pkg['ember-addon'].babel.isParallelSafe) { throw new Error( @@ -13,12 +13,13 @@ async function run(appDir: string, fileLocalPath: string) { let config = (await import(join(appDir, pkg['ember-addon'].babel.filename))).default; let filename = join(appDir, fileLocalPath); let src = readFileSync(filename, 'utf8'); - process.stdout.write(transform(src, Object.assign({ filename }, config) as TransformOptions)!.code!); + return transform(src, Object.assign({ filename }, config) as TransformOptions)!.code!; } -if (argv.length < 4) { - console.log( - ` +if (require.main === module) { + if (argv.length < 4) { + console.log( + ` Usage: node babel-debug-util.js [pathToAppOutputDir] [localPathToFile] @@ -26,11 +27,16 @@ if (argv.length < 4) { and the local path to a JS file within that app, run the app's babel config on that file and print the results. ` - ); - process.exit(-1); -} + ); + process.exit(-1); + } -run(process.argv[2], process.argv[3]).catch(err => { - console.log(err); - process.exit(-1); -}); + transpile(process.argv[2], process.argv[3]) + .then(src => { + console.log(src); + }) + .catch(err => { + console.log(err); + process.exit(-1); + }); +} From a041f4c36ed604674f924d348808f537c46809e2 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Thu, 24 Sep 2020 17:20:10 -0400 Subject: [PATCH 3/3] allow use inside original process leave the error up to the underlying methods --- packages/core/tests/babel-debug-util.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/core/tests/babel-debug-util.ts b/packages/core/tests/babel-debug-util.ts index 77a363a7f..65b30e32a 100644 --- a/packages/core/tests/babel-debug-util.ts +++ b/packages/core/tests/babel-debug-util.ts @@ -5,11 +5,6 @@ import { argv } from 'process'; export async function transpile(appDir: string, fileLocalPath: string): Promise { let pkg = readJSONSync(join(appDir, 'package.json')); - if (!pkg['ember-addon'].babel.isParallelSafe) { - throw new Error( - `can't use this babel config, it's not parallel safe so we can't load it outside its original prorcess` - ); - } let config = (await import(join(appDir, pkg['ember-addon'].babel.filename))).default; let filename = join(appDir, fileLocalPath); let src = readFileSync(filename, 'utf8');