Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bundler: __default is not defined when using named imports #3628

Open
Vadorequest opened this issue Feb 18, 2022 · 7 comments
Open

bundler: __default is not defined when using named imports #3628

Vadorequest opened this issue Feb 18, 2022 · 7 comments

Comments

@Vadorequest
Copy link

Vadorequest commented Feb 18, 2022

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

@kdy1 kdy1 modified the milestones: v1.2.143, v1.2.144, v1.2.145, v1.2.146 Feb 19, 2022
@kdy1 kdy1 modified the milestones: v1.2.146, v1.2.147, v1.2.148, v1.2.149 Feb 27, 2022
@kdy1
Copy link
Member

kdy1 commented Mar 6, 2022

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 kdy1 changed the title __default is not defined when using named imports bundler: __default is not defined when using named imports Mar 6, 2022
@kdy1 kdy1 added the A: bundler label Mar 6, 2022
@Vadorequest
Copy link
Author

@Vadorequest
Copy link
Author

Vadorequest commented Mar 6, 2022

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,
});

@kdy1 kdy1 modified the milestones: v1.2.149, v1.2.150 Mar 6, 2022
@Stuk
Copy link

Stuk commented May 10, 2022

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);

@l3ve
Copy link

l3ve commented Jul 13, 2022

+1 , is there any way to resolve it ?

@Vadorequest
Copy link
Author

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.

@DNA-PC
Copy link

DNA-PC commented Jan 11, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants