forked from fusionjs/fusion-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement pure annotation for createPlugin (fusionjs#287)
- Loading branch information
1 parent
3582b0c
commit 77fb9dc
Showing
18 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
build/babel-plugins/babel-plugin-pure-create-plugin/index.js
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,35 @@ | ||
// @flow | ||
/* eslint-env node */ | ||
const createNamedModuleVisitor = require('../babel-plugin-utils/visit-named-module'); | ||
|
||
module.exports = pureCreatePlugin; | ||
|
||
function pureCreatePlugin(babel /*: Object */) { | ||
const t = babel.types; | ||
const visitor = createNamedModuleVisitor( | ||
t, | ||
'createPlugin', | ||
'fusion-core', | ||
refsHandler | ||
); | ||
return {visitor}; | ||
} | ||
|
||
function refsHandler(t, context, refs = []) { | ||
refs.forEach(refPath => { | ||
const parentPath = refPath.parentPath; | ||
if (!t.isCallExpression(parentPath)) { | ||
return; | ||
} | ||
parentPath.addComment('leading', '#__PURE__'); | ||
if (parentPath.parentPath.type === 'ExportDefaultDeclaration') { | ||
const id = parentPath.parentPath.scope.generateUidIdentifier('default'); | ||
parentPath.parentPath.insertBefore( | ||
t.variableDeclaration('var', [ | ||
t.variableDeclarator(id, parentPath.parentPath.node.declaration), | ||
]) | ||
); | ||
parentPath.parentPath.node.declaration = id; | ||
} | ||
}); | ||
} |
4 changes: 4 additions & 0 deletions
4
...bel-plugins/babel-plugin-pure-create-plugin/test/fixtures/create-plugin-default-and-named
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,4 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
export const p = createPlugin({}); | ||
export default createPlugin({}); |
3 changes: 3 additions & 0 deletions
3
.../babel-plugins/babel-plugin-pure-create-plugin/test/fixtures/create-plugin-default-export
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,3 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
export default createPlugin({}); |
9 changes: 9 additions & 0 deletions
9
...bel-plugins/babel-plugin-pure-create-plugin/test/fixtures/create-plugin-default-multiline
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,9 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
export default createPlugin({ | ||
deps: { | ||
a: 'b', | ||
}, | ||
provides: () => { | ||
} | ||
}); |
10 changes: 10 additions & 0 deletions
10
build/babel-plugins/babel-plugin-pure-create-plugin/test/fixtures/expected-default-and-named
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,10 @@ | ||
import { createPlugin } from 'fusion-core'; | ||
export const p = | ||
/*#__PURE__*/ | ||
createPlugin({}); | ||
|
||
var _default = | ||
/*#__PURE__*/ | ||
createPlugin({}); | ||
|
||
export default _default; |
7 changes: 7 additions & 0 deletions
7
build/babel-plugins/babel-plugin-pure-create-plugin/test/fixtures/expected-default-export
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 @@ | ||
import { createPlugin } from 'fusion-core'; | ||
|
||
var _default = | ||
/*#__PURE__*/ | ||
createPlugin({}); | ||
|
||
export default _default; |
12 changes: 12 additions & 0 deletions
12
build/babel-plugins/babel-plugin-pure-create-plugin/test/fixtures/expected-default-multiline
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,12 @@ | ||
import { createPlugin } from 'fusion-core'; | ||
|
||
var _default = | ||
/*#__PURE__*/ | ||
createPlugin({ | ||
deps: { | ||
a: 'b' | ||
}, | ||
provides: () => {} | ||
}); | ||
|
||
export default _default; |
37 changes: 37 additions & 0 deletions
37
build/babel-plugins/babel-plugin-pure-create-plugin/test/index.js
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,37 @@ | ||
// @flow | ||
/* eslint-env node */ | ||
const fs = require('fs'); | ||
const test = require('tape'); | ||
const {transformFileSync} = require('babel-core'); | ||
const plugin = require('../'); | ||
|
||
function getOutput(file) { | ||
return transformFileSync(__dirname + file, { | ||
plugins: [plugin], | ||
}).code; | ||
} | ||
|
||
function readExpected(file) { | ||
return fs.readFileSync(__dirname + file).toString(); | ||
} | ||
|
||
test('createPlugin default export', t => { | ||
const output = getOutput('/fixtures/create-plugin-default-export'); | ||
const expected = readExpected('/fixtures/expected-default-export'); | ||
t.equal(output, expected); | ||
t.end(); | ||
}); | ||
|
||
test('createPlugin default and named export', t => { | ||
const output = getOutput('/fixtures/create-plugin-default-and-named'); | ||
const expected = readExpected('/fixtures/expected-default-and-named'); | ||
t.equal(output, expected); | ||
t.end(); | ||
}); | ||
|
||
test('createPlugin default export multiline', t => { | ||
const output = getOutput('/fixtures/create-plugin-default-multiline'); | ||
const expected = readExpected('/fixtures/expected-default-multiline'); | ||
t.equal(output, expected); | ||
t.end(); | ||
}); |
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
7 changes: 7 additions & 0 deletions
7
test/fixtures/tree-shaking/src/plugins/default-export-browser.js
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 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
export default createPlugin({ | ||
provides() { | ||
console.log('default-export-browser-plugin'); | ||
}, | ||
}); |
7 changes: 7 additions & 0 deletions
7
test/fixtures/tree-shaking/src/plugins/default-export-node.js
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 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
export default createPlugin({ | ||
provides() { | ||
console.log('default-export-node-plugin'); | ||
}, | ||
}); |
8 changes: 8 additions & 0 deletions
8
test/fixtures/tree-shaking/src/plugins/instrumented-as-pure-browser.js
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,8 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
const PLUGIN = /*@__PURE__*/ createPlugin({ | ||
provides() { | ||
console.log('instrumented-as-pure-browser-plugin'); | ||
}, | ||
}); | ||
export default PLUGIN; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/tree-shaking/src/plugins/instrumented-as-pure-node.js
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,8 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
const PLUGIN = /*@__PURE__*/ createPlugin({ | ||
provides() { | ||
console.log('instrumented-as-pure-node-plugin'); | ||
}, | ||
}); | ||
export default PLUGIN; |
9 changes: 9 additions & 0 deletions
9
test/fixtures/tree-shaking/src/plugins/named-export-browser.js
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,9 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
const plugin = createPlugin({ | ||
provides() { | ||
console.log('named-export-browser-plugin'); | ||
}, | ||
}); | ||
|
||
export {plugin}; |
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,9 @@ | ||
import {createPlugin} from 'fusion-core'; | ||
|
||
const plugin = createPlugin({ | ||
provides() { | ||
console.log('named-export-node-plugin'); | ||
}, | ||
}); | ||
|
||
export {plugin}; |
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