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

[plugin-postcss] v2 postcss plugin causes build (but not develop) to fail #6880

Closed
madeleineostoja opened this issue Jul 31, 2018 · 10 comments
Closed
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved

Comments

@madeleineostoja
Copy link

madeleineostoja commented Jul 31, 2018

Description

Including the new gatsby-plugin-postcss plugin in a v2 site is causing prod build to fail, but dev mode works fine.

  28 | // Run the specified API in any plugins that have implemented it
  29 | module.exports = (api, args, defaultReturn) => {
> 30 |   if (!apis[api]) {
     |        ^
  31 |     console.log(`This API doesn't exist`, api)
  32 |   }
  33 |

  WebpackError: ./src/components/.../styles.module.css

 - api-runner-ssr.js:30 childCompiler.runAsChild
    lib/.cache/api-runner-ssr.js:30:8

  ...

Steps to reproduce

Include gatsby-plugin-postcss and try to build site.

Expected result

Should work the same as develop build.

Actual result

Production build breaks when compiling HTML.

Environment

System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Shell: 5.5.1 - /usr/local/bin/zsh
  Binaries:
    Node: 9.0.0 - /usr/local/bin/node
    npm: 6.2.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 67.0.3396.99
    Firefox: 60.0.1
    Safari: 11.1.2
  npmPackages:
    gatsby: next => 2.0.0-beta.61
    gatsby-image: next => 2.0.0-beta.7
    gatsby-plugin-manifest: next => 2.0.2-beta.3
    gatsby-plugin-netlify: next => 2.0.0-beta.4
    gatsby-plugin-nprogress: next => 2.0.0-beta.4
    gatsby-plugin-offline: next => 2.0.0-beta.5
    gatsby-plugin-postcss: ^1.0.0 => 1.0.0
    gatsby-plugin-react-css-modules: next => 2.0.0-beta.4
    gatsby-plugin-react-helmet: next => 3.0.0-beta.4
    gatsby-plugin-segment: latest => 1.0.2
    gatsby-plugin-sharp: next => 2.0.0-beta.7
    gatsby-plugin-sitemap: next => 2.0.0-beta.3
    gatsby-plugin-svgr: github:coreyward/gatsby-plugin-svgr#gatsby2-compatibility => 1.0.0
    gatsby-source-prismic: next => 2.0.0-alpha.2
    gatsby-transformer-sharp: next => 2.1.1-beta.6
  npmGlobalPackages:
    gatsby-cli: 1.1.58
    gatsby-plugin-react-css-modules: 2.0.0-beta.1
@madeleineostoja madeleineostoja changed the title [plugin-postcss] v2 postcss plugin causes build to fail [plugin-postcss] v2 postcss plugin causes build (but not develop) to fail Aug 2, 2018
@Chuloo
Copy link
Contributor

Chuloo commented Aug 2, 2018

Hi @seaneking, following your steps, I couldn't reproduce your error. Here's a simple reproduction code with the hello-world starter and using gatsby-plugin-postcss.
Upgrade to the latest Gatsby version and see if it persists. 👍

@Chuloo Chuloo added the status: needs more info Needs triaging and reproducible examples or more information to be resolved label Aug 2, 2018
@madeleineostoja
Copy link
Author

Hmm okay, any idea how I could go about debugging this? I'm not familiar enough with Gatsby's internals to know what could be causing Postcss to throw at that API (whatever it is, something to do with the cache-dir package) on build. Updating to latest deps doesn't fix it.

@KyleAMathews
Copy link
Contributor

@seaneking could you share with us a link to a github repo that shows this error? That'd be the easiest way for us to investigate the problem.

@madeleineostoja
Copy link
Author

madeleineostoja commented Aug 3, 2018

@KyleAMathews sure thing, WIP migration here: https://github.com/littleplanetcreative/littleplanetcreative.com/tree/gatsby-v2

Trying to narrow it down but not getting far – removed plugin-react-css-modules and anything else to do with CSS, still erroring in prod builds.

@andrewn
Copy link

andrewn commented Aug 3, 2018

@KyleAMathews, @Chuloo, I'm also seeing this issue. Here's an minimal gatsby project showing this issue (^2.0.0-beta.67) with gatsby-plugin-postcss.

npm run develop loads the index page with the big "Heading" styles from index.module.css as expected.

npm run build fails with the error:

WebpackError: ./src/components/Heading/index.module.css

The full terminal output is here.

Hope that helps and let me know if there's anything I can do to help get this fixed.

@youngboy
Copy link
Contributor

youngboy commented Aug 3, 2018

I was trying to lookup the problem ...

only thing I dig so far is that webpack error(it is swallow by createErrorFromString ...)

Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
TypeError: this[NS] is not a function
    at childCompiler.runAsChild (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/mini-css-extract-plugin/dist/loader.js:148:15)
    at compile (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/webpack/lib/Compiler.js:296:11)
    at hooks.afterCompile.callAsync.err (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/webpack/lib/Compiler.js:553:14)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:24:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/Hook.js:35:21)
    at compilation.seal.err (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/webpack/lib/Compiler.js:550:30)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeAssets.callAsync.err (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/webpack/lib/Compilation.js:1284:35)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeChunkAssets.callAsync.err (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/webpack/lib/Compilation.js:1275:32)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.additionalAssets.callAsync.err (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/webpack/lib/Compilation.js:1270:36)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/yanghaku/github/gatsby-postcss-fail/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
 @ ./src/components/Heading/index.js 2:0-40 5:13-19
 @ ./src/pages/index.js
 @ ./.cache/sync-requires.js
 @ ./.cache/static-entry.js

after little googling, I think the bug might need to wait mini-css-extract-plugin or html-webpack-plugin to fix ..

@youngboy
Copy link
Contributor

youngboy commented Aug 3, 2018

Off topic, I think the webpack error isn't much useful now, also see #6978

gatsby-cli/lib/reporter/errors.js's function createErrorFromString in is not worked as expect currently

according to its code

/**
 * Convert a stringified webpack compilation error back into
 * an Error instance so it can be formatted properly
 * @param {string} errorStr
 */
function createErrorFromString(errorStr = ``, sourceMapFile) {
  let _errorStr$split = errorStr.split(/\r\n|[\n\r]/g),
      message = _errorStr$split[0],
      rest = _errorStr$split.slice(1); // pull the message from the first line then remove the `Error:` prefix
  // FIXME: when https://github.com/AriaMinaei/pretty-error/pull/49 is merged

  message = message.replace(/^(Error:)/, ``);
  let error = new Error(message);
  error.stack = [message, rest.join(`\n`)].join(`\n`);
  error.name = `WebpackError`;
  // ...

  return error;
}

the PR url mentioned in fixme is already merged AriaMinaei/pretty-error#49

so, upgrade pretty-error to 2.2.0-rc.1, then change this function to

function createErrorFromString(errorStr = ``, sourceMapFile) {

  const pe = new PrettyError();
  const error = pe.render(new Error(errorStr));
  try {
    if (sourceMapFile) prepareStackTrace(error, sourceMapFile);
  } catch (err) {// don't shadow a real error because of a parsing issue
  }

  return error;
}

doing the same job, while giving more information here

@youngboy
Copy link
Contributor

youngboy commented Aug 4, 2018

@seaneking @andrewn I spent so much time to debug this ... turn out your gatsby-plugin-postcss plugin version is still in v1, and I am keep looking at v2's source code....... 😂😂😂😂😂

run following command and your error goes away

yarn upgrade gatsby-plugin-postcss@2.0.0-beta.2

the real reason behind this is that v1's gatsby-plugin-postcss include mini-css-extract-plugin in server side rendering mode, and its requiring add MiniCssExtractPlugin to webpack plugin ...

@andrewn
Copy link

andrewn commented Aug 4, 2018

🤦‍♂️ 🤦‍♂️ 🤦‍♂️

I'm so sorry about this @youngboy. You're completely right. Upgrading to gatsby-plugin-postcss@2.0.0-beta.2 works perfectly.

Thanks so much for figuring this out!

@KyleAMathews
Copy link
Contributor

Thanks everyone for working together on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved
Projects
None yet
Development

No branches or pull requests

5 participants