Skip to content

feat: Improve external dependency handling for non-standard project structures (adapter-node) #13796

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare global {

interface AdapterOptions {
out?: string;
externalDependencies?: string[] | 'auto';
precompress?: boolean;
envPrefix?: string;
}
Expand Down
17 changes: 11 additions & 6 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const files = fileURLToPath(new URL('./files', import.meta.url).href);

/** @type {import('./index.js').default} */
export default function (opts = {}) {
const { out = 'build', precompress = true, envPrefix = '' } = opts;
const { out = 'build', precompress = true, envPrefix = '', externalDependencies = 'auto' } = opts;

return {
name: '@sveltejs/adapter-node',
Expand Down Expand Up @@ -46,7 +46,14 @@ export default function (opts = {}) {
].join('\n\n')
);

const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
let external;

if (externalDependencies === 'auto') {
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
external = Object.keys(pkg.dependencies || {});
} else {
external = externalDependencies;
}

// we bundle the Vite output so that deployments only need
// their production dependencies. Anything in devDependencies
Expand All @@ -56,10 +63,8 @@ export default function (opts = {}) {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`
},
external: [
// dependencies could have deep exports, so we need a regex
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
],
// dependencies could have deep exports, so we need a regex
external: external.map((d) => new RegExp(`^${d}(\\/.*)?$`)),
plugins: [
nodeResolve({
preferBuiltins: true,
Expand Down
Loading