Tyler Sebastian
Tyler Sebastian

Reputation: 9458

Webassets + Typescript, cannot resolve symbols/modules

I have a flask project with the following structure:

├─ app.py
├─ project
|  ├─ __init__.py
|  └─ static
|     └─ typescript
|        └─ app.ts
└─ typings
   ├─ globals
   |  └─ ... # multiple imported ts libraries
   └─ index.d.ts

I'm using a webpacker integration called Flask Assets. I've set up the compilation like so (in __init__.py)

ts = get_filter('typescript')
ts.load_paths = [
    #os.path.join(config.APP_ROOT, '..', 'typings'), # doesn't do anything :/
    os.path.join(app.static_folder, 'typescript')
]

assets.register('javascript', Bundle(
    'typescript/app.ts', 
    filters = (ts, 'jsmin'),
    output = 'js/app-%(version)s.js'
))

My app.ts is, more or less,

class SomeClass {
    ... various class methods, using things like jQuery and CryptoJS
}

no imports - I'm not really sure whether or not I need them.

The specific error I'm getting is

Cannot find name 'JQuery'.
../../../../../var/folders/5t/4x0gmsdx0dbbgv_fr3cv3x6m0000gn/T/tmphFTSQo.ts(7,17): error TS2503: Cannot find namespace 'CryptoJS'.
../../../../../var/folders/5t/4x0gmsdx0dbbgv_fr3cv3x6m0000gn/T/tmphFTSQo.ts(10,27): error TS2304: Cannot find name '$'.
... a bunch more about other symbols

Upvotes: 0

Views: 269

Answers (1)

Tyler Sebastian
Tyler Sebastian

Reputation: 9458

I kind of solved it...

glob_string = os.path.join(config.APP_ROOT, '..', 'typings', '*', '*', '*.d.ts')

assets.register('javascript', Bundle(
    glob.glob(glob_string),
    'typescript/app.ts',
    filters = ('typescript', 'jsmin'),
    output = 'js/app-%(version)s.js'
))

basically I just "manually" add all the definition files to the bundle (using glob). It's not sufficient to just add the index.d.ts in the root of the typings dir as the typescript filter copies the .ts to a temp file (in /tmp) before compiling and the paths in index.d.ts are relative.

it should also be noted that ts.load_paths does nothing...

Upvotes: 0

Related Questions