Skip to content

Commit

Permalink
fix: macro should patch the whole file, fixes #142
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 19, 2019
1 parent 132b31e commit 6142a15
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -181,6 +181,10 @@ import {imported, lazy, useImported} from "react-imported-component/macro";
// notice - there is no default import here
```

### Indirect usage
Just importing `react-imported-component/macro` would enable babel transformation for the current file.
If you have `imported` definition in one file, and use it from another - just `import "react-imported-component/macro"` in that another file. See [#142](https://github.com/theKashey/react-imported-component/issues/142)

<a name="api"/>

# API
Expand Down
56 changes: 48 additions & 8 deletions __tests__/__snapshots__/macro.spec.ts.snap
Expand Up @@ -60,6 +60,54 @@ lazy(() =>
"
`;

exports[`babel macro macros flat import: flat import 1`] = `
"
import \\"../macro\\";
import('./a.js')
↓ ↓ ↓ ↓ ↓ ↓
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;
};
importedWrapper(
\\"imported_-1ko6oiq_component\\",
Promise.resolve().then(() => _interopRequireWildcard(require(\\"./a.js\\")))
);
"
`;

exports[`babel macro macros lazy: lazy 1`] = `
"
import {lazy} from \\"../macro\\";
Expand Down Expand Up @@ -177,14 +225,6 @@ import {lazy} from \\"../macro\\";
↓ ↓ ↓ ↓ ↓ ↓
var importedWrapper = function(marker, realImport) {
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
__deoptimization_sideEffect__(marker, realImport);
}
return realImport;
};
import { lazy } from \\"react-imported-component\\";
"
Expand Down
3 changes: 3 additions & 0 deletions __tests__/macro.spec.ts
Expand Up @@ -16,6 +16,9 @@ describe('babel macro', () => {
tests: {
"nothing": "const a = 42;",
"no usage": `import {lazy} from "../macro";`,
"flat import": `import "../macro";
import('./a.js')
`,
"boot": `
import {assignImportedComponents, lazy} from "../macro";
assignImportedComponents([() => import('./a')]);
Expand Down
16 changes: 7 additions & 9 deletions src/babel.ts
Expand Up @@ -47,7 +47,7 @@ export const createTransformer = ({types: t, template}: any) => {
const visitedNodes = new Map();

return {
traverse(programPath: any, file: any) {
traverse(programPath: any, fileName: string) {
programPath.traverse({
ImportDeclaration(path: any) {
const source = path.node.source.value;
Expand Down Expand Up @@ -80,14 +80,13 @@ export const createTransformer = ({types: t, template}: any) => {
return;
}

const localFile = file.opts.filename;
const newImport = parentPath.node;
const importName = parentPath.get('arguments')[0].node.value;

if (!importName) {
return;
}
const requiredFileHash = encipherImport(resolveImport(importName, localFile));
const requiredFileHash = encipherImport(resolveImport(importName, fileName));

let replace = null;

Expand All @@ -96,15 +95,16 @@ export const createTransformer = ({types: t, template}: any) => {
IMPORT: newImport
});

hasImports.add(localFile);
hasImports.add(fileName);
visitedNodes.set(newImport, true);

parentPath.replaceWith(replace);
}
});
},

finish(node: any) {
finish(node: any, filename: string) {
if (!hasImports.has(filename)) return;
node.body.unshift(headerTemplate());
},

Expand All @@ -121,13 +121,11 @@ export default function (babel: any) {
visitor: {
Program: {
enter(programPath: any, {file}: any) {
transformer.traverse(programPath, file);
transformer.traverse(programPath, file.opts.filename);
},

exit({node}: any, {file}: any) {
if (!transformer.hasImports.has(file.opts.filename)) return;

transformer.finish(node);
transformer.finish(node, file.opts.filename);
}
},
}
Expand Down
11 changes: 7 additions & 4 deletions src/macro.ts
Expand Up @@ -19,6 +19,7 @@ function getMacroType(tagName: string) {

function macro({references, state, babel}: any) {
const {types: t} = babel;
const fileName = state.file.opts.filename;

const imports: Record<string, string[]> = {};
const transformer = createTransformer(babel);
Expand All @@ -35,15 +36,17 @@ function macro({references, state, babel}: any) {
let expression = tag.parentPath;

if (t.isCallExpression(expression)) {
transformer.traverse(expression, state.file)
transformer.traverse(expression, fileName);
}
});
}
});

if (addReactImports(babel, state, imports)) {
transformer.finish(state.file.path.node);
}
transformer.traverse(state.file.path, fileName);

addReactImports(babel, state, imports);

transformer.finish(state.file.path.node, fileName);
}

function addReactImports(babel: any, state: any, importsGroups: Record<string, string[]>) {
Expand Down

0 comments on commit 6142a15

Please sign in to comment.