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

Whitelist supported require.extensions #11614

Closed
oojacoboo opened this issue Oct 14, 2016 · 2 comments
Closed

Whitelist supported require.extensions #11614

oojacoboo opened this issue Oct 14, 2016 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@oojacoboo
Copy link

oojacoboo commented Oct 14, 2016

TypeScript Version: 2.0.3

When executing ts-node or just tsc on a TS file that requires files/assets other than .ts, .tsx, .js or other supported file types, the compiler throws Invalid character (1127) errors.

http://files.jacobt.com/pI1CAj0BaV

It seems the most sensible to me to include within the tsconfig a compilerOptions prop for require.extensions to target as a whitelist, while defaulting to include all extensions. It's common in many build environments to require assets such as images and sass, etc. These extensions don't play nice with the compiler.

You can always patch the default require.extensions to prevent the ts compiler from processing these require constructs like so:

require.extensions['.png'] = function(){};
require.extensions['.scss'] = function(){};

However, doing so and altering the global require.extensions should be avoided.

I did try various glob definitions within the files prop of the tsconfig in hopes that it would also apply to require statements. However, it doesn't seem that's supported.

Any thoughts or advice here?

@kitsonk
Copy link
Contributor

kitsonk commented Oct 14, 2016

I think what you are looking for is #6615.

Where:

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

Or any sort of typing you so desire.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 14, 2016
@oojacoboo
Copy link
Author

oojacoboo commented Oct 14, 2016

Thanks @kitsonk, any way to automatically include a typeRoots declaration file in a global context? I tried specifying under the typeRoots prop the following:

"typeRoots": [
  "./node_modules/@types",
  "./typings"
]

Where I have:

+-- typings
|   +-- images
|   |   +-- index.d.ts
|   +-- styles
|   |   +-- index.d.ts

included in the images/index.d.ts is a file with various declare module declarations like you'd mentioned. I realize this is unrelated since I can include it directly within the source, but this may help others as well.

@mhegazy mhegazy closed this as completed Feb 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants