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

[gatsby] gatsby develop produces minified source code #19128

Closed
vcarl opened this issue Oct 29, 2019 · 12 comments · Fixed by #19240
Closed

[gatsby] gatsby develop produces minified source code #19128

vcarl opened this issue Oct 29, 2019 · 12 comments · Fixed by #19240
Labels
help wanted Issue with a clear description that the community can help with. type: bug An issue or pull request relating to a bug in Gatsby

Comments

@vcarl
Copy link
Contributor

vcarl commented Oct 29, 2019

Description

The develop mode is producing minified source code, which means I can't add breakpoints

Steps to reproduce

gatsby new test-site
cd test-site
yarn start

Expected result

Code viewed in developer tools should allow breakpoints to be set on most lines.

Actual result

Only a small percentage of lines are selectable.

Environment

  System:
    OS: macOS 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
    Shell: 3.0.2 - /usr/local/bin/fish
  Binaries:
    Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v10.16.3/bin/npm
  Languages:
    Python: 2.7.10 - /usr/bin/python
  Browsers:
    Chrome: 78.0.3904.70
    Firefox: 69.0.3
    Safari: 13.0.2
  npmPackages:
    gatsby: ^2.15.28 => 2.15.28 
    gatsby-background-image: ^0.8.15 => 0.8.15 
    gatsby-image: ^2.2.23 => 2.2.23 
    gatsby-plugin-eslint: ^2.0.5 => 2.0.5 
    gatsby-plugin-mdx: ^1.0.46 => 1.0.46 
    gatsby-plugin-react-helmet: ^3.1.10 => 3.1.10 
    gatsby-plugin-react-svg: ^2.1.1 => 2.1.1 
    gatsby-plugin-sharp: ^2.2.27 => 2.2.27 
    gatsby-plugin-sitemap: ^2.1.0 => 2.1.0 
    gatsby-plugin-styled-components: ^3.1.8 => 3.1.8 
    gatsby-remark-prismjs: ^3.3.16 => 3.3.16 
    gatsby-remark-smartypants: ^2.1.11 => 2.1.11 
    gatsby-source-contentful: ^2.1.44 => 2.1.44 
    gatsby-source-filesystem: ^2.0.32 => 2.0.32 
    gatsby-source-git: ^1.0.2 => 1.0.2 
    gatsby-source-rss-feed: ^1.1.1 => 1.1.1 
    gatsby-transformer-remark: ^2.6.26 => 2.6.26 
    gatsby-transformer-sharp: ^2.2.19 => 2.2.19 
@vcarl
Copy link
Contributor Author

vcarl commented Oct 29, 2019

2.13.x is the last good minor version. Seeing if I can narrow it down to a specific change.

@vcarl
Copy link
Contributor Author

vcarl commented Oct 29, 2019

2.13.30 is the first bad version, 2.13.29 is the last version that produces un-minified source code with gatsby develop.

Repro with 2 copies of npx gatsby new <name>, with gatsby version pinned to the last good and first bad, is here https://github.com/vcarl/gatsby-minified-source-bug-repro

Screenshots of what I mean:
Screen Shot 2019-10-29 at 6 05 24 PM
Screen Shot 2019-10-29 at 6 12 44 PM
Note how many more lines are selectable in the second.

@minervabot
Copy link
Contributor

minervabot commented Oct 29, 2019

This bumped babel-preset-gatsby from 0.2.6 to 0.2.7. There seem to be significant changes, could this be associated? Does gatsby use this package internally, or is it only for custom .babelrc?

https://github.com/gatsbyjs/gatsby/compare/babel-preset-gatsby@0.2.6...babel-preset-gatsby@0.2.7

This new code introduces options.stage, which seems interesting:

// TODO(v3): Remove process.env.GATSBY_BUILD_STAGE, needs to be passed as an option
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`

@pieh
Copy link
Contributor

pieh commented Oct 30, 2019

Gatsby does use babel-preset-gatsby.

Just to verify before I get to check this tomorrow - is code minified/uglified (so variable names are renamed to 1 letter variables) or compacted (new lines are removed)?

I see there is compact option being set to true which does the latter: "All optional newlines and whitespace will be omitted when generating code in compact mode." and this would be my guess - as this was unconditionally set in for user code in #15875

@pieh
Copy link
Contributor

pieh commented Oct 30, 2019

Quick verification could also help - going to node_modules/gatsby/dist/utils/webpack-utils.js and searching for compact: true and setting it to false, and then deleting node_modules/.cache (that's babel/webpack cache), rerunning gatsby develop and seeing if that fixes the problem could help us narrow it down.

For proper fix we would need to conditionally set this only for production builds

@minervabot
Copy link
Contributor

minervabot commented Oct 30, 2019

The code does not appear to actually be minified, some of my longer variable names remain, but just are on one line.

In gatsby 2.17.6 changing to compact: false in the following block of code resolves the no breakpoints issue for me.

node_modules/gatsby/dist/utils/webpack-utils.js

  {
    let js = (_ref = {}) => {
      let {
        modulesThatUseGatsby = []
      } = _ref,
          options = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["modulesThatUseGatsby"]);
      return {
        test: /\.(js|mjs|jsx)$/,
        include: modulePath => {
          // when it's not coming from node_modules we treat it as a source file.
          if (!vendorRegex.test(modulePath)) {
            return true;
          } // If the module uses Gatsby as a dependency
          // we want to treat it as src so we can extract queries


          return modulesThatUseGatsby.some(module => modulePath.includes(module.path));
        },
        type: `javascript/auto`,
        use: [loaders.js(Object.assign({}, options, {
          configFile: true,
          compact: true
        }))]
      };
    };

    rules.js = js;
  }

@vcarl
Copy link
Contributor Author

vcarl commented Oct 30, 2019

Yep, fixes for me as well!

@vcarl vcarl changed the title gatsby develop produces minified source code [gatsby] gatsby develop produces minified source code Oct 30, 2019
@LekoArts LekoArts added help wanted Issue with a clear description that the community can help with. type: bug An issue or pull request relating to a bug in Gatsby labels Nov 1, 2019
@LekoArts
Copy link
Contributor

LekoArts commented Nov 1, 2019

We'd appreciate a PR fixing that! :)

@minervabot
Copy link
Contributor

The workaround discussed above might affect the production builds as well.

@Toolo
Copy link
Contributor

Toolo commented Nov 3, 2019

I have just opened a PR with @pieh 's suggestion to set the compact value conditionally: #19240

pieh pushed a commit that referenced this issue Nov 4, 2019
* fix(webpack-utils): JS is only compacted in production

- This line was preventing debugging while using `gatsby develop`
- Fixes [issue #19128](#19128)

* test(webpack-utils): updates snapshot for default cases
@arualana
Copy link

arualana commented Aug 17, 2020

Quick verification could also help - going to node_modules/gatsby/dist/utils/webpack-utils.js and searching for compact: true and setting it to false, and then deleting node_modules/.cache (that's babel/webpack cache), rerunning gatsby develop and seeing if that fixes the problem could help us narrow it down.

For proper fix we would need to conditionally set this only for production builds

@pieh where is exactly the cache I need to delete? I don't see any .cache right under node_modules

I need the opposite case, to minify the development build

@pieh
Copy link
Contributor

pieh commented Aug 17, 2020

@arualana After #25192 (which translates to gatsby@2.23.8 and newer) the caches are stored in <project_directory>/.cache/webpack. The compact setting should remain in node_modules/gatsby/dist/utils/webpack-utils.js file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with. type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants