facebook / docusaurus

Easy to maintain open source documentation websites.
https://docusaurus.io
MIT License
56.49k stars 8.48k forks source link

Do not transform static JS assets #10460

Open erke-soptim opened 2 months ago

erke-soptim commented 2 months ago

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

Static assets ending in .js seem to be modified in a docusaurus build.

Reproducible demo

No response

Steps to reproduce

Create a new Docusaurus project.

npx create-docusaurus@latest my-website classic --typescript
cd my-website

Create a static asset containing some JavaScript.

echo "let a = 1; let b = 2;" > ./static/example.js

Build and serve the project.

npm run build
npm run serve

Open localhost:3000/example.js in your browser and observe that the content served by the browser does not look like the original file anymore, but like this.

let a=1,b=2;

This does not happen when renaming the file to use a different extension, such as .txt.

Expected behavior

I would expect Docusaurus not to modify the contents of static .js assets.

Actual behavior

Docusaurus actually transformed the asset.

Your environment

Self-service

OzakIOne commented 2 months ago

This is probably due to webpack minifying css js html files and probably other too to lower the bundle size when building your site.

What is your usecase with these js files in the static folder ?

Josh-Cena commented 2 months ago

Static files should never be changed. They could be example code that's downloaded. I thought there's a similar issue but I can't find it now.

Josh-Cena commented 2 months ago

Ah, https://github.com/facebook/docusaurus/issues/10334 should be similar. Perhaps they are the same issue.

erke-soptim commented 2 months ago

Static files should never be changed.

That is what I understood from the docs saying that none of the files in the static folder will be post-processed, hashed, or minified.

They could be example code that's downloaded. I thought there's a similar issue but I can't find it now.

That is exactly what we are trying to achieve. We have a bunch of code snippets in the static folder that users can view and download. So, naturally, we would like to preserve how those are formatted.

hrumhurum commented 2 months ago

Perhaps related to https://github.com/facebook/docusaurus/issues/10334 "Docusaurus 3.4.0+ does not respect PostCSS plugins when it minimizes CSS files from 'static' folder". The bullet-proof approach is to leave the static resources intact.

allenscha commented 1 month ago

As mentioned above, JS and CSS files in the static folder are getting minified since Docusaurus 3.4.

In our case, we put API reference files in the static folder, which include JS scripts and CSS styles generated by document generators such as Dokka, Jazzy, and the like. In one particular case, the minified assets are actually causing an issue where display of API reference is broken, which shows up properly in the build with Docusaurs 3.3.2 or lower.

In conclusion, I agree with @soptim-erke and @hrumhurum's comments on leaving the static resources intact.

thomasmattheussen commented 3 weeks ago

After upgrading to latest docusaurus (3.1.1 to 3.5.2) I also experienced this issue. We have a static folder full of pre-built code that is now altered after running docusaurus build (the issue is not there when running with docusaurus start). I noticed that something messed up the CSS in the static folder causing weird issues.

For people also experiencing this: I found that using the workaround (outlined in https://github.com/facebook/docusaurus/issues/10334) by setting USE_SIMPLE_CSS_MINIFIER=true fixes my issue. It's probably not the right fix, as static resources should be left intact and unaltered (as stated in this issue thread already) but it does the job for my use case.