-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: improve build & bundle * chore: remove unused deps * chore: jest ignore /test/ folder * ci: fetch-dept: 2 for tests * chore: infer return types
- Loading branch information
Showing
21 changed files
with
355 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Config } from '@jest/types'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const pack = require('./package'); | ||
|
||
const config: Config.InitialOptions = { | ||
roots: ['<rootDir>/src'], | ||
transform: { | ||
'.ts': 'ts-jest', | ||
}, | ||
coveragePathIgnorePatterns: ['/node_modules/', '/test/'], | ||
moduleDirectories: ['node_modules', 'src'], | ||
moduleNameMapper: { | ||
'\\.(css|less|scss)$': 'identity-obj-proxy', | ||
}, | ||
setupFilesAfterEnv: ['<rootDir>/src/test/setup.ts'], | ||
displayName: pack.name, | ||
name: pack.name, | ||
testEnvironment: 'jest-environment-jsdom-sixteen', | ||
globals: { | ||
'ts-jest': { tsconfig: 'tsconfig.jest.json' }, | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-disable import/no-dynamic-require */ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const path = require('path'); | ||
|
||
require('ts-node').register({ | ||
compilerOptions: { | ||
module: 'CommonJS', | ||
}, | ||
}); | ||
|
||
const { bundle } = require(path.join(process.cwd(), 'rollup', 'builder')); | ||
|
||
module.exports = bundle(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
/* eslint-disable import/no-dynamic-require */ | ||
/* eslint-disable global-require */ | ||
import path from 'path'; | ||
|
||
import { createRollupConfig, Options } from './createRollupConfig'; | ||
import { writeCjsEntryFile, getNameFromMain } from './writeCjsEntry'; | ||
|
||
type Format = 'cjs' | 'esm' | 'umd'; | ||
|
||
type FormatOptions = Pick<Options, 'env' | 'format'>; | ||
type BaseOption = Pick<FormatOptions, 'env'>; | ||
|
||
const OPTIONS: Record<Format, BaseOption[]> = { | ||
cjs: [{ env: 'development' }, { env: 'production' }], | ||
esm: [{}], | ||
umd: [{ env: 'development' }, { env: 'production' }], | ||
}; | ||
|
||
type Config = { | ||
name: string; | ||
packageName: string; | ||
source: string; | ||
formats: Format[]; | ||
}; | ||
|
||
const prepare = ({ name, formats, source, packageName }: Config) => { | ||
const uniqueFormats = [...new Set(formats)]; | ||
const rollupOptions = uniqueFormats.reduce((acc: FormatOptions[], format) => { | ||
const formatOptions = OPTIONS[format].map((o: BaseOption) => ({ | ||
...o, | ||
format, | ||
})); | ||
return [...acc, ...formatOptions]; | ||
}, []); | ||
|
||
return rollupOptions.map((option: FormatOptions) => | ||
createRollupConfig({ | ||
...option, | ||
input: source, | ||
name, | ||
tsconfig: 'tsconfig.build.json', | ||
packageName, | ||
}) | ||
); | ||
}; | ||
|
||
export const bundle = () => { | ||
const pkg = require(path.join(process.cwd(), 'package.json')); | ||
const formats: Format[] = ['cjs']; | ||
const name = getNameFromMain(pkg.main); | ||
|
||
if (pkg.module || pkg['jsnext:main']) { | ||
formats.push('esm'); | ||
} | ||
|
||
if (pkg['umd:main']) { | ||
formats.push('umd'); | ||
} | ||
|
||
writeCjsEntryFile(pkg.main); | ||
|
||
return prepare({ name, formats, source: pkg.source, packageName: pkg.name }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable global-require */ | ||
import path from 'path'; | ||
|
||
import external from 'rollup-plugin-peer-deps-external'; | ||
import resolve from '@rollup/plugin-node-resolve'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import typescript from 'rollup-plugin-typescript2'; | ||
import sourcemaps from 'rollup-plugin-sourcemaps'; | ||
import replace from '@rollup/plugin-replace'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import type { ModuleFormat } from 'rollup'; | ||
|
||
import { safePackageName } from './safePackageName'; | ||
import { pascalcase } from './pascalcase'; | ||
|
||
export type Options = { | ||
packageName: string; | ||
input: string; | ||
format: ModuleFormat; | ||
name?: string; | ||
umdName?: string; | ||
minify?: boolean; | ||
env?: string; | ||
tsconfig?: string; | ||
}; | ||
|
||
export const createRollupConfig = (options: Options) => { | ||
const name = options.name || safePackageName(options.packageName); | ||
const umdName = | ||
options.umdName || pascalcase(safePackageName(options.packageName)); | ||
|
||
const shouldMinify = options.minify || options.env === 'production'; | ||
const tsconfigPath = options.tsconfig || 'tsconfig.json'; | ||
|
||
const outputName = [ | ||
path.join('dist', name), | ||
options.format, | ||
options.env, | ||
shouldMinify ? 'min' : '', | ||
'js', | ||
] | ||
.filter(Boolean) | ||
.join('.'); | ||
|
||
const plugins = [ | ||
external(), | ||
typescript({ typescript: require('typescript'), tsconfig: tsconfigPath }), | ||
resolve({ mainFields: ['browser', 'jsnext:main', 'module', 'main'] }), | ||
]; | ||
|
||
if (options.format === 'umd') { | ||
plugins.push(commonjs({ include: /\/node_modules\// })); | ||
} | ||
|
||
if (options.env !== undefined) { | ||
plugins.push( | ||
replace({ 'process.env.NODE_ENV': JSON.stringify(options.env) }) | ||
); | ||
} | ||
|
||
plugins.push(sourcemaps()); | ||
|
||
if (shouldMinify) { | ||
plugins.push( | ||
terser({ | ||
format: { comments: false }, | ||
compress: { drop_console: true }, | ||
}) | ||
); | ||
} | ||
|
||
return { | ||
input: options.input, | ||
output: { | ||
file: outputName, | ||
format: options.format, | ||
name: umdName, | ||
sourcemap: true, | ||
globals: { | ||
react: 'React', | ||
'react-dom': 'ReactDOM', | ||
}, | ||
exports: 'named', | ||
}, | ||
plugins, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import isFunction from 'lodash/isFunction'; | ||
import isNil from 'lodash/isNil'; | ||
|
||
const titlecase = (input: string) => | ||
input[0].toLocaleUpperCase() + input.slice(1); | ||
|
||
export const pascalcase = (value: string) => { | ||
if (isNil(value)) { | ||
return ''; | ||
} | ||
if (!isFunction(value.toString)) { | ||
return ''; | ||
} | ||
|
||
const input = value.toString().trim(); | ||
if (input === '') { | ||
return ''; | ||
} | ||
if (input.length === 1) { | ||
return input.toLocaleUpperCase(); | ||
} | ||
|
||
const match = input.match(/[a-zA-Z0-9]+/g); | ||
if (match) { | ||
return match.map((m) => titlecase(m)).join(''); | ||
} | ||
|
||
return input; | ||
}; |
Oops, something went wrong.