Skip to content

Commit

Permalink
refactor: simplify api bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Aug 31, 2023
1 parent e152662 commit 50dd2b1
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 219 deletions.
6 changes: 5 additions & 1 deletion tooling/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
"type": "opencollective",
"url": "https://opencollective.com/tauri"
},
"main": "./index.cjs",
"module": "./index.js",
"types": "./index.d.ts",
"exports": {
"./package.json": "./package.json"
},
"scripts": {
"build": "yarn tsup && node ./scripts/after-build.cjs",
"build": "yarn tsup",
"npm-pack": "yarn build && cd ./dist && npm pack",
"npm-publish": "yarn build && cd ./dist && yarn publish --access public --loglevel silly",
"lint": "eslint --ext ts \"./src/**/*.ts\"",
Expand Down Expand Up @@ -40,6 +43,7 @@
"yarn": ">= 1.19.1"
},
"devDependencies": {
"@types/node": "^20.5.7",
"@typescript-eslint/eslint-plugin": "5.62.0",
"@typescript-eslint/parser": "5.62.0",
"eslint": "8.46.0",
Expand Down
44 changes: 0 additions & 44 deletions tooling/api/scripts/after-build.cjs

This file was deleted.

4 changes: 2 additions & 2 deletions tooling/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"compilerOptions": {
"target": "es2020",
"target": "es2021",
"module": "esnext",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"skipLibCheck": true,
"noUnusedLocals": true,
"noImplicitAny": true,
Expand Down
63 changes: 54 additions & 9 deletions tooling/api/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,77 @@
// SPDX-License-Identifier: MIT

import { defineConfig } from 'tsup'
import { readFileSync, readdirSync, writeFileSync, copyFileSync } from 'fs'

export default defineConfig(() => [
{
entry: ['src/*.ts'],
entry: ['src/**/*.ts'],
outDir: 'dist',
format: ['esm', 'cjs'],
clean: true,
minify: true,
platform: 'browser',
dts: {
resolve: true
}
target: 'es2021',
clean: true,
minify: false,
bundle: false,
dts: { resolve: true },
onSuccess
},
{
entry: { bundle: 'src/index.ts' },
outDir: '../../core/tauri/scripts',
format: ['iife'],
globalName: '__TAURI_IIFE__',
format: 'iife',
platform: 'browser',
target: 'es2021',
clean: false,
minify: true,
platform: 'browser',
dts: false,
globalName: '__TAURI_IIFE__',
// esbuild `globalName` option generates `var __TAURI_IIFE__ = (() => {})()`
// and var is not guaranteed to assign to the global `window` object so we make sure to assign it
footer: {
js: 'window.__TAURI__ = __TAURI_IIFE__'
}
}
])

async function onSuccess() {
// append our api modules to `exports` in `package.json` then write it to `./dist`
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
const modules = readdirSync('src')
.filter((e) => e !== 'helpers')
.map((mod) => mod.split('.')[0])

const outputPkg = {
...pkg,
devDependencies: {},
exports: Object.assign(
{},
...modules.map((mod) => {
let temp = {}
let key = `./${mod}`
if (mod === 'index') {
key = '.'
}
// @ts-expect-error
temp[key] = {
import: `./${mod}.js`,
require: `./${mod}.cjs`,
types: `./${mod}.d.ts`
}
return temp
}),
// if for some reason in the future we manually add something in the `exports` field
// this will ensure it doesn't get overwritten by the logic above
{ ...(pkg.exports || {}) }
)
}
writeFileSync('dist/package.json', JSON.stringify(outputPkg, undefined, 2))

// copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
const dir = readdirSync('.')
const files = [
...dir.filter((f) => f.startsWith('LICENSE')),
...dir.filter((f) => f.endsWith('.md'))
]
files.forEach((f) => copyFileSync(f, `dist/${f}`))
}
Loading

0 comments on commit 50dd2b1

Please sign in to comment.