Skip to content

Commit

Permalink
Merge pull request #1 from alienfast/initial
Browse files Browse the repository at this point in the history
Initial
  • Loading branch information
rosskevin authored Jul 26, 2023
2 parents 9318db2 + c4c0f90 commit 08923b4
Show file tree
Hide file tree
Showing 13 changed files with 810 additions and 130 deletions.
13 changes: 13 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
packages/*/node_modules
packages/*/dist

**/locales/index.*

.eslintrc.js
.pnp*
.yarn

tsup.config.ts

dist
9 changes: 9 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
root: true,
extends: ['@alienfast'],
rules: {
// this gets really messy in tsx and graphql when types are forced to any e.g. policies
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off'
}
};
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ packages/*/.rpt2_cache
test-report.xml
# ci runs
test-results
/build
/dist
**/__generated__/*.json
.eslintcache
tsconfig.tsbuildinfo
Expand Down
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
packages/*/dist
./build
*.snap
tmp
.storybook-out
.yarn
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
printWidth: 100
semi: false
singleQuote: true
trailingComma: all
bracketSpacing: true
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["arcanis.vscode-zipfs", "dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
},
"eslint.nodePath": ".yarn/sdks",
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"testing.automaticallyOpenPeekView": "never",
"vitest.commandLine": "yarn test --"
// "[typescript]": {
// "editor.defaultFormatter": "vscode.typescript-language-features"
// }
}
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build:ide",
"problemMatcher": ["$tsc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# vite-plugin-tsconfig

[![npm version](https://badge.fury.io/js/vite-plugin-tsconfig.svg)](https://badge.fury.io/js/vite-plugin-tsconfig)

`yarn add -D vite-plugin-tsconfig`

Vite plugin to that allows you to specify an alternate tsconfig.

```ts
import { defineConfig } from 'vite'
import tsconfig from 'vite-plugin-tsconfig'

export default defineConfig({
plugins: [
tsconfig({
filename: 'tsconfig.build.json',

logLevel: 'info', // optional for additional information
}),
],
})
```

## Why?

As of 7/26/2023, `vite` does not allow users to specify alternate tsconfig files. Given a variety of needs including different development
vs CI environments, it is common to use different tsconfig files to meet such needs.

Preferably, this plugin should become obsolete if `vite` includes the option to specify an alternate tsconfig.

## How does it work?

It's a total hack. In the `config` stage, if a current `tsconfig.json` exists, it will back it up. Once that is complete, it will write the content from the provided
alternate tsconfig file to the default filename of `tsconfig.json`. When the build is finishing, it will remove the generated file and replace the original (if one existed).

## Contributing

PRs are accepted! This project is configured with `auto`, so feel free to submit a PR and `auto` will automatically create a `canary` release for you to try out.
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vite-plugin-tsconfig",
"version": "1.0.0",
"packageManager": "[email protected]",
"description": "Vite plugin that allows you to use different tsconfig files.",
"description": "Vite plugin that allows you to use alternate tsconfig files.",
"type": "module",
"main-note": "Though this is ESM only, the following main is to appease tsc and varieties of moduleResolution e.g. node vs nodenext, otherwise types aren't found. see https://github.com/rosskevin/ts-esm-workspaces/tree/bug-main-required-to-build#workaround ",
"main": "./dist/index.js",
Expand All @@ -14,8 +14,7 @@
},
"sideEffects": false,
"publishConfig": {
"access": "public",
"registry": "https://npm.pkg.github.com/"
"access": "public"
},
"scripts": {
"build": "yarn clean && tsup-node",
Expand All @@ -24,12 +23,11 @@
"clean:yarn": "node ./scripts/clean-yarn.js",
"lint": "eslint . --cache --ext .js,.ts,.tsx",
"lint:fix": "eslint . --cache --ext .js,.ts,.tsx --fix",
"test": "vitest",
"test": "echo 'no tests' && exit 0",
"release": "yarn auto shipit"
},
"keywords": [
"vite-plugin",
"loader",
"tsconfig"
],
"licenses": [
Expand All @@ -55,6 +53,8 @@
"@auto-it/all-contributors": "^10.46.0",
"@auto-it/first-time-contributor": "^10.46.0",
"@auto-it/released": "^10.46.0",
"@types/marked": "^5",
"@types/marked-terminal": "^3.1.3",
"auto": "^10.46.0",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
Expand All @@ -69,11 +69,13 @@
"eslint-plugin-storybook": "^0.6.12",
"eslint-plugin-unicorn": "^47.0.0",
"eslint-plugin-unused-imports": "^2.0.0",
"execa": "^7.1.1",
"husky": "^8.0.3",
"lint-staged": "^13.2.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"tsup": "^7.1.0",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vite-plugin-dts": "^3.0.0-beta.3",
Expand All @@ -86,5 +88,9 @@
"first-time-contributor",
"released"
]
},
"dependencies": {
"marked": "^5.1.2",
"marked-terminal": "^5.2.0"
}
}
113 changes: 113 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import fs from 'node:fs'
import path from 'node:path'

import { marked } from 'marked'
import TerminalRenderer from 'marked-terminal'
import { createLogger, LogLevel, Plugin } from 'vite'

marked.setOptions({
renderer: new TerminalRenderer() as any,
})

// unfortunately not exported
export const LogLevels: Record<LogLevel, number> = {
silent: 0,
error: 1,
warn: 2,
info: 3,
}

export interface PluginOptions {
/**
* Set to 'info' for noisy information.
*
* Default: 'warn'
*/
logLevel?: LogLevel

/**
* Name of the replacement tsconfig file to use
*/
filename: string
}

const TSCONFIG = 'tsconfig.json'
const BANNER = `// GENERATED via 'vite-plugin-tsconfig' - this should be automatically created and deleted inside the build process. \n`

const hasBanner = (tsconfigPath: string) => {
const content = fs.readFileSync(tsconfigPath, 'utf8')
return content.startsWith(BANNER.trim())
}

const factory = (options: PluginOptions) => {
const { filename, logLevel = 'warn' } = options
const log = createLogger(logLevel, { prefix: '[tsconfig]' })

let root: string
let backupFilename: string

const plugin: Plugin = {
name: 'vite-plugin-tsconfig',

config(config) {
root ??= config.root ?? process.cwd()

const tsconfigPath = path.resolve(root, TSCONFIG)

// if the tsconfig file already exists, we need to back it up and replace it later
if (fs.existsSync(tsconfigPath)) {
log.info(`${TSCONFIG} already exists, moving it to ${TSCONFIG}.bak`)
backupFilename = path.resolve(root, `${TSCONFIG}.bak`)

// paranoia check
if (fs.existsSync(backupFilename)) {
fs.rmSync(backupFilename)
}

fs.renameSync(tsconfigPath, `${tsconfigPath}.bak`)
}

// now
const providedTsConfig = path.resolve(root, filename)
if (!fs.existsSync(providedTsConfig)) {
throw new Error(`${providedTsConfig} does not exist.`)
}

log.info(`Creating ${TSCONFIG} from ${filename}`)
const providedTsConfigContent = fs.readFileSync(providedTsConfig, 'utf8')
fs.writeFileSync(tsconfigPath, BANNER + providedTsConfigContent)
},

closeBundle() {
if (!root) {
throw new Error('Expected root to be set in the vite config hook.')
}

const tsconfigPath = path.resolve(root, TSCONFIG)

// perhaps we never created the tsconfig file?
if (!fs.existsSync(tsconfigPath)) {
log.info('No tsconfig file found, nothing to do.')
return
}

// perhaps the user has a standard tsconfig file but we did not create it?
if (!hasBanner(tsconfigPath)) {
log.info('tsconfig.json found but it does not contain theb banner, nothing to do.')
return
}

log.info('Removing generated tsconfig.json')
fs.rmSync(tsconfigPath)

if (fs.existsSync(backupFilename)) {
log.info(`Restoring ${TSCONFIG} from backup`)
fs.renameSync(backupFilename, tsconfigPath)
}
},
}

return plugin
}

export default factory
17 changes: 8 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@alienfast/tsconfig/node.json",

"include": ["src", "./typings/**/*"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"module": "ESNext",
"target": "esnext",
"moduleResolution": "node",
"sourceMap": true
},
"include": [
"src"
]
"outDir": "dist",
"rootDir": "src"
}
}
Loading

0 comments on commit 08923b4

Please sign in to comment.