@brillout/vite-plugin-server-entry
Advanced tools
Comparing version 0.4.0 to 0.4.1-commit-4182176
@@ -11,3 +11,3 @@ "use strict"; | ||
async function importServerEntry(outDir) { | ||
const autoImporter = await (0, import_1.import_)('./autoImporter'); | ||
const autoImporter = require('./autoImporter.js'); | ||
(0, debugLogsRuntime_1.debugLogsRuntimePre)(autoImporter); | ||
@@ -20,3 +20,3 @@ (0, utils_1.assertUsage)(autoImporter.status !== 'DISABLED', "As a library author, make sure your library doesn't call importServerEntry() when using `inject: true`"); | ||
try { | ||
autoImporter.loadServerEntry(); | ||
await autoImporter.loadServerEntry(); | ||
await globalThis[serverEntryImportPromise_1.serverEntryImportPromise]; | ||
@@ -23,0 +23,0 @@ success = true; |
@@ -13,3 +13,4 @@ "use strict"; | ||
const debugLogsBuildTime_1 = require("./debugLogsBuildTime"); | ||
const autoImporterFilePath = require.resolve('./importServerEntry/autoImporter'); | ||
const indexEntryName = 'index'; | ||
const autoImporterFilePath = require.resolve('../importServerEntry/autoImporter.js'); | ||
const serverEntryVirtualId = 'virtual:@brillout/vite-plugin-server-entry:serverEntry'; | ||
@@ -27,118 +28,130 @@ // https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention | ||
function serverEntryPlugin(pluginConfigProvidedByLibrary) { | ||
const pluginName = `@brillout/vite-plugin-server-entry:${pluginConfigProvidedByLibrary.libraryName.toLowerCase()}`; | ||
let config; | ||
let serverEntryFilePath; | ||
let serverIndexFilePath; | ||
let library; | ||
let skip; | ||
let injectDone = false; | ||
return { | ||
name: `@brillout/vite-plugin-server-entry:${pluginConfigProvidedByLibrary.libraryName.toLowerCase()}`, | ||
apply: 'build', | ||
configResolved(configUnresolved) { | ||
// Upon the server-side build (`$ vite build --ssr`), we need to override the previous `skip` value set by the client-side build (`$ vite build`). | ||
skip = !(0, utils_1.viteIsSSR)(configUnresolved); | ||
if (skip) | ||
return; | ||
const resolved = resolveConfig(configUnresolved, pluginConfigProvidedByLibrary); | ||
config = resolved.config; | ||
library = resolved.library; | ||
// We can't use isLeader() here but it's fine | ||
const entries = (0, utils_1.normalizeRollupInput)(config.build.rollupOptions.input); | ||
if (entries[serverEntryFileNameBase_1.serverEntryFileNameBase] === serverEntryVirtualId || | ||
entries[serverEntryFileNameBase_1.serverEntryFileNameBaseAlternative] === serverEntryVirtualId) { | ||
// Already set by another library also using @brillout/vite-plugin-server-entry | ||
return; | ||
} | ||
const fileNameBase = !entries[serverEntryFileNameBase_1.serverEntryFileNameBase] | ||
? serverEntryFileNameBase_1.serverEntryFileNameBase | ||
: serverEntryFileNameBase_1.serverEntryFileNameBaseAlternative; | ||
(0, utils_1.assert)(!entries[fileNameBase]); | ||
config.build.rollupOptions.input = (0, utils_1.injectRollupInputs)({ [fileNameBase]: serverEntryVirtualId }, config); | ||
}, | ||
buildStart() { | ||
if (skip) | ||
return; | ||
if (!isLeaderPluginInstance(config, library)) { | ||
skip = true; | ||
return; | ||
} | ||
serverEntryFilePath = config._vitePluginServerEntry.inject ? getServerEntryFilePath(config) : null; | ||
assertApiVersions(config, pluginConfigProvidedByLibrary.libraryName); | ||
clearAutoImporterFile({ status: 'RESET' }); | ||
}, | ||
resolveId(id) { | ||
if (skip) | ||
return; | ||
if (id === serverEntryVirtualId) { | ||
return virtualIdPrefix + serverEntryVirtualId; | ||
} | ||
}, | ||
load(id) { | ||
if (skip) | ||
return; | ||
(0, utils_1.assert)(id !== serverEntryVirtualId); | ||
if (id === virtualIdPrefix + serverEntryVirtualId) { | ||
const serverEntryFileContent = getServerEntryFileContent(config); | ||
return serverEntryFileContent; | ||
} | ||
}, | ||
generateBundle(_rollupOptions, bundle) { | ||
if (skip) | ||
return; | ||
const isInject = config._vitePluginServerEntry.inject; | ||
if (isInject) { | ||
(0, utils_1.assert)(injectDone); | ||
} | ||
const entryFileName = findServerEntry(bundle).fileName; | ||
// Write node_modules/@brillout/vite-plugin-server-entry/dist/autoImporter.js | ||
const isTestCrawler = config._vitePluginServerEntry.testCrawler; | ||
const doNotAutoImport = isInject || (0, utils_1.isYarnPnP)() || isTestCrawler; | ||
if (!doNotAutoImport) { | ||
writeAutoImporterFile(config, entryFileName); | ||
} | ||
else { | ||
const status = isTestCrawler ? 'TEST_CRAWLER' : 'DISABLED'; | ||
clearAutoImporterFile({ status }); | ||
(0, debugLogsBuildTime_1.debugLogsBuildtime)({ disabled: true, paths: null }); | ||
} | ||
if (!isInject) { | ||
; | ||
['importBuild.cjs', 'importBuild.mjs', 'importBuild.js'].forEach((fileName) => { | ||
this.emitFile({ | ||
fileName, | ||
type: 'asset', | ||
source: [ | ||
`globalThis.${serverEntryImportPromise_1.serverEntryImportPromise} = import('./${entryFileName}');`, | ||
`console.warn("[Warning] The server entry has been renamed from dist/server/importBuild.{cjs,mjs,js} to dist/server/entry.{cjs,mjs,js} — update your import('../path/to/dist/server/importBuild.{cjs,mjs,js}') accordingly.");` | ||
].join('\n') | ||
return [ | ||
{ | ||
name: pluginName, | ||
apply: 'build', | ||
enforce: 'post', | ||
configResolved() { | ||
if (skip) | ||
return; | ||
if (!isLeaderPluginInstance(config, library)) { | ||
skip = true; | ||
return; | ||
} | ||
if (!config._vitePluginServerEntry.inject) { | ||
const entries = (0, utils_1.normalizeRollupInput)(config.build.rollupOptions.input); | ||
(0, utils_1.assert)(entries[serverEntryFileNameBase_1.serverEntryFileNameBase] !== serverEntryVirtualId && | ||
entries[serverEntryFileNameBase_1.serverEntryFileNameBaseAlternative] !== serverEntryVirtualId); | ||
const fileNameBase = !entries[serverEntryFileNameBase_1.serverEntryFileNameBase] | ||
? serverEntryFileNameBase_1.serverEntryFileNameBase | ||
: serverEntryFileNameBase_1.serverEntryFileNameBaseAlternative; | ||
(0, utils_1.assert)(!entries[fileNameBase]); | ||
config.build.rollupOptions.input = (0, utils_1.injectRollupInputs)({ [fileNameBase]: serverEntryVirtualId }, config); | ||
} | ||
}, | ||
buildStart() { | ||
if (skip) | ||
return; | ||
serverIndexFilePath = config._vitePluginServerEntry.inject ? getServerIndexFilePath(config) : null; | ||
assertApiVersions(config, pluginConfigProvidedByLibrary.libraryName); | ||
clearAutoImporterFile({ status: 'RESET' }); | ||
}, | ||
resolveId(id) { | ||
if (skip) | ||
return; | ||
if (id === serverEntryVirtualId) { | ||
return virtualIdPrefix + serverEntryVirtualId; | ||
} | ||
}, | ||
load(id) { | ||
if (skip) | ||
return; | ||
(0, utils_1.assert)(id !== serverEntryVirtualId); | ||
if (id === virtualIdPrefix + serverEntryVirtualId) { | ||
const serverEntryFileContent = getServerEntryFileContent(config); | ||
return serverEntryFileContent; | ||
} | ||
}, | ||
generateBundle(_rollupOptions, bundle) { | ||
if (skip) | ||
return; | ||
const isInject = config._vitePluginServerEntry.inject; | ||
if (isInject) { | ||
(0, utils_1.assert)(injectDone); | ||
} | ||
const entry = findServerEntry(bundle); | ||
// Write node_modules/@brillout/vite-plugin-server-entry/dist/autoImporter.js | ||
const isTestCrawler = config._vitePluginServerEntry.testCrawler; | ||
const doNotAutoImport = isInject || (0, utils_1.isYarnPnP)() || isTestCrawler; | ||
if (!doNotAutoImport) { | ||
(0, utils_1.assert)(!isInject && entry); | ||
const entryFileName = entry.fileName; | ||
writeAutoImporterFile(config, entryFileName); | ||
} | ||
else { | ||
const status = isTestCrawler ? 'TEST_CRAWLER' : 'DISABLED'; | ||
clearAutoImporterFile({ status }); | ||
(0, debugLogsBuildTime_1.debugLogsBuildtime)({ disabled: true, paths: null }); | ||
} | ||
if (!isInject) { | ||
; | ||
['importBuild.cjs', 'importBuild.mjs', 'importBuild.js'].forEach((fileName) => { | ||
(0, utils_1.assert)(!isInject && entry); | ||
const entryFileName = entry.fileName; | ||
this.emitFile({ | ||
fileName, | ||
type: 'asset', | ||
source: [ | ||
`globalThis.${serverEntryImportPromise_1.serverEntryImportPromise} = import('./${entryFileName}');`, | ||
`console.warn("[Warning] The server entry has been renamed from dist/server/importBuild.{cjs,mjs,js} to dist/server/entry.{cjs,mjs,js} — update your import('../path/to/dist/server/importBuild.{cjs,mjs,js}') accordingly.");` | ||
].join('\n') | ||
}); | ||
}); | ||
}); | ||
} | ||
}, | ||
transform(code, id) { | ||
if (skip) | ||
return; | ||
if (!config._vitePluginServerEntry.inject) | ||
return; | ||
(0, utils_1.assert)(serverIndexFilePath); | ||
if (id !== serverIndexFilePath) | ||
return; | ||
{ | ||
const moduleInfo = this.getModuleInfo(id); | ||
(0, utils_1.assert)(moduleInfo?.isEntry); | ||
} | ||
injectDone = true; | ||
code = [ | ||
// Convenience so that the user doesn't have to set manually set it, while the user can easily override it (this is the very first line of the server code). | ||
"process.env.NODE_ENV = 'production';", | ||
// Imports the entry of each tool, e.g. the Vike entry and the Telefunc entry. | ||
`import '${serverEntryVirtualId}';`, | ||
code | ||
].join('' | ||
/* We don't insert new lines, otherwise we break the source map. | ||
'\n' | ||
*/ | ||
); | ||
return code; | ||
} | ||
}, | ||
transform(code, id) { | ||
if (skip) | ||
return; | ||
if (!config._vitePluginServerEntry.inject) | ||
return; | ||
(0, utils_1.assert)(serverEntryFilePath); | ||
if (id !== serverEntryFilePath) | ||
return; | ||
{ | ||
const moduleInfo = this.getModuleInfo(id); | ||
(0, utils_1.assert)(moduleInfo?.isEntry); | ||
{ | ||
name: `${pluginName}:config`, | ||
apply: 'build', | ||
configResolved(configUnresolved) { | ||
// Upon the server-side build (`$ vite build --ssr`), we need to override the previous `skip` value set by the client-side build (`$ vite build`). | ||
skip = !(0, utils_1.viteIsSSR)(configUnresolved); | ||
const resolved = resolveConfig(configUnresolved, pluginConfigProvidedByLibrary); | ||
config = resolved.config; | ||
library = resolved.library; | ||
} | ||
injectDone = true; | ||
code = [ | ||
// Convenience so that the user doesn't have to set manually set it, while the user can easily override it (this is the very first line of the server code). | ||
"process.env.NODE_ENV = 'production';", | ||
// Imports the entry of each tool, e.g. the Vike entry and the Telefunc entry. | ||
`import '${serverEntryVirtualId}';`, | ||
code | ||
].join('' | ||
/* We don't insert new lines, otherwise we break the source map. | ||
'\n' | ||
*/ | ||
); | ||
return code; | ||
} | ||
}; | ||
]; | ||
} | ||
@@ -213,3 +226,3 @@ exports.serverEntryPlugin = serverEntryPlugin; | ||
"exports.status = 'SET';", | ||
`exports.loadServerEntry = () => { require(${JSON.stringify(serverEntryFilePathRelative)}) };`, | ||
`exports.loadServerEntry = async () => { await import(${JSON.stringify(serverEntryFilePathRelative)}); };`, | ||
'exports.paths = {', | ||
@@ -261,3 +274,3 @@ ` autoImporterFilePathOriginal: ${JSON.stringify(autoImporterFilePath)},`, | ||
(0, utils_1.assert)((0, utils_1.isAbsolutePath)(root)); | ||
const importerDir = getImporterDir(); | ||
const importerDir = getDirname(); | ||
const rootRelative = path_1.default.posix.relative(importerDir, root); // To `require()` an absolute path doesn't seem to work on Vercel | ||
@@ -279,5 +292,5 @@ let { outDir } = config.build; | ||
} | ||
function getImporterDir() { | ||
const currentDir = (0, utils_1.toPosixPath)(__dirname + (() => '')()); // trick to avoid `@vercel/ncc` to glob import | ||
return path_1.default.posix.join(currentDir, '..'); | ||
function getDirname() { | ||
const dirname = (0, utils_1.toPosixPath)(__dirname + (() => '')()); // trick to avoid `@vercel/ncc` to glob import | ||
return dirname; | ||
} | ||
@@ -302,3 +315,3 @@ function assertApiVersions(config, currentLibraryName) { | ||
else { | ||
// Should be true because of isLeaderPluginInstance() | ||
// Should be true thanks to isLeaderPluginInstance() | ||
(0, utils_1.assert)(apiVersionLib === apiVersion); | ||
@@ -321,4 +334,5 @@ } | ||
// prettier-ignore | ||
(0, utils_1.findRollupBundleEntry)(serverEntryFileNameBase_1.serverEntryFileNameBase, bundle) || | ||
(0, utils_1.findRollupBundleEntry)(serverEntryFileNameBase_1.serverEntryFileNameBaseAlternative, bundle); | ||
(0, utils_1.findRollupBundleEntry)(serverEntryFileNameBase_1.serverEntryFileNameBaseAlternative, bundle) || | ||
(0, utils_1.findRollupBundleEntry)(serverEntryFileNameBase_1.serverEntryFileNameBase, bundle) || | ||
(0, utils_1.findRollupBundleEntry)(indexEntryName, bundle); | ||
(0, utils_1.assert)(entry); | ||
@@ -328,6 +342,5 @@ return entry; | ||
exports.findServerEntry = findServerEntry; | ||
function getServerEntryFilePath(config) { | ||
function getServerIndexFilePath(config) { | ||
const entries = (0, utils_1.normalizeRollupInput)(config.build.rollupOptions.input); | ||
const entryName = 'index'; | ||
let serverEntryFilePath = entries[entryName]; | ||
let serverEntryFilePath = entries[indexEntryName]; | ||
if (!serverEntryFilePath) { | ||
@@ -337,3 +350,3 @@ const entryNames = Object.keys(entries) | ||
.join(', '); | ||
(0, utils_1.assertUsage)(false, `Cannot find server entry '${entryName}'. Make sure your Rollup config doesn't remove the entry '${entryName}' of your server build ${config.build.outDir}. (Found server entries: [${entryNames}].)`); | ||
(0, utils_1.assertUsage)(false, `Cannot find server entry '${indexEntryName}'. Make sure your Rollup config doesn't remove the entry '${indexEntryName}' of your server build ${config.build.outDir}. (Found server entries: [${entryNames}].)`); | ||
} | ||
@@ -340,0 +353,0 @@ serverEntryFilePath = require.resolve(serverEntryFilePath); |
export { assert }; | ||
export { assertUsage }; | ||
export { logLabel }; | ||
declare const logLabel: "[@brillout/vite-plugin-server-entry@0.4.0]"; | ||
declare const logLabel: "[@brillout/vite-plugin-server-entry@0.4.1-commit-4182176]"; | ||
declare function assert(condition: unknown, debugInfo?: unknown): asserts condition; | ||
declare function assertUsage(condition: unknown, errorMessage: string): asserts condition; |
export declare const projectInfo: { | ||
projectName: "@brillout/vite-plugin-server-entry"; | ||
npmPackageName: "@brillout/vite-plugin-server-entry"; | ||
projectVersion: "0.4.0"; | ||
projectVersion: "0.4.1-commit-4182176"; | ||
githubRepository: "https://github.com/brillout/vite-plugin-server-entry"; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.projectInfo = void 0; | ||
const PROJECT_VERSION = '0.4.0'; | ||
const PROJECT_VERSION = '0.4.1-commit-4182176'; | ||
exports.projectInfo = { | ||
@@ -6,0 +6,0 @@ projectName: '@brillout/vite-plugin-server-entry', |
{ | ||
"name": "@brillout/vite-plugin-server-entry", | ||
"version": "0.4.0", | ||
"version": "0.4.1-commit-4182176", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@brillout/import": "^0.2.3" |
51810
1085