| | import { safeVariableName, safePackageName, external } from './utils'; |
| | import { paths } from './constants'; |
| | import { RollupOptions } from 'rollup'; |
| | import { terser } from 'rollup-plugin-terser'; |
| | import { DEFAULT_EXTENSIONS as DEFAULT_BABEL_EXTENSIONS } from '@babel/core'; |
| | import commonjs from '@rollup/plugin-commonjs'; |
| | import json from '@rollup/plugin-json'; |
| | import replace from '@rollup/plugin-replace'; |
| | import resolve, { |
| | DEFAULTS as RESOLVE_DEFAULTS, |
| | } from '@rollup/plugin-node-resolve'; |
| | import sourceMaps from 'rollup-plugin-sourcemaps'; |
| | import typescript from 'rollup-plugin-typescript2'; |
| | import ts from 'typescript'; |
| |
|
| | import { extractErrors } from './errors/extractErrors'; |
| | import { babelPluginTsdx } from './babelPluginTsdx'; |
| | import { TsdxOptions } from './types'; |
| |
|
| | const errorCodeOpts = { |
| | errorMapFilePath: paths.appErrorsJson, |
| | }; |
| |
|
| | |
| | let shebang: any = {}; |
| |
|
| | export async function createRollupConfig( |
| | opts: TsdxOptions, |
| | outputNum: number |
| | ): Promise<RollupOptions> { |
| | const findAndRecordErrorCodes = await extractErrors({ |
| | ...errorCodeOpts, |
| | ...opts, |
| | }); |
| |
|
| | const isEsm = opts.format.includes('es') || opts.format.includes('esm'); |
| |
|
| | const shouldMinify = |
| | opts.minify !== undefined ? opts.minify : opts.env === 'production' || isEsm; |
| |
|
| | let formatString = ['esm', 'cjs'].includes(opts.format) ? '' : opts.format; |
| | let fileExtension = opts.format === 'esm' ? 'mjs' : 'cjs'; |
| |
|
| | const outputName = [ |
| | `${paths.appDist}/${safePackageName(opts.name)}`, |
| | formatString, |
| | opts.env, |
| | shouldMinify ? 'min' : '', |
| | fileExtension, |
| | ] |
| | .filter(Boolean) |
| | .join('.'); |
| |
|
| | const tsconfigPath = opts.tsconfig || paths.tsconfigJson; |
| | |
| | const tsconfigJSON = ts.readConfigFile(tsconfigPath, ts.sys.readFile).config; |
| | |
| | const tsCompilerOptions = ts.parseJsonConfigFileContent( |
| | tsconfigJSON, |
| | ts.sys, |
| | './' |
| | ).options; |
| |
|
| | return { |
| | |
| | input: opts.input, |
| | |
| | external: (id: string) => { |
| | |
| | if (id.startsWith('regenerator-runtime')) { |
| | return false; |
| | } |
| |
|
| | return external(id); |
| | }, |
| | |
| | treeshake: { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | propertyReadSideEffects: false, |
| | }, |
| | |
| | output: { |
| | |
| | file: outputName, |
| | |
| | format: opts.format, |
| | |
| | |
| | freeze: false, |
| | |
| | esModule: Boolean(tsCompilerOptions?.esModuleInterop), |
| | name: opts.name || safeVariableName(opts.name), |
| | sourcemap: true, |
| | globals: { react: 'React', 'react-native': 'ReactNative', 'lodash-es': 'lodashEs', 'lodash/fp': 'lodashFp' }, |
| | exports: 'named', |
| | }, |
| | plugins: [ |
| | !!opts.extractErrors && { |
| | async transform(code: string) { |
| | try { |
| | await findAndRecordErrorCodes(code); |
| | } catch (e) { |
| | return null; |
| | } |
| | return { code, map: null }; |
| | }, |
| | }, |
| | resolve({ |
| | mainFields: [ |
| | 'module', |
| | 'main', |
| | opts.target !== 'node' ? 'browser' : undefined, |
| | ].filter(Boolean) as string[], |
| | extensions: [...RESOLVE_DEFAULTS.extensions, '.cjs', '.mjs', '.jsx'], |
| | }), |
| | |
| | commonjs({ |
| | |
| | include: |
| | opts.format === 'umd' |
| | ? /\/node_modules\// |
| | : /\/regenerator-runtime\//, |
| | }), |
| | json(), |
| | { |
| | |
| | |
| | |
| | |
| | |
| | transform(code: string) { |
| | let reg = /^#!(.*)/; |
| | let match = code.match(reg); |
| |
|
| | shebang[opts.name] = match ? '#!' + match[1] : ''; |
| |
|
| | code = code.replace(reg, ''); |
| |
|
| | return { |
| | code, |
| | map: null, |
| | }; |
| | }, |
| | }, |
| | typescript({ |
| | typescript: ts, |
| | tsconfig: opts.tsconfig, |
| | tsconfigDefaults: { |
| | exclude: [ |
| | |
| | '**/*.spec.ts', |
| | '**/*.test.ts', |
| | '**/*.spec.tsx', |
| | '**/*.test.tsx', |
| | |
| | 'node_modules', |
| | 'bower_components', |
| | 'jspm_packages', |
| | paths.appDist, |
| | ], |
| | compilerOptions: { |
| | sourceMap: true, |
| | declaration: true, |
| | jsx: 'react', |
| | }, |
| | }, |
| | tsconfigOverride: { |
| | compilerOptions: { |
| | |
| | target: 'esnext', |
| | |
| | ...(outputNum > 0 |
| | ? { declaration: false, declarationMap: false } |
| | : {}), |
| | }, |
| | }, |
| | check: !opts.transpileOnly && outputNum === 0, |
| | useTsconfigDeclarationDir: Boolean(tsCompilerOptions?.declarationDir), |
| | }), |
| | babelPluginTsdx({ |
| | exclude: 'node_modules/**', |
| | extensions: [...DEFAULT_BABEL_EXTENSIONS, 'ts', 'tsx'], |
| | passPerPreset: true, |
| | custom: { |
| | targets: opts.target === 'node' ? { node: '14' } : undefined, |
| | extractErrors: opts.extractErrors, |
| | format: opts.format, |
| | }, |
| | babelHelpers: 'bundled', |
| | }), |
| | opts.env !== undefined && |
| | replace({ |
| | preventAssignment: true, |
| | 'process.env.NODE_ENV': JSON.stringify(opts.env), |
| | }), |
| | sourceMaps(), |
| | shouldMinify && |
| | terser({ |
| | output: { comments: false }, |
| | compress: { |
| | keep_infinity: true, |
| | pure_getters: true, |
| | passes: 10, |
| | }, |
| | ecma: opts.legacy ? 5 : 2020, |
| | module: isEsm, |
| | toplevel: opts.format === 'cjs' || isEsm, |
| | warnings: true, |
| | }), |
| | |
| | |
| | |
| | |
| | |
| | { |
| | renderChunk: async (code: string, chunk: any) => { |
| | if (chunk.exports.includes('default') || !isEsm) { |
| | return null; |
| | } |
| |
|
| | return { |
| | code: `${code}\nexport default {};`, |
| | map: null, |
| | }; |
| | }, |
| | }, |
| | ], |
| | }; |
| | } |
| |
|