Skip to content

Commit

Permalink
Add support for separate index entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone authored and rtsao committed Jan 26, 2018
1 parent d4d3b8a commit 6cd2f13
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/create-universal-package/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ args
.option('skip-preflight', 'Skip preflight check', false)
.option('skip-flow', 'Skip generation of flow libdef', false)
.option('force-flow', 'Force generation of flow libdef', false)
.option(
'separate-entries',
'Use src/index.browser.js and src/index.node.js instead of src/index.js',
false,
)
.option('tests', 'Build tests', false)
.option('tests-only', 'Build test bundles only', false);

Expand Down
29 changes: 21 additions & 8 deletions packages/create-universal-package/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ function build(opts, variants = {}, preflight) {
exposedMethods: ['build', 'preflight', 'buildBrowser', 'genFlowLibdef'],
});

const inputOptions = {
const baseInputOptions = {
input: path.join(opts.dir, 'src/index.js'),
pureExternalModules: true,
};

const userBabelConfig = validateConfig(opts.dir);
const browserInputOptions = {
...baseInputOptions,
};
const nodeInputOptions = {
...baseInputOptions,
};

const generateFlowLibdef =
!opts.skipFlow && (opts.forceFlow || hasFlowConfig(opts.dir));
!opts.separateEntries &&
!opts.skipFlow &&
(opts.forceFlow || hasFlowConfig(opts.dir));

if (opts.separateEntries) {
browserInputOptions.input = path.join(opts.dir, 'src/index.browser.js');
nodeInputOptions.input = path.join(opts.dir, 'src/index.node.js');
}

const userBabelConfig = validateConfig(opts.dir);

let jobs = [];

Expand Down Expand Up @@ -62,7 +75,7 @@ function build(opts, variants = {}, preflight) {
variants.node && {
name: 'build:node',
args: [
inputOptions,
nodeInputOptions,
getBabelConfig({env: 'node', target: '8.9.0', userBabelConfig}),
[
{
Expand All @@ -81,7 +94,7 @@ function build(opts, variants = {}, preflight) {
variants.browser && {
name: 'build:browser (es5)',
args: [
inputOptions,
browserInputOptions,
getBabelConfig({
env: 'browser',
target: '5',
Expand All @@ -105,7 +118,7 @@ function build(opts, variants = {}, preflight) {
variants.browser && {
name: 'build:browser (es2015)',
args: [
inputOptions,
browserInputOptions,
getBabelConfig({
env: 'browser',
target: '2015',
Expand All @@ -124,7 +137,7 @@ function build(opts, variants = {}, preflight) {
variants.browser && {
name: 'build:browser (es2017)',
args: [
inputOptions,
browserInputOptions,
getBabelConfig({env: 'browser', target: '2017', userBabelConfig}),
[
{
Expand Down
2 changes: 1 addition & 1 deletion packages/create-universal-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"cup-clean": "./bin/clean.js"
},
"scripts": {
"test": "echo TODO tests",
"test": "node test/index.js",
"lint": "eslint {lib,bin}"
},
"dependencies": {
Expand Down
62 changes: 62 additions & 0 deletions packages/create-universal-package/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const path = require('path');
const tape = require('tape');
const {promisify} = require('util');
const cp = require('child_process');
const fs = require('fs');
const exec = promisify(cp.exec);

tape('fixture package transpile', async t => {
const dir = path.join(__dirname, '../../fixture-package/');
await exec(`yarn build`, {cwd: dir});
const expectedFiles = [
'browser.es2015.es.js',
'browser.es2017.es.js',
'browser.es5.es.js',
'browser.es5.js',
'index.es.js',
'index.js',
'index.js.map',
'browser.es2015.es.js.map',
'browser.es2017.es.js.map',
'browser.es5.es.js.map',
'browser.es5.js.map',
'index.es.js.map',
'index.js.flow',
];
expectedFiles
.map(file => path.join(dir, 'dist', file))
.forEach((file, index) => {
t.ok(fs.existsSync(file), `${expectedFiles[index]} exists`);
});
t.end();
});

tape('fixture package separate indexes transpile', async t => {
const dir = path.join(__dirname, '../../fixture-package-separate-indexes/');
await exec(`yarn build`, {cwd: dir});
const expectedFiles = [
'browser.es2015.es.js',
'browser.es2017.es.js',
'browser.es5.es.js',
'browser.es5.js',
'index.es.js',
'index.js',
'index.js.map',
'browser.es2015.es.js.map',
'browser.es2017.es.js.map',
'browser.es5.es.js.map',
'browser.es5.js.map',
'index.es.js.map',
];
expectedFiles
.map(file => path.join(dir, 'dist', file))
.forEach((file, index) => {
t.ok(fs.existsSync(file), `${expectedFiles[index]} exists`);
});

t.notok(
fs.existsSync(path.join(dir, 'index.js.flow')),
'does not generate flow when using separate entries',
);
t.end();
});
5 changes: 5 additions & 0 deletions packages/fixture-package-separate-indexes/.cuprc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
babel: {
presets: [require.resolve('babel-preset-react')],
},
};
2 changes: 2 additions & 0 deletions packages/fixture-package-separate-indexes/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
flow-typed/
37 changes: 37 additions & 0 deletions packages/fixture-package-separate-indexes/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
extends: [
'plugin:react/recommended',
require.resolve('eslint-config-cup'),
require.resolve('eslint-config-cup-recommended'),
],

parser: 'babel-eslint',

parserOptions: {
ecmaVersion: 2018,

ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,
},
},

plugins: ['eslint-plugin-prettier'],

rules: {
'prettier/prettier': [
'error',
{
useTabs: false,
printWidth: 80,
tabWidth: 2,
singleQuote: true,
trailingComma: 'all',
bracketSpacing: false,
jsxBracketSameLine: false,
parser: 'babylon',
semi: true,
},
],
},
};
11 changes: 11 additions & 0 deletions packages/fixture-package-separate-indexes/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ignore]
<PROJECT_ROOT>/dist/**
[include]

[libs]

[lints]

[options]

[strict]
3 changes: 3 additions & 0 deletions packages/fixture-package-separate-indexes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
dist-tests/
coverage/
3 changes: 3 additions & 0 deletions packages/fixture-package-separate-indexes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# fixture-package

An example package using create-universal-package
9 changes: 9 additions & 0 deletions packages/fixture-package-separate-indexes/flow-typed/cup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow

declare var __BROWSER__: boolean;
declare var __NODE__: boolean;
declare var __DEV__: boolean;

declare module '@cup/fixture-dependency-pure' {
declare export default any
}
63 changes: 63 additions & 0 deletions packages/fixture-package-separate-indexes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@cup/fixture-package-separate-indexes",
"private": true,
"version": "1.1.13",
"description": "A fixture universal package",
"author": "Ryan Tsao <ryan.j.tsao@gmail.com>",
"homepage": "https://github.com/rtsao/create-universal-package",
"repository": "git@github.com:rtsao/create-universal-package.git",
"bugs": "https://github.com/rtsao/create-universal-package/issues",
"files": [
"dist",
"src"
],
"main": "./dist/index.js",
"module": "./dist/index.es.js",
"browser": {
"./dist/index.js": "./dist/browser.es5.js",
"./dist/index.es.js": "./dist/browser.es5.es.js"
},
"es2015": {
"./dist/browser.es5.es.js": "./dist/browser.es2015.es.js"
},
"es2017": {
"./dist/browser.es5.es.js": "./dist/browser.es2017.es.js",
"./dist/browser.es2015.es.js": "./dist/browser.es2017.es.js"
},
"scripts": {
"clean": "cup clean",
"pretest": "cup build-tests",
"test": "nyc --reporter=lcov unitest --browser=dist-tests/browser.js --node=dist-tests/node.js",
"build": "cup build --separate-entries",
"lint": "eslint src/**",
"prepublish": "npm run build"
},
"dependencies": {
"@cup/fixture-dependency-pure": "^0.0.3",
"babel-eslint": "7"
},
"peerDependencies": {
"react": "^16.1.0"
},
"devDependencies": {
"babel-preset-cup": "^1.0.0-rc.3",
"babel-preset-react": "^7.0.0-beta.3",
"create-universal-package": "^3.2.6",
"enzyme": "^3.1.1",
"enzyme-adapter-react-16": "^1.0.4",
"eslint": "4.x",
"eslint-config-cup": "^1.0.0",
"eslint-config-cup-recommended": "^1.0.0",
"eslint-plugin-cup": "^1.0.0",
"eslint-plugin-prettier": "^2.0.1",
"eslint-plugin-react": "^7.5.1",
"nyc": "^11.3.0",
"prettier": "^1.2.2",
"react": "^16.1.0",
"react-dom": "^16.1.0",
"react-test-renderer": "^16.1.0",
"tape-cup": "^4.7.1",
"unitest": "^2.0.0"
},
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'tape-cup';

test('should not be run', t => {
t.fail('a failing assertion');
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from 'tape-cup';

import {configure, mount} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});

import React from 'react';

import {Component} from '../index.browser.js';

test('a browser only test', t => {
t.pass('browser only assertion');
t.end();
});

test('full dom render', t => {
const wrapper = mount(<Component />);
t.ok(wrapper);
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from 'tape-cup';

import {configure, shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});

import React from 'react';

import {Component} from '../index.node.js';

test('a node only test', t => {
t.pass('node only assertion');
t.end();
});

test('shallow render', t => {
const wrapper = shallow(<Component />);
t.ok(wrapper);
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'tape-cup';

import * as a from '../a.js';

test('a', t => {
t.equal(a.default, 'a');
t.end();
});

if (__NODE__) {
test('a (node)', t => {
t.equal(a.default, 'a');
t.end();
});
}

if (__BROWSER__) {
test('a (browser)', t => {
t.equal(a.default, 'a');
t.end();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import test from 'tape-cup';

import * as b from '../b.js';

test('b', t => {
t.equal(b.default, 'b');
t.end();
});
3 changes: 3 additions & 0 deletions packages/fixture-package-separate-indexes/src/foo/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = 'a';

export default a;
3 changes: 3 additions & 0 deletions packages/fixture-package-separate-indexes/src/foo/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const b = 'b';

export default b;
Loading

0 comments on commit 6cd2f13

Please sign in to comment.