Skip to content
This repository was archived by the owner on Jul 19, 2021. It is now read-only.
This repository was archived by the owner on Jul 19, 2021. It is now read-only.

Trying to add ttypescript to webpack #41

Closed

Description

@nidomiro

I'm trying to get https://github.com/rimeto/ts-transform-optchain running in my typescript react app.
It needs https://github.com/cevek/ttypescript and therefore a webpack config:

{
    test: /\.(ts|tsx)$/,
    loader: require.resolve('ts-loader'),
    options: {
        compiler: 'ttypescript'
    }
}

Using #5 I came up with:
.rescriptsrc.js

module.exports = config => {
  config.modules.rules.unshift({
      test: /\.(ts|tsx)$/,
      use: [
          {
              loader: require.resolve('ts-loader'),
              options: {
                  compiler: 'ttypescript'
              }
          }
      ]
  });
  return config
};

Even if I add an explicit error to the file to verify that its executed, the app starts normally?!

My package.json:

{
  "name": "reactsandbox",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/polyfill": "^7.4.0",
    "@material-ui/core": "^3.9.3",
    "@material-ui/icons": "^3.0.2",
    "classnames": "latest",
    "prop-types": "latest",
    "proxy-polyfill": "^0.3.0",
    "react": "^16.8.6",
    "react-app-polyfill": "^0.2.2",
    "react-dom": "^16.8.6",
    "react-redux": "^6.0.1",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.1.8",
    "redux": "^4.0.1",
    "redux-logger": "^3.0.6",
    "redux-saga": "^1.0.2",
    "ts-transform-optchain": "^0.1.6"
  },
  "scripts": {
    "start": "BROWSER=none rescripts start",
    "build": "rescripts build",
    "test": "rescripts test",
    "eject": "rescripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie < 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "@rescripts/cli": "0.0.10",
    "@rescripts/rescript-env": "0.0.10",
    "@types/classnames": "^2.2.7",
    "@types/jest": "^24.0.11",
    "@types/node": "^11.12.2",
    "@types/react": "^16.8.10",
    "@types/react-dom": "^16.8.3",
    "@types/react-redux": "^7.0.6",
    "@types/react-router-dom": "^4.3.1",
    "@types/redux-logger": "^3.0.7",
    "ttypescript": "^1.5.6",
    "typescript": "^3.4.1"
  },
  "rescripts": [
    "env"
  ]
}

I cannot get it working, could you please help?

Activity

  1. harrysolovay commented on Apr 3, 2019

    @harrysolovay
    Owner

    @nidomiro you're pointing to the env rescript. To solve your problem, delete the following from your package.json:

    - "rescripts": [
    -   "env"
    - ]

    Then, change your .rescriptsrc.js to the following:

    module.exports = [
      'env',
      config => {
        config.modules.rules.unshift({
          test: /\.(ts|tsx)$/,
          use: [
            {
              loader: require.resolve('ts-loader'),
              options: {
                compiler: 'ttypescript'
              }
            }
          ]
        });
        return config
      }
    ]
  2. nidomiro commented on Apr 3, 2019

    @nidomiro
    Author

    @harrysolovay thanks for your reply.
    The .rescriptsrc.js is used now. :)
    Unfortunately it does not work, an produces the error:

    > BROWSER=none rescripts start
    
    Cannot read property 'rules' of undefined
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! reactsandbox@0.1.0 start: `BROWSER=none rescripts start`
    npm ERR! Exit status 1
    npm ERR! 
    npm ERR! Failed at the reactsandbox@0.1.0 start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /home/niclas/.npm/_logs/2019-04-03T17_37_41_108Z-debug.log
    
    Process finished with exit code 1
    

    Maybe you can help me better if you know the Repository: https://gitlab.com/nidomiro/reactsandbox/tree/ie11

  3. harrysolovay commented on Apr 4, 2019

    @harrysolovay
    Owner

    One thing that might help is rewriting your rule-overriding "Rescript". If you follow these guidelines, you'll avoid possible conflicts with the env Rescript. At first glance, that's what I think is causing your problem. You could also try swapping the order (place the env rescript last). Also try using the full name @rescripts/rescript-env instead of just env. If all else fails, require the env Rescript into your config:

    module.exports = [
      config => {
        config.modules.rules.unshift({
          test: /\.(ts|tsx)$/,
          use: [
            {
              loader: require.resolve('ts-loader'),
              options: {
                compiler: 'ttypescript'
              }
            }
          ]
        });
        return config
      },
      require('@rescripts/rescript-env'),
    ]

    Please let me know if these solutions don't work, and I'll get back to you with more support (hopefully it works now though!)

    Kind regards,

    Harry

  4. nidomiro commented on Apr 5, 2019

    @nidomiro
    Author

    Thanks for your reply :)
    Your given code is not working for me.
    While trying to fix the problem I realized that CRA uses the ts-compiler only to verify the types and babel to compile to js.
    I found https://github.com/epeli/babel-plugin-ts-optchain and it fixed the root-problem of using oc without Proxy (ie11 compatibility).

    Thanks for your work and time,
    Niclas

  5. harrysolovay commented on Apr 5, 2019

    @harrysolovay
    Owner

    Gotcha––glad to hear you were able to find a solution & thanks trying Rescripts. Please let me know if you need any more support :)

  6. bretuobay commented on Aug 22, 2019

    @bretuobay

    For anyone running into this, I found out that the given script is wrong. This might help :)

    config => {
      config.module.rules.unshift({
        test: /\.(ts|tsx)$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              compiler: 'ttypescript'
            }
          }
        ]
      });
      return config
    },
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions