-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Optional chaining is not compiled correctly #760
Comments
Can I help somehow to speed up this fix? I'm not even intermediate in Rust however :/ |
I'm working on this, but I failed to reproduce the issue.
|
So.. One thing is that I have this resolve: {
extensions: ["*", ".js", ".jsx"],
alias: {
moment: "window.moment",
jQuery: "window.jQuery",
jquery: "window.jQuery",
"aws-amplify": 'window["aws-amplify"]',
firebase: 'window["firebase"]',
},
},
externals: [
{
moment: "moment",
jQuery: "jQuery",
jquery: "jQuery",
"aws-amplify": 'window["aws-amplify"]',
firebase: 'window["firebase"]',
},
], I also have aws-amplify defined in new webpack.ProvidePlugin({
"aws-amplify": "aws-amplify",
'window["aws-amplify"]': "aws-amplify",
'window["firebase"]': "firebase",
}), |
I removed the last question mark and it works.. However ran into another problem with legacy decorators.. You don't support these, right? |
@jansedlon It's supported, but not documented yet. I verified that the legacy decorator works with the config below. I'll update the documentation. {
"jsc": {
"parser": {
"syntax": "ecmascript",
"decorators": true
},
"transform": {
"legacyDecorator": true
}
}
}
` |
@kdy1 That's awesome, thank you! Let me know if you try it with that babel config and if you need more code. |
@jansedlon Thanks! Please feel free to file an issue when you encounter a bug. |
@kdy1 Hello, I've got update. Looks like that this happens in more places and might not be specifically optional-chaining issue.. Original code const initialState = {...}
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case SET:
return ...
case REPLACE:
return ...
case RESET_CURSOR:
return ...
case TOGGLE_BUSY: {
return...
}
default:
return state
}
} |
I'll fix it asap. I found that logic exists. It's a bug of the optimizer pass. Anyway, it seems like an easy bug. |
In a file where this reducer is defined are also some consts.. Which I can't find in the compiled source.. Looks like it has been just thrown out. |
@kdy1 Thank you! Is there any way I can buy you https://www.buymeacoffee.com/ ? |
@jansedlon I created an account :) |
@kdy1 You have to setup payment method so I can send you something 😅 |
@jansedlon I configured it. Thanks :) |
Published swc/core@v1.1.41 |
@kdy1 Great job! Looks like that previous problem is eliminated. However! 😅 Another problem with "dead code elimination" Original code import {
INSTAGRAM_CHECK_PATTERN,
RESOURCE_FACEBOOK,
RESOURCE_INSTAGRAM,
RESOURCE_WEBSITE,
} from '../../../../consts'
const resources = [
{
value: RESOURCE_WEBSITE,
label: 'Webové stránky',
},
{
value: RESOURCE_FACEBOOK,
label: 'Facebook',
},
{
value: RESOURCE_INSTAGRAM,
label: 'Instagram',
},
]
// export default decorated React class component Compiled code // About 15 empty lines
var resources = [
{
value: RESOURCE_WEBSITE,
label: 'Webové stránky'
},
{
value: RESOURCE_FACEBOOK,
label: 'Facebook'
},
{
value: _consts__WEBPACK_IMPORTED_MODULE_6__["RESOURCE_INSTAGRAM"],
label: 'Instagram'
},
];
var _dec = Object(_validator__WEBPACK_IMPORTED_MODULE_9__["i18n"])(); |
I found that the variables are declared in the compiled code like this /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RESOURCE_WEBSITE", function() { return RESOURCE_WEBSITE; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RESOURCE_FACEBOOK", function() { return RESOURCE_FACEBOOK; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RESOURCE_INSTAGRAM", function() { return RESOURCE_INSTAGRAM; }); However it looks like that |
Moved to #763 |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
I tried to use SWC for a quite big project... Around 130k LOC. In a code, there an attempt to use optional chaining to window object and its key. However when I try to compile it, it throws an error that
ref1 is undefined
. Rust probably tries to use some variable that it did not created.Source code
const Auth = window?.['aws-amplify']?.Auth
Compiled code
var Auth = window === null || window === void 0 ? void 0 : window['aws-amplify'] === null || ref1 === void 0 ? void 0 : window['aws-amplify'].Auth;
Config
Expected behavior
Working optional chaining without undefined variable
Version
The version of @swc/core: 1.1.40
The text was updated successfully, but these errors were encountered: