Docker have the same file in multiple layers
Solution 1:
The pattern where you copy a file like package.json first and then later copy all files is done to support a development process where you build often and want to minimise the time that process takes.
If you have other concerns that you want to prioritize, there's no reason why you shouldn't be able to do that.
For instance, if you build often, your Dockerfile could look like this
FROM alpine
WORKDIR /app
COPY a.txt .
COPY . .
CMD ["ls", "-la"]
If you want to minimise final image size, you can create another Dockerfile that copies everything from that image in a single operation like this
FROM my-build-image as build
FROM alpine as final
COPY --from=build /app /app
Having a single Dockerfile that is both quick when you build a lot and also minimizes space use is not doable, I think.