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

fix: handle .cjs & .mjs extensions #1181

Merged
merged 2 commits into from
Jan 13, 2023

Conversation

layershifter
Copy link
Contributor

Motivation

Fixes #1177.

Summary

This PR adds .cjs & .mjs to the list of default ones, so Linaria will handle them and load them properly.

Before

  linaria:module:00001 [eval-cache] ❌ module:00002 is going to be initialized +0ms
  linaria:module:00002 [init] /private/var/folders/v4/l_41mr81261dpdvcdldkgdbh0000gn/T/nextjs-98284-78vGr91L2kUL/node_modules/@griffel/react/colors.cjs +1s
  linaria:module:00002 [evaluated] the whole exports was overridden with '/private/var/folders/v4/l_41mr81261dpdvcdldkgdbh0000gn/T/nextjs-98284-78vGr91L2kUL/node_modules/@griffel/react/colors.cjs' +0ms

After

  linaria:module:00001 [require] @griffel/react/colors.cjs -> /private/var/folders/v4/l_41mr81261dpdvcdldkgdbh0000gn/T/nextjs-98284-78vGr91L2kUL/node_modules/@griffel/react/colors.cjs +0ms
  linaria:module:00001 [eval-cache] ❌ module:00002 is going to be initialized +0ms
  linaria:module:00002 [init] /private/var/folders/v4/l_41mr81261dpdvcdldkgdbh0000gn/T/nextjs-98284-78vGr91L2kUL/node_modules/@griffel/react/colors.cjs +1s
  linaria:module:00002 [code-cache] ✅ +0ms
  linaria:module:00002 [evaluate:fragment-01] 
  linaria:module:00002 	module.exports = {
  linaria:module:00002 	  red: 'pink',
  linaria:module:00002 	} +1ms
  linaria:module:00002 [evaluated] the whole exports was overridden with { red: 'pink' } +0ms

Test plan

I didn't find a proper place to test it, it seems that module.test.ts is nearest, so I modified it.

@changeset-bot
Copy link

changeset-bot bot commented Jan 12, 2023

🦋 Changeset detected

Latest commit: 95f2598

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 15 packages
Name Type
@linaria/babel-preset Patch
@linaria/testkit Patch
@linaria/cli Patch
@linaria/esbuild Patch
linaria Patch
@linaria/rollup Patch
@linaria/stylelint Patch
@linaria/vite Patch
@linaria/webpack4-loader Patch
@linaria/webpack5-loader Patch
esbuild-example Patch
rollup-example Patch
vite-example Patch
vpssr-linaria-solid-example Patch
@linaria/webpack-loader Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines -27 to -35
rules: [
{
action: evaluator,
},
{
test: /\/node_modules\//,
action: 'ignore',
},
],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rules are not used in Module, so I removed it and the evaluator() as it caused a false expectation that a required file will be evaluated.

@layershifter
Copy link
Contributor Author

Hm... Getting an interesting failure now:

rollup-example:build: [!] (plugin linaria) SyntaxError: /Users/olfedias/WebstormProjects/linaria/packages/atomic/dist/index.mjs: Invalid usage of `styled` tag
rollup-example:build:   13 |   css_default as css,
rollup-example:build:   14 |   cx,
rollup-example:build: > 15 |   styled
rollup-example:build:      |   ^^^^^^
rollup-example:build:   16 | };
rollup-example:build:   17 | //# sourceMappingURL=index.mjs.map
rollup-example:build: ../../packages/atomic/dist/index.mjs

In code:

import { styled } from "@linaria/react";
import { cx } from "@linaria/core";
export {
  css_default as css,
  cx,
  styled
};

It seems that styled identifier triggers the problem, will look into it.

@layershifter layershifter force-pushed the fix/add-new-extensions branch from 01734f9 to 95f2598 Compare January 12, 2023 14:05
@layershifter
Copy link
Contributor Author

Naive fix for the problem is to add a check to packages/babel/src/utils/processTemplateExpression.ts:

const processTemplateExpression = (
  p: NodePath<Identifier>,
  fileContext: IFileContext,
  options: Pick<
    StrictOptions,
    'classNameSlug' | 'displayName' | 'evaluate' | 'tagResolver'
  >,
  emit: (processor: BaseProcessor) => void
) => {
+ if (p.parentPath.isExportSpecifier()) return;
  if (processed.has(p.node)) return;

  const tagProcessor = getTagProcessor(p, fileContext, options);

I don't see any case when ImportSpecifier could be used in any way.

@Anber Anber merged commit 2d3a741 into callstack:master Jan 13, 2023
@layershifter layershifter deleted the fix/add-new-extensions branch January 13, 2023 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

module evaluates ESM helpers from SWC output
2 participants