frankwallis / plugin-typescript

TypeScript loader for SystemJS
MIT License
248 stars 47 forks source link

Is it possible to load global script? #213

Open unional opened 6 years ago

unional commented 6 years ago

I tried to load a file like this and failed:

(function() {
  window['MyCompany'] = {
    component: {}
  }

  const foo: string = 'foo'
  window['MyCompany'].component.foo = foo
}())

error: 'Missing initializer in const declaration'. If I remove the type, i.e. from const foo: string = 'foo' to const foo = 'foo', it works.

unional commented 6 years ago

Repro: https://github.com/unional/domture/tree/global-script-with-type

It uses plugin-typescript internally.

unional commented 6 years ago

Here is the systemjs config being used:

{
  "baseURL": "node_modules",
  "packageConfigPaths": [
    "@*/*/package.json",
    "*/package.json"
  ],
  "map": {
    "app": "./fixtures/ts"
  },
  "packages": {
    "app": {
      "defaultExtension": "ts"
    },
    "typescript": {
      "main": "lib/typescript.js",
      "meta": {
        "lib/typescript.js": {
          "exports": "ts"
        }
      }
    },
    "plugin-typescript": {
      "main": "lib/plugin.js"
    }
  },
  "transpiler": "plugin-typescript"
}
unional commented 6 years ago

Seems like even when loading global ts files without typed information also have some quirks.

I don't have a repro on this one because it is related to my work, but in essence, it throws a TypeError: x is not a constructor when I create a ExtJS class and instantiates it:

// source.ts
TestClass = Ext.extends(BaseClass, { ... })

// test.ts
console.log(TestClass)
const instance = new TestClass({}) // TypeError: TestClass is not a constructor

If I load the source from transpiled js, it is working, but if I load source.ts, it gives the above error.

The console.log(TestClass) outputs are the same no matter if I am loading js or ts:

{ [Function: constructor]
      superclass:
       constructor {
         constructor:
          { [Function: constructor]
            superclass: [Object],
            override: [Function],
            extend: [Function],
            xtype: 'store',
            Error: [Object] },
         supr: [Function],
         superclass: [Function],
         override: [Function: io],
         writer: undefined,
  ...
frankwallis commented 6 years ago

Looking at the first issue it seems that the file is not getting passed through the transpiler, I'm not exactly sure why, but it's likely to be a configuration issue.

unional commented 6 years ago

Thanks Frank.

I try to change some config as you suggest and now this one works:

{
  "baseURL": "node_modules",
  "packageConfigPaths": [
    "@*/*/package.json",
    "*/package.json"
  ],
  "map": {
    "app": "./fixtures/ts"
  },
  "packages": {
    "app": {
      "defaultExtension": "ts",
       "meta": {
         "*.ts": {
           "loader": "plugin-typescript"
         }
      }
    },
    "typescript": {
      "main": "lib/typescript.js",
      "meta": {
        "lib/typescript.js": {
          "exports": "ts"
        }
      }
    },
    "plugin-typescript": {
      "main": "lib/plugin.js"
    }
  },
  "transpiler": "plugin-typescript"
}

But I thought the meta is not necessary as by default it will go through the transpiler. Maybe an issue with systemjs@0.20.19?

I'll continue to see if the second issue is also resolved.

unional commented 6 years ago

YES! that also solves the second issue.

Now the remaining question is why it doesn't work without the meta/*.ts/loader config.