question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rollup: Using the "styled" tag in runtime is not supported

See original GitHub issue

Environment

rollup: ^1.2.2 linaria: ^1.3.1

Description

Project is TypeScript + Linaria transpiled via Babel using @babel/preset-typescript. The module bundler I am using is rollup. Despite having included the linaria/babel preset in my babel.config.js file I am still seeing an error in the console: Error: Using the "styled" tag in runtime is not supported. Make sure you have set up the Babel plugin correctly.

Reproducible Demo

/**
 * babel.config.js
 */

module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-typescript',
    '@babel/preset-react',
    'linaria/babel',
  ],
};
/**
 * rollup.config.js
 */

import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import linaria from 'linaria/rollup';
import css from 'rollup-plugin-css-only';
import pkg from './package.json';

const extensions = ['.js', '.jsx', '.ts', '.tsx'];

const name = 'RollupTypeScriptBabel';

export default {
  input: './src/index.ts',

  external: ['react', 'react-dom'],

  plugins: [
    // Allows node_modules resolution
    resolve({ extensions }),

    // Allow bundling cjs modules. Rollup doesn't understand cjs
    commonjs({
      namedExports: {
        '../../node_modules/linaria/react.js': ['styled'],
        '../../node_modules/react-is/index.js': ['isElement', 'isValidElementType', 'ForwardRef'],
      },
    }),

    // Compile TypeScript/JavaScript files
    babel({ extensions, include: ['src/**/*'] }),

    linaria({
      sourceMap: process.env.NODE_ENV !== 'production',
      evaluate: true,
    }),

    css({
      output: 'styles.css',
    }),
  ],

  output: [
    {
      file: pkg.main,
      format: 'cjs',
    },
    {
      file: pkg.module,
      format: 'es',
    },
  ],
};

/**
 * BasicImageViewer.tsx
 */
import { styled } from 'linaria/react';

const BasicImageViewer = styled.img`
  width: 250px;
  height: 250px;
`;

export default BasicImageViewer;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
Glazycommented, Mar 22, 2019

No problem, thanks for your help so far 👊

Whenever I get this resolved, if I think there is space for improvement, I will make a PR to the rollup section of the documentation 👍

3reactions
willmcvaycommented, Jun 10, 2020

@TMaszko So @tanphamhaiduong is on my team and we found a valid workaround - basically the issue is in the tsconfig - if you set your target to es6, it will strip out the Linaria template literals but if you set the target to ESNEXT, it will preserve them and the rollup plugin works fine with the typescript2 rollup plugin.

We are using the TSDX package to build our app, so this looks a bit janky as had to override the existing rollup config but hopefully the comments explain what is going on: https://github.com/reapit/foundations/blob/master/packages/elements/tsdx.config.js

Basically the plugin order we found worked was (pseudo code);

....
commonjs(),
resolve()
typescript(), // with a tsconfig target set to ESNEXT
linaria(), // to strip out the styles
sass(), // to process the css / sass
babel() // with preset env and plugin transform runtime to transpile down to IE 11
...

Hopefully helps!

BTW, really nice work on the library - was looking for ages for a CSS in JS solution that allows export to a static stylesheet as well as React Components. We distribute a UI lib to our users that exports both React Components and for non-react users, just a stylesheet. With the friendly output classes in the classNameSlug option, it’s absolutely the perfect tool for us - thanks for your work 💯

Read more comments on GitHub >

github_iconTop Results From Across the Web

styled-jsx is not enabled in rollup's project, it seems that babel ...
and I find that style tag is still in html, it seems that rollup don't load the babel config? Is there an error...
Read more >
Styled-jsx-for-rollup - npm.io
Full, scoped and component-friendly CSS support for JSX (rendered on the server or the client). Code and docs are for v3 which we...
Read more >
rollup.js
Rollup allows you to write your code using the new module system, and will then compile it back down to existing supported formats...
Read more >
Linaria - Best of JS
Linaria currently supports webpack and Rollup to extract the CSS at build time. To configure your bundler ... Dynamic styles are not supported...
Read more >
Releases - styled-components
breaking removed runtime prop validation functionality; use transient props for ... Support css tagged templates inside style objects, by @roginfarrer and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found