build(api.js): simplify output JS files (#8025)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2023-10-24 00:22:00 +03:00 committed by GitHub
parent 93c8a77b34
commit a0f7b88fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 845 additions and 1326 deletions

View File

@ -69,7 +69,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'

View File

@ -20,7 +20,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: yarn
@ -40,7 +40,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: yarn

View File

@ -10,3 +10,4 @@ dist
/tooling/cli/node
/tooling/cli/schema.json
/core/tauri-config-schema/schema.json
CHANGELOG.md

5
.prettierrc Normal file
View File

@ -0,0 +1,5 @@
{
"singleQuote": true,
"semi": false,
"trailingComma": "none"
}

View File

@ -1,9 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
module.exports = {
singleQuote: true,
semi: false,
trailingComma: 'none'
}

View File

@ -21,12 +21,14 @@ const ignore = [
'node_modules',
'gen',
'dist',
'bundle.js',
'bundle.global.js'
]
async function checkFile(file) {
if (extensions.some((e) => file.endsWith(e)) && !ignore.some((i) => file.includes(`/${i}/`))) {
if (
extensions.some((e) => file.endsWith(e)) &&
!ignore.some((i) => file.includes(`/${i}/`) || path.basename(file) == i)
) {
const fileStream = fs.createReadStream(file)
const rl = readline.createInterface({
input: fileStream,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -480,12 +480,12 @@ dependencies = [
[[package]]
name = "cargo_toml"
version = "0.15.3"
version = "0.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "599aa35200ffff8f04c1925aa1acc92fa2e08874379ef42e210a80e527e60838"
checksum = "e3f9629bc6c4388ea699781dc988c2b99766d7679b151c81990b4fa1208fafd3"
dependencies = [
"serde",
"toml 0.7.8",
"toml 0.8.2",
]
[[package]]

View File

@ -11,7 +11,8 @@
"url": "https://github.com/tauri-apps/tauri.git"
},
"scripts": {
"format": "prettier --write --end-of-line=auto \"./**/*.{cjs,js,jsx,ts,tsx,html,css,json}\" --ignore-path .prettierignore",
"format": "prettier --write . --config .prettierrc --ignore-path .prettierignore",
"format:check": "prettier --check . --config .prettierrc --ignore-path .prettierignore",
"postinstall": "husky install"
},
"devDependencies": {

53
tooling/api/.eslintrc Normal file
View File

@ -0,0 +1,53 @@
{
"root": true,
"env": {
"node": true,
"jest": true
},
"parser": "@typescript-eslint/parser",
"extends": [
"standard-with-typescript",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
// TODO: make this work with typescript
// "plugin:node/recommended"
"prettier"
],
"plugins": ["@typescript-eslint", "node", "security"],
"parserOptions": {
"project": "./tsconfig.json"
},
"globals": {
"__statics": true,
"process": true
},
// add your custom rules here
"rules": {
"no-console": "error",
"no-debugger": "error",
"no-process-exit": "off",
"security/detect-non-literal-fs-filename": "warn",
"security/detect-unsafe-regex": "error",
"security/detect-buffer-noassert": "error",
"security/detect-child-process": "warn",
"security/detect-disable-mustache-escape": "error",
"security/detect-eval-with-expression": "error",
"security/detect-no-csrf-before-method-override": "error",
"security/detect-non-literal-regexp": "error",
"security/detect-non-literal-require": "warn",
"security/detect-object-injection": "warn",
"security/detect-possible-timing-attacks": "error",
"security/detect-pseudoRandomBytes": "error",
"space-before-function-paren": "off",
"@typescript-eslint/default-param-last": "off",
"@typescript-eslint/strict-boolean-expressions": 0,
"no-return-await": "warn",
"@typescript-eslint/return-await": "off"
}
}

View File

@ -1,56 +0,0 @@
module.exports = {
root: true,
env: {
node: true,
jest: true
},
parser: '@typescript-eslint/parser',
extends: [
'standard-with-typescript',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
// TODO: make this work with typescript
// 'plugin:node/recommended'
'prettier'
],
plugins: ['@typescript-eslint', 'node', 'security'],
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json'
},
globals: {
__statics: true,
process: true
},
// add your custom rules here
rules: {
// allow console.log during development only
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
// allow debugger during development only
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-process-exit': 'off',
'security/detect-non-literal-fs-filename': 'warn',
'security/detect-unsafe-regex': 'error',
'security/detect-buffer-noassert': 'error',
'security/detect-child-process': 'warn',
'security/detect-disable-mustache-escape': 'error',
'security/detect-eval-with-expression': 'error',
'security/detect-no-csrf-before-method-override': 'error',
'security/detect-non-literal-regexp': 'error',
'security/detect-non-literal-require': 'warn',
'security/detect-object-injection': 'warn',
'security/detect-possible-timing-attacks': 'error',
'security/detect-pseudoRandomBytes': 'error',
'space-before-function-paren': 'off',
'@typescript-eslint/default-param-last': 'off',
'@typescript-eslint/strict-boolean-expressions': 0,
'no-return-await': 'warn',
'@typescript-eslint/return-await': 'off'
}
}

View File

@ -1,4 +0,0 @@
babel.config.js
rollup.config.js
tsconfig.json
src/

View File

@ -1,5 +0,0 @@
module.exports = {
singleQuote: true,
semi: false,
trailingComma: 'none'
}

View File

@ -2,23 +2,10 @@
"name": "@tauri-apps/api",
"version": "2.0.0-alpha.9",
"description": "Tauri API definitions",
"type": "module",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/tauri"
},
"exports": {
"./package.json": "./package.json"
},
"scripts": {
"build": "yarn tsup && node ./scripts/after-build.cjs",
"npm-pack": "yarn build && cd ./dist && npm pack",
"npm-publish": "yarn build && cd ./dist && yarn publish --access public --loglevel silly --tag next",
"lint": "eslint --ext ts \"./src/**/*.ts\"",
"lint-fix": "eslint --fix --ext ts \"./src/**/*.ts\"",
"format": "prettier --write --end-of-line=auto \"./**/*.{cjs,js,jsx,ts,tsx,html,css,json}\" --ignore-path ../../.prettierignore",
"format:check": "prettier --check --end-of-line=auto \"./**/*.{cjs,js,jsx,ts,tsx,html,css,json}\" --ignore-path ../../.prettierignore"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tauri-apps/tauri.git"
@ -31,34 +18,43 @@
"url": "https://github.com/tauri-apps/tauri/issues"
},
"homepage": "https://github.com/tauri-apps/tauri#readme",
"publishConfig": {
"access": "public"
"type": "module",
"scripts": {
"build": "yarn rollup --config rollup.config.ts --configPlugin typescript",
"npm-pack": "yarn build && cd ./dist && npm pack",
"npm-publish": "yarn build && cd ./dist && yarn publish --access public --loglevel silly --tag next",
"check": "tsc",
"lint": "eslint --ext ts \"./src/**/*.ts\"",
"lint:fix": "eslint --fix --ext ts \"./src/**/*.ts\"",
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore --ignore-path ../../.prettierignore",
"format:check": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore --ignore-path ../../.prettierignore"
},
"devDependencies": {
"@rollup/plugin-terser": "0.4.4",
"@rollup/plugin-typescript": "11.1.5",
"@types/node": "20.8.7",
"@typescript-eslint/eslint-plugin": "6.9.0",
"@typescript-eslint/parser": "6.9.0",
"eslint": "8.52.0",
"eslint-config-prettier": "9.0.0",
"eslint-config-standard-with-typescript": "39.1.1",
"eslint-plugin-import": "2.29.0",
"eslint-plugin-n": "16.2.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-security": "1.7.1",
"fast-glob": "3.3.1",
"prettier": "3.0.3",
"rollup": "4.1.4",
"tslib": "2.6.2",
"typescript": "5.2.2"
},
"exports": {
"./package.json": "./package.json"
},
"engines": {
"node": ">= 18",
"npm": ">= 6.6.0",
"yarn": ">= 1.19.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "6.8.0",
"@typescript-eslint/parser": "6.8.0",
"eslint": "8.51.0",
"eslint-config-prettier": "9.0.0",
"eslint-config-standard-with-typescript": "39.1.1",
"eslint-plugin-import": "2.28.1",
"eslint-plugin-n": "16.2.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-security": "1.7.1",
"prettier": "3.0.3",
"tsup": "7.2.0",
"typedoc": "0.25.2",
"typedoc-plugin-markdown": "3.16.0",
"typedoc-plugin-mdn-links": "3.1.0",
"typescript": "5.2.2"
},
"resolutions": {
"semver": ">=7.5.2",
"optionator": ">=0.9.3"
}
}

View File

@ -0,0 +1,129 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { defineConfig, Plugin } from 'rollup'
import typescript from '@rollup/plugin-typescript'
import terser from '@rollup/plugin-terser'
import fg from 'fast-glob'
import { basename, join } from 'path'
import {
writeFileSync,
copyFileSync,
opendirSync,
rmSync,
Dir,
readFileSync
} from 'fs'
import { fileURLToPath } from 'url'
// cleanup dist dir
const __dirname = fileURLToPath(new URL('.', import.meta.url))
cleanDir(join(__dirname, './dist'))
const modules = fg.sync(['!./src/*.d.ts', './src/*.ts'])
export default defineConfig([
{
input: Object.fromEntries(modules.map((p) => [basename(p, '.ts'), p])),
output: [
{
format: 'esm',
dir: './dist',
entryFileNames: '[name].js',
preserveModules: true
},
{
format: 'cjs',
dir: './dist',
entryFileNames: '[name].cjs',
preserveModules: true
}
],
plugins: [
typescript({
declaration: true,
declarationDir: './dist',
rootDir: 'src'
}),
makeFlatPackageInDist()
]
},
{
input: 'src/index.ts',
output: {
format: 'iife',
name: '__TAURI_IIFE__',
file: '../../core/tauri/scripts/bundle.global.js',
footer: 'window.__TAURI__ = __TAURI_IIFE__'
},
plugins: [typescript(), terser()]
}
])
function makeFlatPackageInDist(): Plugin {
return {
name: 'makeFlatPackageInDist',
writeBundle() {
// append our api modules to `exports` in `package.json` then write it to `./dist`
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
const mods = modules.map((p) => basename(p).split('.')[0])
const outputPkg = {
...pkg,
devDependencies: {},
exports: Object.assign(
{},
...mods.map((mod) => {
let temp: Record<string, { import: string; require: string }> = {}
let key = `./${mod}`
if (mod === 'index') {
key = '.'
}
temp[key] = {
import: `./${mod}.js`,
require: `./${mod}.cjs`
}
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`
fg.sync('(LICENSE*|*.md)').forEach((f) => copyFileSync(f, `dist/${f}`))
}
}
}
function cleanDir(path: string) {
let dir: Dir
try {
dir = opendirSync(path)
} catch (err: any) {
switch (err.code) {
case 'ENOENT':
return // Noop when directory don't exists.
case 'ENOTDIR':
throw new Error(`'${path}' is not a directory.`)
default:
throw err
}
}
let file = dir.readSync()
while (file) {
const filePath = join(path, file.name)
rmSync(filePath, { recursive: true })
file = dir.readSync()
}
dir.closeSync()
}

View File

@ -1,44 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
const { readFileSync, readdirSync, writeFileSync, copyFileSync } = require('fs')
// 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.replace('.ts', ''))
const outputPkg = {
...pkg,
devDependencies: {},
exports: Object.assign(
{},
...modules.map((mod) => {
let temp = {}
let key = `./${mod}`
if (mod === 'index') {
key = '.'
}
temp[key] = {
import: `./${mod}.js`,
require: `./${mod}.cjs`
}
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}`))

View File

@ -1,14 +1,12 @@
{
"compilerOptions": {
"target": "es2020",
"target": "es2021",
"module": "esnext",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"skipLibCheck": true,
"strict": true,
"noUnusedLocals": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"noEmit": true
}
}

View File

@ -1,34 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { defineConfig } from 'tsup'
export default defineConfig(() => [
{
entry: ['src/*.ts'],
outDir: 'dist',
format: ['esm', 'cjs'],
clean: true,
minify: true,
platform: 'browser',
dts: {
resolve: true
}
},
{
entry: { bundle: 'src/index.ts' },
outDir: '../../core/tauri/scripts',
format: ['iife'],
globalName: '__TAURI_IIFE__',
clean: false,
minify: true,
platform: 'browser',
dts: false,
// 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__'
}
}
])

File diff suppressed because it is too large Load Diff

View File

@ -47,8 +47,7 @@
"prettier": "2.8.8"
},
"resolutions": {
"json5": "2.2.3",
"semver": ">=7.5.2"
"json5": "2.2.3"
},
"engines": {
"node": ">= 10"
@ -67,6 +66,5 @@
"tauri": "node ./tauri.js",
"format": "prettier --write ./package.json ./tauri.js",
"format:check": "prettier --check ./package.json ./tauri.js"
},
"dependencies": {}
}
}

View File

@ -103,7 +103,12 @@
"@babel/helper-split-export-declaration" "^7.22.6"
"@babel/helper-validator-identifier" "^7.22.20"
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.22.5", "@babel/helper-plugin-utils@^7.8.0":
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.8.0":
version "7.16.7"
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.16.7.tgz#aa3a8ab4c3cceff8e65eb9e73d87dc4ff320b2f5"
integrity sha512-Qg3Nk7ZxpgMrsox6HreY1ZNKdBq7K72tDSliA6dCl5f007jR4ne8iD5UzuNnCJH2xBf2BEEVGr+/OL6Gdp7RxA==
"@babel/helper-plugin-utils@^7.22.5":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.22.5.tgz#dd7ee3735e8a313b9f7b05a773d892e88e6d7295"
integrity sha512-uLls06UVKgFG9QD4OeFYLEGteMIAa5kpTPcFL28yuCIIzsf6ZyKZMllKVOCZFhiZ5ptnwX4mtKdWCBE/uT4amg==
@ -1156,12 +1161,7 @@ globals@^11.1.0:
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
integrity sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==
graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "4.2.9"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.9.tgz#041b05df45755e587a24942279b9d113146e1c96"
integrity sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==
graceful-fs@^4.2.9:
graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.9:
version "4.2.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"
integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==
@ -1610,7 +1610,7 @@ jest-snapshot@^29.7.0:
jest-transform-toml@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/jest-transform-toml/-/jest-transform-toml-1.0.0.tgz#0a255066d4e59620ec3c3cc5a555321bb05f153d"
integrity sha1-CiVQZtTlliDsPDzFpVUyG7BfFT0=
integrity sha512-S+U7zvCcBgtnwhnjJXYJWVHt7oe7xe3/AuOtVdFrd+YDn1ZXqXuQ6vgM/wFPfblNDUVXJzf9AP/FBGZyLx4/eA==
dependencies:
toml-loader "^1.0.0"
@ -1971,10 +1971,15 @@ resolve@^1.20.0:
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"
semver@>=7.5.2, semver@^6.3.0, semver@^6.3.1, semver@^7.5.3, semver@^7.5.4:
version "7.5.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.3.tgz#161ce8c2c6b4b3bdca6caadc9fa3317a4c4fe88e"
integrity sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==
semver@^6.3.0, semver@^6.3.1:
version "6.3.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==
semver@^7.5.3, semver@^7.5.4:
version "7.5.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e"
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==
dependencies:
lru-cache "^6.0.0"
@ -2124,7 +2129,7 @@ to-regex-range@^5.0.1:
toml-loader@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/toml-loader/-/toml-loader-1.0.0.tgz#05249b9294b623601148260caa480b22a653a19a"
integrity sha1-BSSbkpS2I2ARSCYMqkgLIqZToZo=
integrity sha512-/1xtg+pmD6IOuJBGtFp75hSofSVx3F8akeOS+31Rk1R5n8mYSZGKuaLM39qcxUm+SEx7zuntWR2puwYSntUKAg==
dependencies:
toml "^2.2.2"

View File

@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'