Skip to content

Commit

Permalink
initizzle this shizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer committed Jan 24, 2019
0 parents commit 8ee9ead
Show file tree
Hide file tree
Showing 7 changed files with 3,886 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.log
.DS_Store
node_modules
.rts2_cache_cjs
.rts2_cache_es
.rts2_cache_umd
dist
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# tsdx

Formik's build setup...as a CLI tool.
164 changes: 164 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/usr/bin/env node

const sade = require('sade');
const prog = sade('tsdx');
const fs = require('fs-extra');
const path = require('path');
const { rollup, watch } = require('rollup');
const commonjs = require('rollup-plugin-commonjs');
const replace = require('rollup-plugin-replace');
const typescript = require('rollup-plugin-typescript2');
const resolve = require('rollup-plugin-node-resolve');
const sourceMaps = require('rollup-plugin-sourcemaps');
const babel = require('rollup-plugin-babel');
const { terser } = require('rollup-plugin-terser');
const { sizeSnapshot } = require('rollup-plugin-size-snapshot');

var PrettyError = require('pretty-error');
var pe = new PrettyError();

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);

const pkg = fs.readJSONSync(resolveApp('package.json'));

const paths = {
appPkg: resolveApp('package.json'),
appRoot: resolveApp('.'),
appSrc: resolveApp('src/'),
appEntry: resolveApp(pkg.source),
appDist: resolveApp('dist'),
};

const external = id => !id.startsWith('.') && !id.startsWith('/');
const replacements = [{ original: 'lodash', replacement: 'lodash-es' }];
const babelOptions = {
exclude: /node_modules/,
plugins: [
'annotate-pure-calls',
'dev-expression',
['transform-rename-import', { replacements }],
],
};

function getConfig(format, env) {
return {
input: paths.appEntry,
external,
output: {
file: `${paths.appDist}/${pkg.name}.${format}.${env}.js`,
format,
sourcemap: true,
globals: { react: 'React', 'react-native': 'ReactNative' },
exports: 'named',
},
plugins: [
resolve({
module: true,
jsnext: true,
browser: true,
}),
commonjs({
// use a regex to make sure to include eventual hoisted packages
include: /\/node_modules\//,
}),
typescript({
typescript: require('typescript'),
cacheRoot: `./.rts2_cache_${format}`,
tsconfigDefaults: {
compilerOptions: {
sourceMap: true,
declaration: true,
jsx: 'react',
},
},
tsconfigOverride: {
compilerOptions: {
target: 'esnext',
},
},
}),
babel(babelOptions),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
sourceMaps(),
sizeSnapshot(),
env === 'production' &&
terser({
sourcemap: true,
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true,
},
ecma: 5,
toplevel: format === 'es' || format === 'cjs',
warnings: true,
}),
],
};
}

async function moveTypes() {
// Move the typescript types to the base of the ./dist folder
await fs.copy(paths.appDist + '/src', paths.appDist, {
overwrite: true,
});
await fs.remove(paths.appDist + '/src');
}

prog.version(pkg.version);

prog
.command('watch')
.describe('Build your project in watch mode')
.action(async opts => {
await watch(
[
getConfig('cjs', 'development'),
getConfig('cjs', 'production'),
getConfig('es', 'production'),
getConfig('umd', 'development'),
getConfig('umd', 'production'),
].map(inputOptions => ({
watch: {
include: 'src/**',
exclude: 'node_modules/**',
},
...inputOptions,
}))
).on('event', async event => {
if (event.code === 'ERROR') {
console.log(pe.render(event.error));
}
if (event.code === 'FATAL') {
console.log(pe.render(event.error));
}
if (event.code === 'END') {
await moveTypes();
}
});
});

prog
.command('build')
.describe('Build your project for production')
.action(async opts => {
await Promise.all(
[
getConfig('cjs', 'production'),
getConfig('es', 'production'),
getConfig('umd', 'development'),
getConfig('umd', 'production'),
].map(async inputOptions => {
let bundle = await rollup(inputOptions);
await bundle.write(inputOptions.output);
})
);
await moveTypes();
});

prog.parse(process.argv);
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "tsdx",
"version": "0.1.0",
"license": "MIT",
"bin": {
"tsdx": "index.js"
},
"dependencies": {
"@babel/core": "^7.2.2",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-transform-rename-import": "^2.3.0",
"cross-env": "5.0.5",
"fs-extra": "^7.0.1",
"pretty-error": "^2.1.1",
"rollup": "^0.66.4",
"rollup-plugin-babel": "^4.0.3",
"rollup-plugin-commonjs": "^9.1.8",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-size-snapshot": "^0.7.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-terser": "^4.0.2",
"rollup-plugin-typescript2": "^0.19.2",
"sade": "^1.4.2",
"typescript": "^3.2.2"
},
"devDependencies": {
"tslint": "^5.12.0",
"tslint-config-palmerhq": "^1.0.2",
"tslint-config-prettier": "^1.17.0",
"tslint-react": "^3.6.0"
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
}
}
85 changes: 85 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5",
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "ESNext",

/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["dom", "esnext"],
/* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true,
/* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true,
/* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./dist",
/* Redirect output structure to the directory. */
"rootDir": "./",
/* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true,
/* Enable all strict type-checking options. */
"noImplicitAny": true,
/* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true,
/* Enable strict null checks. */
"strictFunctionTypes": true,
/* Enable strict checking of function types. */
"strictPropertyInitialization": true,
/* Enable strict checking of property initialization in classes. */
"noImplicitThis": true,
/* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true,
/* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"noUnusedLocals": true,
/* Report errors on unused locals. */
"noUnusedParameters": true,
/* Report errors on unused parameters. */
"noImplicitReturns": true,
/* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true,
/* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node",
/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./",
/* Base directory to resolve non-absolute module names. */
"paths": {
"*": ["src/*", "node_modules/*"]
},
/* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": ["src/**/*"],
/* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},

}
10 changes: 10 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"tslint-react",
"tslint-config-palmerhq",
"tslint-config-prettier"
],
"linterOptions": {
"exclude": ["src/api/**/*"]
}
}

0 comments on commit 8ee9ead

Please sign in to comment.