Skip to content

const enum in a component always generates a runtime object #682

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

Closed
dkzlv opened this issue Nov 20, 2020 · 1 comment
Closed

const enum in a component always generates a runtime object #682

dkzlv opened this issue Nov 20, 2020 · 1 comment
Labels
question A user question

Comments

@dkzlv
Copy link

dkzlv commented Nov 20, 2020

Describe the bug
If you use a const enum in the <script> tag it will still generate a runtime object.

To Reproduce

  1. use official template and run the official script for TS support
  2. disable terser rollup plugin (to have readable output). Also, even though the default state of preserveConstEnums is false, you can set it just to be sure.
  3. replace App.svelte contents with this:
<script lang='ts'>
  const enum State {
    opened,
    closed,
  }
  let state = State.opened;
  // We add this so it is not moved into module context
  state++
</script>
  1. run npm build or just look into IDE tab with the pre-built version of the component. It will look like this:
/* generated by Svelte v3.29.7 */
import { SvelteComponent, init, safe_not_equal } from "svelte/internal";

function instance($$self) {
	var State;

	(function (State) {
		State[State["opened"] = 0] = "opened";
		State[State["closed"] = 1] = "closed";
	})(State || (State = {}));

	let state = State.opened;

	// We add this so it is not moved into module context
	state++;

	return [];
}

class Component extends SvelteComponent {
	constructor(options) {
		super();
		init(this, options, instance, null, safe_not_equal, {});
	}
}

export default Component;

Expected behavior
Should be built into something like this:

/* generated by Svelte v3.29.7 */
import { SvelteComponent, init, safe_not_equal } from "svelte/internal";

function instance($$self) {
	let state = 0;

	// We add this so it is not moved into module context
	state++;

	return [];
}

class Component extends SvelteComponent {
	constructor(options) {
		super();
		init(this, options, instance, null, safe_not_equal, {});
	}
}

export default Component;
@dkzlv dkzlv added the bug Something isn't working label Nov 20, 2020
@dummdidumm
Copy link
Member

Since this is a build error, the right place to file this would be svelte-preprocess since that does the TS->JS preprocessing.

The reason why this happens (I guess) is because the tsconfig contains isolatedModules: true so that each file can be transpiled independently. const enum needs to know the whole project though, so using it is not possible. To not error out, TypeScript's transpilation seems to fall back to generating a regular enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A user question
Projects
None yet
Development

No branches or pull requests

2 participants