linaria icon indicating copy to clipboard operation
linaria copied to clipboard

[1.4.0-beta.0] A build error

Open Anber opened this issue 6 years ago • 22 comments

This problem was reported by @Guria (#495)

Environment

Description

node_modules/react-scripts/node_modules/@babel/runtime/helpers/esm/taggedTemplateLiteral.js:1
export default function _taggedTemplateLiteral(strings, raw) {
^^^^^^

SyntaxError: Unexpected token 'export'

Reproducible Demo

Anber avatar Dec 31 '19 09:12 Anber

@Guria could you please provide some details?

Anber avatar Dec 31 '19 09:12 Anber

Will try to dig in and provide reproduction

Guria avatar Dec 31 '19 10:12 Guria

After upgrading to 1.4.0-beta.0, seeing a the same (or at least, very similar) error message:

Module build failed (from ./node_modules/linaria/loader.js):
...\node_modules\react-dnd\dist\esm\index.js:1
export * from './common';

SyntaxError: Unexpected token export
    at new Script (vm.js:83:7)
    at Module.evaluate (...\node_modules\linaria\lib\babel\module.js:200:18)
    at Module.<anonymous> (...\node_modules\linaria\lib\babel\module.js:184:13)
    at Module.require (...\node_modules\linaria\lib\babel\module.js:134:23)
    at ...\lib\components\Component2.js:32:17
    at Script.runInContext (vm.js:133:20)
    at Module.evaluate (...\node_modules\linaria\lib\babel\module.js:203:12)
    at Module.<anonymous> (...\node_modules\linaria\lib\babel\module.js:184:13)
    at Module.require (...\node_modules\linaria\lib\babel\module.js:134:23)
    at...\lib\components\Component1.js:3:22
 @ ../lib/components/MainView.js
 @ ../lib/index.js
 @ ./src/App.js
 @ ./src/index.js

Not sure if @Guria is also seeing it from linaria loader? Seems to me that for some reason, babel is somehow getting confused when getting run through linaria, as both errors seem to be triggered by unrelated modules having ES6 code being included but not getting transpiled by babel?

filenames anonymized a bit; but the use case is basically nested, styled components (eg. const Component1 = styled.div'..'; const Component1 = styled(Component2)'..'; (and it works a-ok without that nesting) so seems like a possible regression from #343? Will try to get a better reproduction going as, well.

matiasbuildops avatar Jan 02 '20 14:01 matiasbuildops

@Guria @matiasbuildops hi guys. Any news here?

Anber avatar Jan 18 '20 09:01 Anber

@anber clean cra + linaria has no such problem. I am still wondering what is needed to add to reproduce this problem But I still have this problem on beta

Guria avatar Jan 21 '20 10:01 Guria

@matiasbuildops could you try to describe your setup. Are you using yarn workspaces or something similar?

Guria avatar Jan 21 '20 10:01 Guria

Hi @Guria!

I was not using yarn workspaces when I hit that but after investigating another issue relating to them (see #529), I think I might know what is causing at least my report - not sure if your error was raised in loader, as well? As these both seem to be somehow trying to import non-babelized files, I'm guessing both of these originate from the call to babel in transform.ts that only passes in linaria/loader as the presets and not what the user had defined in their babelrc or anything like that.

However, I'm really not at all sure why or when transform() actually gets called on files or whether this is/was a side-effect of other configuration so can't do more than a guess for now. Will try to investigate further when I have some time.

I can add one more datapoint, though. In my case, the problematic use was one module importing another module that was importing linaria. eg; all the problems happened when the code was running inside node_modules which is why I feel it could just be a side-effect of babel ignoring node_modules at some point above linaria? Not sure if that makes sense either but hey..

matiasbuildops avatar Jan 21 '20 12:01 matiasbuildops

@matiasbuildops Hi. I have applied your patch and seems it works for me:

diff --git a/node_modules/linaria/lib/loader.js b/node_modules/linaria/lib/loader.js
index b6be4cf..44eaba0 100644
--- a/node_modules/linaria/lib/loader.js
+++ b/node_modules/linaria/lib/loader.js
@@ -68,7 +68,7 @@ function loader(content, inputSourceMap) {
     };
 
     result = (0, _transform.default)(content, {
-      filename: this.resourcePath,
+      filename: _path.default.relative(process.cwd(), this.resourcePath),
       inputSourceMap: inputSourceMap != null ? inputSourceMap : undefined,
       outputFilename: outputFilename,
       pluginOptions: rest,

Guria avatar Feb 03 '20 15:02 Guria

@Anber could we release beta.1 with above patch?

Guria avatar Feb 03 '20 15:02 Guria

@Guria probably, yes. I'll check regressions and build the next beta tomorrow.

Anber avatar Feb 03 '20 18:02 Anber

it's weird. this patch works for me on local machine, but doesn't work on CI where it applied with patch-package

Guria avatar Feb 04 '20 06:02 Guria

@Guria beta.2 has been released. Could you please check it?

Anber avatar Feb 09 '20 15:02 Anber

Didn't worked unfortunately :( image

Will try to investigate further

Guria avatar Feb 09 '20 19:02 Guria

ok, I digged in a bit now. So it seems babel.transformFromAstSync fails to parse with following error for some of style files:

node_modules/@babel/runtime/helpers/esm/taggedTemplateLiteral.js:1
(function (exports) { export default function _taggedTemplateLiteral(strings, raw) {
                      ^^^^^^

SyntaxError: Unexpected token 'export'
    at new Script (vm.js:88:7)
    at Module.evaluate (./node_modules/linaria/lib/babel/module.js:264:20)
    at Module.Object.assign.ensure (./node_modules/linaria/lib/babel/module.js:177:15)
    at ./web/desktop/VisitCardMultiService/styles.ts:10:54
    at ./web/desktop/VisitCardMultiService/styles.ts:434:3
    at Script.runInContext (vm.js:131:20)
    at Module.evaluate (./node_modules/linaria/lib/babel/module.js:267:12)
    at Module.Object.assign.ensure (./node_modules/linaria/lib/babel/module.js:177:15)
    at ./web/desktop/VisitCardMultiService/ServiceRow/styles.ts:11:19
    at ./web/desktop/VisitCardMultiService/ServiceRow/styles.ts:20:3 {
  code: 'BABEL_TRANSFORM_ERROR'
}

I've checked the source of this files and common part for them is that they have import from another style.tsx file.

Guria avatar Feb 11 '20 21:02 Guria

The only working patch I were succeded for now is replacing export default with module.exports = in node_modules/@babel/runtime/helpers/esm/taggedTemplateLiteral.js. But I fill that isn't an appropriate solution :)

Guria avatar Feb 11 '20 21:02 Guria

@Guria Could you please try that workaround https://github.com/callstack/linaria/issues/540#issuecomment-585228851?

Anber avatar Feb 16 '20 10:02 Anber

I also have this problem. I have configured the following information in linaria.config.js to solve this problem. It seems that Linaria does not convert the content in node_modules by default. You need to specify it manually.

module.exports = {
	ignore: /node_modules[\/\\](?!some-module|other-module)/
}

You can get more information here https://github.com/callstack/linaria/blob/master/docs/CONFIGURATION.md#options

skywalker512 avatar Mar 28 '20 11:03 skywalker512

I will try to reproduce it and see if it can be solved.

jayu avatar Apr 01 '20 10:04 jayu

Fixed

Anber avatar Apr 05 '20 07:04 Anber

just tried linaria@^2.0.0-alpha.5 and it builds without errors,

Guria avatar May 27 '20 06:05 Guria