Skip to content
Permalink
1f6a1b6819
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
136 lines (133 sloc) 3.98 KB
import {
safeVariableName,
safePackageName,
resolveApp,
removeScope,
external,
} from './utils';
import { paths } from './constants';
import { sizeSnapshot } from 'rollup-plugin-size-snapshot';
import { terser } from 'rollup-plugin-terser';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import replace from 'rollup-plugin-replace';
import resolve from 'rollup-plugin-node-resolve';
import sourceMaps from 'rollup-plugin-sourcemaps';
import typescript from 'rollup-plugin-typescript2';
import shebangPlugin from '@jaredpalmer/rollup-plugin-preserve-shebang';
const replacements = [{ original: 'lodash', replacement: 'lodash-es' }];
const babelOptions = {
exclude: /node_modules/,
plugins: [
require.resolve('babel-plugin-annotate-pure-calls'),
require.resolve('babel-plugin-dev-expression'),
[require.resolve('babel-plugin-transform-rename-import'), { replacements }],
],
};
export function createRollupConfig(
format: 'cjs' | 'umd' | 'es',
env: 'development' | 'production',
opts: { input: string; name: string; target: 'node' | 'browser' }
) {
let shebang;
return {
// Tell Rollup the entry point to the package
input: opts.input,
// Tell Rollup which packages to ignore
external,
// Establish Rollup output
output: {
// Set filenames of the consumer's package
file: `${paths.appDist}/${safePackageName(
opts.name
)}.${format}.${env}.js`,
// Pass through the file format
format,
// Do not let Rollup call Object.freeze() on namespace import objects
// (i.e. import * as namespaceImportObject from...) that are accessed dynamically.
freeze: false,
// Do not let Rollup add a `__esModule: true` property when generating exports for non-ESM formats.
esModule: 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 on objects.
//
// @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' },
exports: 'named',
},
plugins: [
resolve({
mainFields: [
'module',
'main',
opts.target !== 'node' ? 'browser' : undefined,
].filter(Boolean) as string[],
}),
format === 'umd' &&
commonjs({
// use a regex to make sure to include eventual hoisted packages
include: /\/node_modules\//,
}),
json(),
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({
printInfo: false,
}),
env === 'production' &&
terser({
sourcemap: true,
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true,
collapse_vars: false,
},
ecma: 5,
toplevel: format === 'es' || format === 'cjs',
warnings: true,
}),
shebangPlugin({
shebang,
}),
],
};
}