Skip to content

Commit

Permalink
failed attempt 2 for a workaround for vitejs/vite#2390
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Mar 15, 2021
1 parent 71d4c20 commit c64d6d1
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 19 deletions.
111 changes: 110 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"dependencies": {
"@brillout/libassert": "^0.3.0",
"@brillout/path-to-regexp": "^0.1.2",
"@types/fs-extra": "^9.0.8",
"devalue": "^2.0.1",
"fast-glob": "^3.2.5"
"fast-glob": "^3.2.5",
"fs-extra": "^9.1.0"
},
"devDependencies": {
"@types/node": "^14.14.22",
Expand Down
6 changes: 3 additions & 3 deletions src/plugin.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { assert, assertUsage } from './utils/assert'
import * as glob from 'fast-glob'
import { workaroundViteIssue2390 } from './workaroundViteIssue2390'
const SERVER_ENTRY = require.resolve('./user-files/infra.node.vite-entry')

export { plugin }

Expand Down Expand Up @@ -57,9 +56,10 @@ function entryPoints(config: UserConfig): Record<string, string> {
}

function serverEntryPoints(): Record<string, string> {
const entryName = pathFilename(SERVER_ENTRY).replace(/\.js$/, '')
const serverEntry = require.resolve('./user-files/infra.node.vite-entry')
const entryName = pathFilename(serverEntry).replace(/\.js$/, '')
const entryPoints = {
[entryName]: SERVER_ENTRY
[entryName]: serverEntry
}
return entryPoints
}
Expand Down
94 changes: 80 additions & 14 deletions src/workaroundViteIssue2390.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,98 @@
// Workaround for https://github.com/vitejs/vite/issues/2390

import { renameSync, symlinkSync, mkdirSync } from 'fs'
import {
symlinkSync,
mkdirSync,
existsSync,
unlinkSync,
copyFileSync
} from 'fs'
import { copySync, ensureFileSync } from 'fs-extra'
import {
join as pathJoin,
relative as pathRelative,
resolve as pathResolve,
dirname as pathDirname
} from 'path'
import { assert } from './utils'
import * as glob from 'fast-glob'

export { workaroundViteIssue2390 }

function workaroundViteIssue2390(userProjectRoot: string): void {
function workaroundViteIssue2390(userRoot: string): void {
if (!__dirname.includes('node_modules')) return

assert(__dirname.endsWith('node_modules/vite-plugin-ssr/dist'))
const vitePluginSsrRoot = pathResolve(`${__dirname}/..`)

const distDir = pathJoin(userProjectRoot, 'dist')
try {
mkdirSync(distDir)
} catch (_) {}
const pluginRoot = pathResolve(`${__dirname}/..`)
const pluginDist = pathJoin(pluginRoot, 'dist')
const pluginDistPristine = pathJoin(pluginRoot, 'dist-copy')

if (!existsSync(pluginDistPristine)) {
mkdirSync(pluginDistPristine)
copySync(pluginDist, pluginDistPristine)
}

const userDist = pathJoin(userRoot, 'dist')
mkdirp(userDist)
const pluginDistUserland = pathJoin(userDist, 'vite-plugin-ssr')
mkdirp(pluginDistUserland)

const filesToSymlink = getFilesToSymlink(pluginDistPristine)
filesToSymlink.forEach((file) => {
const source = pathJoin(pluginDist, file)
const target = pathJoin(pluginDistUserland, file)
const origin = pathJoin(pluginDistPristine, file)

const oldPath = vitePluginSsrRoot
const newPath = pathJoin(distDir, 'vite-plugin-ssr')
ensureFileSync(target)
copyFileSync(origin, target)

console.log(oldPath, newPath)
// Replace `oldPath` with a symlink to `newPath`
renameSync(oldPath, newPath)
symlinkSync(pathRelative(pathDirname(oldPath), newPath), oldPath)
const targetRelative = pathRelative(pathDirname(source), target)

// console.log(file, source, targetRelative)
unlinkSync(source)
symlinkSync(targetRelative, source)
})
}

function getFilesToSymlink(pluginDistPristine: string): string[] {
const clientFiles = glob.sync('**/*', { cwd: pluginDistPristine })
return clientFiles
}
// function getFilesToSymlink(pluginDistPristine: string): string[] {
// const clientFiles = glob.sync('**/*.client.js', { cwd: pluginDistPristine })
// const sharedFiles = glob.sync('**/*.shared.js', { cwd: pluginDistPristine })
// const nodeFiles = []
// const clientEntry = 'client.js'
// require.resolve(pathJoin(pluginDistPristine, clientEntry))
// clientFiles.push(clientEntry)
// const nodeEntry = pathJoin('user-files', 'infra.node.vite-entry.js')
// require.resolve(pathJoin(pluginDistPristine, nodeEntry))
// nodeFiles.push(nodeEntry)
// return [...clientFiles, ...sharedFiles, ...nodeFiles]
// }
// function getFilesToSymlink(pluginDistPristine: string): string[] {
// const clientFiles = glob.sync(`${pluginDistPristine}/**/*.client.js`)
// const sharedFiles = glob.sync(`${pluginDistPristine}/**/*.shared.js`)
// const nodeFiles = [require.resolve(pathJoin(pluginDistPristine, 'user-files', 'infra.node.vite-entry.js'))]
// return [...clientFiles, ...sharedFiles, ...nodeFiles]
// }
// function getFilesToSymlink(pluginDist: string, pluginDistPristine: string): string[] {
// const cwd = pluginDistPristine
// const clientFiles = glob.sync('**/*.client.js', {cwd})
// const sharedFiles = glob.sync('**/*.shared.js', {cwd})
// const nodeFiles = [pathJoin('user-files', 'infra.node.vite-entry.js')]
// return (
// [...clientFiles, ...sharedFiles, ...nodeFiles]
// .map(pathRelative => {
// // ensure that file actually exits
// require.resolve(pathJoin(pluginDistPristine, pathRelative))
// return pathJoin(pluginDist, pathRelative)
// })
// )
// }

function mkdirp(dirPath: string) {
try {
mkdirSync(dirPath)
} catch (_) {}
}

0 comments on commit c64d6d1

Please sign in to comment.