From c64d6d1bc255d4cb99794e3e3641942478836c87 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Sun, 14 Mar 2021 22:03:43 +0100 Subject: [PATCH] failed attempt 2 for a workaround for vitejs/vite#2390 --- package-lock.json | 111 ++++++++++++++++++++++++++++++++- src/package.json | 4 +- src/plugin.node.ts | 6 +- src/workaroundViteIssue2390.ts | 94 +++++++++++++++++++++++----- 4 files changed, 196 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index de17b6cade..14bacbb877 100644 --- a/package-lock.json +++ b/package-lock.json @@ -861,6 +861,14 @@ "@babel/types": "^7.3.0" } }, + "node_modules/@types/fs-extra": { + "version": "9.0.8", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.8.tgz", + "integrity": "sha512-bnlTVTwq03Na7DpWxFJ1dvnORob+Otb8xHyUqUWhqvz/Ksg8+JXPlR52oeMSZ37YEOa5PyccbgUNutiQdi13TA==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/graceful-fs": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", @@ -1193,6 +1201,14 @@ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, + "node_modules/at-least-node": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/atob": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", @@ -2951,6 +2967,28 @@ "node": ">=0.10.0" } }, + "node_modules/fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "dependencies": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/fs-extra/node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "engines": { + "node": ">= 10.0.0" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -4878,6 +4916,26 @@ "node": ">=6" } }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dependencies": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/jsonfile/node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "engines": { + "node": ">= 10.0.0" + } + }, "node_modules/jsonparse": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz", @@ -8496,8 +8554,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", @@ -9247,6 +9307,14 @@ "@babel/types": "^7.3.0" } }, + "@types/fs-extra": { + "version": "9.0.8", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.8.tgz", + "integrity": "sha512-bnlTVTwq03Na7DpWxFJ1dvnORob+Otb8xHyUqUWhqvz/Ksg8+JXPlR52oeMSZ37YEOa5PyccbgUNutiQdi13TA==", + "requires": { + "@types/node": "*" + } + }, "@types/graceful-fs": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", @@ -9511,6 +9579,11 @@ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, + "at-least-node": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==" + }, "atob": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", @@ -10833,6 +10906,24 @@ "resolved": "https://registry.npmjs.org/fs-exists-sync/-/fs-exists-sync-0.1.0.tgz", "integrity": "sha1-mC1ok6+RjnLQjeyehnP/K1qNat0=" }, + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "dependencies": { + "universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==" + } + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -12312,6 +12403,22 @@ "minimist": "^1.2.5" } }, + "jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + }, + "dependencies": { + "universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==" + } + } + }, "jsonparse": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz", @@ -14769,9 +14876,11 @@ "requires": { "@brillout/libassert": "^0.3.0", "@brillout/path-to-regexp": "^0.1.2", + "@types/fs-extra": "^9.0.8", "@types/node": "^14.14.22", "devalue": "^2.0.1", "fast-glob": "^3.2.5", + "fs-extra": "^9.1.0", "typescript": "^4.1.5", "vite": "^2.0.0" } diff --git a/src/package.json b/src/package.json index 6e0b859b00..6b4a313b62 100644 --- a/src/package.json +++ b/src/package.json @@ -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", diff --git a/src/plugin.node.ts b/src/plugin.node.ts index 850f673af1..7521049b5e 100644 --- a/src/plugin.node.ts +++ b/src/plugin.node.ts @@ -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 } @@ -57,9 +56,10 @@ function entryPoints(config: UserConfig): Record { } function serverEntryPoints(): Record { - 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 } diff --git a/src/workaroundViteIssue2390.ts b/src/workaroundViteIssue2390.ts index 2a2dd82678..5c485922e6 100644 --- a/src/workaroundViteIssue2390.ts +++ b/src/workaroundViteIssue2390.ts @@ -1,6 +1,13 @@ // 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, @@ -8,25 +15,84 @@ import { 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 (_) {} }