Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 17, 2022
0 parents commit c220370
Show file tree
Hide file tree
Showing 10 changed files with 6,427 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: 'Release'

on:
push:
branches:
- main

jobs:
release:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GH_REPO_TOKEN }}

- name: 'Get yarn cache folder'
id: yarn-cache
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT

- name: 'Handle yarn cache'
uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn install

- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GH_REPO_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea/
.vscode/
lib/
node_modules/

.DS_Store
.yarn/*
!.yarn/releases
!.yarn/patches
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
807 changes: 807 additions & 0 deletions .yarn/releases/yarn-3.3.0.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-3.3.0.cjs
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# vite-plugin-custom-tsconfig

[![NPM](https://img.shields.io/npm/v/vite-plugin-custom-tsconfig?style=for-the-badge)](https://www.npmjs.com/package/vite-plugin-custom-tsconfig/)

A very simple plugin to support using filenames like tsconfig.build.json or tsconfig.app.json with Vite. It copies your tsconfig.*.json to tsconfig.json before Vite starts, and removes it when the build is finished.

## Installation

**Using Yarn**

```bash
yarn add -D vite-plugin-custom-tsconfig
```

**Using pnpm**

```bash
pnpm add -D vite-plugin-custom-tsconfig
```

**Using npm**

```bash
npm install --save-dev vite-plugin-custom-tsconfig
```

## Usage

```ts
// vite.config.ts
import customTsConfig from 'vite-plugin-custom-tsconfig';
import {defineConfig} from 'vite';

export default defineConfig({
plugins: [
customTsConfig({
// default: 'tsconfig.build.json'
filename: 'tsconfig.app.json',
}),
],
});
```
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "vite-plugin-custom-tsconfig",
"version": "0.0.0-development",
"repository": "github:edielemoine/vite-plugin-custom-tsconfig",
"license": "MIT",
"author": "Edie Lemoine <edie.lemoine@gmail.com>",
"type": "module",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.cjs",
"import": "./lib/index.js"
}
},
"main": "lib/index.cjs",
"module": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
"release": {
"branches": [
"main"
]
},
"scripts": {
"build": "tsup src/index.ts --dts --out-dir lib --format esm,cjs --target node14",
"prepare": "is-ci || husky install",
"semantic-release": "semantic-release"
},
"lint-staged": {
"*.ts": "prettier --write",
"package.json": "npx -q sort-package-json"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.3",
"@types/node": "^18.11.9",
"husky": "^8.0.2",
"is-ci": "^3.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"semantic-release": "^19.0.5",
"tsup": "^6.5.0",
"typescript": "^4.9.3",
"vite": "^3.2.4"
},
"packageManager": "yarn@3.3.0",
"engines": {
"node": ">=14"
},
"publishConfig": {
"access": "public"
}
}
62 changes: 62 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Plugin } from "vite";
import fs from "fs";

export interface PluginOptions {
tsConfigPath?: string;
}

const NAME = "vite-plugin-custom-tsconfig";
const TSCONFIG_PATH = "tsconfig.json";
const BANNER = `// GENERATED BY ${NAME} \n`;

const tsConfigExists = () => {
return fs.existsSync(TSCONFIG_PATH);
};

const tsConfigHasBanner = () => {
const tsconfigContent = fs.readFileSync(TSCONFIG_PATH, "utf8");

return tsconfigContent.startsWith(BANNER);
};

function customTsConfigPlugin(options?: PluginOptions): Plugin {
const resolvedOptions: Required<PluginOptions> = {
tsConfigPath: "tsconfig.build.json",
...options,
};

return {
name: NAME,

config() {
if (tsConfigExists() && tsConfigHasBanner()) {
throw new Error(
"tsconfig.json already exists. Please delete it or remove vite-plugin-custom-tsconfig from your Vite config"
);
}

const customTsConfigContent = fs.readFileSync(
resolvedOptions.tsConfigPath,
"utf8"
);

fs.writeFileSync(TSCONFIG_PATH, BANNER + customTsConfigContent);
},

closeBundle() {
if (!tsConfigExists()) {
return;
}

if (!tsConfigHasBanner()) {
throw new Error(
"tsconfig.json does not contain the expected banner. Please delete it or remove vite-plugin-custom-tsconfig from your Vite config"
);
}

fs.rmSync("tsconfig.json", { force: true });
},
};
}

export default customTsConfigPlugin;
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"target": "esnext",
"moduleResolution": "node",
"sourceMap": true
},
"include": [
"src"
]
}
Loading

0 comments on commit c220370

Please sign in to comment.