Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

doesn't work with Typescript #22

Closed
AntonPilyak opened this issue May 30, 2019 · 4 comments
Closed

doesn't work with Typescript #22

AntonPilyak opened this issue May 30, 2019 · 4 comments

Comments

@AntonPilyak
Copy link

Works fine with plain JavaScript, but doesn't work with Typescript. Here is the repository for reproducing: https://github.com/AntonPilyak/rollup-image-bug

@Ashot-KR
Copy link

Ashot-KR commented May 30, 2019

Error comes from typescript, not from url plugin.
I don't have much experience in typescript, but it looks like typescript compiler doesn't like importing images.

But you can use babel with @babel/preset-typescript – it works for me in your example.

import url from 'rollup-plugin-url'
import babel from 'rollup-plugin-babel'

export default [{
  input: 'bug-ts.ts',
  output: [
    {
      file: 'dist/bug-ts.js',
      name: 'BugTs',
      format: 'esm',
      sourcemap: true
    }
  ],
  plugins: [
    babel({
      exclude: 'node_modules/**',
      extensions: ['.ts'],
      presets: ["@babel/preset-typescript"]
    }),
    url()
  ]
}]

@AntonPilyak
Copy link
Author

AntonPilyak commented May 30, 2019

Hi! That looks like a workaround, thanks for this solution, but still I think it should work out of the box with typescript2 plugin. Here is why:

  1. plugins should be executed in the same order as the are listed in 'plugins' section.
  2. the url plugin, theoretically, should replace the import statement to something like var myImage = 'base64encoding'; which is valid both in Typescript and Javascript, so, theoretically, Typescript2 plugin shouldn't know anything about importing or images in this case - it should just get the var definition. Am I wrong somewhere?

@Ashot-KR
Copy link

  1. execution order defined by rollup itself, plugins can't affect here, we can't do anything with this
  2. error comes from typescript compiler, not from plugins. Not sure how all this should work, but i'll try to figure it out and find solution. For now to make it work i recommend to use babel with typescript

@Ashot-KR
Copy link

Ashot-KR commented May 30, 2019

As i expected issue should be fixed on typescript level, it can be done with custom typings
create typings file with following code

declare module "*.png" {
  const value: any;
  export default value;
}

It tells typescript how to handle .png files.
With this typings everything works as expected.

Based on https://stackoverflow.com/a/51163365

@Ashot-KR Ashot-KR closed this as completed Jun 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants