Skip to content

Add loader for Typescript #9

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

Closed
ghost opened this issue Jun 13, 2017 · 4 comments
Closed

Add loader for Typescript #9

ghost opened this issue Jun 13, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Jun 13, 2017

No description provided.

@davidmpaz
Copy link
Contributor

Hi,

thanks for this project. I has put lot of knowledge in place thanks to it. I would like to provide some imput from my current experience.

I am using master branch, it means i have in package.json:

"@symfony/webpack-encore": "git+https://github.com/symfony/webpack-encore.git#master"

This allow me to use the recently addition to public API of addLoader() method. At time of this writting of course.

Adding my TypeScript loader is then easy:

    // allow TypeScript files to be processed
    .addLoader({
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: [
            {
                loader: 'babel-loader',
                // @see https://babeljs.io/docs/usage/api/#options
                // @see https://github.com/babel/babel-loader#options
                options: {}
            },
            {
                loader: 'ts-loader',
                // @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
                options: {}
            }
        ]
    })

My problem is with the extensions to resolve. I did follow the approach described in #37 without succes.

This means I can sucessfully add alias to resolve object from configuration; but can not modify the extensions object with a simple concat like this:

// add extensions for typescript.
config.resolve.extensions.concat(['.json', '.ts', '.tsx']);

Not being able to do this cause me problems resolving local modules.

 ERROR  Failed to compile with 2 errors  
These dependencies were not found:

* local/module1 in ./resources/src/main.ts
* local/module2 in ./resources/src/main.ts

To install them, you can run: npm install --save local/module1 local/module2
    + 543 hidden modules

If I hardcode the extensions in the config generator, then everything goes nice and dandy.

things I have tried:

  • Copying the entire config object and modiying it. In case the object is inmutable. No success.
  • Change the variables declaration in module from const to let or var. No success.

What am I missing?

Cheers.

@weaverryan
Copy link
Member

Hey @davidmpaz!

Thanks for sharing your experience! Instead of concat, try push:

config.resolve.extensions.push('.json');
config.resolve.extensions.push('.ts');
config.resolve.extensions.push('.tsx');

The problem is that contact doesn't modify the original object, it returns a new Array object. Another option would be:

var extensions = config.resolve.extensions;
extensions = extensions.concat(['.json', '.ts', '.tsx']);
// set the new object back on this key
config.resolve.extensions = extensions;

Obviously, this will be solved when we have an actual TypeScript loader :). But we need to decide (in general) if we should make this a bit easier, or have people work directly with the webpack config :). See #37

@davidmpaz
Copy link
Contributor

Oops ^ ^... there was the missing point. Thanks!!

Regarding:

But we need to decide (in general) if we should make this a bit easier, or have people work directly with the webpack config :)

My 5 cents goes for an opinionated way to change configuration in general, through public API methods and allow (through callbacks/strategie or similar) to modify sections in configuration. This mean:

Add more methods like in this PR and provide extension points in those methods to allow for modifications.

This will allow (for new comers like me) to have the benefits of getting "some fancy configuration" done out of the box (no brainer), but still if "i want to get out of the car and walk, instead drive", I will be able to do that also :)

The main value I see in this module is that: I dont want to learn all quirk and dirty things about webpack (unless I need to :D). Public API with verbose/explicit method names will help on that.

Thanks again for the help.
Cheers

@ghost
Copy link
Author

ghost commented Jun 26, 2017

see #50

@ghost ghost closed this as completed Jun 26, 2017
weaverryan added a commit that referenced this issue Jun 29, 2017
…yan)

This PR was merged into the master branch.

Discussion
----------

Add support to enableTypescriptLoader

Hi,

please would you consider adding this to main stream?

this add public API to enable TypeScript loader ([ts-loader](https://github.com/TypeStrong/ts-loader)). It follows much of what is described in #9

Cuurrently only add vanilla loader as described in [here](https://github.com/TypeStrong/ts-loader/blob/master/examples/vanilla/webpack.config.js) playing side by side with babel.

Future work would be to include also support to `happypack` and `fork-ts-checker-webpack-plugin`

Thanks in advance

Commits
-------

6d5dee8 Fixing bad merge
35d9e6f Merge branch 'master' into typescript
82e17e3 Remove unnecessary functional test
4d5319d Update yarn.lock with typescript deps
144acd2 Cleaning up unnecessary code.
f6bc6e7 Rename to enableTypeScriptLoader
facc132 Remove package-lock.json
25b9f18 Remove duplicated default configuration options
23619b2 Add test for error handling on missing ts loader
d887a79 Add typescript to missing loader error handling
61370ac Add .ts & .tsx to resolved extension
6ec2b52 Revert "Add .idea directory to gitignore rules"
356aaa2 Make tests check for content in browser context
822ceff Improve tests for compiling .tsx files
ae1bea3 Update packages-lock.json file
a02d8ab Add .idea directory to gitignore rules
32c2853 Revert accidental version change
846d142 Update tests to take into account refactoring
b43f5c9 Refactor to favor configure by callback
1352d1c Add missing extensions to config.resolve
7e8c118 Fix copy'n paste errors
58b7ef7 Add unit and functional test for TypeScript loader
fc26e1b Add support to enable TypeScript loader
This issue was closed.
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