From bf8af9f45d3c68b9bd4aef116c2d5a3e1b66714b Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Thu, 11 Apr 2024 16:44:16 +0200 Subject: [PATCH] fix(build): fix typescript error when importing delivery options --- .../private/getSharedConfig.ts | 11 ++++++-- apps/sandbox/vite.config.ts | 5 ++-- libs/build-vite/index.d.ts | 4 ++- libs/build-vite/index.js | 24 +++++++++++++++- libs/shared/package.json | 19 ++++--------- libs/shared/src/data/constants.ts | 15 ++++++---- libs/shared/vite.config.ts | 28 +++++++++++++++++++ libs/shared/vitest.config.ts | 8 ------ package.json | 6 +--- tsconfig.base.json | 5 ++++ 10 files changed, 84 insertions(+), 41 deletions(-) create mode 100644 libs/shared/vite.config.ts delete mode 100644 libs/shared/vitest.config.ts diff --git a/apps/delivery-options/private/getSharedConfig.ts b/apps/delivery-options/private/getSharedConfig.ts index d1f30fbe..15d56759 100644 --- a/apps/delivery-options/private/getSharedConfig.ts +++ b/apps/delivery-options/private/getSharedConfig.ts @@ -1,5 +1,6 @@ import {fileURLToPath} from 'node:url'; import {type UserConfig, type ConfigEnv} from 'vite'; +import {resolveAlias} from '@myparcel-do/build-vite'; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export const getSharedConfig = ({mode}: ConfigEnv) => { @@ -12,9 +13,13 @@ export const getSharedConfig = ({mode}: ConfigEnv) => { }, resolve: { - alias: { - pinia: fileURLToPath(new URL('../../../node_modules/pinia/dist/pinia.prod.cjs', import.meta.url)), - }, + alias: [ + ...resolveAlias, + { + find: 'pinia', + replacement: fileURLToPath(new URL('../../../node_modules/pinia/dist/pinia.prod.cjs', import.meta.url)), + }, + ], }, } satisfies Partial; }; diff --git a/apps/sandbox/vite.config.ts b/apps/sandbox/vite.config.ts index 7468c55c..2dc4ab0e 100644 --- a/apps/sandbox/vite.config.ts +++ b/apps/sandbox/vite.config.ts @@ -1,6 +1,7 @@ import {defineConfig} from 'vitest/config'; import customTsConfig from 'vite-plugin-custom-tsconfig'; import vue from '@vitejs/plugin-vue'; +import {resolveAlias} from '@myparcel-do/build-vite'; export const PORT = 9860; @@ -32,9 +33,7 @@ export default defineConfig(({mode}) => { }, resolve: { - alias: { - '@myparcel/delivery-options': `${dirname}/../delivery-options/src`, - }, + alias: resolveAlias, }, define: { diff --git a/libs/build-vite/index.d.ts b/libs/build-vite/index.d.ts index 48176630..27ba3f1f 100644 --- a/libs/build-vite/index.d.ts +++ b/libs/build-vite/index.d.ts @@ -1,7 +1,9 @@ import {type ConfigEnv, type UserConfigFn} from 'vitest/config'; -import {type UserConfig} from 'vite'; +import {type UserConfig, type Alias} from 'vite'; import {type PromiseOr, type RecursivePartial} from '@myparcel/ts-utils'; declare function createViteConfig<>( config?: RecursivePartial | ((env: ConfigEnv) => PromiseOr>), ): UserConfigFn; + +declare const resolveAlias: Alias[]; diff --git a/libs/build-vite/index.js b/libs/build-vite/index.js index 763a3fbf..8d64e809 100644 --- a/libs/build-vite/index.js +++ b/libs/build-vite/index.js @@ -1,11 +1,29 @@ +import path from 'node:path'; import customTsConfig from 'vite-plugin-custom-tsconfig'; import {mergeConfig} from 'vite'; import vue from '@vitejs/plugin-vue'; +const dirname = path.dirname(new URL(import.meta.url).pathname); + +/** @type {import('vitest/config').Alias[]} */ +export const resolveAlias = [ + { + find: '@myparcel/delivery-options', + replacement: path.resolve(dirname, '../../apps/delivery-options/src'), + }, + { + find: '@myparcel-do/shared/testing', + replacement: path.resolve(dirname, '../../libs/shared/src/__tests__'), + }, + { + find: /^@myparcel-do\/(.+)/, + replacement: path.resolve(dirname, '../../libs/$1/src'), + }, +]; + /** @type {import('vitest/config').UserConfigFn} */ const createCommonViteConfig = (env) => { const isProd = env.mode === 'production'; - return { plugins: [vue(), customTsConfig({tsConfigPath: 'tsconfig.base.json'})], @@ -14,6 +32,10 @@ const createCommonViteConfig = (env) => { sourcemap: !isProd, }, + resolve: { + alias: resolveAlias, + }, + test: { coverage: { all: true, diff --git a/libs/shared/package.json b/libs/shared/package.json index 7fe5697a..cb006faa 100644 --- a/libs/shared/package.json +++ b/libs/shared/package.json @@ -11,25 +11,16 @@ "sideEffects": false, "type": "module", "exports": { - ".": { - "import": "./src/index.ts", - "require": "./src/index.ts" - }, - "./testing": { - "import": "./src/__tests__/index.ts", - "require": "./src/__tests__/index.ts" - } + ".": "./dist/index.js", + "./testing": "./dist/testing.js" }, - "main": "./src/index.ts", - "module": "./src/index.ts", "types": "./dist/index.d.ts", "files": [ - "./src", "./dist" ], "scripts": { - "build": "run ws:vue:build \"$(pwd)\"", - "build:dev": "run ws:vue:build \"$(pwd)\"", + "build": "vite build", + "build:dev": "vite build --mode development", "clean": "run ws:clean \"$(pwd)\"", "lint": "run ws:lint \"$(pwd)\"", "lint:fix": "run ws:lint:fix \"$(pwd)\"", @@ -37,7 +28,7 @@ "test:coverage": "run ws:test:coverage \"$(pwd)\"", "test:run": "run ws:test:run \"$(pwd)\"", "test:update": "run ws:test:update \"$(pwd)\"", - "typecheck": "run ws:vue:typecheck \"$(pwd)\"" + "typecheck": "vue-tsc --project tsconfig.base.json --noEmit" }, "dependencies": { "@myparcel-do/shared": "workspace:*", diff --git a/libs/shared/src/data/constants.ts b/libs/shared/src/data/constants.ts index 82426e97..f6fd1649 100644 --- a/libs/shared/src/data/constants.ts +++ b/libs/shared/src/data/constants.ts @@ -2,23 +2,26 @@ import {DeliveryTypeName, PackageTypeName, PlatformName, ShipmentOptionName} fro import {type DropOffEntryObject} from '../types'; import {CarrierSetting} from './enums'; -export const SUPPORTED_PLATFORMS = [PlatformName.MyParcel, PlatformName.SendMyParcel] as const; +export const SUPPORTED_PLATFORMS = Object.freeze([PlatformName.MyParcel, PlatformName.SendMyParcel]); -export const SUPPORTED_DELIVERY_TYPES = [ +export const SUPPORTED_DELIVERY_TYPES = Object.freeze([ DeliveryTypeName.Standard, DeliveryTypeName.Evening, DeliveryTypeName.Morning, DeliveryTypeName.Pickup, -] as const; +]); -export const SUPPORTED_SHIPMENT_OPTIONS = [ShipmentOptionName.OnlyRecipient, ShipmentOptionName.Signature] as const; +export const SUPPORTED_SHIPMENT_OPTIONS = Object.freeze([ + ShipmentOptionName.OnlyRecipient, + ShipmentOptionName.Signature, +]); -export const SUPPORTED_PACKAGE_TYPES = [ +export const SUPPORTED_PACKAGE_TYPES = Object.freeze([ PackageTypeName.Package, PackageTypeName.Mailbox, PackageTypeName.DigitalStamp, PackageTypeName.PackageSmall, -] as const; +]); export const DEFAULT_PLATFORM = PlatformName.MyParcel; diff --git a/libs/shared/vite.config.ts b/libs/shared/vite.config.ts new file mode 100644 index 00000000..7bf62b00 --- /dev/null +++ b/libs/shared/vite.config.ts @@ -0,0 +1,28 @@ +import dts from 'vite-plugin-dts'; +import {isCI} from 'ci-info'; +import {createViteConfig} from '@myparcel-do/build-vite'; + +const dirname = new URL('.', import.meta.url).pathname; + +export default createViteConfig((env) => { + const isProd = env.mode === 'production'; + + return { + plugins: [dts({entryRoot: 'src'})], + + build: { + sourcemap: !isCI && isProd, + lib: { + entry: { + index: `${dirname}/src/index.ts`, + testing: `${dirname}/src/__tests__/index.ts`, + }, + formats: ['es'], + }, + }, + + test: { + setupFiles: [`${dirname}/src/__tests__/vitest-setup.ts`], + }, + }; +}); diff --git a/libs/shared/vitest.config.ts b/libs/shared/vitest.config.ts deleted file mode 100644 index f3d770ed..00000000 --- a/libs/shared/vitest.config.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {createViteConfig} from '@myparcel-do/build-vite'; - -const dirname = new URL('.', import.meta.url).pathname; -export default createViteConfig({ - test: { - setupFiles: [`${dirname}/src/__tests__/vitest-setup.ts`], - }, -}); diff --git a/package.json b/package.json index 3c6bc910..d8b2f704 100644 --- a/package.json +++ b/package.json @@ -36,11 +36,7 @@ "ws:test": "cd $0 && vitest --passWithNoTests", "ws:test:coverage": "cd $0 && vitest run --coverage --passWithNoTests", "ws:test:run": "cd $0 && vitest run --passWithNoTests", - "ws:test:update": "cd $0 && vitest run -u --passWithNoTests", - "ws:ts:build": "cd $0 && tsc --project tsconfig.base.json --declaration --emitDeclarationOnly --noEmit false --declarationDir dist", - "ws:ts:typecheck": "cd $0 && tsc --project tsconfig.base.json --noEmit", - "ws:vue:build": "cd $0 && vue-tsc --project tsconfig.base.json --declaration --emitDeclarationOnly --noEmit false --declarationDir dist", - "ws:vue:typecheck": "cd $0 && vue-tsc --project tsconfig.base.json --noEmit" + "ws:test:update": "cd $0 && vitest run -u --passWithNoTests" }, "lint-staged": { "*.{json,md,yml}": "prettier --write", diff --git a/tsconfig.base.json b/tsconfig.base.json index fd312404..fac71a12 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -2,6 +2,11 @@ "extends": "@vue/tsconfig/tsconfig.dom.json", "compilerOptions": { "noEmitOnError": false, + "paths": { + "@myparcel/delivery-options": ["./apps/delivery-options/src"], + "@myparcel-do/shared/testing": ["./libs/shared/src/__tests__"], + "@myparcel-do/*": ["./libs/*/src"] + }, "types": ["node", "vite/client"] }, "include": [],