Skip to content
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

fix: Move copying translation files before npm run build in Docker #30099

Merged
Merged
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
35 changes: 24 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ FROM --platform=${BUILDPLATFORM} node:20-bullseye-slim AS superset-node

ARG NPM_BUILD_CMD="build"

# Include translations in the final build. The default supports en only to
# reduce complexity and weight for those only using en
ARG BUILD_TRANSLATIONS="false"

# Used by docker-compose to skip the frontend build,
# in dev we mount the repo and build the frontend inside docker
ARG DEV_MODE="false"
Expand All @@ -51,7 +55,7 @@ RUN --mount=type=bind,target=/frontend-mem-nag.sh,src=./docker/frontend-mem-nag.

WORKDIR /app/superset-frontend
# Creating empty folders to avoid errors when running COPY later on
RUN mkdir -p /app/superset/static/assets && mkdir -p /app/superset/translations
RUN mkdir -p /app/superset/static/assets
RUN --mount=type=bind,target=./package.json,src=./superset-frontend/package.json \
--mount=type=bind,target=./package-lock.json,src=./superset-frontend/package-lock.json \
if [ "$DEV_MODE" = "false" ]; then \
Expand All @@ -62,20 +66,21 @@ RUN --mount=type=bind,target=./package.json,src=./superset-frontend/package.json

# Runs the webpack build process
COPY superset-frontend /app/superset-frontend
# This copies the .po files needed for translation
RUN mkdir -p /app/superset/translations
COPY superset/translations /app/superset/translations
RUN if [ "$DEV_MODE" = "false" ]; then \
npm run ${BUILD_CMD}; \
BUILD_TRANSLATIONS=$BUILD_TRANSLATIONS npm run ${BUILD_CMD}; \
else \
echo "Skipping 'npm run ${BUILD_CMD}' in dev mode"; \
fi

# This copies the .po files needed for translation
RUN mkdir -p /app/superset/translations
COPY superset/translations /app/superset/translations

# Compiles .json files from the .po files, then deletes the .po files
RUN if [ "$DEV_MODE" = "false" ]; then \
RUN if [ "$BUILD_TRANSLATIONS" = "true" ]; then \
npm run build-translation; \
else \
echo "Skipping translations in dev mode"; \
echo "Skipping translations as requested by build flag"; \
fi
RUN rm /app/superset/translations/*/LC_MESSAGES/*.po
RUN rm /app/superset/translations/messages.pot
Expand All @@ -85,6 +90,10 @@ RUN rm /app/superset/translations/messages.pot
######################################################################
FROM python:${PY_VER} AS lean

# Include translations in the final build. The default supports en only to
# reduce complexity and weight for those only using en
ARG BUILD_TRANSLATIONS="false"

WORKDIR /app
ENV LANG=C.UTF-8 \
LC_ALL=C.UTF-8 \
Expand Down Expand Up @@ -133,10 +142,14 @@ COPY --chown=superset:superset --from=superset-node /app/superset/translations s

# Compile translations for the backend - this generates .mo files, then deletes the .po files
COPY ./scripts/translations/generate_mo_files.sh ./scripts/translations/
RUN ./scripts/translations/generate_mo_files.sh \
&& chown -R superset:superset superset/translations \
&& rm superset/translations/messages.pot \
&& rm superset/translations/*/LC_MESSAGES/*.po
RUN if [ "$BUILD_TRANSLATIONS" = "true" ]; then \
./scripts/translations/generate_mo_files.sh \
&& chown -R superset:superset superset/translations \
&& rm superset/translations/messages.pot \
&& rm superset/translations/*/LC_MESSAGES/*.po; \
else \
echo "Skipping translations as requested by build flag"; \
fi

COPY --chmod=755 ./docker/run-server.sh /usr/bin/
USER superset
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/installation/docker-builds.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ script and the [docker.yml](https://github.com/apache/superset/blob/master/.gith
GitHub action.

## Key ARGs in Dockerfile
- `BUILD_TRANSLATIONS`: whether to build the translations into the image. For the
frontend build this tells webpack to strip out all locales other than `en` from
the `moment-timezone` library. For the backendthis skips compiling the
`*.po` translation files
- `DEV_MODE`: whether to skip the frontend build, this is used by our `docker-compose` dev setup
where we mount the local volume and build using `webpack` in `--watch` mode, meaning as you
alter the code in the local file system, webpack, from within a docker image used for this
Expand Down
13 changes: 6 additions & 7 deletions superset-frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const ROOT_DIR = path.resolve(__dirname, '..');
const TRANSLATIONS_DIR = path.resolve(__dirname, '../superset/translations');

const getAvailableTranslationCodes = () => {
const LOCALE_CODE_MAPPING = {
zh: 'zh-cn',
};
try {
if (process.env.BUILD_TRANSLATIONS === 'true') {
const LOCALE_CODE_MAPPING = {
zh: 'zh-cn',
};
const files = fs.readdirSync(TRANSLATIONS_DIR);
return files
.filter(file =>
Expand All @@ -57,10 +57,9 @@ const getAvailableTranslationCodes = () => {
.filter(dirName => !dirName.startsWith('__'))
.map(dirName => dirName.replace('_', '-'))
.map(dirName => LOCALE_CODE_MAPPING[dirName] || dirName);
} catch (err) {
console.error('Error reading the directory:', err);
return [];
}
// Indicates to the MomentLocalesPlugin that we only want to keep 'en'.
return [];
};

const {
Expand Down
Loading