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

add non-es6-compat to importSync #1076

Merged
merged 5 commits into from
Jan 14, 2022
Merged

add non-es6-compat to importSync #1076

merged 5 commits into from
Jan 14, 2022

Conversation

ef4
Copy link
Contributor

@ef4 ef4 commented Jan 13, 2022

importSync should do the same thing that regular static import does when you use it against as non-es-module.

importSync should do the same thing that regular static import does when you use it against as non-es-module.
@ef4
Copy link
Contributor Author

ef4 commented Jan 13, 2022

Last failures here are some tests that need to be updated to expect this newer format.

But I'm thinking I'll take a stab at sharing this new compat code, rather than inlining it everywhere. It's tiny, but we use it a lot.

@ef4 ef4 merged commit 0201b62 into master Jan 14, 2022
@ef4 ef4 deleted the import-sync-compat branch January 14, 2022 02:28
@ef4 ef4 added the bug Something isn't working label Jan 14, 2022
chancancode added a commit to tildeio/embroider that referenced this pull request Jul 13, 2023
`importSync()` is intended to work like `import()` (buy sync),
which, in turns, is expected to work like a namespace import –
`import * as NS from "foo";`. In general, we would expect that to
be interchangable with `const NS = importSync("foo");`.

Prior to this commit, this is not true for commonjs packages:

```js
import * as QUnit1 from "qunit";
// => { QUnit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit");
// => { default: { QUnit, assert, begin, config, ... } }

QUnit2.assert // => undefined
```

Of course, using ES imports on commonjs packages is, in itself, an
ill-defined concept, but nevertheless, it is a very common interop
requirement.

The previous behavior is introduced in embroider-build#1076. The intent appears to
be fixing a different interop issue, where we would expect these to
work the same way:

```js
import QUnit1 from "qunit";
// => { Qunit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit").default;
// => { QUnit, assert, begin, config, ... }

QUnit2.assert // => function
```

The fix in embroider-build#1076, however, broke the exepctation that `import()`
should behave like a namespace improt statement for CJS modules.

This commit attempts to satisfy both of these requirements, with
one caveat: `importSync("foo").default` is present on the shimed
CJS modules, but in `import * as FOO from "foo";`, `FOO.default`
is undefined.

Arguably, this is a bug in webpack's implementation – if you are
able to write an import statement that references the default
export, you should be able to do the same in the namespace import
as per the spec.

So, the implementation here is different but more correct.
chancancode added a commit to chancancode/embroider that referenced this pull request Jul 13, 2023
`importSync()` is intended to work like `import()` (buy sync),
which, in turns, is expected to work like a namespace import –
`import * as NS from "foo";`. In general, we would expect that to
be interchangable with `const NS = importSync("foo");`.

Prior to this commit, this is not true for commonjs packages:

```js
import * as QUnit1 from "qunit";
// => { QUnit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit");
// => { default: { QUnit, assert, begin, config, ... } }

QUnit2.assert // => undefined
```

Of course, using ES imports on commonjs packages is, in itself, an
ill-defined concept, but nevertheless, it is a very common interop
requirement.

The previous behavior is introduced in embroider-build#1076. The intent appears to
be fixing a different interop issue, where we would expect these to
work the same way:

```js
import QUnit1 from "qunit";
// => { Qunit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit").default;
// => { QUnit, assert, begin, config, ... }

QUnit2.assert // => function
```

The fix in embroider-build#1076, however, broke the exepctation that `import()`
should behave like a namespace improt statement for CJS modules.

This commit attempts to satisfy both of these requirements, with
one caveat: `importSync("foo").default` is present on the shimed
CJS modules, but in `import * as FOO from "foo";`, `FOO.default`
is undefined.

Arguably, this is a bug in webpack's implementation – if you are
able to write an import statement that references the default
export, you should be able to do the same in the namespace import
as per the spec.

So, the implementation here is different but more correct.
chancancode added a commit to chancancode/embroider that referenced this pull request Jul 13, 2023
`importSync()` is intended to work like `import()` (buy sync),
which, in turns, is expected to work like a namespace import –
`import * as NS from "foo";`. In general, we would expect that to
be interchangable with `const NS = importSync("foo");`.

Prior to this commit, this is not true for commonjs packages:

```js
import * as QUnit1 from "qunit";
// => { QUnit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit");
// => { default: { QUnit, assert, begin, config, ... } }

QUnit2.assert // => undefined
```

Of course, using ES imports on commonjs packages is, in itself, an
ill-defined concept, but nevertheless, it is a very common interop
requirement.

The previous behavior is introduced in embroider-build#1076. The intent appears to
be fixing a different interop issue, where we would expect these to
work the same way:

```js
import QUnit1 from "qunit";
// => { Qunit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit").default;
// => { QUnit, assert, begin, config, ... }

QUnit2.assert // => function
```

The fix in embroider-build#1076, however, broke the exepctation that `import()`
should behave like a namespace improt statement for CJS modules.

This commit attempts to satisfy both of these requirements, with
one caveat: `importSync("foo").default` is present on the shimed
CJS modules, but in `import * as FOO from "foo";`, `FOO.default`
is undefined.

Arguably, this is a bug in webpack's implementation – if you are
able to write an import statement that references the default
export, you should be able to do the same in the namespace import
as per the spec.

So, the implementation here is different but more correct.
chancancode added a commit to chancancode/embroider that referenced this pull request Jul 13, 2023
`importSync()` is intended to work like `import()` (buy sync),
which, in turns, is expected to work like a namespace import –
`import * as NS from "foo";`. In general, we would expect that to
be interchangable with `const NS = importSync("foo");`.

Prior to this commit, this is not true for commonjs packages:

```js
import * as QUnit1 from "qunit";
// => { QUnit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit");
// => { default: { QUnit, assert, begin, config, ... } }

QUnit2.assert // => undefined
```

Of course, using ES imports on commonjs packages is, in itself, an
ill-defined concept, but nevertheless, it is a very common interop
requirement.

The previous behavior is introduced in embroider-build#1076. The intent appears to
be fixing a different interop issue, where we would expect these to
work the same way:

```js
import QUnit1 from "qunit";
// => { Qunit, assert, begin, config, ... }

QUnit1.assert // => function

const QUnit2 = importSync("qunit").default;
// => { QUnit, assert, begin, config, ... }

QUnit2.assert // => function
```

The fix in embroider-build#1076, however, broke the exepctation that `import()`
should behave like a namespace improt statement for CJS modules.

This commit attempts to satisfy both of these requirements, with
one caveat: `importSync("foo").default` is present on the shimed
CJS modules, but in `import * as FOO from "foo";`, `FOO.default`
is undefined.

Arguably, this is a bug in webpack's implementation – if you are
able to write an import statement that references the default
export, you should be able to do the same in the namespace import
as per the spec.

So, the implementation here is different but more correct.

Fixes embroider-build#1530
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant