Skip to content

Commit 35923f2

Browse files
authored
Merge pull request #2 from motiondeveloper/wrapping
Add option to wrap compiled code in `get()` method
2 parents 8235c6f + 32f96ec commit 35923f2

File tree

4 files changed

+250
-95
lines changed

4 files changed

+250
-95
lines changed

README.md

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ npm install rollup-plugin-ae-jsx --save-dev
2525
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files), import the plugin, and add it to the `plugins` array:
2626

2727
```js
28-
import afterEffectJsx from "./rollup-plugin-ae-jsx";
29-
import pkg from "./package.json";
28+
import afterEffectJsx from './rollup-plugin-ae-jsx';
29+
import pkg from './package.json';
3030

3131
export default {
32-
input: "src/index.ts",
32+
input: 'src/index.ts',
3333
output: {
34-
file: "dist/index.jsx",
35-
format: "es",
34+
file: 'dist/index.jsx',
35+
format: 'es',
3636
},
3737
external: Object.keys(pkg.dependencies),
3838
plugins: [afterEffectJsx()],
@@ -44,13 +44,86 @@ export default {
4444
4545
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
4646

47-
## Proccess
47+
## Options
48+
49+
### `wrap`
50+
51+
Type: `boolean` \
52+
Default: `false`
53+
54+
Wraps compiled code in a `get()` function. See [Wrapping](#wrapping) for more detail.
55+
56+
## Process
4857

4958
1. Creating a list of the exported functions and variables from the index file
5059
2. Removing non-compatible statements: `['ExpressionStatement', 'DebuggerStatement', 'ImportDeclaration', 'ExportNamedDeclaration'];`
5160
3. Converting function and variable declarations into `.jsx` compliant syntax
5261
4. Wrapping in braces (`{}`)
5362

63+
## Wrapping
64+
65+
Compiling code that references top level functions or variables will error when run in After Effects, since each exported property is isolated from the surrounding code.
66+
67+
For example the following source code:
68+
69+
```js
70+
function add(a, b) {
71+
return a + b;
72+
}
73+
74+
function getFour() {
75+
return add(2, 2);
76+
}
77+
78+
export { add, getFour };
79+
```
80+
81+
Will compile to the following `.jsx` file:
82+
83+
```js
84+
{
85+
add(a, b) {
86+
return a + b;
87+
},
88+
getFour() {
89+
return add(2, 2); // error, add is not defined
90+
}
91+
}
92+
```
93+
94+
Which will error, since `add()` is not defined within the scope of `getFour()`.
95+
96+
This can be solved by wrapping all of your code in a parent function, which `rollup-plugin-jsx` will do for you if `wrap` is set to true.
97+
98+
```js
99+
// rollup.config.js
100+
plugins: [afterEffectJsx({ wrap: true })],
101+
```
102+
103+
The compiled `.jsx` would then be:
104+
105+
```js
106+
{
107+
get() {
108+
function add(a, b) {
109+
return a + b;
110+
}
111+
112+
function getFour() {
113+
return add(2, 2);
114+
}
115+
116+
return { add, getFour }
117+
}
118+
}
119+
```
120+
121+
You then would need to call `.get()` in your expressions:
122+
123+
```js
124+
const { getFour, add } = footage('index.jsx').sourceData.get();
125+
```
126+
54127
## Meta
55128

56129
[CONTRIBUTING](/.github/CONTRIBUTING.md)

package-lock.json

Lines changed: 46 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
"homepage": "https://github.com/motiondeveloper/rollup-plugin-ae-jsx#readme",
3939
"dependencies": {
4040
"estree-walker": "^2.0.1",
41-
"magic-string": "^0.25.7"
41+
"magic-string": "^0.25.9"
42+
},
43+
"prettier": {
44+
"useTabs": false,
45+
"singleQuote": true
4246
}
4347
}

0 commit comments

Comments
 (0)