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

@linaria/babel-preset's resolver cannot resolve node builtin modules #1353

Closed
Shinyaigeek opened this issue Sep 26, 2023 · 5 comments · Fixed by #1350
Closed

@linaria/babel-preset's resolver cannot resolve node builtin modules #1353

Shinyaigeek opened this issue Sep 26, 2023 · 5 comments · Fixed by #1350
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked bundler: webpack 📦 Issue is related to webpack bundler needs: complete repro 🖥️ Issue need to have complete repro provided

Comments

@Shinyaigeek
Copy link

Shinyaigeek commented Sep 26, 2023

Environment

  • Linaria version: 5.0.2
  • Bundler (+ version): not using on the environment in which this behavior happens
  • Node.js version: 20
  • OS: macOS

Description

Thank you for developing nice tool!

My service uses linaria. My service is based on react + webpack. I develop react based application, and build(bundle, transpile, and also process linaria stylesheet) it with webpack. I uses linaria/webpack-loader to process linaria stylesheet on building. And also, I take a snapshot testing for the react component styled with linaria, with jest. To make linaria's processor work on jest, I uses @linaria/babel-preset with babel configuration for the testing environment.

With v5.0.2, linaria/webpack-loader works fine so I can build my service correctly, but I cannot test my service because linaria's processor fail with below message.

/~~/mycomponent.jsx: ENOENT: no such file or directory, open 'url'

This is caused in resolving nodejs's builtin url package.

In my application, I prepare some styling tokens for the stylesheet, such as color: black. These styling tokens include icon's url, such as url(~~) and icon's url is built with nodejs's builtin url module.

example)

import { URL } from "url";

export const getIcon = function(icon) {
  const BASE_URL = "https://~~/";
  switch (icon) {
    case "logo": {
       const url = new URL(`${BASE_URL}/{file path}`)
       return `url(${url})`
    }
  }
}

linaria/babel-preset seems to fail in resolving nodejs's builtin url module.

I assume this difference is caused by module resolver's difference. linaria/webpack-loader uses module resolver ( asyncResolver ) based on the webpack's resolver (ref: https://github.com/callstack/linaria/blob/master/packages/webpack5-loader/src/index.ts#L71), but linaria/babel-presets uses module resolver (syncResolver) based on the linaria/utils (ref: https://github.com/callstack/linaria/blob/master/packages/utils/src/asyncResolveFallback.ts#L19)

Reproducible Demo

I create reproduction: https://github.com/Shinyaigeek/linaria-babel-plugin-resolver-bug-report

pnpm i //setup

pnpm preview // webpack-loader works

pnpm test // jest based on babel presets fails
@Shinyaigeek Shinyaigeek added bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided needs: triage 🏷 Issue needs to be checked and prioritized labels Sep 26, 2023
@github-actions github-actions bot added bundler: webpack 📦 Issue is related to webpack bundler and removed needs: triage 🏷 Issue needs to be checked and prioritized labels Sep 26, 2023
@straxico
Copy link

straxico commented Sep 26, 2023

i get this error on my project after update to 5.0.2

- error ./src/Components/Atoms/Button/index.tsx
/home/aaa/Desktop/kutam/node_modules/@linaria/babel-preset/lib/module.js:224
      throw new EvalError(`${e.message} in${this.callstack.join('\n| ')}\n`);
      ^

EvalError: Missing initializer in const declaration in/home/aaa/Desktop/kutam/src/utils/Token.tsx
| /home/aaa/Desktop/kutam/src/Components/Atoms/Button/index.tsx
    at evalFile.next (<anonymous>)
Import trace for requested module:
./src/Components/Atoms/Button/index.tsx

@Anber
Copy link
Collaborator

Anber commented Sep 27, 2023

@straxico, you need to add @babel/preset-typescript to your Linaria config:

babelOptions: {
  presets: [
    [
      '@babel/preset-typescript',
    ],
  ],
}

@Anber
Copy link
Collaborator

Anber commented Sep 27, 2023

Hi @Shinyaigeek!

Thank you for your report! The problem was fixed.

@straxico
Copy link

straxico commented Sep 28, 2023

presets: [
[
'@babel/preset-typescript',
],
],

tanks @Anber .
i use this config as my .bablerc and this is work fine

{
"presets": ["next/babel", "@linaria","@babel/preset-typescript"]
}

@Anber Anber closed this as completed Sep 28, 2023
@Shinyaigeek
Copy link
Author

@Anber v5.0.3 works fine in my application, Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked bundler: webpack 📦 Issue is related to webpack bundler needs: complete repro 🖥️ Issue need to have complete repro provided
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants