-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-server] Allow whitelisted imports in babel (PostHog/plugin-se…
…rver#284) * Allow whitelisted imports in babel * add test for fetching via `import` * Reword whitelisted → provided * also support "require" and handle defaults nodejs style * support requires * fix test Co-authored-by: Michael Matloka <[email protected]>
- Loading branch information
1 parent
ba495ee
commit c2de8d5
Showing
6 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
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
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,73 @@ | ||
import { PluginsServer } from '../../../types' | ||
import { PluginGen } from './common' | ||
|
||
export const replaceImports: PluginGen = (server: PluginsServer, imports: Record<string, any> = {}) => ({ | ||
types: t, | ||
}) => ({ | ||
visitor: { | ||
ImportDeclaration: { | ||
exit(path: any) { | ||
const { node } = path | ||
const importSource = node.source.value | ||
const importedVars = new Map<string, string>() | ||
|
||
if (typeof imports[importSource] === 'undefined') { | ||
throw new Error( | ||
`Cannot import '${importSource}'! This package is not provided by PostHog in plugins.` | ||
) | ||
} | ||
|
||
for (const specifier of node.specifiers) { | ||
if (t.isImportSpecifier(specifier)) { | ||
if (t.isStringLiteral(specifier.imported)) { | ||
importedVars.set(specifier.local.name, specifier.imported.value) | ||
} else { | ||
importedVars.set(specifier.local.name, specifier.imported.name) | ||
} | ||
} else if (t.isImportDefaultSpecifier(specifier)) { | ||
importedVars.set(specifier.local.name, 'default') | ||
} else if (t.isImportNamespaceSpecifier(specifier)) { | ||
importedVars.set(specifier.local.name, 'default') | ||
} | ||
} | ||
|
||
path.replaceWith( | ||
t.variableDeclaration( | ||
'const', | ||
Array.from(importedVars.entries()).map(([varName, sourceName]) => { | ||
const importExpression = t.memberExpression( | ||
t.identifier('__pluginHostImports'), | ||
t.stringLiteral(importSource), | ||
true | ||
) | ||
return t.variableDeclarator( | ||
t.identifier(varName), | ||
sourceName === 'default' | ||
? importExpression | ||
: t.memberExpression(importExpression, t.stringLiteral(sourceName), true) | ||
) | ||
}) | ||
) | ||
) | ||
}, | ||
}, | ||
CallExpression: { | ||
exit(path: any) { | ||
const { node } = path | ||
if (t.isIdentifier(node.callee) && node.callee.name === 'require' && node.arguments.length === 1) { | ||
const importSource = node.arguments[0].value | ||
|
||
if (typeof imports[importSource] === 'undefined') { | ||
throw new Error( | ||
`Cannot import '${importSource}'! This package is not provided by PostHog in plugins.` | ||
) | ||
} | ||
|
||
path.replaceWith( | ||
t.memberExpression(t.identifier('__pluginHostImports'), t.stringLiteral(importSource), true) | ||
) | ||
} | ||
}, | ||
}, | ||
}, | ||
}) |
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
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