Skip to content

Commit 2f41e44

Browse files
author
Daniel Del Core
committed
Initial refactors for the worker file
1 parent 9415f5d commit 2f41e44

File tree

1 file changed

+16
-42
lines changed

1 file changed

+16
-42
lines changed

packages/core/lib/Worker.js

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ if (module.parent) {
4141
}
4242

4343
function prepareJscodeshift(options) {
44-
const parser =
45-
parserFromTransform || getParser(options.parser, options.parserConfig);
46-
return jscodeshift.withParser(parser);
44+
return jscodeshift.withParser(
45+
parserFromTransform || getParser(options.parser, options.parserConfig),
46+
);
4747
}
4848

4949
function retrieveTransformId(str) {
5050
if (str.includes('#')) return false;
5151
return (str.match(/[^@]*(?:[@](?!.*[@]))(.*)$/) || [, ''])[1];
5252
}
53+
5354
function retrievePresetId(str) {
5455
return (str.match(/[^#]*(?:[#](?!.*[#]))(.*)$/) || [, ''])[1];
5556
}
@@ -59,59 +60,34 @@ function retrievePath(str) {
5960
}
6061

6162
function getModule(mod) {
62-
return mod.hasOwnProperty('default') ? mod.default : mod;
63+
return Boolean(mod.default) ? mod.default : mod;
6364
}
6465

65-
function setup(entryPath, babel) {
66-
if (babel === 'babel') {
67-
const presets = [];
68-
if (presetEnv) {
69-
presets.push([presetEnv.default, { targets: { node: true } }]);
70-
}
71-
72-
presets.push(require('@babel/preset-typescript').default);
73-
74-
require('@babel/register')({
75-
configFile: false,
76-
babelrc: false,
77-
presets,
78-
plugins: [
79-
require('@babel/plugin-proposal-class-properties').default,
80-
require('@babel/plugin-proposal-nullish-coalescing-operator').default,
81-
require('@babel/plugin-proposal-optional-chaining').default,
82-
require('@babel/plugin-transform-modules-commonjs').default,
83-
],
84-
extensions: [...DEFAULT_EXTENSIONS, '.ts', '.tsx'],
85-
// By default, babel register only compiles things inside the current working directory.
86-
// https://github.com/babel/babel/blob/2a4f16236656178e84b05b8915aab9261c55782c/packages/babel-register/src/node.js#L140-L157
87-
ignore: [
88-
// Ignore parser related files
89-
/@babel\/parser/,
90-
/\/flow-parser\//,
91-
/\/recast\//,
92-
/\/ast-types\//,
93-
],
94-
});
95-
}
66+
async function getModuleName(path) {
67+
const moduleName = retrievePath(path).split('node_modules/')[1];
68+
const pkg = await import(moduleName);
69+
return getModule(pkg);
70+
}
9671

72+
async function setup(entryPath) {
9773
const transformId = retrieveTransformId(entryPath);
9874
const presetId = retrievePresetId(entryPath);
9975

10076
let transformPkg;
10177
let transformModule;
10278

10379
if (transformId) {
104-
transformPkg = getModule(require(path.resolve(retrievePath(entryPath))));
80+
transformPkg = await getModuleName(entryPath);
10581
transformModule = transformPkg.transforms[transformId];
10682
}
10783

10884
if (presetId) {
109-
transformPkg = getModule(require(path.resolve(retrievePath(entryPath))));
85+
transformPkg = await getModuleName(entryPath);
11086
transformModule = transformPkg.presets[presetId];
11187
}
11288

11389
if (!transformId && !presetId) {
114-
transformModule = require(path.resolve(entryPath));
90+
transformModule = await import(path.resolve(entryPath));
11591
}
11692

11793
transform = getModule(transformModule);
@@ -171,10 +147,7 @@ function run(data) {
171147
try {
172148
const jscodeshift = prepareJscodeshift(options);
173149
const out = await transform(
174-
{
175-
path: file,
176-
source: source,
177-
},
150+
{ path: file, source: source },
178151
{
179152
j: jscodeshift,
180153
jscodeshift: jscodeshift,
@@ -184,6 +157,7 @@ function run(data) {
184157
},
185158
options,
186159
);
160+
187161
if (!out || out === source) {
188162
updateStatus(out ? 'nochange' : 'skip', file);
189163
callback();

0 commit comments

Comments
 (0)