From 2a5e5a5595ba85c68af9f9fc12370f90ec224d5c Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Thu, 19 Dec 2019 10:10:49 -0500 Subject: [PATCH] (types/fix): explicit Rollup typing, fix treeshake location (#371) - treeshake was in output, but it's not a config of output :/ :/ :/ - once explicit types were added, this was a big red underline :/ :/ --- src/createRollupConfig.ts | 47 +++++++++++++++++++++------------------ src/index.ts | 2 +- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index 3669f7509..4ddba4926 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -5,6 +5,7 @@ import { resolveApp, } from './utils'; import { paths } from './constants'; +import { RollupOptions } from 'rollup'; import { terser } from 'rollup-plugin-terser'; import { DEFAULT_EXTENSIONS } from '@babel/core'; // import babel from 'rollup-plugin-babel'; @@ -26,7 +27,9 @@ const errorCodeOpts = { // shebang cache map thing because the transform only gets run once let shebang: any = {}; -export async function createRollupConfig(opts: TsdxOptions) { +export async function createRollupConfig( + opts: TsdxOptions +): Promise { const findAndRecordErrorCodes = await extractErrors({ ...errorCodeOpts, ...opts, @@ -60,6 +63,27 @@ export async function createRollupConfig(opts: TsdxOptions) { } return external(id); }, + // Rollup has treeshaking by default, but we can optimize it further... + treeshake: { + // We assume reading a property of an object never has side-effects. + // This means tsdx WILL remove getters and setters defined directly on objects. + // Any getters or setters defined on classes will not be effected. + // + // @example + // + // const foo = { + // get bar() { + // console.log('effect'); + // return 'bar'; + // } + // } + // + // const result = foo.bar; + // const illegalAccess = foo.quux.tooDeep; + // + // Punchline....Don't use getters and setters + propertyReadSideEffects: false, + }, // Establish Rollup output output: { // Set filenames of the consumer's package @@ -71,27 +95,6 @@ export async function createRollupConfig(opts: TsdxOptions) { freeze: false, // Respect tsconfig esModuleInterop when setting __esModule. esModule: tsconfigJSON ? tsconfigJSON.esModuleInterop : false, - // Rollup has treeshaking by default, but we can optimize it further... - treeshake: { - // We assume reading a property of an object never has side-effects. - // This means tsdx WILL remove getters and setters defined directly on objects. - // Any getters or setters defined on classes will not be effected. - // - // @example - // - // const foo = { - // get bar() { - // console.log('effect'); - // return 'bar'; - // } - // } - // - // const result = foo.bar; - // const illegalAccess = foo.quux.tooDeep; - // - // Punchline....Don't use getters and setters - propertyReadSideEffects: false, - }, name: opts.name || safeVariableName(opts.name), sourcemap: true, globals: { react: 'React', 'react-native': 'ReactNative' }, diff --git a/src/index.ts b/src/index.ts index 7e608b5ea..3c9155657 100755 --- a/src/index.ts +++ b/src/index.ts @@ -49,7 +49,7 @@ try { // check for custom tsdx.config.js let tsdxConfig = { - rollup(config: any, _options: any) { + rollup(config: RollupOptions, _options: TsdxOptions): RollupOptions { return config; }, };