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

file-loader with default export #344

Closed
mohsen1 opened this issue Oct 27, 2016 · 3 comments
Closed

file-loader with default export #344

mohsen1 opened this issue Oct 27, 2016 · 3 comments

Comments

@mohsen1
Copy link

mohsen1 commented Oct 27, 2016

I'm using TypeScript 2.0 wildcard module name matching to allow importing files like images:

declare module '*.png' {
    const fileName: string;
    export = fileName;
}

And now I can import images with * as syntax:

import * as myImage from './my-image.png'

This works but I love to ditch the * as syntax and simply do import myImage from './my-image.png'. For that I can change the module declaration to:

declare module '*.png' {
    const fileName: string;
    export default fileName;
}
import myImage from './my-image.png'

Problem is, when I do this, the transpiled code becomes:

var myImage = require(42).default;

My tsconfig:

    "target": "es5",
    "module": "commonjs"

Do you have a recommendation for fixing this?

@johnnyreilly
Copy link
Member

You could give this a go: (no guarantees)

"allowSyntheticDefaultImports": true

@andrewMuntyan
Copy link

andrewMuntyan commented Apr 15, 2018

Hi, all! Had same situation.
allowSyntheticDefaultImports did not help.
TL;DR;
desired options is "esModuleInterop": true,

i use "typescript": "^2.8.1"

tsconfig.json

{
  "compilerOptions": {
    "outDir": "../dist/",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "sourceMap": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react",
    "allowJs": true,
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": true,
    "typeRoots": [
      "../typings",
      "../node_modules/@types"
    ]
  },
  "include": [
    "../src/**/*",
    "../typings/**/*"
  ],
  "exclude": [
    "../node_modules",
    "**/*.spec.ts"
  ]
}```

when i use 
```javascript
declare module '*.jpg' {
  const fileName: string;
  export default fileName;
}

and then

import image from './large-image.jpg';

i get next picture in my compiled code
2018-04-15_1402
it is compiled to

var large_image_jpg_1 = __webpack_require__(4);
console.log(large_image_jpg_1.default);

large_image_jpg_1 becomes string - image url.
and string.default is undefined
but if i use

declare module '*.jpg' {
  const fileName: string;
  export = fileName;
}

and then

import * as image from './large-image.jpg';

it works fine.

@johnnyreilly
Copy link
Member

cc @mohsen1 - I'm sure you're past this now but see @andrewMuntyan in case you're not!

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

3 participants