Skip to content

Commit

Permalink
Add named hooks test case built with Rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
motiz88 committed Jul 2, 2021
1 parent ed6c091 commit f260bcf
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// export {Component as ComponentWithCustomHook} from './ComponentWithCustomHook';
// export {Component as ComponentWithExternalCustomHooks} from './ComponentWithExternalCustomHooks';
export {Component as Example} from './Example';
// export {Component as InlineRequire} from './InlineRequire';
// import * as ToDoList from './ToDoList';
// export {ToDoList};
// export {default as useTheme} from './useTheme';
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ describe('parseHookNames', () => {
]);
});

describe('inline and external source maps', () => {
describe('inline, external and bundle source maps', () => {
it('should work for simple components', async () => {
async function test(path) {
const Component = require(path).Component;
async function test(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count', // useState
Expand All @@ -198,6 +198,7 @@ describe('parseHookNames', () => {
await test('./__source__/Example'); // original source (uncompiled)
await test('./__source__/__compiled__/inline/Example'); // inline source map
await test('./__source__/__compiled__/external/Example'); // external source map
await test('./__source__/__compiled__/bundle/index', 'Example'); // bundle source map
});

it('should work with more complex files and components', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ const {
} = require('fs');
const {emptyDirSync} = require('fs-extra');
const {resolve} = require('path');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const commonjs = require('rollup-plugin-commonjs');
const jsx = require('acorn-jsx');
const rollupResolve = require('rollup-plugin-node-resolve');

const sourceDir = resolve(__dirname, '__source__');
const buildRoot = resolve(sourceDir, '__compiled__');
const externalDir = resolve(buildRoot, 'external');
const inlineDir = resolve(buildRoot, 'inline');
const bundleDir = resolve(buildRoot, 'bundle');

// Remove previous builds
emptyDirSync(buildRoot);
mkdirSync(externalDir);
mkdirSync(inlineDir);
mkdirSync(bundleDir);

function compile(fileName) {
const code = readFileSync(resolve(sourceDir, fileName), 'utf8');
Expand Down Expand Up @@ -66,6 +73,27 @@ function compile(fileName) {
);
}

async function bundle() {
const entryFileName = resolve(sourceDir, 'index.js');

// Bundle all modules with rollup
const result = await rollup.rollup({
input: entryFileName,
acornInjectPlugins: [jsx()],
plugins: [
rollupResolve(),
commonjs(),
babel({presets: ['@babel/preset-react'], sourceMap: true}),
],
external: ['react'],
});
await result.write({
file: resolve(bundleDir, 'index.js'),
format: 'cjs',
sourcemap: true,
});
}

// Compile all files in the current directory
const entries = readdirSync(sourceDir);
entries.forEach(entry => {
Expand All @@ -74,3 +102,8 @@ entries.forEach(entry => {
compile(entry);
}
});

bundle().catch(e => {
console.error(e);
process.exit(1);
});

0 comments on commit f260bcf

Please sign in to comment.