Skip to content

Make build folder of adapter-node self-sufficient #6797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import { fileURLToPath } from 'url';
import * as esbuild from 'esbuild';

Expand Down Expand Up @@ -45,7 +45,10 @@ export default function (opts = {}) {
`export const manifest = ${builder.generateManifest({ relativePath: './' })};`
);

const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
const optimized_package_json = builder.generatePackageJson();
writeFileSync(`${out}/package.json`, optimized_package_json);

const pkg = JSON.parse(optimized_package_json);

await esbuild.build({
platform: 'node',
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/src/core/adapt/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pipeline } from 'stream';
import { promisify } from 'util';
import { copy, rimraf, mkdirp } from '../../utils/filesystem.js';
import { generate_manifest } from '../generate_manifest/index.js';
import { generate_package_json } from '../generate_package_json/index.js';

const pipe = promisify(pipeline);

Expand Down Expand Up @@ -113,6 +114,13 @@ export function create_builder({ config, build_data, routes, prerendered, log })
});
},

generatePackageJson: () => {
return generate_package_json({
build_data,
cwd: process.cwd()
});
},

getBuildDirectory(name) {
return `${config.kit.outDir}/${name}`;
},
Expand Down
79 changes: 79 additions & 0 deletions packages/kit/src/core/generate_package_json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import fs from 'node:fs';
import path from 'node:path';

/**
* Generates a production package.json
* @param {{
* build_data: import('types').BuildData;
* cwd: string;
* }} opts
*/
export function generate_package_json({ build_data, cwd }) {
const package_json_text = fs.readFileSync(path.resolve(cwd, 'package.json'), 'utf8');
const package_json = JSON.parse(package_json_text);
const dependences_with_versions = Object.assign(
{},
package_json.devDependencies || {},
package_json.dependencies || {}
);

const dependencies = Object.keys(dependences_with_versions);

/** @type {Set<string>} */
const dependencies_set = new Set(dependencies);

const prod_package_json = Object.assign({}, package_json);
delete prod_package_json.devDependencies;
delete prod_package_json.scripts;
prod_package_json.dependencies = {};

const chunks = [build_data.client.chunks, build_data.server.chunks].flat();
if (chunks.length > 0) {
/** @type {string[]} */
const dependency_names = filter_import_packages(chunks, dependencies_set);
dependency_names.sort();
prod_package_json.dependencies = create_dependency_object(
dependency_names,
dependences_with_versions
);
}

return JSON.stringify(prod_package_json, null, 2);
}
/**
*
* @param {import('rollup').OutputChunk[]} chunks
* @param {Set<string>} dependencies_set
*/
function filter_import_packages(chunks, dependencies_set) {
/** @type {string[]} */
const import_names = chunks.reduce((/** @type {string[]} */ deps, chunk) => {
if (chunk.imports.length > 0) {
for (let i = 0; i < chunk.imports.length; i++) {
const importName = chunk.imports[i];
if (dependencies_set.has(importName)) {
deps.push(importName);
}
}
}

return deps;
}, []);

return import_names;
}

/**
*
* @param {string[]} dependency_names
* @param {{[key: string]: string}} dependences_with_versions
*/
function create_dependency_object(dependency_names, dependences_with_versions) {
/** @type {{[key: string]: string}} */
const dependencies = {};
for (let i = 0; i < dependency_names.length; i++) {
dependencies[dependency_names[i]] = dependences_with_versions[dependency_names[i]];
}

return dependencies;
}
10 changes: 10 additions & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { preview } from './preview/index.js';
import { get_aliases, resolve_entry, prevent_illegal_rollup_imports, get_env } from './utils.js';
import { fileURLToPath } from 'node:url';
import { create_static_module, create_dynamic_module } from '../../core/env.js';
import { generate_package_json } from '../../core/generate_package_json/index.js';

const cwd = process.cwd();

Expand Down Expand Up @@ -404,6 +405,15 @@ function kit() {
})};\n`
);

const package_json_path = `${paths.output_dir}/package.json`;
fs.writeFileSync(
package_json_path,
generate_package_json({
build_data,
cwd
})
);

log.info('Prerendering');
await new Promise((fulfil, reject) => {
const results_path = `${svelte_config.kit.outDir}/generated/prerendered.json`;
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface Builder {
createEntries(fn: (route: RouteDefinition) => AdapterEntry): Promise<void>;

generateManifest: (opts: { relativePath: string; format?: 'esm' | 'cjs' }) => string;
generatePackageJson: () => string;

getBuildDirectory(name: string): string;
getClientDirectory(): string;
Expand Down