Skip to content

Commit

Permalink
refactor: ♻️ convert package to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed May 22, 2024
1 parent 1b5b61d commit 89e23d3
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
jsx: true,
ecmaVersion: 2018
ecmaVersion: 2018,
sourceType: "module"
},
overrides: [
// linting for js files
Expand Down
52 changes: 52 additions & 0 deletions .rollup/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import typescript from '@rollup/plugin-typescript'
import replace from '@rollup/plugin-replace'
import fs from 'fs'

const defaultOptions = () => ({
// additional variables to define with '@rollup/plugin-replace'
// e.g. '{ ABC: 123 }' is equivalent to running 'globalThis.ABC = 123'
additionalReplacements: {},
// additional external dependencies, such as '@bugsnag/browser'
external: [],
// the entry point for the bundle
internal: undefined,
})

function createRollupConfig (options = defaultOptions()) {
const packageJson = JSON.parse(fs.readFileSync(`${process.cwd()}/package.json`))

return {
input: options.internal || 'lib/index.ts',
output: {
dir: 'dist',
format: 'esm',
preserveModules: true,
generatedCode: {
preset: 'es2015',
},
},
external: ['@bugsnag/core'].concat(options.external),
plugins: [
replace({
preventAssignment: true,
values: {
__VERSION__: packageJson.version,
...options.additionalReplacements,
},
}),
typescript({
// don't output anything if there's a TS error
noEmitOnError: true,
// turn on declaration files and declaration maps
compilerOptions: {
declaration: true,
declarationMap: true,
emitDeclarationOnly: true,
declarationDir: 'dist/types',
},
}),
],
}
}

export default createRollupConfig
16 changes: 16 additions & 0 deletions .rollup/no-tree-shaking.plugin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// when functional components are used in JSX tags, rollup doesn't recognise them as actually being used and tree-shakes them away
// this plugin prevents that from happening by explicity telling rollup not to tree-shake the module
function noTreeShakingPlugin(files) {
return {
name: 'no-treeshaking-plugin',
transform(code, id) {
for (const file of files) {
if (id.indexOf(file) >= 0) {
return { moduleSideEffects: 'no-treeshake' }
}
}
}
}
}

export default noTreeShakingPlugin
Loading

0 comments on commit 89e23d3

Please sign in to comment.