swc icon indicating copy to clipboard operation
swc copied to clipboard

bundler: `__default` is not defined when using named imports

Open Vadorequest opened this issue 2 years ago • 6 comments

Describe the bug

When importing a file with named exports, the function code isn't present in the output. A __default call is present but doesn't reference anything.

Reproduction at https://github.com/UnlyEd/stacker-public-scripts/tree/issue-_default

Input code

import '../common/enhancers';
import '../common/polyfills';
import '../iife/applyWhiteLabel';
import findStackerUser from '../lib/findStackerUser';

findStackerUser();

Config

{
  "minify": false,
  "jsc": {
    "minify": {
      "compress": {
        "unused": false
      },
      "mangle": false
    },
    "target": "es5"
  }
}

Playground link

https://github.com/UnlyEd/stacker-public-scripts/blob/issue-_default/spack.config.js

Expected behavior

The function findStackerUser is not present in the output, it should be.

Actual behavior

Yield the following output:

function _instanceof(left, right) {
    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
        return right[Symbol.hasInstance](left);
    } else {
        return left instanceof right;
    }
}
/**
 * Finds the first element that matches the text for the given selector.
 *
 * @example querySelectorIncludesText('button', 'Send')
 */ document.querySelectorIncludesText = function querySelectorIncludesText(selector, text) {
    return Array.from(document.querySelectorAll(selector)).find(function(el) {
        return el.textContent.includes(text);
    });
};
String.prototype.startsWith || (String.prototype.startsWith = function(search, pos) {
    return this.slice(pos || 0, search.length) === search;
}), Array.prototype.includes || (Array.prototype.includes = function(search, start) {
    if (_instanceof(search, RegExp)) throw TypeError("first argument must not be a RegExp");
    return void 0 === start && (start = 0), -1 !== this.indexOf(search, start);
}), String.prototype.includes || (String.prototype.includes = function(search, start) {
    if (_instanceof(search, RegExp)) throw TypeError("first argument must not be a RegExp");
    return void 0 === start && (start = 0), -1 !== this.indexOf(search, start);
});
/**
 * Apply on all pages where "Powered by Stacker" is shown.
 */ !function() {
    console.log("[Custom script] Applying white labelling");
    // Remove "Powered by" text (white labeling)
    var poweredByEl = document.querySelectorIncludesText("a", "Powered By Stacker");
    poweredByEl && poweredByEl.remove();
}();
__default();

Version

1.2.142

Additional context

No response

Vadorequest avatar Feb 18 '22 22:02 Vadorequest

Are you using bundler? I tried reproducing it, but failed, and the only code which can create __default is in the bundler, if I remember correctly

kdy1 avatar Mar 06 '22 01:03 kdy1

Yes, I am.

https://github.com/UnlyEd/stacker-public-scripts/blob/issue-_default/spack.config.js

Vadorequest avatar Mar 06 '22 05:03 Vadorequest

What I did in the meantime was to use require instead.

Ah, I changed the repo visibility to private since creating this issue.

spack.config.js

const fs = require('fs');
const { config } = require('@swc/core/spack');

// Use the same options for spack as the ones in the .swcrc file (better intellisense there)
const options = JSON.parse(fs.readFileSync('.swcrc', { encoding: 'utf8', flag: 'r' }));

module.exports = config({
  entry: {
    'univers': __dirname + '/src/Stacker X - [PPO] Plateforme Univers - univers.propulseo.unly.org/index.ts',
  },
  output: {
    path: __dirname + '/dist',
  },
  module: {},
  mode: 'production',
  target: 'browser',
  options,
});

Vadorequest avatar Mar 06 '22 05:03 Vadorequest

Here's a pretty straightforward repro gist https://gist.github.com/Stuk/4e1c11e867c720608a1c7bee129654ca. Specifically in the output the following incorrect line appears:

    return __default.createElement("div", null, hour, ":", minute, ":", second);

Stuk avatar May 10 '22 20:05 Stuk

+1 , is there any way to resolve it ?

l3ve avatar Jul 13 '22 07:07 l3ve

This is becoming more and more of a daily issue, I cannot use TypeScript properly.

const NcoUser = require('../../types/NcoUser').default;
const user: typeof NcoUser = findNcoUser();

What I really want is to take a bunch of JS/TS files and make a single dist file that's browser-compatible, I feel like I'm using the wrong solution.

Vadorequest avatar Aug 30 '22 17:08 Vadorequest

Is this on the roadmap? I'm looking for a tool that allows bundling different output files (like Spack does), but I'd prefer using TS and Import, which is currently not possible.

DNA-PC avatar Jan 11 '23 10:01 DNA-PC