-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Main inside package.json is referencing to es6 modules #929
Comments
[...]
"module": "lib/es/index.js", // for ES modules
"main": "lib/cjs/index.js", // for CommonJS modules
[...] |
I had this same problem several time.
This worked, but I didn't get very far, cause the same issue is currently beating me in my unit tests (where I don't use webpack, cause I don't like too much none of the suggested setup https://github.com/avajs/ava/blob/master/docs/recipes/precompiling-with-webpack.md).
With this configuration my own source files get transpiled, but not node_modules, cause babel default is ignoring them. I would like to know if you plan to modify your package.json as suggested above, or if you have any suggestions/work around for this case. Thank you |
Temporarily use this. import { __moduleExports as mdRipple } from '@material/ripple/dist/mdc.ripple';
[].forEach.call(
document.querySelectorAll('.mdc-button'),
ripple => mdRipple.MDCRipple.attachTo(ripple)
); |
That worked (almost)... since I was already transpiling my own sources, I had just to modify the import like this: import { Ripple } from '@material/ripple/dist/mdc.ripple'; Still, looking forward for a better solution, that allows to remove the ugly import. |
For me the only option that works at the moment is the one @Shyam-Chen suggests, I made a list of possible imports and those are the results: import imports from '@material/textfield/dist/mdc.textfield';
// which is equivalent to import {default as imports } from '@material/textfield/dist/mdc.textfield';
imports = undefined
import { MDCTextfield } from '@material/textfield/dist/mdc.textfield';
// Non-existent export 'MDCTextField' is imported from...
import * as imports from '@material/textfield/dist/mdc.textfield';
imports = {
default: undefined,
__moduleExports: {
MDCTextfield,
MDCTextfieldFoundation
}
}
import * as imports from '@material/textfield';
imports = {
MDCTextfield,
MDCTextfieldFoundation
} Don't know why is webpack placing the exports under __moduleExports or if that is the normal behaviour though. |
I can't tell you how long I spent trying to figure out all of the UglifyJS errors I was getting because of this. I'm running my JS through the babel-loader in webpack and have it excluding At the very least I think this needs to be documented unless I've been searching in all the wrong places. I'm currently using a similar approach to @brunoscopelliti and it seems to be working. UglifyJS isn't choking on ES6+ syntax found in my bundle at least. Here's the babel-loader rule for my config so far. I'm using webpack for my tests so I haven't run into the same issues as @brunoscopelliti on that front.
|
Well if someone is wondering how to solve this directly with Babel I use the include option (it cannot be used with exclude, everything not included will be excluded). babel({
include: [
'node_modules/@material/**',
'src/**'
]
}) |
Being bound to Laravel Mix I can't follow the babel/include way to include material-components-web into my js, after some tough headaches in the meantime I workarounded including the whole dists like
Still following for a less hackish solution ;) |
I'd just like to +1 my support recommending the change to the
It's a pretty common pattern across just about every other repository you can find on NPM. I'm not sure why it's not the case here. Some examples can be found in my duplicate issue. |
Also +1 on this. By referencing ES6+ code in the |
Is there any movement on this? I can open a PR if there is consensus on what should happen. |
In case someone is having problems with {
"transform": {
"^.+\\.jsx$": "babel-jest",
"^.+\\.js$": "babel-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!@material)"
],
} |
I did this:
But I'm still facing the following error:
Any advice about how would I to fix that? |
+1 to getting this fixed. The general convention these days is to have package.json's There's really no current spec for exposing untranspiled es6+ code in a standard way. There's some movement around a In the current state, folks that want to use the core modules have to set up transpilation to use them, which opens the door to version mismatches and configuration mismatches and bugs. |
Bug Main inside package.json is referencing to es6 modules
What MDC-Web Version are you using?
This happens with all components which have different versions, for instance the TextField is in version 0.3.1
What are the steps to reproduce the bug?
npm init
node_modules/@material/textfield
What is the expected behavior?
The "main" property inside the package.json should reference to the transpiled and bundled module instead of the es6 version so developers don't have to transpile it in order to avoid incompatibilities.
For example the TextField component should be pointing to dist/mdc.textfield.min.js instead of index.js.
There's actually another problem because the dist versions are exporting the utilities differently. The following snippet should work equally in the original and the minified/transpiled version:
but using the later i get the following error:
'MDCTextfield' is not exported by 'node_modules@material\textfield\dist\mdc.textfield.min.js'
What is the actual behavior?
The "main" is pointing to index.js which contains the es6 module, and if I don't transpile it some things won't work, for example I cannot use Uglify.
(uglify plugin) Error transforming bundle with 'uglify' plugin: Unexpected token: name (MDCFoundation)
That's because it finds MDCFoundation defined as a class.
Any other information you believe would be useful?
There is a proposal to specify two entry points or main references inside package.json:
For more information refer to this issue: jsforum/jsforum#5
The text was updated successfully, but these errors were encountered: