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

Can't import assets on typescript #1514

Closed
dortamiguel opened this issue Jun 8, 2018 · 5 comments
Closed

Can't import assets on typescript #1514

dortamiguel opened this issue Jun 8, 2018 · 5 comments

Comments

@dortamiguel
Copy link

🐛 bug report

When I try to load an asset inside typescript I get undefined instead of the route for the file.

import characterImage from '@client/assets/img/character.png'
console.log(characterImage) // undefined

The code above works in normal .js

🎛 Configuration (.babelrc, package.json, cli command)

This is my .babelrc

{
  "env": { "browser": true },
  "ignore": [],
  "plugins": [
    ["transform-runtime", {
      "polyfill": false,
      "regenerator": true
    }],
    ["module-resolver", {
      "alias": {
        "@client": "./src/client",
        "@server": "./src/server"
      }
    }],
    "transform-object-rest-spread",
    "ramda"
  ],
  "presets": [
    "env"
  ]
}

🤔 Expected Behavior

Import should return the string for the url of the asset.

😯 Current Behavior

I get undefined when trying to import.

💁 Possible Solution

Maybe it can help but it looks like require works as expected

const characterImage = require('@client/assets/img/character.png')

console.log(characterImage) // /character.f3137b4d.png
Software Version(s)
{
    "parcel-bundler": "^1.8.1",
    "typescript": "^2.9.1"
}
@mohsen1
Copy link
Contributor

mohsen1 commented Jun 8, 2018

  1. Add a file called declaration.d.ts anywhere in your project.
  2. Add this line in there: declare module "*.png"
  3. Add a module declaration for other asset types if you need to

@dortamiguel
Copy link
Author

@mohsen1 I did that, that fixes the problem with types. The thing is that the value of the import is undefined

@mohsen1
Copy link
Contributor

mohsen1 commented Jun 16, 2018

TypeScript doesn't know about runtime value. if you're getting runtime value of undefined you're having an issue unrelated to TypeScript

@dortamiguel
Copy link
Author

That's why this is very weird. If I rename the file into .js the import works with exactly the same code.

@dortamiguel
Copy link
Author

Oh! I removed the plugin

"parcel-plugin-typescript"
and the asset imports started to work.

I moved this issue to the plugin repo fathyb/parcel-plugin-typescript#42

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

No branches or pull requests

2 participants