frankwallis / plugin-typescript

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

TypeError: Invalid System.register call. Anonymous System.register calls can only be made by modules loaded by SystemJS.import and not via script tags. #191

Closed alaingalvan closed 7 years ago

alaingalvan commented 7 years ago

Hey, was running into an error today while trying to compile some files using SystemJS Builder and transpile them with plugin-typescript.

Stack Trace

TypeError: Invalid System.register call. Anonymous System.register calls can only be made by modules loaded by SystemJS.import and not via script tags.
    at SystemJSNodeLoader.<anonymous> (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:4285:33)
    at SystemJSNodeLoader.<anonymous> (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:4285:33)
    at SystemJSNodeLoader.<anonymous> (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:4285:33)
    at SystemJSNodeLoader.reduceRegister_ (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:4285:33)
    at SystemJSNodeLoader.pushRegister_ (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:2766:14)
    at SystemJSNodeLoader.SystemJSLoader.register (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:3004:10)
    at Object.<anonymous> (D:\Documents\Github\alain.xyz\portfolio\node_modules\plugin-typescript\lib\plugin.js:1:8)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at SystemJSNodeLoader.getNodeModule (D:\Documents\Github\alain.xyz\portfolio\node_modules\systemjs\dist\system.src.js:1707:30)

I tried giving a name to plugin-typescript/lib/plugin.js and that did clear the error, but that resulted in an empty bundle.js output file.


Dependencies

I'm using the latest versions of plugin-typescript, systemjs-builder, and typescript.

"dependencies:" {
    "plugin-typescript": "^6.0.4",
    "systemjs-builder": "^0.16.3",
    "typescript": "^2.1.6"
}

I've tried using the version from the master branch here on github, and tried upgrading systemjs to 0.20, no dice,

Builder Config

let config = {
      globalEvaluationScope: false,
      baseURL: './',
      format: 'commonjs',
      defaultJSExtensions: true,
      packages: {
        main: {
          main: 'main',
          defaultExtension: 'tsx',
          transpiler: 'ts',
          format: 'system',
          typescriptOptions: {
            module: 'system',
            target: 'es5',
            jsx: 'react',
            typeCheck: false,
            tsconfig: true
          },
          externals: [
            'react',
            'react-dom',
            'react-router',
            'react-redux',
            'redux'
          ],
          meta: {
            '*.ts': {
              'loader': 'ts'
            },
            '*.tsx': {
              'loader': 'ts'
            }
          }
        }
      },
      meta: {
        typescript: {
          format: 'cjs'
        },
        ts: {
          format: 'register'
        }
      },
      map: {
        ts: '@node/plugin-typescript',
        typescript: '@node/typescript',
        crypto: '@node/crypto',
        os: '@node/os',
        buffer: '@node/buffer',
        stream: '@node/stream',
        child_process: '@node/child_process',
        assert: '@node/assert',
        fs: '@node/fs',
        path: '@node/path',
        string_decoder: '@node/string_decoder',
        vm: '@node/vm',
        constants: '@node/constants',
        process: '@node/process',
        util: '@node/util',
        events: '@node/events'
      }
 };
alaingalvan commented 7 years ago

I tried compiling the plugin to commonjs and it got rid of the error, but now it seems as though SystemJS isn't configured by the time the transpiler is running.

I set the default config in createFactory to include jsx: 'react and module:system` and that fixed my problem for now.

My question is Why isn't SystemJS configured with what I'm passing to SystemJS Builder's Configuration? I guess that's a question for Builder and not this repo.

Here's a repo with the changes I did, I can send a pull request if it's ok:

https://github.com/alaingalvan/plugin-typescript

frankwallis commented 7 years ago

Thanks for raising this and sorry for not responding before. There have been a number of issues related to systemjs@0.20 and plugin-typescript 6.0.0 so it is hard for me to know which one this was, but I hope it is working in 7.0.4 (although without type-checking support)

Looking at your configuration I see that you have typescriptOptions specified in packages configuration - this was not supported in 6.x.x and that was probably why it was not getting picked up. The good news is it is supported as a new feature in 7.x.x so hopefully that will work now, let me know if you still have issues and I will reopen this.