-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
.js in import of ESM #35
Comments
same bug typescript is not support esm but build package with esm..... export async function loadEsmModule<T>(modulePath: string|URL): Promise<T> {
const namespaceObject =
(await new Function('modulePath', `return import(modulePath);`)(modulePath));
// If it is not ESM then the values needed will be stored in the `default` property.
// TODO_ESM: This can be removed once `@angular/*` packages are ESM only.
if (namespaceObject.default) {
return namespaceObject.default;
} else {
return namespaceObject;
}
} like this but is not perfect |
FWIW, Node.js requires file extensions and it's likely required in a browser environment, too. Skipping them is really only a feature of various packagers. From "
Some more information on this can be read in the TypeScript 4.7 Beta announcement:
Basically, the suggested fix is to add
That would fix it for Node.js. I'm not sure if it would break stuff in e.g. Webpack. For now, I'm using a loader when using this package with Node.js. Use it like this: /**
* This module contains loader hooks to force loading `@material/material-color-utilities` as ECMAScript modules, even though its imports lack file endings.
*/
import { createRequire } from 'module';
const PACKAGE_NAME = '@material/material-color-utilities';
/**
* `resolve` loader hook to force loading `@material/material-color-utilities` as ECMAScript modules.
*
* The package's imports lack file endings (e.g. ".js"), so we need to resolve them ourselves. This requires a loader hook.
*
* @param specifier {string}
* @param context {{ conditions: string[], parentURL?: string }}
* @param defaultResolve {any}
* @returns {{ format: string, url: string }}
*/
export async function resolve(specifier, context, defaultResolve) {
const specifierInsidePackage =
context.parentURL && context.parentURL.includes(PACKAGE_NAME);
if (specifierInsidePackage) {
const require = createRequire(context.parentURL);
const url = new URL(require.resolve(specifier), 'file://').toString();
return {
format: 'module',
url,
};
}
return defaultResolve(specifier, context, defaultResolve);
} |
AFAICT, TypeScript emits imports in the same style as they appear in the source file. That means to fix this issue, you just need to append export * from './blend/blend.js';
export * from './hct/cam16.js';
export * from './hct/hct.js';
... |
When you get this typescript error:
Then as a workaround, pin the package to version 0.1.1 in your package.json, like this: {
"dependencies": {
"@material/material-color-utilities": "0.1.1"
}
} This workaround will be unnecessary when the fix from my previous comment is implemented and published on npm. |
If webpack is used, configuring
|
Hello, I wish to know if a pull request for this issue would be accepted anyway, given its chore-esque nature |
@immjs Hi. I don't know about that. You can check pr history for details. |
This command will append
Execute in project directory! if you want it to be done automatically after
EDIT: |
I am facing the same issue. I am using rollup for bundling. Can someone guide, how can I resolve this?
|
Hi all, since It replaces tsc with unbuild to fix this issue and support CJS, and it works fine for now. (only partially tested) feel free to use it! |
Is there any word from the Material Foundation team regarding this? |
Thanks for your patience. This will be fixed in the next release, closing |
I am using Yarn to download this package:
After fixing #29 I got these error:
When I checking
dist\index.js
:I made some changes to this:
After I adding
.js
to all imports. It works perfect to me.More info: export - JavaScript | MDN
The text was updated successfully, but these errors were encountered: