mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-21 09:16:30 -05:00
fix(commons): replace microbundle with bash based compile script
The commons package ships wrong types because it is refering to the same files for the ESM and the CJS build. See https://arethetypeswrong.github.io/?p=%40mrdrogdrog%2Foptional%401.1.0 This happens because microbundle can handle the generation of `.mjs` and `.cjs` from files itself but delegates the generation of types entirely to typescript by running it once. Microbundle uses the "type" field from the package.json to know if a `.js` file is meant to be mjs or cjs and generates the other type by using the specific file extension `.cjs` and `.mjs` (so if your package is a `type: module`, then `.js` file are interpreted as ECMAModule and if you have a commonjs file you need to name it `.cjs`). But this causes a problem with typescript. If you use typescript with the newer module resolver then it expects the type declarations to be named exactly like the file you wanna import. So if you have a `.js` file it will try to look up types in a `.d.ts` file. If it is resolving a `.mjs` file it is looking for a `.d.mts` file. This clashes with the types generated by microbundle because you can't use a `.mjs` file with a `.d.ts` file. Running typescript multiple times can also be complicated. When generating type declaration files, typescript takes a look at the source file extension. So a `.mts` file will generate a `.mjs` and a `.d.mts` file. A `.ts` will generate a `.js` and `.d.ts` file. It doesn't matter if you run microbundle on `.ts`, `.mts` or `.cjs` files, it will only generate the type declarations once. How do you get the other type declaration? To solve this problem you either have to run typescript multiple times and manipulate the input or output data to have correct `.d.mts` / `.d.cts` files AND imports... or do what this PR changes. It runs typescript multiple times but places the complied files in different directories. It then places a package.json in both directories which declares if `.js` is commonjs or ESM. This way the resolver is happy because it can import `.js` files according to the package.json content and typescript is happy because it can find type declarations. And because package.json files are inheriting properties from other package.json files no necessary file is missing. Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
1155fdd248
commit
8e2d59ff3c
15 changed files with 128 additions and 1515 deletions
|
@ -8,7 +8,7 @@ module.exports = {
|
||||||
"parser": "@typescript-eslint/parser",
|
"parser": "@typescript-eslint/parser",
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"project": [
|
"project": [
|
||||||
"./tsconfig.json"
|
"./tsconfig.test.json"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"plugins": [
|
"plugins": [
|
||||||
|
|
33
commons/build.sh
Executable file
33
commons/build.sh
Executable file
|
@ -0,0 +1,33 @@
|
||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
#
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "Clear dist directory.."
|
||||||
|
rm -rf dist
|
||||||
|
|
||||||
|
echo "Compile to CJS.."
|
||||||
|
tsc --project tsconfig.cjs.json
|
||||||
|
|
||||||
|
echo "Compile to ESM.."
|
||||||
|
tsc --project tsconfig.esm.json
|
||||||
|
|
||||||
|
echo "Fix CJS package.json.."
|
||||||
|
cat > dist/cjs/package.json <<!EOF
|
||||||
|
{
|
||||||
|
"type": "commonjs"
|
||||||
|
}
|
||||||
|
!EOF
|
||||||
|
|
||||||
|
echo "Fix ESM package.json.."
|
||||||
|
cat > dist/esm/package.json <<!EOF
|
||||||
|
{
|
||||||
|
"type": "module"
|
||||||
|
}
|
||||||
|
!EOF
|
||||||
|
|
||||||
|
echo "Done!"
|
|
@ -19,6 +19,7 @@
|
||||||
"^.+\\.tsx?$" : [
|
"^.+\\.tsx?$" : [
|
||||||
"ts-jest",
|
"ts-jest",
|
||||||
{
|
{
|
||||||
|
"tsconfig" : "tsconfig.test.json",
|
||||||
"useESM" : true
|
"useESM" : true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@hedgedoc/commons",
|
"name": "@hedgedoc/commons",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"description": "Common code between frontend and backend",
|
"description": "Common code between frontend and backend",
|
||||||
"author": "The HedgeDoc Authors",
|
"author": "The HedgeDoc Authors",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rm -rf dist && microbundle",
|
"build": "./build.sh",
|
||||||
"build:watch": "rm -rf dist && microbundle -w",
|
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"test:ci": "jest --ci",
|
"test:ci": "jest --ci",
|
||||||
"prepublish": "rm -rf dist && yarn lint && yarn build && yarn test",
|
"prepublish": "rm -rf dist && yarn lint && yarn build && yarn test",
|
||||||
|
@ -15,14 +14,19 @@
|
||||||
"lint:fix": "eslint --fix --ext .ts src"
|
"lint:fix": "eslint --fix --ext .ts src"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.cjs",
|
|
||||||
"module": "dist/index.mjs",
|
|
||||||
"types": "dist/index.d.ts",
|
|
||||||
"source": "src/index.ts",
|
"source": "src/index.ts",
|
||||||
|
"main": "dist/cjs/index.js",
|
||||||
|
"types": "dist/cjs/index.d.ts",
|
||||||
|
"module": "./dist/esm/index.js",
|
||||||
"exports": {
|
"exports": {
|
||||||
"types": "./dist/index.d.ts",
|
"import": {
|
||||||
"import": "./dist/index.mjs",
|
"types": "./dist/esm/index.d.ts",
|
||||||
"require": "./dist/index.cjs"
|
"default": "./dist/esm/index.js"
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/cjs/index.d.ts",
|
||||||
|
"default": "./dist/cjs/index.js"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"LICENSES/*",
|
"LICENSES/*",
|
||||||
|
@ -58,7 +62,6 @@
|
||||||
"eslint-plugin-jest": "27.2.3",
|
"eslint-plugin-jest": "27.2.3",
|
||||||
"eslint-plugin-prettier": "5.0.0",
|
"eslint-plugin-prettier": "5.0.0",
|
||||||
"jest": "29.6.2",
|
"jest": "29.6.2",
|
||||||
"microbundle": "0.15.1",
|
|
||||||
"prettier": "3.0.0",
|
"prettier": "3.0.0",
|
||||||
"ts-jest": "29.1.1",
|
"ts-jest": "29.1.1",
|
||||||
"typescript": "5.1.6"
|
"typescript": "5.1.6"
|
||||||
|
|
|
@ -1,26 +1,21 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
|
||||||
"removeComments": true,
|
"removeComments": true,
|
||||||
"preserveConstEnums": true,
|
"preserveConstEnums": true,
|
||||||
"lib": [
|
"lib": [
|
||||||
"es2020",
|
"es2022",
|
||||||
"dom"
|
"dom"
|
||||||
],
|
],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"module": "esnext",
|
|
||||||
"moduleResolution": "NodeNext",
|
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"outDir": "dist/",
|
|
||||||
"rootDir": "./src",
|
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"declarationMap":true,
|
"declarationMap":true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"typeRoots": ["./types"]
|
"typeRoots": ["./types"]
|
||||||
},
|
},
|
||||||
"include": ["./src", "./types"],
|
"include": ["./src", "./types"],
|
||||||
"exclude": ["./dist"]
|
"exclude": ["./dist", "**/*.test.ts"]
|
||||||
}
|
}
|
3
commons/tsconfig.base.json.license
Normal file
3
commons/tsconfig.base.json.license
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
SPDX-FileCopyrightText: 2023 Tilman Vatteroth
|
||||||
|
|
||||||
|
SPDX-License-Identifier: CC0-1.0
|
10
commons/tsconfig.cjs.json
Normal file
10
commons/tsconfig.cjs.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"extends" : "./tsconfig.base.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "CommonJS",
|
||||||
|
"target": "ES2015",
|
||||||
|
"outDir": "dist/cjs",
|
||||||
|
"declarationDir": "dist/cjs",
|
||||||
|
"moduleResolution": "node"
|
||||||
|
}
|
||||||
|
}
|
3
commons/tsconfig.cjs.json.license
Normal file
3
commons/tsconfig.cjs.json.license
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
SPDX-FileCopyrightText: 2023 Tilman Vatteroth
|
||||||
|
|
||||||
|
SPDX-License-Identifier: CC0-1.0
|
10
commons/tsconfig.esm.json
Normal file
10
commons/tsconfig.esm.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"extends" : "./tsconfig.base.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext",
|
||||||
|
"target" : "esnext",
|
||||||
|
"outDir": "dist/esm",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"declarationDir": "dist/esm"
|
||||||
|
}
|
||||||
|
}
|
3
commons/tsconfig.esm.json.license
Normal file
3
commons/tsconfig.esm.json.license
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
SPDX-FileCopyrightText: 2023 Tilman Vatteroth
|
||||||
|
|
||||||
|
SPDX-License-Identifier: CC0-1.0
|
|
@ -1,3 +0,0 @@
|
||||||
SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
|
||||||
|
|
||||||
SPDX-License-Identifier: CC0-1.0
|
|
4
commons/tsconfig.test.json
Normal file
4
commons/tsconfig.test.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"extends" : "./tsconfig.esm.json",
|
||||||
|
"exclude": ["./dist"]
|
||||||
|
}
|
3
commons/tsconfig.test.json.license
Normal file
3
commons/tsconfig.test.json.license
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
SPDX-FileCopyrightText: 2023 Tilman Vatteroth
|
||||||
|
|
||||||
|
SPDX-License-Identifier: CC0-1.0
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
import type { TransportAdapter } from '@hedgedoc/commons'
|
import type { TransportAdapter } from '@hedgedoc/commons'
|
||||||
import { ConnectionState } from '@hedgedoc/commons'
|
import { ConnectionState } from '@hedgedoc/commons'
|
||||||
import type { Message, MessageType } from '@hedgedoc/commons/dist'
|
import type { Message, MessageType } from '@hedgedoc/commons'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements a transport adapter that communicates using a browser websocket.
|
* Implements a transport adapter that communicates using a browser websocket.
|
||||||
|
|
Loading…
Reference in a new issue