最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

SWC with JavaScript: How to handle CSS imports and how to absolute imports? - Stack Overflow

programmeradmin1浏览0评论

TL;DR

  • How can you tell SWC to pile CSS files imported in React ponents?
  • How can you tell SWC to pile absolute imports in tests and in React ponents?

Here is a minimal reproducible example.


Context

We're migrating from Babel to SWC. (I asked a question a little while ago. I'm improving on that question's answer.)

We're migrated the mand from:

"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js' | tap-nirvana",

to

"test": "SWC_NODE_PROJECT=./jsconfig.json riteway -r @swc-node/register src/**/*.test.js | tap-nirvana",

where the jsconfig.json looks like this:

{
  "pilerOptions": {
    "allowJs": true,
    "baseUrl": "./src",
    "jsx": "react-jsx"
  }
}

If we write try to pile a test for a self-contained ponent (no absolute imports, no CSS) it works:

import { describe } from 'riteway';
import render from 'riteway/render-ponent';

function HomePageComponent({ user: { email } }) {
  return <p>{email}</p>;
}

describe('home page ponent', async assert => {
  const user = { email: 'foo' };
  const $ = render(<HomePageComponent user={user} />);

  assert({
    given: 'a user',
    should: 'render its email',
    actual: $('p').text(),
    expected: user.email,
  });
});

The test piles fine.

With Babel we had a .babelrc like this:

{
  "env": {
    "test": {
      "plugins": [
        [
          "module-resolver",
          {
            "root": [
              "."
            ],
            "alias": {
              "ponents": "./src/ponents",
              "config": "./src/config",
              "features": "./src/features",
              "hocs": "./src/hocs",
              "hooks": "./src/hooks",
              "pages": "./src/pages",
              "redux": "./src/redux",
              "styles": "./src/styles",
              "tests": "./src/tests",
              "utils": "./src/utils"
            }
          }
        ]
      ]
    }
  },
  "presets": [
    [
      "next/babel",
      {
        "ramda": {}
      }
    ]
  ],
  "plugins": [
    ["styled-ponents", { "ssr": true }]
  ]
}

Where the styles where taken care of by styled-ponents and the absolute imports where defined via the module-resolver plugin. (We switched away from styled-ponents to CSS modules, which is why we import from .module.css CSS files. Anyways ...)

If we write the test how we wanted to write it with their actual imports like this:

import { describe } from 'riteway';
import render from 'riteway/render-ponent';
import { createPopulatedUserProfile } from 'user-profile/user-profile-factories';

import HomePageComponent from './home-page-ponent';

describe('home page ponent', async assert => {
  const user = createPopulatedUserProfile();
  const $ = render(<HomePageComponent user={user} />);

  assert({
    given: 'a user',
    should: 'render its email',
    actual: $('p').text(),
    expected: user.email,
  });
});

It fails with:

$ SWC_NODE_PROJECT=./jsconfig.json riteway -r @swc-node/register src/features/home/home-page-ponent.test.js | tap-nirvana
/Users/janhesters/dev/my-project/src/features/home/home.module.css:1
(function (exports, require, module, __filename, __dirname) { .container {
                                                              ^

SyntaxError: Unexpected token '.'

when we leave in the CSS import in home-page-ponent.js, or with:

$ SWC_NODE_PROJECT=./jsconfig.json riteway -r @swc-node/register src/features/home/home-page-ponent.test.js | tap-nirvana
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'user-profile/user-profile-factories'
Require stack:
- /Users/janhesters/dev/my-project/src/features/home/home-page-ponent.test.js
- /Users/janhesters/dev/my-project/node_modules/riteway/bin/riteway

respectively, when we get rid of the CSS import.

How can we help SWC understand CSS (or mock CSS modules) and how can we help it understand absolute imports?

We already set the baseUrl in jsconfig.json ...

TL;DR

  • How can you tell SWC to pile CSS files imported in React ponents?
  • How can you tell SWC to pile absolute imports in tests and in React ponents?

Here is a minimal reproducible example.


Context

We're migrating from Babel to SWC. (I asked a question a little while ago. I'm improving on that question's answer.)

We're migrated the mand from:

"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js' | tap-nirvana",

to

"test": "SWC_NODE_PROJECT=./jsconfig.json riteway -r @swc-node/register src/**/*.test.js | tap-nirvana",

where the jsconfig.json looks like this:

{
  "pilerOptions": {
    "allowJs": true,
    "baseUrl": "./src",
    "jsx": "react-jsx"
  }
}

If we write try to pile a test for a self-contained ponent (no absolute imports, no CSS) it works:

import { describe } from 'riteway';
import render from 'riteway/render-ponent';

function HomePageComponent({ user: { email } }) {
  return <p>{email}</p>;
}

describe('home page ponent', async assert => {
  const user = { email: 'foo' };
  const $ = render(<HomePageComponent user={user} />);

  assert({
    given: 'a user',
    should: 'render its email',
    actual: $('p').text(),
    expected: user.email,
  });
});

The test piles fine.

With Babel we had a .babelrc like this:

{
  "env": {
    "test": {
      "plugins": [
        [
          "module-resolver",
          {
            "root": [
              "."
            ],
            "alias": {
              "ponents": "./src/ponents",
              "config": "./src/config",
              "features": "./src/features",
              "hocs": "./src/hocs",
              "hooks": "./src/hooks",
              "pages": "./src/pages",
              "redux": "./src/redux",
              "styles": "./src/styles",
              "tests": "./src/tests",
              "utils": "./src/utils"
            }
          }
        ]
      ]
    }
  },
  "presets": [
    [
      "next/babel",
      {
        "ramda": {}
      }
    ]
  ],
  "plugins": [
    ["styled-ponents", { "ssr": true }]
  ]
}

Where the styles where taken care of by styled-ponents and the absolute imports where defined via the module-resolver plugin. (We switched away from styled-ponents to CSS modules, which is why we import from .module.css CSS files. Anyways ...)

If we write the test how we wanted to write it with their actual imports like this:

import { describe } from 'riteway';
import render from 'riteway/render-ponent';
import { createPopulatedUserProfile } from 'user-profile/user-profile-factories';

import HomePageComponent from './home-page-ponent';

describe('home page ponent', async assert => {
  const user = createPopulatedUserProfile();
  const $ = render(<HomePageComponent user={user} />);

  assert({
    given: 'a user',
    should: 'render its email',
    actual: $('p').text(),
    expected: user.email,
  });
});

It fails with:

$ SWC_NODE_PROJECT=./jsconfig.json riteway -r @swc-node/register src/features/home/home-page-ponent.test.js | tap-nirvana
/Users/janhesters/dev/my-project/src/features/home/home.module.css:1
(function (exports, require, module, __filename, __dirname) { .container {
                                                              ^

SyntaxError: Unexpected token '.'

when we leave in the CSS import in home-page-ponent.js, or with:

$ SWC_NODE_PROJECT=./jsconfig.json riteway -r @swc-node/register src/features/home/home-page-ponent.test.js | tap-nirvana
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'user-profile/user-profile-factories'
Require stack:
- /Users/janhesters/dev/my-project/src/features/home/home-page-ponent.test.js
- /Users/janhesters/dev/my-project/node_modules/riteway/bin/riteway

respectively, when we get rid of the CSS import.

How can we help SWC understand CSS (or mock CSS modules) and how can we help it understand absolute imports?

We already set the baseUrl in jsconfig.json ...

Share Improve this question edited Feb 2, 2022 at 23:40 AmerllicA 32.7k17 gold badges144 silver badges167 bronze badges asked Jan 28, 2022 at 19:58 J. HestersJ. Hesters 14.9k34 gold badges155 silver badges267 bronze badges 7
  • Hi, I would like to help you. If it's possible, can you create a github project for that? minimal one.. – Alon Shmiel Commented Jan 28, 2022 at 20:04
  • 1 @AlonShmiel Thank you sooo much! I created it here: github./janhesters/riteway-swc-example – J. Hesters Commented Jan 29, 2022 at 10:39
  • I will check it and let you know – Alon Shmiel Commented Jan 29, 2022 at 14:26
  • Hi, I didn't succeed to solve your issue. I tried to change jsconfig to: { "pilerOptions": { "allowJs": true, "baseUrl": ".", "jsx": "react-jsx", // "target": "esnext", // "module": "monjs", "paths": { "@utils/*": ["./src/utils/*"] } }, "include": ["./src/utils/**/*"] } with no success. Can you please try it and then run: npm run test:absolute-import (In addition, I added type: module in package.json) – Alon Shmiel Commented Jan 29, 2022 at 23:15
  • Hi @AlonShmiel, I tried it and it didn't work neither. Thanks for trying to help!
发布评论

评论列表(0)

  1. 暂无评论