Home  >  Q&A  >  body text

Translate specific packages in node_modules to es5

<p>I'm making an Angular 12 (IE compatible) project where some of my dependencies in node_modules are non-es5. </p> <p>In my understanding, <code>tsc</code> will not do any processing on node_modules, but will only retrieve <code>main</code> from <code>angular.json</code> ;Option starts evaluation. </p> <p>While looking for options on how to do this, I saw a lot of suggestions to use babel, but I'm not sure</p> <ol> <li><p>Should I mix babel with tsc. Or do I do away with <code>tsc</code> and just use babel via custom-webpack? </p> </li> <li><p>As I understand it, in all transpilations, the transpiled code goes into an output directory, but since I need to transpile the js files in node_modules, the output of these files should just replace them in Original files in node_modules? How do we achieve this? </p> </li> </ol><p><br /></p>
P粉481366803P粉481366803450 days ago593

reply all(1)I'll reply

  • P粉786800174

    P粉7868001742023-08-18 15:16:11

    You can add specific overrides in include in tsconfig.

    "include": [
      "src/**/*",
      "node_modules/foo/index.ts",
      "node_modules/bar/quux.baz.mjs"
    ]
    

    But when you package for the client, you usually don't include the dependencies as separate scripts, but let the packaging tool decide where to put them. You didn't mention what packaging tool you're currently using, but if you set allowJS: true, Babel is not required - for example, the relevant changes to the Webpack config would be:

    {
      test: /\.(js|ts)$/,
      exclude: /node_modules\/(?!(foo|bar)\/).*/, // 这一行
    

    However, if you want autofill, using @babel/preset-env with a Browserslist string is probably the best option.

    reply
    0
  • Cancelreply