Skip to content

Commit 39b853f

Browse files
committed
Support async babel plugins
This allows people to use async babel plugins and babel plugins authored as ES modules. It's a relatively small change since this package was already async in the right areas, it was just calling the synchronous babel APIs when it could use the async equivalent.
1 parent e287816 commit 39b853f

File tree

4 files changed

+70
-37
lines changed

4 files changed

+70
-37
lines changed

lib/parallel-api.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,35 +236,36 @@ function serialize(options) {
236236
return serialized;
237237
}
238238

239-
function transformString(string, options, buildOptions) {
240-
const isParallelizable = transformIsParallelizable(options.babel).isParallelizable;
239+
async function transformString(string, options, buildOptions) {
240+
const isParallelizable = transformIsParallelizable(
241+
options.babel
242+
).isParallelizable;
241243
if (JOBS > 1 && isParallelizable) {
242244
let pool = getWorkerPool();
243-
_logger.info('transformString is parallelizable');
244-
let serializedObj = { babel : serialize(options.babel), 'cacheKey': options.cacheKey };
245-
return pool
246-
.exec('transform', [string, serializedObj])
247-
.then((result) => {
248-
if (result.error) {
249-
// when the worker has an error it still resolves, but it has `error`
250-
// and `stack` properties instead of `code` + `metadata`
251-
//
252-
// throw an error to properly fail the "top level" process as needed
253-
throw new Error(result.error + result.stack);
254-
}
255-
256-
return result;
257-
});
245+
_logger.info("transformString is parallelizable");
246+
let serializedObj = {
247+
babel: serialize(options.babel),
248+
cacheKey: options.cacheKey,
249+
};
250+
let result = await pool.exec("transform", [string, serializedObj]);
251+
252+
if (result.error) {
253+
// when the worker has an error it still resolves, but it has `error`
254+
// and `stack` properties instead of `code` + `metadata`
255+
//
256+
// throw an error to properly fail the "top level" process as needed
257+
throw new Error(result.error + result.stack);
258+
}
259+
260+
return result;
258261
} else {
259262
if (JOBS <= 1) {
260-
_logger.info('JOBS <= 1, skipping worker, using main thread');
263+
_logger.info("JOBS <= 1, skipping worker, using main thread");
261264
} else {
262-
_logger.info('transformString is NOT parallelizable');
265+
_logger.info("transformString is NOT parallelizable");
263266
}
264267

265-
return new Promise(resolve => {
266-
resolve(transpiler.transform(string, deserialize(options)));
267-
});
268+
return await transpiler.transformAsync(string, deserialize(options));
268269
}
269270
}
270271

lib/worker.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ const Promise = require('rsvp').Promise;
66
const ParallelApi = require('./parallel-api');
77

88
// transpile the input string, using the input options
9-
function transform(string, options) {
10-
return Promise.resolve().then(() => {
11-
try {
12-
let result = transpiler.transform(string, ParallelApi.deserialize(options));
9+
async function transform(string, options) {
10+
try {
11+
let result = await transpiler.transformAsync(
12+
string,
13+
ParallelApi.deserialize(options)
14+
);
1315

14-
return {
15-
code: result.code,
16-
metadata: result.metadata
17-
};
18-
} catch (error) {
19-
return {
20-
error: error.message,
21-
stack: error.stack,
22-
};
23-
}
24-
});
16+
return {
17+
code: result.code,
18+
metadata: result.metadata,
19+
};
20+
} catch (error) {
21+
return {
22+
error: error.message,
23+
stack: error.stack,
24+
};
25+
}
2526
}
2627

2728
// create worker and register public functions

tests/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,27 @@ describe('transpile ES6 to ES5', function() {
309309
});
310310
});
311311

312+
it('supports async babel plugins (non-parallel implementation)', function () {
313+
return babel('files', {
314+
babel: {
315+
inputSourceMap: false,
316+
sourceMap: false,
317+
plugins: [
318+
'@babel/transform-strict-mode',
319+
'@babel/transform-block-scoping',
320+
async function() { return { visitor: {} }}
321+
]
322+
}
323+
}).then(results => {
324+
let outputPath = results.directory;
325+
326+
let output = fs.readFileSync(path.join(outputPath, 'fixtures.js'), 'utf8');
327+
let input = fs.readFileSync(path.join(expectations, 'expected.js'), 'utf8');
328+
329+
expect(output).to.eql(input);
330+
});
331+
});
332+
312333
it('basic - parallel API', function () {
313334
return babel('files', {
314335
babel: {
@@ -1577,4 +1598,11 @@ describe('workerpool', function() {
15771598
}
15781599
});
15791600
});
1601+
1602+
it("supports async babel plugins (parallel implementation)", async function () {
1603+
// a plugin whose factory is async is enough to exercise the behavior. Babel
1604+
// will throw if you use `transform` as opposed to `transformAsync`.
1605+
options.babel.plugins.push(require.resolve('./utils/parallel-plugin'));
1606+
await ParallelApi.transformString(stringToTransform, options)
1607+
});
15801608
});

tests/utils/parallel-plugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = async function() {
2+
return { visitor: {} }
3+
}

0 commit comments

Comments
 (0)