@brillout/vite-plugin-server-entry
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -13,2 +13,3 @@ "use strict"; | ||
const debugLogsBuildTime_1 = require("./debugLogsBuildTime"); | ||
const indexEntryName = 'index'; | ||
const autoImporterFilePath = require.resolve('../importServerEntry/autoImporter.js'); | ||
@@ -18,3 +19,3 @@ const serverEntryVirtualId = 'virtual:@brillout/vite-plugin-server-entry:serverEntry'; | ||
const virtualIdPrefix = '\0'; | ||
const apiVersion = 3; | ||
const apiVersion = 4; | ||
/** | ||
@@ -28,118 +29,135 @@ * This plugin does two things: | ||
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', | ||
// We need to run this plugin after other plugin instances, so that assertApiVersions() works also for libraries using older plugin versions | ||
enforce: 'post', | ||
configResolved() { | ||
if (skip) | ||
return; | ||
if (!isLeaderPluginInstance(config, library)) { | ||
skip = true; | ||
return; | ||
} | ||
assertApiVersions(config, pluginConfigProvidedByLibrary.libraryName); | ||
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; | ||
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', | ||
// We need to run this plugin before in order to make isLeaderPluginInstance() work | ||
enforce: 'pre', | ||
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; | ||
} | ||
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; | ||
} | ||
}; | ||
]; | ||
} | ||
@@ -300,3 +318,3 @@ exports.serverEntryPlugin = serverEntryPlugin; | ||
else { | ||
// Should be true because of isLeaderPluginInstance() | ||
// Should be true thanks to isLeaderPluginInstance() | ||
(0, utils_1.assert)(apiVersionLib === apiVersion); | ||
@@ -319,4 +337,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); | ||
@@ -326,6 +345,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) { | ||
@@ -335,3 +353,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}].)`); | ||
} | ||
@@ -338,0 +356,0 @@ serverEntryFilePath = require.resolve(serverEntryFilePath); |
export { assert }; | ||
export { assertUsage }; | ||
export { logLabel }; | ||
declare const logLabel: "[@brillout/vite-plugin-server-entry@0.4.1]"; | ||
declare const logLabel: "[@brillout/vite-plugin-server-entry@0.4.2]"; | ||
declare function assert(condition: unknown, debugInfo?: unknown): asserts condition; | ||
declare function assertUsage(condition: unknown, errorMessage: string): asserts condition; |
@@ -10,4 +10,3 @@ "use strict"; | ||
const usageErrorPrefix = `${logLabel}[Wrong Usage]`; | ||
// We set to `0` because @brillout/vite-plugin-server-entry is never used directly by the user | ||
const numberOfStackTraceLinesToRemove = 0; | ||
const numberOfStackTraceLinesToRemove = 2; | ||
function assert(condition, debugInfo) { | ||
@@ -14,0 +13,0 @@ if (condition) { |
export declare const projectInfo: { | ||
projectName: "@brillout/vite-plugin-server-entry"; | ||
npmPackageName: "@brillout/vite-plugin-server-entry"; | ||
projectVersion: "0.4.1"; | ||
projectVersion: "0.4.2"; | ||
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.1'; | ||
const PROJECT_VERSION = '0.4.2'; | ||
exports.projectInfo = { | ||
@@ -6,0 +6,0 @@ projectName: '@brillout/vite-plugin-server-entry', |
{ | ||
"name": "@brillout/vite-plugin-server-entry", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@brillout/import": "^0.2.3" |
51986
1089