project initialized

This commit is contained in:
crusader
2018-09-05 19:01:59 +09:00
commit 0762bde89e
49 changed files with 8413 additions and 0 deletions

66
config/global.d.ts vendored Normal file
View File

@@ -0,0 +1,66 @@
// ts-jest types require 'babel-core'
declare module 'babel-core' {
interface TransformOptions {}
}
declare module 'jest-config' {
const defaults: jest.DefaultOptions
}
declare module 'sort-object-keys' {
const sortPackageJson: <T extends {}>(
object: T,
sortWith?: (...args: any[]) => any
) => T
export = sortPackageJson
}
type RollupPluginFn<O extends object = {}> = (
options?: O
) => import('rollup').Plugin
declare module 'rollup-plugin-json' {
export interface Options {
/**
* All JSON files will be parsed by default, but you can also specifically include/exclude files
*/
include?: string | string[]
exclude?: string | string[]
/**
* for tree-shaking, properties will be declared as variables, using either `var` or `const`
* @default false
*/
preferConst?: boolean
/**
* specify indentation for the generated default export — defaults to '\t'
* @default '\t'
*/
indent?: string
}
const plugin: RollupPluginFn<Options>
export default plugin
}
declare module 'rollup-plugin-sourcemaps' {
const plugin: RollupPluginFn
export default plugin
}
declare module 'rollup-plugin-node-resolve' {
const plugin: RollupPluginFn
export default plugin
}
declare module 'rollup-plugin-commonjs' {
const plugin: RollupPluginFn
export default plugin
}
declare module 'rollup-plugin-replace' {
const plugin: RollupPluginFn
export default plugin
}
declare module 'rollup-plugin-uglify' {
const uglify: RollupPluginFn
export { uglify }
}
declare module 'rollup-plugin-terser' {
const terser: RollupPluginFn
export { terser }
}

60
config/helpers.js Normal file
View File

@@ -0,0 +1,60 @@
// helpers
module.exports = {
camelCaseToDash,
dashToCamelCase,
toUpperCase,
pascalCase,
normalizePackageName,
getOutputFileName,
}
/**
*
* @param {string} myStr
*/
function camelCaseToDash(myStr) {
return myStr.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}
/**
*
* @param {string} myStr
*/
function dashToCamelCase(myStr) {
return myStr.replace(/-([a-z])/g, (g) => g[1].toUpperCase())
}
/**
*
* @param {string} myStr
*/
function toUpperCase(myStr) {
return `${myStr.charAt(0).toUpperCase()}${myStr.substr(1)}`
}
/**
*
* @param {string} myStr
*/
function pascalCase(myStr) {
return toUpperCase(dashToCamelCase(myStr))
}
/**
*
* @param {string} rawPackageName
*/
function normalizePackageName(rawPackageName) {
const scopeEnd = rawPackageName.indexOf('/') + 1
return rawPackageName.substring(scopeEnd)
}
/**
*
* @param {string} fileName
* @param {boolean?} isProd
*/
function getOutputFileName(fileName, isProd = false) {
return isProd ? fileName.replace(/\.js$/, '.min.js') : fileName
}

43
config/jest.config.js Normal file
View File

@@ -0,0 +1,43 @@
// @ts-check
const { defaults } = require('jest-config')
/**
* @type {import('./types').TsJestConfig}
*/
const tsJestConfig = {
skipBabel: true,
}
/**
* @type {Partial<jest.InitialOptions>}
*/
const config = {
rootDir: '..',
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
testMatch: [
'<rootDir>/src/**/__tests__/**/*.ts?(x)',
'<rootDir>/src/**/?(*.)+(spec|test).ts?(x)',
],
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
globals: {
'ts-jest': tsJestConfig,
},
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
setupFiles: ['<rootDir>/config/setup-tests.js'],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
}
module.exports = config

128
config/rollup.config.js Normal file
View File

@@ -0,0 +1,128 @@
import { resolve } from 'path'
import sourceMaps from 'rollup-plugin-sourcemaps'
import nodeResolve from 'rollup-plugin-node-resolve'
import json from 'rollup-plugin-json'
import commonjs from 'rollup-plugin-commonjs'
import replace from 'rollup-plugin-replace'
import { uglify } from 'rollup-plugin-uglify'
import { terser } from 'rollup-plugin-terser'
import { getIfUtils, removeEmpty } from 'webpack-config-utils'
import pkg from '../package.json'
const {
pascalCase,
normalizePackageName,
getOutputFileName,
} = require('./helpers')
/**
* @typedef {import('./types').RollupConfig} Config
*/
/**
* @typedef {import('./types').RollupPlugin} Plugin
*/
const env = process.env.NODE_ENV || 'development'
const { ifProduction } = getIfUtils(env)
const LIB_NAME = pascalCase(normalizePackageName(pkg.name))
const ROOT = resolve(__dirname, '..')
const DIST = resolve(ROOT, 'dist')
/**
* @type {{entry:{esm5: string, esm2015: string},bundles:string}}
*/
const PATHS = {
entry: {
esm5: resolve(DIST, 'esm5'),
esm2015: resolve(DIST, 'esm2015'),
},
bundles: resolve(DIST, 'bundles'),
}
/**
* @type {string[]}
*/
const external = Object.keys(pkg.peerDependencies) || []
/**
* @type {Plugin[]}
*/
const plugins = /** @type {Plugin[]} */ ([
// Allow json resolution
json(),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
nodeResolve(),
// Resolve source maps to the original source
sourceMaps(),
// properly set process.env.NODE_ENV within `./environment.ts`
replace({
exclude: 'node_modules/**',
'process.env.NODE_ENV': JSON.stringify(env),
}),
])
/**
* @type {Config}
*/
const CommonConfig = {
input: {},
output: {},
inlineDynamicImports: true,
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external,
}
/**
* @type {Config}
*/
const UMDconfig = {
...CommonConfig,
input: resolve(PATHS.entry.esm5, 'index.js'),
output: {
file: getOutputFileName(
resolve(PATHS.bundles, 'index.umd.js'),
ifProduction()
),
// file: getOutputFileName('dist/bundles/index.umd.js', ifProduction()),
format: 'umd',
name: LIB_NAME,
sourcemap: true,
},
plugins: /** @type {Plugin[]} */ (removeEmpty([
...plugins,
ifProduction(uglify()),
])),
}
/**
* @type {Config}
*/
const FESMconfig = {
...CommonConfig,
input: resolve(PATHS.entry.esm2015, 'index.js'),
output: [
{
file: getOutputFileName(
resolve(PATHS.bundles, 'index.esm.js'),
ifProduction()
),
format: 'es',
sourcemap: true,
},
],
plugins: /** @type {Plugin[]} */ (removeEmpty([
...plugins,
ifProduction(terser()),
])),
}
export default [UMDconfig, FESMconfig]

4
config/setup-tests.js Normal file
View File

@@ -0,0 +1,4 @@
// add here any code that you wanna execute before tests like
// - polyfills
// - some custom code
// for more docs check see https://jestjs.io/docs/en/configuration.html#setupfiles-array

14
config/tsconfig.json Normal file
View File

@@ -0,0 +1,14 @@
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"checkJs": true,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noEmit": true
}
}

30
config/types.js Normal file
View File

@@ -0,0 +1,30 @@
export {}
// ===== JEST ====
/**
* @typedef {import('ts-jest/dist/jest-types').TsJestConfig} TsJestConfig
*/
// @TODO https://github.com/Microsoft/TypeScript/issues/24916
/**
* @typedef {Partial<jest.ProjectConfig & jest.GlobalConfig>} JestConfig
*/
/**
* @typedef {typeof import('jest-config').defaults} JestDefaultConfig
*/
// ==== PRETTIER ====
/**
* @typedef {import('prettier').Options} PrettierConfig
*/
// ==== ROLLUP ====
/**
* @typedef {import('rollup').InputOptions & { output: import('rollup').OutputOptions | Array<import('rollup').OutputOptions | null> }} RollupConfig
*/
/**
* @typedef {import('rollup').Plugin} RollupPlugin
*/