From f7cd88ed61342e357a9f4c7b126f564fad30249e Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Tue, 7 Jan 2025 14:11:02 -0500 Subject: [PATCH] chore(build): Switch from microbundle to vite lib mode --- package.json | 7 ++++--- src/api/carto-api-error.ts | 2 +- src/api/endpoints.ts | 2 +- src/api/index.ts | 4 ++-- src/constants-internal.ts | 6 ------ src/global.d.ts | 8 -------- src/index.ts | 6 +++--- src/models/model.ts | 1 - tsconfig.json | 3 ++- vite.config.ts | 26 ++++++++++++++++++++++++++ yarn.lock | 17 +++++++++++++++++ 11 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 vite.config.ts diff --git a/package.json b/package.json index 5051478..a17171e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ ".": { "types": "./build/index.d.ts", "require": "./build/api-client.cjs", - "default": "./build/api-client.modern.js" + "default": "./build/api-client.js" } }, "browserslist": [ @@ -29,8 +29,8 @@ "node >= 18" ], "scripts": { - "build": "microbundle --format cjs,modern --no-compress --define __CARTO_API_CLIENT_VERSION=$npm_package_version", - "build:watch": "microbundle watch --format cjs,modern --no-compress --define __CARTO_API_CLIENT_VERSION=$npm_package_version", + "build": "vite build", + "build:watch": "vite build --watch", "dev": "concurrently \"yarn build:watch\" \"vite --config examples/vite.config.ts --open\"", "test": "vitest run --typecheck", "test:watch": "vitest watch --typecheck", @@ -92,6 +92,7 @@ "react-dom": "^18.3.1", "react-map-gl": "^7.1.7", "rimraf": "^3.0.2", + "rollup-plugin-typescript2": "^0.36.0", "semver": "^7.6.3", "svelte": "^4.2.17", "typescript": "~5.3.3", diff --git a/src/api/carto-api-error.ts b/src/api/carto-api-error.ts index 7257538..af88c64 100644 --- a/src/api/carto-api-error.ts +++ b/src/api/carto-api-error.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors -import {MapType} from '../types'; +import type {MapType} from '../types'; export type APIRequestType = | 'Map data' diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index e36a178..9d305be 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors -import {MapType} from '../types.js'; +import type {MapType} from '../types.js'; export type V3Endpoint = 'maps' | 'stats' | 'sql'; diff --git a/src/api/index.ts b/src/api/index.ts index 4531ee2..f8a2d29 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -4,8 +4,8 @@ export { CartoAPIError, - APIErrorContext, - APIRequestType, + type APIErrorContext, + type APIRequestType, } from './carto-api-error.js'; // Internal, but required for fetchMap(). export {buildPublicMapUrl, buildStatsUrl} from './endpoints.js'; diff --git a/src/constants-internal.ts b/src/constants-internal.ts index 811dd34..246fc39 100644 --- a/src/constants-internal.ts +++ b/src/constants-internal.ts @@ -1,9 +1,3 @@ -/** - * Current version of @carto/api-client. - * @internal - */ -export const API_CLIENT_VERSION = __CARTO_API_CLIENT_VERSION; - /** @internal */ export const V3_MINOR_VERSION = '3.4'; diff --git a/src/global.d.ts b/src/global.d.ts index f523574..f652eb0 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,10 +1,2 @@ -/** - * Injected by microbundle, with: - * ``` - * --define __CARTO_API_CLIENT_VERSION=$npm_package_version - * ``` - */ -declare const __CARTO_API_CLIENT_VERSION: string; - /** Defined by @deck.gl/core. */ declare const deck: {VERSION: string | undefined} | undefined; diff --git a/src/index.ts b/src/index.ts index ff0b61a..931b467 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,10 +7,10 @@ export * from './widget-sources/index.js'; export * from './types.js'; export { - APIErrorContext, - APIRequestType, + type APIErrorContext, + type APIRequestType, CartoAPIError, - QueryOptions, + type QueryOptions, buildPublicMapUrl, // Internal, but required for fetchMap(). buildStatsUrl, // Internal, but required for fetchMap(). query, diff --git a/src/models/model.ts b/src/models/model.ts index c890775..7bd16c5 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -6,7 +6,6 @@ import { QueryParameters, SpatialFilter, } from '../types.js'; -import {$TODO} from '../types-internal.js'; import {assert, isPureObject} from '../utils.js'; import {ModelRequestOptions, makeCall} from './common.js'; import {ApiVersion} from '../constants.js'; diff --git a/tsconfig.json b/tsconfig.json index 5d67631..d742fe3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ "lib": ["es2022", "DOM", "DOM.Iterable"], "types": ["vite/client"], "jsx": "react", - "strict": true + "strict": true, + "isolatedModules": true } } diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..b7d0191 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,26 @@ +import {resolve} from 'path'; +import {PluginOption, defineConfig} from 'vite'; +import {readFile} from 'node:fs/promises'; +import typescript from 'rollup-plugin-typescript2'; + +const pkg = JSON.parse(await readFile('./package.json', 'utf-8')); + +// NOTE: 'isolatedModules' must be enabled in tsconfig.json, +// see https://github.com/vitejs/vite/discussions/16680. +export default defineConfig({ + plugins: [{...typescript(), enforce: 'pre'} as PluginOption], + build: { + outDir: 'build', + target: 'esnext', + minify: false, + lib: { + entry: resolve(__dirname, 'src/index.ts'), + fileName: 'api-client', + formats: ['es', 'cjs'], + }, + rollupOptions: { + // Externalize deps that shouldn't be bundled. + external: Object.keys(pkg.dependencies), + }, + }, +}); diff --git a/yarn.lock b/yarn.lock index 3de8e8d..b1a26e8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1486,6 +1486,7 @@ __metadata: react-dom: "npm:^18.3.1" react-map-gl: "npm:^7.1.7" rimraf: "npm:^3.0.2" + rollup-plugin-typescript2: "npm:^0.36.0" semver: "npm:^7.6.3" svelte: "npm:^4.2.17" typescript: "npm:~5.3.3" @@ -8573,6 +8574,22 @@ __metadata: languageName: node linkType: hard +"rollup-plugin-typescript2@npm:^0.36.0": + version: 0.36.0 + resolution: "rollup-plugin-typescript2@npm:0.36.0" + dependencies: + "@rollup/pluginutils": "npm:^4.1.2" + find-cache-dir: "npm:^3.3.2" + fs-extra: "npm:^10.0.0" + semver: "npm:^7.5.4" + tslib: "npm:^2.6.2" + peerDependencies: + rollup: ">=1.26.3" + typescript: ">=2.4.0" + checksum: 10c0/3c8d17cd852ded36eaad2759caf170f90e091d8f86ff7b016d1823bc8b507b8f689156bcccda348fc88471681dc79cc9eb13ddb09a4dfcf0d07ac9a249e2d79b + languageName: node + linkType: hard + "rollup-plugin-visualizer@npm:^5.6.0": version: 5.12.0 resolution: "rollup-plugin-visualizer@npm:5.12.0"