Skip to content

Conversation

sommeeeer
Copy link
Contributor

This PR introduces a new feature for open-next.config.ts. You can now specify additional files to be copied after the server function(s) has been bundled. This feature also supports split functions.

The srcPath can be a relative or absolute path and can accept glob patterns. If a glob matches multiple files, dstPath will be treated as a directory. If only a single file is matched, dstPath will be treated as a file.

Here is an example:

import type { OpenNextConfig } from '@opennextjs/aws/types/open-next';
const config = {
  default: {
    copyFiles: [
      {
        // srcPath can be relative or absolute
        // can also be a glob pattern
        srcPath: '**/*.js',
        // dstPath is relative to the functions output
        // will become a directory if multiple files is matched in srcPath
        dstPath: '/myfolder/here',
      },
    ],
  },
  functions: {
    testfunction: {
      routes: ['app/api/test/route'], 
      patterns: ['api/test/route'],
      override: {
        wrapper: 'aws-lambda-streaming',
      },
      copyFiles: [
        {
          srcPath: '/home/user/Downloads/important.wasm',
          dstPath: 'wasm/my.wasm',
        },
      ],
    },
  },
} satisfies OpenNextConfig;

Copy link

changeset-bot bot commented Apr 1, 2025

⚠️ No Changeset found

Latest commit: d28ff4d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

pkg-pr-new bot commented Apr 1, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@807

commit: d28ff4d

export function copyCustomFiles(copyFiles: CopyFile[], outputPath: string) {
copyFiles.forEach(({ srcPath, dstPath }) => {
// Find all files matching the pattern
const matchedFiles = globSync(srcPath, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would that work with "relative" files?
and relative to what?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would that work with "relative" files?
and relative to what?

yeah, and by default its relative to process.cwd(). perhaps i should change that in-case you are in a monorepo.

"another-filedsfdsf.txt": "another content",
},
"empty-file.txtfdsf": "",
"imporant-files": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"imporant-files": {
"important-files": {

return;
}

if (matchedFiles.length === 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I want to copy *.js to /some/path, will be behavior be different if there is one vs multiple files? (if yes, that does not sound right)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I want to copy *.js to /some/path, will be behavior be different if there is one vs multiple files? (if yes, that does not sound right)

yeah your correct about that one. after some more thought i should probably think about a better way to handle this. im open for suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to handle that could be to have exclusive dstFile (error on multiple match) and dstDir options

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I am not sure what use case you are solving with this PR. Maybe having a hook accepting a function taking the buildOptions as a parameter could be good enough - that would mean the options are part of the API and must be documented and ~stable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to handle that could be to have exclusive dstFile (error on multiple match) and dstDir options

thats a good idea. probably something I would go for if we choose to proceed with this PR.

Also I am not sure what use case you are solving with this PR

the idea was that sometimes you need or want a file that's not there. we've had a few cases where people were missing a file in the server function's output. i.e this discord thread

however, if you guys think this wont be needed i can close it. Next does have outputFileTracingIncludes which probably might be enough for most cases anyways.

Maybe having a hook accepting a function taking the buildOptions as a parameter could be good enough - that would mean the options are part of the API and must be documented and ~stable.

that is an excellent idea. im gonna think about this until tomorrow. with more thoroughly thought this PR might not be needed for any use-cases.

@sommeeeer
Copy link
Contributor Author

thanks for the feedback on this @vicb. after a night sleep I decided to close this, and I will instead write about outputFileTracingIncludes in our docs. will do some more testing on it with OpenNext beforehand.

@sommeeeer sommeeeer closed this Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants