-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use swc for import component transpilation (in case of esbuild)
- Loading branch information
Showing
7 changed files
with
238 additions
and
12 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
packages/react/import-component/import-component-loader.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
function createAstHelper(span = { start: 0, end: 0, ctxt: 0 }) { | ||
const ast = { | ||
IdentifierOrExpression: (subject) => { | ||
if (typeof subject === 'string') { | ||
return { | ||
type: 'Identifier', | ||
span, | ||
value: subject, | ||
optional: false, | ||
}; | ||
} else { | ||
return subject; | ||
} | ||
}, | ||
ObjectExpression: (properties) => { | ||
return { | ||
type: 'ObjectExpression', | ||
span, | ||
properties: Object.entries(properties).map(([key, value]) => { | ||
return { | ||
type: 'KeyValueProperty', | ||
key: { | ||
type: 'Identifier', | ||
span, | ||
value: key, | ||
optional: false, | ||
}, | ||
value, | ||
}; | ||
}), | ||
}; | ||
}, | ||
ArrowFunctionExpression: (body) => { | ||
return { | ||
type: 'ArrowFunctionExpression', | ||
span, | ||
params: [], | ||
body, | ||
}; | ||
}, | ||
CallExpression: (callee, ...args) => { | ||
return { | ||
type: 'CallExpression', | ||
span, | ||
callee: ast.IdentifierOrExpression(callee), | ||
arguments: args.map((expression) => { | ||
return { | ||
spread: null, | ||
expression, | ||
}; | ||
}), | ||
typeArguments: null, | ||
}; | ||
}, | ||
MemberExpression: (object, property) => { | ||
return { | ||
type: 'MemberExpression', | ||
span, | ||
object: ast.IdentifierOrExpression(object), | ||
property: ast.IdentifierOrExpression(property), | ||
computed: false, | ||
}; | ||
}, | ||
}; | ||
|
||
return ast; | ||
} | ||
|
||
module.exports = { | ||
createAstHelper, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { transformSync } = require('@swc/core'); | ||
const { ImportComponentTransformer } = require('./swc-transformer'); | ||
|
||
function importComponentLoader(source, inputSourceMap) { | ||
if (!/[^\w]importComponent\s*\(/.test(source)) return source; | ||
|
||
const { resourcePath: sourceFileName } = this; | ||
const ts = /\.tsx?$/.test(sourceFileName); | ||
const tsx = /\.tsx$/.test(sourceFileName); | ||
const parser = ts | ||
? { syntax: 'typescript', tsx } | ||
: { syntax: 'ecmascript', jsx: true }; | ||
|
||
const { code } = transformSync(source, { | ||
sourceFileName, | ||
inputSourceMap: inputSourceMap && JSON.stringify(inputSourceMap), | ||
jsc: { | ||
target: 'es2022', | ||
parser, | ||
}, | ||
plugin: (m) => new ImportComponentTransformer().visitProgram(m), | ||
}); | ||
|
||
return code; | ||
} | ||
|
||
module.exports = importComponentLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { default: Visitor } = require('@swc/core/Visitor'); | ||
const { createAstHelper } = require('./swc-ast-helper'); | ||
|
||
class ImportComponentTransformer extends Visitor { | ||
visitCallExpression(expression) { | ||
if ( | ||
expression.callee.type !== 'Identifier' || | ||
expression.callee.value !== 'importComponent' | ||
) { | ||
return expression; | ||
} | ||
|
||
if ( | ||
!Array.isArray(expression.arguments) || | ||
expression.arguments.length < 1 | ||
) { | ||
throw new Error( | ||
'"importComponent" must be called with at least one parameter!' | ||
); | ||
} | ||
|
||
const argument = expression.arguments[0].expression; | ||
|
||
if (argument.type !== 'ArrowFunctionExpression') { | ||
return expression; | ||
} | ||
|
||
const ast = createAstHelper(expression.span); | ||
const importValueExpression = argument.body.arguments[0].expression; | ||
|
||
return ast.CallExpression( | ||
'importComponent', | ||
ast.ObjectExpression({ | ||
load: ast.ArrowFunctionExpression( | ||
ast.CallExpression('import', importValueExpression) | ||
), | ||
moduleId: ast.CallExpression( | ||
ast.MemberExpression('require', 'resolveWeak'), | ||
importValueExpression | ||
), | ||
}) | ||
); | ||
} | ||
} | ||
|
||
module.exports = { ImportComponentTransformer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters