swc-project / swc-loader

Moved to https://github.com/swc-project/pkgs
MIT License
394 stars 29 forks source link

`options.minify` cannot remove comments #66

Closed awdr74100 closed 2 years ago

awdr74100 commented 2 years ago

First of all, thanks for developing such a great tool as SWC!

Talk directly from the code:

// main.ts (entry file)

const merge = <T extends object, U extends object>(target1: T, target2: U) => {
  return { ...target1, ...target2 };
};

const result = merge({ name: 'Ian' }, { age: 24 });

// eslint-disable-next-line no-console
console.log(result);
// webpack.config.ts

const config: Configuration = {
  ...
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'swc-loader',
            options: {
              minify: true,
              jsc: {
                minify: {
                  compress: true,
                  mangle: true,
                },
                parser: {
                  syntax: 'typescript',
                  tsx: true,
                  decorators: true,
                  dynamicImport: true,
                },
                target: 'es5',
              },
            },
          },
        ],
      },
    ],
  },
};

Compile the results:

default terser compression has been turned off.

/******/ (() => { // webpackBootstrap
/******/    "use strict";
var __webpack_exports__ = {};
function e(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}var r,n,t=function(r){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{},o=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(t).filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),o.forEach(function(n){e(r,n,t[n])})}return r}({},r={name:"Ian"},n={age:24});console.log(t);
/******/ })()
;
//# sourceMappingURL=main.9a6e608b.js.map

Compile results using TerserPlugin.swcMinify:

inside the plugin will call the swc.minify('...', { module: false, ecma: 2015, compress: { ecma: 2015 }, mangle: true, sourceMap: true }) method of @swc/core.

(()=>{"use strict";var e={};function r(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}var n,t,o=function(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{},o=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(t).filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),o.forEach(function(n){r(e,n,t[n])})}return e}({},{name:"Ian"},{age:24});console.log(o)})();
//# sourceMappingURL=main.4def3736.js.map

It is obvious that swc-loader left uncleared comments.

I will be grateful for any help you can provide!

kdy1 commented 2 years ago

Those comments not related to swc. See webpack docs