-
Notifications
You must be signed in to change notification settings - Fork 31
manual build via rollup #109
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const fs = require('fs'); | ||
const { cp, rm } = require('shelljs'); | ||
|
||
if (fs.existsSync('dist/ts-4.1')) { | ||
rm('-r', 'dist/ts-4.1'); | ||
} | ||
|
||
cp('-r', 'dist/ts/', 'dist/ts-4.1'); | ||
|
||
const stubTs41Types = ` | ||
import { EndpointDefinitions } from './endpointDefinitions'; | ||
export declare type TS41Hooks<Definitions extends EndpointDefinitions> = unknown; | ||
export {}; | ||
`; | ||
|
||
fs.writeFileSync('dist/ts/ts41Types.d.ts', stubTs41Types); | ||
|
||
fs.writeFileSync( | ||
'dist/index.js', | ||
` | ||
'use strict' | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./index.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./index.cjs.development.js') | ||
} | ||
` | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import babel from '@rollup/plugin-babel'; | ||
import typescript from '@rollup/plugin-typescript'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import replace from '@rollup/plugin-replace'; | ||
|
||
/** @type {import("rollup").RollupOptions} */ | ||
const defaultConfig = { | ||
input: 'src/index.ts', | ||
external: [/@babel\/runtime/, /@reduxjs\/toolkit/, /react$/, /react-redux/, /immer/, /tslib/], | ||
treeshake: { | ||
propertyReadSideEffects: false, | ||
}, | ||
}; | ||
|
||
const defaultTerserOptions = { | ||
output: { comments: false }, | ||
compress: { | ||
keep_infinity: true, | ||
pure_getters: true, | ||
passes: 10, | ||
}, | ||
ecma: 5, | ||
warnings: true, | ||
}; | ||
|
||
const defaultTsConfig = { declaration: false, declarationMap: false, target: 'ESNext', module: 'esnext' }; | ||
|
||
/** @type {import("rollup").RollupOptions} */ | ||
const configs = [ | ||
// ESM | ||
{ | ||
...defaultConfig, | ||
output: [ | ||
{ | ||
dir: 'dist/esm', | ||
format: 'es', | ||
sourcemap: true, | ||
preserveModules: true, | ||
}, | ||
], | ||
plugins: [ | ||
typescript(defaultTsConfig), | ||
babel({ | ||
exclude: 'node_modules/**', | ||
extensions: ['.js', '.ts'], | ||
babelHelpers: 'runtime', | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: { node: true, browsers: ['defaults', 'not IE 11', 'maintained node versions'] }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this now has two builds: an ESM build that does not bundle up the transpiled files, so just a file-for-file transpilation. This should help modern bundlers with tree-shaking quite a lot. The CJS build includes IE as well and builds only one file. Do you think that's sensible @markerikson ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference: including IE there bumps the bundle sizes for the ESM module up to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, fine by me. |
||
bugfixes: true, | ||
loose: true, | ||
}, | ||
], | ||
], | ||
plugins: [['@babel/plugin-transform-runtime', { useESModules: true }]], | ||
}), | ||
], | ||
}, | ||
// CJS: | ||
...withMinify((minified) => ({ | ||
...defaultConfig, | ||
output: [ | ||
{ | ||
dir: 'dist', | ||
format: 'cjs', | ||
sourcemap: true, | ||
entryFileNames: minified ? '[name].cjs.production.min.js' : '[name].cjs.development.js', | ||
}, | ||
], | ||
plugins: [ | ||
typescript( | ||
minified | ||
? defaultTsConfig | ||
: { ...defaultTsConfig, declarationDir: 'dist/ts', declaration: true, declarationMap: true } | ||
), | ||
replace({ | ||
values: { | ||
'process.env.NODE_ENV': JSON.stringify(minified ? 'production' : 'development'), | ||
}, | ||
}), | ||
babel({ | ||
exclude: 'node_modules/**', | ||
extensions: ['.js', '.ts'], | ||
babelHelpers: 'runtime', | ||
presets: [['@babel/preset-env', { targets: { node: true, browsers: ['defaults'] } }]], | ||
plugins: [['@babel/plugin-transform-runtime', { useESModules: false }]], | ||
}), | ||
...(minified ? [terser({ ...defaultTerserOptions, toplevel: true })] : []), | ||
], | ||
})), | ||
]; | ||
|
||
function withMinify(build) { | ||
return [build(false), build(true)]; | ||
} | ||
|
||
export default configs; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class HandledError { | ||
constructor(public readonly value: any) {} | ||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, I had no idea this was a thing! That's really cool!
Can we set this up for RTK too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will, as soon as I recreate the changes I did here over there ;)