-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: custom loaders for webpack integration. #12828
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
Comments
The only thing that I believe is currently problematic is
I often get types that are incidentally on disk in parent directories. For the remaining items this does the trick: tsconfig.json {
"compilerOptions": {
"moduleResolution": "node",
"target": "es2015",
"module": "es2015",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"paths": {
"*":[
"modules/*"
]
}
}
} globals.d.ts declare module '*.css' {
const css: React.CSSProperties;
export default css;
} src/modules/main.tsx import $ from 'jquery'; // typings/globals/jquery/index.d.ts
import * as React from 'react'; // node_modules/@types/react/index.d.ts
import {render} from 'react-dom'; // node_modules/@types/react/index.d.ts
import square from 'square'; // src/modules/square.ts
import style from 'main.css'; // globals.d.ts
export default () => render(
<div style={style}>
{square(4)}
</div>,
$('#app')[0]
); |
Sorry I think I misunderstood the issue. Are you asking for the language service to parse the CSS files? resolveModule: "./config/typescript/resolve.js", would be immensely valuable but that readFile: "typescript", is not a good idea. |
Thanks for the With respect to this issue, I guess it's two separate parts.
I hope this makes sense. |
It makes sense but I'm not sure if there is any reason to couple the two things because given the first capability, the second is just a matter of generating declaration files to describe the shape of these files. The first definitely seems in scope but the second is not currently a TypeScript concern and does not need to become one for this to work. Ultimately, it is some other tool, e.g. a Webpack plugin that would actually provide the runtime values for these assets. That is perfect, the TypeScript compiler doesn't care about that - all it needs is a type declaration. |
I think we're in agreement. For sure, TypeScript does not need to know css. It just needs to allow configurable function to map a non-TypeScript import to a real or virtual I'm ignorant in TypeScript compiler internals, but in theory it works the same way as a JavaScript file import, where you need to map a non-TypeScript (in this case JavaScript) file to the corresponding definition file. It's just a bit more general. |
Technically this is not quite right since TypeScript handles JavaScript code just fine and infers tons of information from and can also transpile it directly. I think they key point is pluggable resolvers for real or virtual module identifiers. |
Yeah that's the main point. What do you think? |
Anything on this yet? |
We already offer the |
Please see my prior issue. (#11975)
Scenario
I have a pretty normal looking react project layout.
I have the following goals.
/node_modules
.$HOME/node_modules
./src
by name (not by relative path)/typings
./node_modules/@types
.It's unclear how to accomplish my entire list. Webpack works great for JavaScript, but the issue is TypeScript can never find anything. I want static typing but it's too difficult to figure out. Instead, I end up with a hybrid code base.
Solution
See Jest transform for inspiration.
It would be nice if TypeScript allowed user defined resolution and file reading functions. I'd imagine the types could look like this:
I could provide nodejs modules that adhere to the interface.
It would open up a world of possibilities for developers without needing constant changes to TypeScript core. The most popular strategies (like
commonjs
andtypescript
) could be bundled as a string constant instead of a path to code.If I want to do unusual things, like import image files as
type string
or parse CSS files for safe css module access, I should be allowed. The concept is similar to webpack loaders.It's simple and configurable. It doesn't lock developers into using specific file extensions, or module resolution strategies, or frameworks, or build systems. TypeScript should just check types, and let developers provide modules and definitions as needed.
The text was updated successfully, but these errors were encountered: