linaria icon indicating copy to clipboard operation
linaria copied to clipboard

@linaria/esbuild bundler shows error "TypeError: linaria is not a function"

Open dave-stevens-net opened this issue 3 years ago • 3 comments

Environment

  • Linaria version: 3.0.0-beta.15
  • Bundler (+ version): @linaria/esbuild 3.0.0-beta.15 with esbuild v0.14.11
  • Node.js version: v16.13.1
  • OS: POP-OS (Ubuntu)

Description

I followed your instructions on https://github.com/callstack/linaria/blob/master/docs/BUNDLERS_INTEGRATION.md#esbuild to add the @linaria/esbuild plugin to esbuild. However, when I add the import as follows it returns an object (containing the default property) instead of the plugin function itself.

// My esbuild.mjs module
import {build} from 'esbuild'
import linaria from '@linaria/esbuild'

  build({
    ...
    plugins: [linaria()],
  }).then(() => {

The above logic produces the error, TypeError: linaria is not a function. I can get around this issue by coding the plugins as follows:

import linaria from '@linaria/esbuild'

  build({
    ...
    plugins: [linaria.default()],
  }).then(() => {

dave-stevens-net avatar Jan 14 '22 01:01 dave-stevens-net

Hi @dave-stevens-net Looks like @linaria/esbuild in your project is resolved to lib-version instead of esm. Could you please provide a repo that reproduces that behaviour?

Anber avatar Feb 06 '22 16:02 Anber

Node simply wont import the esm file:

import linaria from '@linaria/esbuild/esm/index.js';
> To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

Changing the @linaria/esbuild/esm/index.js file extension to .mjs allows me to import the ESM module. In order for Node to choose a specific module system based on how the module is imported, some changes to the package.json is required: https://nodejs.org/api/packages.html#dual-commonjses-module-packages

Here is a minimal repo that reproduces the issue: https://github.com/dsod/linaria-esbuild-import-issue

dsod avatar Feb 14 '22 23:02 dsod

The change required is simply replacing this part of the package.json file

  "main": "lib/index.js",
  "module": "esm/index.js",

with this:

  "type": "module",
  "exports": {
    "import": "./esm/index.js",
    "require": "./lib/index.js"
  },

was tested locally and it works well

iMoses avatar Sep 03 '22 01:09 iMoses

Fixed and released. Thank you @iMoses and @dsod!

Anber avatar Oct 19 '22 19:10 Anber