5

所以我有一个create-react-app-ts应用程序,我想 Dockerize 并在 Zeit Now 上托管。

一切都在本地运行良好,运行yarn tsc良好react-scripts-ts build

从以下 Dockerfile 创建 Docker 映像也很有效:

FROM mhart/alpine-node:10.9
WORKDIR /usr/src

ARG REACT_APP_API_ENDPOINT
ARG NODE_ENV

COPY yarn.lock package.json ./
RUN yarn

COPY . .
RUN yarn build && mv build /public

但是,当发布到 Now 时,构建脚本在 Typescript 编译时失败,输出项目中大多数文件的编译错误。

如果我ENV NODE_ENV production在上面的 Dockerfile 中设置,我也可以在本地重现它WORKDIR...

react-scripts-ts因此,Typescript 或NODE_ENV=production. 我以前从未遇到过这个错误,也不知道如何调试它。在本地运行NODE_ENV=production tsc或也可以正常工作。NODE_ENV=production react-scripts-ts build

我正在使用以下配置运行 Typescript v 3.0.1:

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es6",
    "lib": ["es6", "dom", "esnext.asynciterable"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "allowSyntheticDefaultImports": true,
    "strict": true
  },
  "exclude": ["node_modules", "build", "scripts", "acceptance-tests", "webpack", "jest", "src/setupTests.ts"]
}

任何建议将不胜感激!:)

编辑:向 Dockerfile 添加了 env var args。为了简洁起见,它最初被遗漏了,但它最终成为问题和解决方案的一部分

4

1 回答 1

15

所以我终于找到了问题!在我原来Dockerfile的,NODE_ENV之前 yarn install设定的。这意味着对于生产构建,yarn不会 install devDependencies,因此不会安装我的任何@types库。这导致了整个项目的所有编译错误。

在解决问题中移动NODE_ENV 下面/之后 的定义。yarn installDockerfile

FROM mhart/alpine-node:10.9
WORKDIR /usr/src

COPY yarn.lock package.json ./
RUN yarn

ARG REACT_APP_API_ENDPOINT
ARG NODE_ENV

COPY . .
RUN yarn build && mv build /public

注意:据我所知,yarn build将确保devDependencies再次删除,所以不要担心这会使您的构建膨胀。:)

于 2018-08-26T10:01:45.907 回答