-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
241 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`babel macro macros many: many 1`] = ` | ||
" | ||
import {imported, useImported} from \\"../macro\\"; | ||
const v = imported(() => import('./a')); | ||
const x = () => useImported(() => import('./b')); | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
function _interopRequireWildcard(obj) { | ||
if (obj && obj.__esModule) { | ||
return obj; | ||
} else { | ||
var newObj = {}; | ||
if (obj != null) { | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = | ||
Object.defineProperty && Object.getOwnPropertyDescriptor | ||
? Object.getOwnPropertyDescriptor(obj, key) | ||
: {}; | ||
if (desc.get || desc.set) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
return newObj; | ||
} | ||
} | ||
var importedWrapper = function(marker, realImport) { | ||
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") { | ||
__deoptimization_sideEffect__(marker, realImport); | ||
} | ||
return realImport; | ||
}; | ||
import { imported, useImported } from \\"react-imported-component\\"; | ||
const v = imported(() => | ||
importedWrapper( | ||
\\"imported_-mg71kn_component\\", | ||
Promise.resolve().then(() => _interopRequireWildcard(require(\\"./a\\"))) | ||
) | ||
); | ||
const x = () => | ||
useImported(() => | ||
importedWrapper( | ||
\\"imported_15vaa6j_component\\", | ||
Promise.resolve().then(() => _interopRequireWildcard(require(\\"./b\\"))) | ||
) | ||
); | ||
" | ||
`; |
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,30 @@ | ||
const pluginTester = require("babel-plugin-tester"); | ||
const plugin = require("babel-plugin-macros"); | ||
const prettier = require("prettier"); | ||
|
||
describe('babel macro', () => { | ||
pluginTester({ | ||
plugin, | ||
snapshot: true, | ||
babelOptions: { | ||
filename: __filename, | ||
plugins: ['dynamic-import-node'], | ||
}, | ||
formatResult(result: string) { | ||
return prettier.format(result, {trailingComma: "es5"}); | ||
}, | ||
tests: { | ||
"nothing": "const a = 42;", | ||
"no usage": `import {lazy} from "../macro";`, | ||
"lazy": ` | ||
import {lazy} from "../macro"; | ||
const v = lazy(() => import('./a')); | ||
`, | ||
"many": ` | ||
import {imported, useImported} from "../macro"; | ||
const v = imported(() => import('./a')); | ||
const x = () => useImported(() => import('./b')); | ||
`, | ||
}, | ||
}); | ||
}); |
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,7 @@ | ||
{ | ||
"private": true, | ||
"main": "../dist/es5/macro.js", | ||
"jsnext:main": "../dist/es2015/macro.js", | ||
"module": "../dist/es2015/macro.js", | ||
"types": "../dist/es2015/macro.d.ts" | ||
} |
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
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,89 @@ | ||
// @ts-ignore | ||
import {createMacro} from "babel-plugin-macros"; | ||
|
||
import {createTransformer} from "./babel" | ||
|
||
function getMacroType(tagName: string) { | ||
switch (tagName) { | ||
case "imported": | ||
case "lazy": | ||
case "useImported": | ||
return true; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function macro({references, state, babel}: any) { | ||
const {types: t} = babel; | ||
|
||
const imports: string[] = []; | ||
const transformer = createTransformer(babel); | ||
|
||
Object | ||
.keys(references) | ||
.forEach((tagName: string) => { | ||
if (getMacroType(tagName)) { | ||
imports.push(tagName); | ||
const tags = references[tagName]; | ||
tags.forEach((tag: any) => { | ||
let expression = tag.parentPath; | ||
|
||
if (t.isCallExpression(expression)) { | ||
transformer.traverse(expression, state.file) | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
if (addReactImports(babel, state, imports)) { | ||
transformer.finish(state.file.path.node); | ||
} | ||
} | ||
|
||
function addReactImports(babel: any, state: any, imports: string[]) { | ||
if (!imports.length) return false; | ||
|
||
const {types: t} = babel; | ||
|
||
const importedImport = state.file.path.node.body.find( | ||
(importNode: any) => | ||
t.isImportDeclaration(importNode) && | ||
importNode.source.value === "react-imported-component" | ||
); | ||
|
||
// Handle adding the import or altering the existing import | ||
if (importedImport) { | ||
imports.forEach(name => { | ||
if ( | ||
!importedImport.specifiers.find( | ||
(specifier: any) => specifier.imported && specifier.imported.name === name | ||
) | ||
) { | ||
importedImport.specifiers.push( | ||
t.importSpecifier(t.identifier(name), t.identifier(name)) | ||
) | ||
} | ||
}) | ||
} else { | ||
state.file.path.node.body.unshift( | ||
t.importDeclaration( | ||
imports.map(name => | ||
t.importSpecifier(t.identifier(name), t.identifier(name)) | ||
), | ||
t.stringLiteral("react-imported-component") | ||
) | ||
) | ||
} | ||
|
||
return true; | ||
} | ||
|
||
const lazy: typeof import('./HOC').lazy = null as any; | ||
const imported: typeof import('./HOC').default = null as any; | ||
const useImported: typeof import('./useImported').useImported = null as any; | ||
|
||
export {lazy, imported, useImported}; | ||
|
||
export default createMacro(macro) |