Skip to content

Commit

Permalink
Merge pull request #1 from Froxz/feature/ts
Browse files Browse the repository at this point in the history
Added TypeScript support
  • Loading branch information
SergkeiM authored Mar 17, 2023
2 parents afca3c9 + feffb00 commit 8e086c9
Show file tree
Hide file tree
Showing 20 changed files with 6,313 additions and 2,343 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
node_modules/
*.d.ts
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "@antfu",
"overrides": [
{
"files": [
"**/*.md/*.*"
],
"rules": {
"@typescript-eslint/no-this-alias": "off",
"n/handle-callback-err": "off",
"no-restricted-syntax": "off",
"no-labels": "off"
}
}
]
}
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16.15.1'
node-version: 16.15.1
registry-url: https://registry.npmjs.org/

# Configure Git
Expand All @@ -37,7 +37,7 @@ jobs:
echo "RELEASE_TAG=latest" >> $GITHUB_ENV
env:
RELEASE_TYPE: ${{ github.event.inputs.release-type }}
# Update changelog unreleased section with new version
# Update changelog unreleased section with new version
- name: Update changelog
uses: superfaceai/release-changelog-action@v2
with:
Expand Down Expand Up @@ -81,4 +81,4 @@ jobs:
body: ${{ steps.get-changelog.outputs.changelog }}
prerelease: ${{ startsWith(github.event.inputs.release-type, 'pre') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align='center'>
<br>
S3 compatible file uploader Plugin for Vite
AWS S3 or any other S3 compatible provider file uploader Plugin for Vite
</p>

<p align='center'>
Expand All @@ -27,9 +27,9 @@ $ npm i @froxz/vite-plugin-s3
Add `vite-plugin-s3` plugin to `vite.config.js / vite.config.ts` and configure it:

```javascript
```ts
import { defineConfig } from 'vite'
import S3Plugin from '@froxz/vite-plugin-s3'
import { S3Plugin } from '@froxz/vite-plugin-s3'

export default defineConfig({
plugins: [
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist'
7,988 changes: 5,946 additions & 2,042 deletions package-lock.json

Large diffs are not rendered by default.

106 changes: 64 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,68 @@
{
"name": "@froxz/vite-plugin-s3",
"version": "1.1.2",
"description": "Allows you to upload files to S3",
"type": "module",
"keywords": [
"vite",
"s3",
"vite-plugin"
],
"files": [
"dist"
],
"main": "./dist/main.cjs",
"module": "./dist/main.js",
"exports": {
".": {
"import": "./dist/main.js",
"require": "./dist/main.cjs"
}
"name": "@froxz/vite-plugin-s3",
"type": "module",
"version": "1.1.2",
"description": "Allows you to upload files to AWS S3 or any other S3 compatible provider",
"author": "Froxz([email protected])",
"license": "MIT",
"homepage": "https://github.com/Froxz/vite-plugin-s3",
"repository": "https://github.com/Froxz/vite-plugin-s3",
"bugs": {
"url": "https://github.com/Froxz/vite-plugin-s3/issues"
},
"keywords": [
"vite",
"s3",
"vite-plugin",
"do-spaces"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"repository": "https://github.com/Froxz/vite-plugin-s3",
"author": "Froxz([email protected])",
"license": "MIT",
"bugs": {
"url": "https://github.com/Froxz/vite-plugin-s3/issues"
},
"homepage": "https://github.com/Froxz/vite-plugin-s3",
"dependencies": {
"@aws-sdk/client-s3": "^3.0.0",
"mime-types": "^2.1.35",
"progress": "^2.0.3",
"recursive-readdir": "^2.2.3"
},
"devDependencies": {
"kolorist": "^1.7.0",
"lodash": "^4.17.21",
"vite": "^4.0.0",
"vite-plugin-target": "^0.1.1"
"./*": "./*"
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./*"
]
}
},
"files": [
"dist",
"*.d.ts"
],
"scripts": {
"lint": "eslint .",
"lint-fix": "eslint . --fix",
"dev": "npx tsup src/index.ts --watch --ignore-watch dist",
"build": "npx tsup src/index.ts --dts --format cjs,esm"
},
"peerDependencies": {
"vite": "^3.1.0 || ^4.0.0"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.0.0",
"mime-types": "^2.1.35",
"recursive-readdir": "^2.2.3"
},
"devDependencies": {
"@antfu/eslint-config": "^0.36.0",
"@types/lodash": "^4.14.191",
"@types/mime-types": "^2.1.1",
"@types/node": "^18.15.3",
"@types/recursive-readdir": "^2.2.1",
"eslint": "^8.36.0",
"kolorist": "^1.7.0",
"lodash": "^4.17.21",
"tsup": "^6.6.3",
"typescript": "^4.9.5"
}
}
30 changes: 0 additions & 30 deletions src/context.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Options } from './types'

import {
addTrailingS3Sep,
} from './helpers'

export function createContext(options: Options): Options {
const basePath = options.basePath ? addTrailingS3Sep(options.basePath) : ''

return {
...options,
basePath,
}
}
42 changes: 0 additions & 42 deletions src/helpers.js

This file was deleted.

48 changes: 48 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from 'node:path'
import readDir from 'recursive-readdir'

import type { File } from './types'

export const S3_PATH_SEP = '/'
export const PATH_SEP: string = path.sep

export const DEFAULT_UPLOAD_OPTIONS: object = {
ACL: 'public-read',
}

export const UPLOAD_IGNORES: string[] = [
'.DS_Store',
]

export function addTrailingS3Sep(fPath: string): string {
return fPath ? fPath.replace(/\/?(\?|#|$)/, '/$1') : fPath
}

export function addSeperatorToPath(fPath: string): string {
if (!fPath)
return fPath

return fPath.endsWith(PATH_SEP) ? fPath : fPath + PATH_SEP
}

export function translatePathFromFiles(dir: string, files: string[]): File[] {
return files.map((file: string): File => ({
path: file,
name: file
.replace(dir, '')
.split(PATH_SEP)
.join(S3_PATH_SEP),
}))
}

export const getDirectoryFilesRecursive = (dir: string, ignores: ReadonlyArray<string> = []): Promise<File[]> => {
return new Promise((resolve, reject) => {
readDir(dir, ignores, (error: Error, files: string[]) => {
if (error)
reject(error)

else
resolve(translatePathFromFiles(dir, files))
})
})
}
32 changes: 32 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ConfigEnv, Plugin, ResolvedConfig, UserConfig } from 'vite'
import { createContext } from './context'
import Uploader from './uploader'

import type { Options } from './types'

export function S3Plugin(enabled: boolean, userOptions: Options): Plugin {
const options: Options = createContext(userOptions)
let vite: ResolvedConfig

return {
name: 'vite-plugin-s3',
enforce: 'post',
apply(config: UserConfig, { command }: ConfigEnv) {
return command === 'build' && enabled
},
configResolved(config: ResolvedConfig) {
vite = config
},
closeBundle: {
async handler() {
if (!vite.build.ssr && enabled) {
const uploader = new Uploader(options, vite)

await uploader.apply()
}
},
},
}
}

export type { Options }
20 changes: 0 additions & 20 deletions src/log.js

This file was deleted.

Loading

0 comments on commit 8e086c9

Please sign in to comment.