plugin-typescript icon indicating copy to clipboard operation
plugin-typescript copied to clipboard

Is it possible to load global script?

Open unional opened this issue 8 years ago • 6 comments

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 avatar Nov 05 '17 21:11 unional

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

It uses plugin-typescript internally.

unional avatar Nov 05 '17 21:11 unional

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 avatar Nov 05 '17 21:11 unional

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,
  ...

unional avatar Nov 06 '17 20:11 unional

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.

frankwallis avatar Nov 06 '17 23:11 frankwallis

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 [email protected]?

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

unional avatar Nov 07 '17 00:11 unional

YES! that also solves the second issue.

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

unional avatar Nov 07 '17 00:11 unional