UNPKG

telefunc

Version:

Remote functions. Instead of API.

42 lines (41 loc) 1.48 kB
/* * We create a file `dist/server/package.json` to support ESM users. * Otherwise, following error is thrown: * Must use import to load ES Module: dist/server/pageFiles.js * require() of ES modules is not supported. * require() of dist/server/pageFiles.js from node_modules/vike/dist/esm/node/page-files/setup.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. * Reproduction: https://github.com/brillout/vite-plugin-ssr-server-import-syntax */ export { packageJsonFile }; import { rollupIsEsm } from '../helpers.js'; function packageJsonFile() { let config; return { name: 'telefunc:packageJsonFile', apply: 'build', configResolved(config_) { config = config_; }, generateBundle(options, bundle) { if (!config.build.ssr) return; const isEsm = rollupIsEsm(options); const fileName = 'package.json'; if (bundle[fileName]) return; // E.g. already generated by Telefunc / Vike this.emitFile({ fileName, type: 'asset', source: getPackageJsonContent(isEsm), }); }, }; } function getPackageJsonContent(isEsm) { if (isEsm) { return `{ "type": "module" }`; } else { return `{ "type": "commonjs" }`; } }