Skip to content

Commit

Permalink
Merge pull request #28 from jaredpalmer/feat/entry-and-ending
Browse files Browse the repository at this point in the history
Remove source requirement, sniff .ts or .tsx, add multi-entry
  • Loading branch information
jaredpalmer authored Jan 31, 2019
2 parents b54b7ce + 18b6212 commit 0409976
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 23 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"execa": "^1.0.0",
"fs-extra": "^7.0.1",
"jest": "23.6.0",
"jpjs": "^1.2.1",
"mkdirp": "^0.5.1",
"ms": "^2.1.1",
"ora": "^3.0.0",
Expand All @@ -41,6 +42,7 @@
"rollup-plugin-terser": "^4.0.2",
"rollup-plugin-typescript2": "^0.19.2",
"sade": "^1.4.2",
"tiny-glob": "^0.2.6",
"ts-jest": "^23.10.5",
"tslib": "^1.9.3",
"typescript": "^3.2.2"
Expand Down
2 changes: 1 addition & 1 deletion src/createRollupConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createRollupConfig(format, env, opts) {
let shebang;
return {
// Tell Rollup the entry point to the package
input: resolveApp(opts.input),
input: opts.input,
// Tell Rollup which packages to ignore
external,
// Establish Rollup output
Expand Down
142 changes: 120 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env node

import sade from 'sade';
import glob from 'tiny-glob/sync';
import { rollup, watch } from 'rollup';
import asyncro from 'asyncro';
import chalk from 'chalk';
import util from 'util';
import fs from 'fs-extra';
import jest from 'jest';
import logError from './logError';
Expand All @@ -17,6 +19,7 @@ import { createRollupConfig } from './createRollupConfig';
import { createJestConfig } from './createJestConfig';
import { safeVariableName, resolveApp, safePackageName } from './utils';
import * as Output from './output';
import { concatAllArray } from 'jpjs';

const prog = sade('tsdx');

Expand All @@ -25,17 +28,59 @@ try {
appPackageJson = fs.readJSONSync(resolveApp('package.json'));
} catch (e) {}

const createBuildConfigs = opts => {
opts.name = opts.name || appPackageJson.name;
opts.input = appPackageJson.source;
return [
createRollupConfig('cjs', 'development', opts),
createRollupConfig('cjs', 'production', opts),
createRollupConfig('es', 'production', opts),
createRollupConfig('umd', 'development', opts),
createRollupConfig('umd', 'production', opts),
];
};
const stat = util.promisify(fs.stat);

export const isDir = name =>
stat(name)
.then(stats => stats.isDirectory())
.catch(() => false);

export const isFile = name =>
stat(name)
.then(stats => stats.isFile())
.catch(() => false);

async function jsOrTs(filename) {
const extension = (await isFile(resolveApp(filename + '.ts')))
? '.ts'
: (await isFile(resolveApp(filename + '.tsx')))
? '.tsx'
: '.js';

return resolveApp(`${filename}${extension}`);
}

async function getInputs(entries, source) {
let inputs = [];
let stub = [];
stub
.concat(
entries && entries.length
? entries
: (source && resolveApp(source)) ||
((await isDir(resolveApp('src'))) && (await jsOrTs('src/index')))
)
.map(file => glob(file))
.forEach(input => inputs.push(input));

return concatAllArray(inputs);
}
function createBuildConfigs(opts) {
return concatAllArray(
opts.input.map(input => [
opts.format.includes('cjs') &&
createRollupConfig('cjs', 'development', { ...opts, input }),
opts.format.includes('cjs') &&
createRollupConfig('cjs', 'production', { ...opts, input }),
opts.format.includes('es') &&
createRollupConfig('es', 'production', { ...opts, input }),
opts.format.includes('umd') &&
createRollupConfig('umd', 'development', { ...opts, input }),
opts.format.includes('umd') &&
createRollupConfig('umd', 'production', { ...opts, input }),
])
);
}

async function moveTypes() {
try {
Expand Down Expand Up @@ -70,15 +115,11 @@ prog
const pkgJson = {
name: safeName,
version: '0.1.0',
source: 'src/index.ts',
main: 'index.js',
main: 'dist/index.js',
'umd:main': `dist/${safeName}.umd.production.js`,
module: `dist/${safeName}.es.production.js`,
typings: 'dist/index.d.ts',
files: [
"dist",
"index.js"
],
files: ['dist'],
scripts: {
start: 'tsdx watch',
build: 'tsdx build',
Expand Down Expand Up @@ -110,12 +151,38 @@ prog
prog
.command('watch')
.describe('Rebuilds on any change')
.option('--entry, -i', 'Entry module(s)')
.option('--target', 'Specify your target environment', 'web')
.example('tsdx watch --target node')
.option('--name', 'Specify name exposed in UMD builds')
.option('--format', 'Specify module format(s)', 'cjs,es,umd')
.action(async opts => {
opts.name = opts.name || appPackageJson.name;
opts.input = await getInputs(opts.entry, appPackageJson.source);
const [cjsDev, cjsProd, ...otherConfigs] = createBuildConfigs(opts);
if (opts.format.includes('cjs')) {
try {
await fs.writeFile(
resolveApp('dist/index.js'),
`
'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${safeVariableName(
opts.name
)}.cjs.production.js')
} else {
module.exports = require('./${safeVariableName(
opts.name
)}.cjs.development.js')
}`,
'utf8'
);
} catch (e) {}
}

await watch(
createBuildConfigs(opts).map(inputOptions => ({
[cjsDev, cjsProd, ...otherConfigs].map(inputOptions => ({
watch: {
silent: true,
include: 'src/**',
Expand All @@ -141,12 +208,43 @@ prog
prog
.command('build')
.describe('Build your project once and exit')
.option('--entry, -i', 'Entry module(s)')
.option('--target', 'Specify your target environment', 'web')
.example('tsdx build --target node')
.option('--name', 'Specify name exposed in UMD builds')
.option('--format', 'Specify module format(s)', 'cjs,es,umd')
.action(async opts => {
opts.name = opts.name || appPackageJson.name;
opts.input = await getInputs(opts.entry, appPackageJson.source);
const [cjsDev, cjsProd, ...otherConfigs] = createBuildConfigs(opts);
if (opts.format.includes('cjs')) {
try {
await fs.writeFile(
resolveApp('dist/index.js'),
`
'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${safeVariableName(
opts.name
)}.cjs.production.js')
} else {
module.exports = require('./${safeVariableName(
opts.name
)}.cjs.development.js')
}`,
'utf8'
);
} catch (e) {}
}
try {
await asyncro.map(createBuildConfigs(opts), async inputOptions => {
let bundle = await rollup(inputOptions);
await bundle.write(inputOptions.output);
});
await asyncro.map(
[cjsDev, cjsProd, ...otherConfigs],
async inputOptions => {
let bundle = await rollup(inputOptions);
await bundle.write(inputOptions.output);
}
);
await moveTypes();
} catch (error) {
logError(error);
Expand Down
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,11 @@ globals@^9.18.0:
resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a"
integrity sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==

globalyzer@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.0.tgz#cb76da79555669a1519d5a8edf093afaa0bf1465"
integrity sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q==

globby@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-6.1.0.tgz#f5a6d70e8395e21c858fb0489d64df02424d506c"
Expand All @@ -2211,6 +2216,11 @@ globby@^6.1.0:
pify "^2.0.0"
pinkie-promise "^2.0.0"

globrex@^0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/globrex/-/globrex-0.1.2.tgz#dd5d9ec826232730cd6793a5e33a9302985e6098"
integrity sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==

graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6:
version "4.1.15"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
Expand Down Expand Up @@ -3204,6 +3214,11 @@ jest@23.6.0:
import-local "^1.0.0"
jest-cli "^23.6.0"

jpjs@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/jpjs/-/jpjs-1.2.1.tgz#f343833de8838a5beba1f42d5a219be0114c44b7"
integrity sha512-GxJWybWU4NV0RNKi6EIqk6IRPOTqd/h+U7sbtyuD7yUISUzV78LdHnq2xkevJsTlz/EImux4sWj+wfMiwKLkiw==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -5477,6 +5492,14 @@ timers-browserify@^2.0.4:
dependencies:
setimmediate "^1.0.4"

tiny-glob@^0.2.6:
version "0.2.6"
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.6.tgz#9e056e169d9788fe8a734dfa1ff02e9b92ed7eda"
integrity sha512-A7ewMqPu1B5PWwC3m7KVgAu96Ch5LA0w4SnEN/LbDREj/gAD0nPWboRbn8YoP9ISZXqeNAlMvKSKoEuhcfK3Pw==
dependencies:
globalyzer "^0.1.0"
globrex "^0.1.1"

tmpl@1.0.x:
version "1.0.4"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"
Expand Down

0 comments on commit 0409976

Please sign in to comment.