NextJS:使用outputStandalone选项强制依赖

Hug*_*ace 4 reactjs next.js

通过新的outputStandalone实验性功能(https://nextjs.org/docs/advanced-features/output-file-tracing#automatically-copying-traced-files-experimental),我们可以在构建后拥有一个独立的文件夹,其中包含必要的依赖项,我们只需将其复制到我们的docker中,不需要在docker内重建。它会自动检测源代码中必要的依赖项,除非该依赖项仅在我们的 package.json 中使用。

我们cross-env用来启动 Next 应用程序,当然这个库永远不会导入到我们的源代码中,但它需要存在于独立文件夹的 node_modules 中。

那么我怎样才能强制@vercel/nft包含特定的依赖项呢?

Tan*_*kom 9

我的案例是我们的Dockerized Next.js项目使用Knex.js,其中使用 npm 脚本调用迁移。此后,一些仅需要的依赖项knex.migrations()从未包含在独立输出中。


感谢sbstn您可以使用stable_includeFiles创建依赖项列表,该列表应包含在独立输出中。此解决方案扫描目录中的.js和。.jsonnode_modules/

应该强调的是,此设置在 内部不可用next.config.js,但作为页面配置道具,这意味着它必须从与和路由export const config = {}不同的页面导出到内部。_app.jsx/api

// pages/index.jsx

export default function Home(){
   ...
}

const requiredStandaloneDependencies = [
    // some required deps that have not been included in standalone
    "commander",
    "debug",
    "escalade",
    "get-package-type",
    "getopts",
    "interpret",
    "lodash",
    "pg-connection-string",
    "rechoir",
    "resolve-from",
    "tarn",
    "tildify",
];

export const config = {
    unstable_includeFiles: requiredStandaloneDependencies.map(
        (dep) => `node_modules/${dep}/**/*.+(js|json)`
    ),
};
Run Code Online (Sandbox Code Playgroud)

作为我的案例的最后一个想法,在使用自定义 npm 脚本调用迁移的情况下npm run migrate:latest,我还应该能够使用以编程方式调用的迁移来规避该问题。我只是不确定如何将该方法添加到 Next.js 项目树中以在运行时初始化期间运行。