Skip to content

Commit

Permalink
Remove support for CJS modules (#40)
Browse files Browse the repository at this point in the history
The CJS build for @itwin/saved-views-react was encountering an error.
Instead of fixing the error, I removed the CJS build because I think
it's safe to assume everyone will want to use ES modules nowadays. ESM
requires that module imports include file extensions, so I went through
every TypeScript file and updated imports.

Then I discovered that @itwin/saved-views-client was being included in
the transpilation output. I figured out the cause (see
tsconfig.build.json) and updated config files.
  • Loading branch information
roluk authored Feb 1, 2024
1 parent 810ee81 commit 5a42a59
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 149 deletions.
15 changes: 4 additions & 11 deletions packages/saved-views-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@
"name": "Bentley Systems, Inc.",
"url": "https://www.bentley.com"
},
"types": "./lib/cjs/index.d.ts",
"type": "module",
"exports": {
".": {
"import": {
"types": "./lib/esm/index.d.ts",
"default": "./lib/esm/index.js"
},
"require": {
"types": "./lib/cjs/index.d.ts",
"default": "./lib/cjs/index.js"
}
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"files": [
"./lib",
"./public"
"./lib"
],
"scripts": {
"build": "tsc --project ./tsconfig.build.json",
Expand Down
4 changes: 1 addition & 3 deletions packages/saved-views-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
"compilerOptions": {
"lib": [ "ES2022", "DOM" ],
"isolatedModules": true,
"jsx": "react-jsx",
"declaration": true,
"esModuleInterop": true,
"outDir": "./lib/esm",
"outDir": "./lib",
},
"include": [ "src" ],
"references": [ { "path": "./tsconfig.node.json" } ],
Expand Down
29 changes: 12 additions & 17 deletions packages/saved-views-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,27 @@
"name": "Bentley Systems, Inc.",
"url": "https://www.bentley.com"
},
"types": "./lib/cjs/index.d.ts",
"type": "module",
"exports": {
".": {
"import": {
"types": "./lib/esm/index.d.ts",
"default": "./lib/esm/index.js"
},
"require": {
"types": "./lib/cjs/index.d.ts",
"default": "./lib/cjs/index.js"
}
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./experimental": "./lib/esm/experimental.js"
"./experimental": {
"types": "./lib/experimental.d.ts",
"default": "./lib/experimental.js"
}
},
"files": [
"./lib",
"./public"
],
"scripts": {
"build": "run-p build:esm build:cjs",
"build:esm": "run-s build:transpile:esm build:css:esm",
"build:cjs": "run-s build:transpile:cjs build:css:cjs",
"build:transpile:esm": "tsc --project ./tsconfig.build.json",
"build:transpile:cjs": "tsc --project ./tsconfig.build.json --module CommonJS --moduleResolution Node16 --outDir ./lib/cjs",
"build:css:esm": "postcss src/**/*.css --base ./src --dir ./lib/esm",
"build:css:cjs": "postcss src/**/*.css --base ./src --dir ./lib/cjs",
"build": "run-p build:*",
"build:transpile": "run-s build:transpile:*",
"build:transpile:client": "cd ../saved-views-client && npm run build",
"build:transpile:react": "tsc --project ./tsconfig.build.json",
"build:css": "postcss src/**/*.css --base ./src --dir ./lib",
"test": "vitest run --passWithNoTests",
"test:cover": "vitest run --coverage --passWithNoTests",
"test:watch": "vitest watch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/no-var-requires */
const fs: typeof import("fs/promises") = require("fs/promises");
const postcssModules: typeof import("postcss-modules") = require("postcss-modules");
const recursiveReaddir: typeof import("recursive-readdir") = require("recursive-readdir");
const packageJson: typeof import("./package.json") = require("./package.json");
import fs from "fs/promises";
import postcssModules from "postcss-modules";
import recursiveReaddir from "recursive-readdir";
import packageJson from "./package.json";

const replacements = new Map<string, string>();
const majorVersion = packageJson.version.substring(0, packageJson.version.indexOf("."));
Expand Down
14 changes: 0 additions & 14 deletions packages/saved-views-react/src/SavedViewTypes.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import type { ExtensionMin, ExtensionSavedViewCreate, SavedViewWithDataRepresentation, ViewData } from "@itwin/saved-views-client";
import type {
ExtensionMin, ExtensionSavedViewCreate, SavedViewWithDataRepresentation, ViewData,
} from "@itwin/saved-views-client";

import type { SavedView, SavedViewGroup, SavedViewTag } from "../SavedView.js";

Expand Down
5 changes: 3 additions & 2 deletions packages/saved-views-react/src/SavedViewsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { PropsWithChildren, ReactElement, createContext, useContext, useMemo } from "react";
import { LocalizationStrings, defaultLocalization } from "./localization.js";
import { createContext, useContext, useMemo, type PropsWithChildren, type ReactElement } from "react";

import { defaultLocalization, type LocalizationStrings } from "./localization.js";

const savedViewsContext = createContext<SavedViewsContext>({ localization: defaultLocalization });
savedViewsContext.displayName = "SavedViewsContext";
Expand Down
1 change: 1 addition & 0 deletions packages/saved-views-react/src/TileGrid/TileGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { Button, Surface, Text } from "@itwin/itwinui-react";
import { useState, type CSSProperties, type ReactElement, type ReactNode } from "react";

import { useSavedViewsContext } from "../SavedViewsContext.js";

import "./TileGrid.css";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import type { Id64Array } from "@itwin/core-bentley";
import { QueryRowFormat } from "@itwin/core-common";
import { type IModelConnection } from "@itwin/core-frontend";
import type { IModelConnection } from "@itwin/core-frontend";

/**
* Helper function to execute ECSql queries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import type { LegacySavedView, LegacySavedViewBase } from "../utilities/SavedViewTypes";
import type { LegacySavedView, LegacySavedViewBase } from "../utilities/SavedViewTypes.js";

/** Is a 3d saved view */
export function isSavedView3d(view: LegacySavedViewBase): view is LegacySavedView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
} from "@itwin/core-common";
import type { Range3dProps } from "@itwin/core-geometry";

import type { ModelCategoryOverrideProviderProps } from "../../ui/viewlist/ModelCategoryOverrideProvider";
import type { ModelCategoryOverrideProviderProps } from "../../ui/viewlist/ModelCategoryOverrideProvider.js";

/** Per Model Category Visibility Props */
export interface PerModelCategoryVisibilityProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
*--------------------------------------------------------------------------------------------*/
import { IModelReadRpcInterface, ViewQueryParams, ViewStateProps } from "@itwin/core-common";
import {
DrawingViewState, EmphasizeElements, IModelConnection, ScreenViewport, SheetViewState,
SpatialViewState, Viewport, ViewState,
DrawingViewState, EmphasizeElements, IModelConnection, ScreenViewport, SheetViewState, SpatialViewState, ViewState,
Viewport,
} from "@itwin/core-frontend";
import {
Extension, SavedViewWithDataRepresentation, ViewData, ViewDataItwin3d, ViewDataITwinDrawing,
Extension, SavedViewWithDataRepresentation, ViewData, ViewDataITwinDrawing, ViewDataITwinSheet, ViewDataItwin3d,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ViewDataITwinSheet, ViewDataWithLegacy,
ViewDataWithLegacy,
} from "@itwin/saved-views-client";

import { ViewTypes } from "../../../SavedViewTypes.js";
import {
isDrawingSavedView, isSheetSavedView, isSpatialSavedView,
} from "../../clients/ISavedViewsClient.js";
import {
LegacySavedView, LegacySavedView2d,
LegacySavedViewBase,
} from "../SavedViewTypes.js";
import { isDrawingSavedView, isSheetSavedView, isSpatialSavedView } from "../../clients/ISavedViewsClient.js";
import { LegacySavedView, LegacySavedView2d, LegacySavedViewBase } from "../SavedViewTypes.js";
import { applyHiddenModelsAndCategories } from "./ModelsAndCategoriesHelper.js";
import { SavedViewsExtensionHandlers } from "./SavedViewsExtensionHandlers.js";
import {
cleanLegacyViewModelSelectorPropsModels, savedViewITwin3dToLegacy3dSavedView,
savedViewItwinDrawingToLegacyDrawingView, savedViewItwinSheetToLegacySheetSavedView,
} from "./viewExtractorSavedViewToLegacySavedView.js";

enum ViewTypes {
SheetViewDefinition,
ViewDefinition3d,
DrawingViewDefinition,
}

/**
* Type-check for {@link ViewDataItwin3d}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { type EmphasizeElementsProps } from "@itwin/core-common";
import {
EmphasizeElements, PerModelCategoryVisibility,
type Viewport,
} from "@itwin/core-frontend";
import type { EmphasizeElementsProps } from "@itwin/core-common";
import { EmphasizeElements, PerModelCategoryVisibility, type Viewport } from "@itwin/core-frontend";

import {
ModelCategoryOverrideProvider,
} from "../../../ui/viewlist/ModelCategoryOverrideProvider.js";
import { ModelCategoryOverrideProvider } from "../../../ui/viewlist/ModelCategoryOverrideProvider.js";
import {
extractEmphasizeElements, extractPerModelCategoryVisibility, extractVisibilityOverride,
} from "./extensionExtractor";
} from "./extensionExtractor.js";

export interface ExtensionHandler {
extensionName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import {
type ExtractionFunc,
applyExtraction,
extractArray2d,
extractArrayConditionally,
extractBoolean,
extractNumber,
extractObject,
extractSimpleArray,
simpleTypeOf,
} from "./extractionUtilities";
applyExtraction, extractArray2d, extractArrayConditionally, extractBoolean, extractNumber, extractObject,
extractSimpleArray, simpleTypeOf, type ExtractionFunc,
} from "./extractionUtilities.js";

const isPoint = (val: unknown): val is [number, number, number] =>
Array.isArray(val) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,17 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import {
type ExtractionFunc,
applyExtraction,
extractArray,
extractArrayConditionally,
extractBoolean,
extractColor,
extractColorLegacy,
extractConditionally,
extractNumber,
extractNumberOrBool,
extractObject,
extractPlainTypedMap,
extractRGB,
extractSimpleArray,
extractString,
extractStringOrArray,
extractStringOrNumber,
extractStringOrNumberArray,
isAnyColorFormat,
simpleTypeOf,
} from "./extractionUtilities";
import { type LegacySavedView, type LegacySavedView2d } from "../SavedViewTypes";

import { type ViewState } from "@itwin/core-frontend";
import type { ViewState } from "@itwin/core-frontend";
import { ViewITwin2d, ViewITwin3d } from "@itwin/saved-views-client";

import type { LegacySavedView, LegacySavedView2d } from "../SavedViewTypes.js";
import {
applyExtraction, extractArray, extractArrayConditionally, extractBoolean, extractColor, extractColorLegacy,
extractConditionally, extractNumber, extractNumberOrBool, extractObject, extractPlainTypedMap, extractRGB,
extractSimpleArray, extractString, extractStringOrArray, extractStringOrNumber, extractStringOrNumberArray,
isAnyColorFormat, simpleTypeOf, type ExtractionFunc,
} from "./extractionUtilities.js";

const viewFlagMappings: ExtractionFunc<void, void>[] = [
extractNumber("renderMode"),
extractBoolean("noConstructions", "noConstruct"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import {
type ExtractionFunc,
applyExtraction,
extractArray,
extractBoolean,
extractColor,
extractNumber,
extractObject,
extractSimpleArray,
extractString,
simpleTypeOf,
} from "./extractionUtilities";

import { type EmphasizeElementsProps } from "@itwin/core-common";

import { type PerModelCategoryVisibilityProps } from "../SavedViewTypes";
import { featureAppearanceMappings } from "./displayStyleExtractor";
import { ModelCategoryOverrideProviderProps } from "../../../ui/viewlist/ModelCategoryOverrideProvider.js";
import type { PerModelCategoryVisibilityProps } from "../SavedViewTypes.js";
import { featureAppearanceMappings } from "./displayStyleExtractor.js";
import {
applyExtraction, extractArray, extractBoolean, extractColor, extractNumber, extractObject, extractSimpleArray,
extractString, simpleTypeOf, type ExtractionFunc,
} from "./extractionUtilities.js";

/** Appearance Override type for EmphasizeElements */
const appearanceOverrideEmphElemMappings: ExtractionFunc<void, void>[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { ColorDef } from "@itwin/core-common";
import { Rgba, RgbatColorProps } from "./RgbColor";

import type { Rgba, RgbatColorProps } from "./RgbColor.js";

/**
* Returns a function that does a simple typeof check on a value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { BaseMapLayerProps, ImageMapLayerProps } from "@itwin/core-common";
import {
ViewData, ViewDataItwin3d, ViewDataITwinDrawing, ViewDataITwinSheet,
} from "@itwin/saved-views-client";
import type { ViewData, ViewDataITwinDrawing, ViewDataITwinSheet, ViewDataItwin3d } from "@itwin/saved-views-client";

/**
* Convert url that potentially contains subtituted characters ('++and++' or '++dot++') to use restricated characters ('&' or '.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { type Id64Array } from "@itwin/core-bentley";
import {
type DrawingViewState,
type SpatialViewState,
SheetViewState,
} from "@itwin/core-frontend";
import {
SavedViewBase, SavedViewTag, SavedViewWithDataRepresentation, ViewDataItwin3d,
ViewDataITwinDrawing, ViewDataITwinSheet, ViewITwin3d,
import type { Id64Array } from "@itwin/core-bentley";
import { SheetViewState, type DrawingViewState, type SpatialViewState } from "@itwin/core-frontend";
import type {
SavedViewBase, SavedViewTag, SavedViewWithDataRepresentation, ViewDataITwinDrawing, ViewDataITwinSheet,
ViewDataItwin3d, ViewITwin3d,
} from "@itwin/saved-views-client";

import {
LegacySavedView, LegacySavedView2d, LegacyTag,
} from "../SavedViewTypes";
import { extractClipVectors } from "./clipVectorsExtractor";
import { extractDisplayStyle, extractDisplayStyle3d } from "./displayStyleExtractor";
import { convertAllLegacyUrlsToUrls, urlToLegacyUrl } from "./urlConverter";
import type { LegacySavedView, LegacySavedView2d, LegacyTag } from "../SavedViewTypes.js";
import { extractClipVectors } from "./clipVectorsExtractor.js";
import { extractDisplayStyle, extractDisplayStyle3d } from "./displayStyleExtractor.js";
import { convertAllLegacyUrlsToUrls, urlToLegacyUrl } from "./urlConverter.js";

const UNGROUPED_ID = "-1";

Expand Down
7 changes: 6 additions & 1 deletion packages/saved-views-react/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"extends": "./tsconfig.json",
"exclude": ["./src/tests", "**/*.test.*"],
"compilerOptions": {
// Specifying @itwin/saved-views-client path mapping in the parent config makes TypeScript think its source code
// is part of the current package and include its files in the output
"paths": { },
},
"exclude": [ "**/*.test.*" ],
}
Loading

0 comments on commit 5a42a59

Please sign in to comment.