vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
44 lines (43 loc) • 1.68 kB
JavaScript
/*
* 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/runtime/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
*/
// TODO/refactor: prefix all other plugins with `plugin` as well?
export { pluginDistPackageJsonFile };
import { rollupIsEsm } from '../../shared/rollupIsEsm.js';
import { isViteServerBuild } from '../../shared/isViteServerBuild.js';
function pluginDistPackageJsonFile() {
let config;
return {
name: 'vike:build:pluginDistPackageJsonFile',
apply: 'build',
configResolved(config_) {
config = config_;
},
generateBundle(options, bundle) {
if (!isViteServerBuild(config))
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" }`;
}
}