Skip to content

Commit

Permalink
Implement pure annotation for createPlugin (fusionjs#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGrandon committed Mar 20, 2018
1 parent 3582b0c commit 77fb9dc
Show file tree
Hide file tree
Showing 18 changed files with 249 additions and 0 deletions.
35 changes: 35 additions & 0 deletions build/babel-plugins/babel-plugin-pure-create-plugin/index.js
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;
}
});
}
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({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {createPlugin} from 'fusion-core';

export default createPlugin({});
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: () => {
}
});
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;
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;
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 build/babel-plugins/babel-plugin-pure-create-plugin/test/index.js
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();
});
4 changes: 4 additions & 0 deletions build/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ function getConfig({target, env, dir, watch, cover}) {
//cup-globals works with webpack.EnvironmentPlugin(['NODE_ENV']) to implement static conditionals
require.resolve('@babel/plugin-syntax-dynamic-import'),
require.resolve('./babel-plugins/babel-plugin-asseturl'),
require.resolve(
'./babel-plugins/babel-plugin-pure-create-plugin'
),
require.resolve(
'./babel-plugins/babel-plugin-sync-chunk-ids'
),
Expand Down Expand Up @@ -452,6 +455,7 @@ function getConfig({target, env, dir, watch, cover}) {
}),
].filter(Boolean),
optimization: {
sideEffects: true,
splitChunks: target === 'web' && {
// See https://webpack.js.org/guides/code-splitting/
// See https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693
Expand Down
66 changes: 66 additions & 0 deletions test/cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,69 @@ test('`fusion build` with dynamic imports', async t => {
);
t.end();
});

test('`fusion build` tree shaking', async t => {
const dir = path.resolve(__dirname, '../fixtures/tree-shaking');
await cmd(`build --dir=${dir} --production=true`);

const serverMain = path.resolve(
dir,
`.fusion/dist/production/server/server-main.js`
);
const clientFiles = await readdir(
path.resolve(dir, '.fusion/dist/production/client')
);
const clientMainFile = clientFiles.filter(f =>
/client-main-(.*?).js$/.test(f)
)[0];
const clientMain = path.resolve(
dir,
'.fusion/dist/production/client',
clientMainFile
);

// Manually instrumented
t.ok(
!fs
.readFileSync(serverMain, 'utf-8')
.includes('instrumented-as-pure-browser-plugin'),
'should not include browserPlugin in node'
);

t.ok(
!fs
.readFileSync(clientMain, 'utf-8')
.includes('instrumented-as-pure-node-plugin'),
'should not include nodePlugin in browser'
);

// Default export
t.ok(
!fs
.readFileSync(serverMain, 'utf-8')
.includes('default-export-browser-plugin'),
'should not include default browser export in node'
);

t.ok(
!fs
.readFileSync(clientMain, 'utf-8')
.includes('default-export-node-plugin'),
'should not include default node export in browser'
);

// Named export
t.ok(
!fs
.readFileSync(serverMain, 'utf-8')
.includes('named-export-browser-plugin'),
'should not include named browser export in node'
);

t.ok(
!fs.readFileSync(clientMain, 'utf-8').includes('named-export-node-plugin'),
'should not include named node export in browser'
);

t.end();
});
13 changes: 13 additions & 0 deletions test/fixtures/tree-shaking/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ import readline from 'readline';
import repl from 'repl';
import tls from 'tls';

import defaultExportBrowserPlugin from './plugins/default-export-browser.js';
import defaultExportNodePlugin from './plugins/default-export-node.js';
import instrumentedAsPureBrowserPlugin from './plugins/instrumented-as-pure-browser.js';
import instrumentedAsPureNodePlugin from './plugins/instrumented-as-pure-node.js';
import {plugin as namedExportBrowserPlugin} from './plugins/named-export-browser.js';
import {plugin as namedExportNodePlugin} from './plugins/named-export-node.js';

export default async function() {
const app = new App('element', el => el);
__BROWSER__ && app.register(defaultExportBrowserPlugin);
__NODE__ && app.register(defaultExportNodePlugin);
__BROWSER__ && app.register(instrumentedAsPureBrowserPlugin);
__NODE__ && app.register(instrumentedAsPureNodePlugin);
__BROWSER__ && app.register(namedExportBrowserPlugin);
__NODE__ && app.register(namedExportNodePlugin);
__NODE__ &&
app.middleware((ctx, next) => {
if (ctx.url === '/fs') {
Expand Down
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 test/fixtures/tree-shaking/src/plugins/default-export-node.js
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');
},
});
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;
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;
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};
9 changes: 9 additions & 0 deletions test/fixtures/tree-shaking/src/plugins/named-export-node.js
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};
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require('./route-prefix.js');
/*
require('./browser-support');
*/
require('../build/babel-plugins/babel-plugin-pure-create-plugin/test');
require('../build/babel-plugins/babel-plugin-asseturl/test');
require('../build/babel-plugins/babel-plugin-chunkid/test');
require('../build/babel-plugins/babel-plugin-experimentation/test');
Expand Down

0 comments on commit 77fb9dc

Please sign in to comment.