brunch / typescript-brunch

Adds TypeScript support to Brunch

Home Page:http://brunch.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BaseUrl support

kuon opened this issue · comments

To avoid having import "web/static/app/foo/bar" in my phoenix app, I tried to add "baseUrl":"web/static/app" to my tsconfig.conf so I could do import "foo/bar"

But, while this works if I call tsc directly, brunch-typescript fails to resolve the dependency.

Is there any way to have baseUrl support in brunch-typescript?

You should be able to do this with Brunch itself (even before TypeScript 2) using the nameCleaner config. Something like this:

modules: {
  nameCleaner: path => path.replace(/^web\/static\/app\//, '')
}

However I agree it would be nice if the plugin could provide similar support since it is already reading the tsconfig file. I'm not sure whether plugins can affect how the files are resolved, but I'll look into it.

It will let you do import 'foo/bar', indeed the default behaviour is what strips off the leading app/ when used with normal Brunch conventions. I use a custom nameCleaner with a nested brunch app quite successfully in conjunction with brunch-typescript.

The nameCleaner is used at build time to create require.register() calls with the appropriately cleaned module names (again by default with CommonJS modules). You can see the results in your joined output files.

I just took a look at the default Phoenix brunch-config.js file and it looks like it should support nameCleaner just fine.

I tried to just print the path in nameCleaner for debug, but if I import a file that cannot be resolved, I have DEPS_RESOLVE_FAILED and my name cleaner function is not called.

my config

  modules: {
    autoRequire: {
      "js/app.js": ["web/static/app/app"]
    },
    nameCleaner: path => {
      if (path.match(/test/)) {
        console.log(path)
      }
      return path
    }
  },

my test file:

import DropDown, { Title, Item } from "test/drop_down"

produces

22 Nov 05:46:48 - info: compiling
22 Nov 05:46:49 - error: DEPS_RESOLVE_FAILED of web/static/app/pages/assets/components/label_drop_down.tsx failed. Could not load module 'test/drop_down' from '/Users/kuon/Repositories/memoways/kura/web/static/app/pages/assets/components'. Possible solution: add 'test' to package.json and `npm install`.

If I remove the if in my nameCleaner it is properly called with all files in my project.

I'm not sure where you put the test folder, but I suspect it would need to be at /web/static/test for this to work. I think that Brunch is doing the initial dependency check relative to the watched folders. That is a separate process to the module resolution, but obviously very linked to it. I've tested it locally even with a non-standard watched folder and it seems to work.

I can take a closer look in the morning if you continue to have problems.

It is also possible that the test name itself is the issue since Phoenix has a watched folder at test/static which might be confusing Brunch.

I tried other foler names, same thing.

My test folder is in /web/static/app/test.

Then try referencing it as app/test/filename and see if that works.

Nop, and nameCleaner is never called when the resolution fails it seems.