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

Sass in browser not workig #2113

Closed
nbsharath opened this issue Aug 20, 2023 · 9 comments · Fixed by #2114
Closed

Sass in browser not workig #2113

nbsharath opened this issue Aug 20, 2023 · 9 comments · Fixed by #2114
Assignees
Labels

Comments

@nbsharath
Copy link

I am trying to use sass in the browser through webpack bundle.

import * as sass from "sass";
const compiledCSS = await sass.compileStringAsync(value);

But its giving this error

error TypeError: J.getInterceptor$x(...) is undefined
    get$isTTY$x sass.dart.js:28035
    hasTerminal0 sass.dart.js:22668
    compileStringAsync1 sass.dart.js:19377
    installTearOffs sass.dart.js:112321
    Primitives_applyFunction sass.dart.js:1143
    Function_apply sass.dart.js:5375
    _callDartFunctionFast sass.dart.js:7244
    ret sass.dart.js:7222
    logic scss-to-css-converter.ts:5
    onClickHandle index.tsx:54
    callCallback react-dom.development.js:19443
    invokeGuardedCallbackImpl react-dom.development.js:19492
    invokeGuardedCallback react-dom.development.js:19567
    invokeGuardedCallbackAndCatchFirstError react-dom.development.js:19581
    executeDispatch react-dom.development.js:30635
    processDispatchQueueItemsInOrder react-dom.development.js:30667
    processDispatchQueue react-dom.development.js:30680
    dispatchEventsForPlugins react-dom.development.js:30691
    dispatchEventForPluginEventSystem react-dom.development.js:30881
    batchedUpdates$1 react-dom.development.js:23761
    batchedUpdates react-dom.development.js:27597
    dispatchEventForPluginEventSystem react-dom.development.js:30880
    dispatchEvent react-dom.development.js:28653
    dispatchDiscreteEvent react-dom.development.js:28624

I have webpack.

Browser:

firefox

OS:

windows

Version

^1.66.1

@Goodwine
Copy link
Member

Could you provide a minimal repro? That would be either a github repo that we can clone and test against, or a set of npm install ... commands and a repro script.

I imagine that given the error you are seeing, browsers and OSs don't make a difference and get the same error, right?

@nbsharath
Copy link
Author

here is the example repo. https://github.com/nbsharath/example-scss
Just to explain more. Its working fine if i directly use it in a component. The issue happens only when i use useEffect to make it work in browser. (I have other imports which are working fine)

@nex3
Copy link
Contributor

nex3 commented Aug 22, 2023

This reproduction has 29 dependencies. Can you provide a minimal reproduction that includes as few dependencies as possible to reproduce the issue? Ideally without frameworks like React.

@nbsharath
Copy link
Author

Sorry. I have update the the repo

@jgerigmeyer
Copy link
Contributor

jgerigmeyer commented Aug 23, 2023

Next.js sets a global process variable, which causes an error when Sass tries to check for process.stdout.isTTY here. In the example repo, adding a single line before the call to compileString fixes it:

globalThis.process = null;

I suppose a quick fix would be to more defensively check for process?.stdout?.isTTY or process?.stdout && process.stdout.isTTY == true or something. It's too fragile to rely on browser contexts to never have process defined, I guess.

HT to @jamesnw for help debugging this!

@Goodwine Goodwine added bug and removed needs info labels Aug 23, 2023
Front-line-dev referenced this issue in Front-line-dev/markup-educator Aug 27, 2023
브라우저 내 sass 컴파일러를 적용하려고 했으나 nextjs의 구조 때문에 힘든 상황. 일단 package의 sass 버전만 브라우저 컴파일이 가능한 버전으로 업데이트

https://github.com/sass/sass/issues/3662
@nbsharath
Copy link
Author

@jgerigmeyer Thanks 👍 Will try it.

@nex3
Copy link
Contributor

nex3 commented Sep 5, 2023

I think it might be worth adding an isNodeJS getter to package:cli_pkg/js.dart to encapsulate the best-practice way of distinguishing Node.js from other contexts, and just using that in Dart Sass.

@Goodwine
Copy link
Member

Finally having some time to look at this. The "robust" way to check whether a script runs on node seems to be ({}).toString.call(X) where X can be either globalThis or process. But I don't think that this will actually fix this bug because this bug isn't failing for incorrectly identifying whether it's running in Node.js. The bug happens because we assume process only exists in Node.js contexts. That said.. we can definitely move both to dart_cli_pkg.

Either way, I think being more defensive with optional chain operators is a good idea, and also setting the process variable only when isNode is true

@Goodwine
Copy link
Member

Goodwine commented Oct 9, 2023

I'm gonna transfer this issue to sass/dart-sass since that's where the problem is, I'll have a PR ready in a few minutes

@Goodwine Goodwine transferred this issue from sass/sass Oct 9, 2023
Goodwine added a commit that referenced this issue Oct 10, 2023
* Fix crash in browser when running alongside NextJS

Fixes #2113

* update sass_api pubspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants