Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
feat: set to module
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Jan 6, 2024
1 parent d93f46d commit f7357cc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 39 deletions.
92 changes: 92 additions & 0 deletions bin/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env node

import { readFileSync, readdirSync, unlinkSync, existsSync } from 'fs'
import { dirname } from 'path'

/*
* Argv helpers.
*/

const argument = (name) => {
const index = process.argv.findIndex(argument => argument.startsWith(`--${name}=`))

return index === -1
? undefined
: process.argv[index].substring(`--${name}=`.length)
}

const option = (name) => process.argv.includes(`--${name}`)

/*
* Helpers.
*/
const info = option(`quiet`) ? (() => undefined) : console.log
const error = option(`quiet`) ? (() => undefined) : console.error

/*
* Clean.
*/

const main = () => {
const manifestPaths = argument(`manifest`) ? [argument(`manifest`)] : (option(`ssr`)
? [`./bootstrap/ssr/ssr-manifest.json`, `./bootstrap/ssr/manifest.json`]
: [`./public/build/manifest.json`])

const foundManifestPath = manifestPaths.find(existsSync)

if (! foundManifestPath) {
error(`Unable to find manifest file.`)

process.exit(1)
}

info(`Reading manifest [${foundManifestPath}].`)

const manifest = JSON.parse(readFileSync(foundManifestPath).toString())

const manifestFiles = Object.keys(manifest)

const isSsr = Array.isArray(manifest[manifestFiles[0]])

isSsr
? info(`SSR manifest found.`)
: info(`Non-SSR manifest found.`)

const manifestAssets = isSsr
? manifestFiles.flatMap(key => manifest[key])
: manifestFiles.flatMap(key => [
...manifest[key].css ?? [],
manifest[key].file,
])

const assetsPath = argument('assets') ?? dirname(foundManifestPath)+'/assets'

info(`Verify assets in [${assetsPath}].`)

const existingAssets = readdirSync(assetsPath, { withFileTypes: true })

const orphanedAssets = existingAssets.filter(file => file.isFile())
.filter(file => manifestAssets.findIndex(asset => asset.endsWith(`/${file.name}`)) === -1)

if (orphanedAssets.length === 0) {
info(`No orphaned assets found.`)
} else {
orphanedAssets.length === 1
? info(`[${orphanedAssets.length}] orphaned asset found.`)
: info(`[${orphanedAssets.length}] orphaned assets found.`)

orphanedAssets.forEach(asset => {
const path = `${assetsPath}/${asset.name}`

option(`dry-run`)
? info(`Orphaned asset [${path}] would be removed.`)
: info(`Removing orphaned asset [${path}].`)

if (! option(`dry-run`)) {
unlinkSync(path)
}
})
}
}

main()
3 changes: 0 additions & 3 deletions import.meta.url-polyfill.js

This file was deleted.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "litestar-vite-plugin",
"version": "0.4.3",
"version": "0.5.1",
"type":"module",
"description": "Litestar plugin for Vite.",
"keywords": [
"litestar",
Expand All @@ -16,27 +17,26 @@
"author": "Litestar",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./inertia-helpers": {
"import": "./inertia-helpers/index.js",
"types": "./inertia-helpers/index.d.ts",
"node": "./inertia-helpers/index.js"
"default": "./inertia-helpers/index.js"
}
},
"types": "./dist/index.d.ts",
"files": [
"/dist",
"/inertia-helpers"
],
],"bin": {
"clean-orphaned-assets": "bin/clean.js"
},
"scripts": {
"build": "npm run build-plugin && npm run build-inertia-helpers",
"build-plugin": "rm -rf dist && npm run build-plugin-types && npm run build-plugin-esm && npm run build-plugin-cjs && cp src/dev-server-index.html dist/",
"build-plugin": "rm -rf dist && npm run build-plugin-types && npm run build-plugin-esm && cp src/dev-server-index.html dist/",
"build-plugin-types": "tsc --emitDeclarationOnly",
"build-plugin-cjs": "esbuild src/index.ts --platform=node --format=cjs --outfile=dist/index.cjs --define:import.meta.url=import_meta_url --inject:./import.meta.url-polyfill.js",
"build-plugin-esm": "esbuild src/index.ts --platform=node --format=esm --outfile=dist/index.mjs",
"build-plugin-esm": "esbuild src/index.ts --platform=node --format=esm --outfile=dist/index.js",
"build-inertia-helpers": "rm -rf inertia-helpers && tsc --project tsconfig.inertia-helpers.json",
"lint": "eslint --ext .ts ./src ./tests",
"test": "vitest run"
Expand All @@ -59,6 +59,6 @@
},
"dependencies": {
"picocolors": "^1.0.0",
"vite-plugin-full-reload": "^1.0.5"
"vite-plugin-full-reload": "^1.1.0"
}
}
30 changes: 7 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import fs from "fs";
import { AddressInfo } from "net";
import { fileURLToPath } from "url";
import path from "path";
import colors from "picocolors";
import {
Plugin,
loadEnv,
UserConfig,
ConfigEnv,
ResolvedConfig,
SSROptions,
PluginOption,
} from "vite";
import fullReload, {
Config as FullReloadConfig,
} from "vite-plugin-full-reload";
import fs from 'fs'
import { AddressInfo } from 'net'
import { fileURLToPath } from 'url'
import path from 'path'
import colors from 'picocolors'
import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption } from 'vite'
import fullReload, { Config as FullReloadConfig } from 'vite-plugin-full-reload'

interface PluginConfig {
/**
Expand Down Expand Up @@ -738,9 +728,3 @@ function dirname(): string {
return fileURLToPath(new URL(".", import.meta.url));
}

/**
* Path to certificates.
*/
function certConfigPath(): string {
return path.resolve(process.cwd(), ".config")
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"compilerOptions": {
"outDir": "./dist",
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"strict": true,
"declaration": true,
Expand Down

0 comments on commit f7357cc

Please sign in to comment.