Skip to content

webpack 4.0.0-beta.0 different behavior with .mjs files #6459

Closed

Description

@artemxgruden

Do you want to request a feature or report a bug?
I want to report unexpected behavior

What is the current behavior?

file: /server/main.mjs - with --experimental-modules flag

import * as React from 'react'
import * as ReactDOM from 'react-dom'

import * as Redux from 'redux'
import * as ReactRedux from 'react-redux'

console.log( 'React.createElement', React.createElement ? true : false )
console.log( 'React.default.createElement', React.default && React.default.createElement ? true : false )
console.log( 'React.Component', React.Component ? true : false )
console.log( 'React.default.Component', React.default && React.default.Component ? true : false )
console.log( 'ReactDOM.hydrate', ReactDOM.hydrate ? true : false )
console.log( 'ReactDOM.default.hydrate', ReactDOM.default && ReactDOM.default.hydrate ? true : false )
console.log( 'Redux.createStore', Redux.createStore ? true : false )
console.log( 'Redux.default.createStore', Redux.default && Redux.default.createStore ? true : false )
console.log( 'ReactRedux.connect', ReactRedux.connect ? true : false )
console.log( 'ReactRedux.default.connect', ReactRedux.default && ReactRedux.default.connect ? true : false )

output:

React.createElement false
React.default.createElement true
React.Component false
React.default.Component true
ReactDOM.hydrate false
ReactDOM.default.hydrate true
Redux.createStore false
Redux.default.createStore true
ReactRedux.connect false
ReactRedux.default.connect true

file: /client/main.mjs

import * as React from 'react'
import * as ReactDOM from 'react-dom'

import * as Redux from 'redux'
import * as ReactRedux from 'react-redux'

console.log( 'React.createElement', React.createElement ? true : false )
console.log( 'React.default.createElement', React.default && React.default.createElement ? true : false )
console.log( 'React.Component', React.Component ? true : false )
console.log( 'React.default.Component', React.default && React.default.Component ? true : false )
console.log( 'ReactDOM.hydrate', ReactDOM.hydrate ? true : false )
console.log( 'ReactDOM.default.hydrate', ReactDOM.default && ReactDOM.default.hydrate ? true : false )
console.log( 'Redux.createStore', Redux.createStore ? true : false )
console.log( 'Redux.default.createStore', Redux.default && Redux.default.createStore ? true : false )
console.log( 'ReactRedux.connect', ReactRedux.connect ? true : false )
console.log( 'ReactRedux.default.connect', ReactRedux.default && ReactRedux.default.connect ? true : false )

output:

React.createElement false
React.default.createElement true
React.Component false
React.default.Component true
ReactDOM.hydrate false
ReactDOM.default.hydrate true
Redux.createStore true
Redux.default.createStore false
ReactRedux.connect true
ReactRedux.default.connect false

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

I expect what webpack will give the same result as node

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

webpack config for /client/main.mjs:

const CleanObsoleteChunks = require( 'webpack-clean-obsolete-chunks' )

module.exports = ( { NODE_ENV, PORT, VIEW_TYPE, DATA_PROVIDER, ASSETS_PROVIDER_CLIENT_FUNCTIONS_BASE_HREF }, webpack, joinPath ) => ( {
  entry: {
    main: joinPath( `./client/${ VIEW_TYPE }/main.mjs` ),
    vendor: [ 
      'react',
      'prop-types',
      'react-dom',
      'react-redux',
      'redux',
      'loadable-components',
      'styled-components',
      'gsap',
      'smooth-scrolling',
      'dom-classes'
    ]
  },
  target: 'web',
  output: {
    path: joinPath( `./../assets/functions/${ VIEW_TYPE }` ),
    filename: '[name].js',
    chunkFilename: '[chunkhash].main.js',
    publicPath: `${ ASSETS_PROVIDER_CLIENT_FUNCTIONS_BASE_HREF }/${ VIEW_TYPE }/`,
  },
  resolve: {
    modules: [
      'node_modules',
    ],
    extensions: [ '.mjs', '.js', '.json', '.jsx', '.css' ],
  },
  devtool: 'source-map',
  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: {
          name: 'vendor',
          enforce: true
        }
      }
    }
  },
  plugins: [
    new webpack.DefinePlugin( {
      'process.env': {
        'NODE_ENV': JSON.stringify( NODE_ENV ),
        'PORT': JSON.stringify( PORT ),
        'RUNNING_ENV': JSON.stringify( 'CLIENT' ),
        'DATA_PROVIDER': JSON.stringify( DATA_PROVIDER ),
        'VIEW_TYPE': JSON.stringify( VIEW_TYPE )
      }
    } ),
    new CleanObsoleteChunks( {
      verbose: true,
      deep: true
    } )
  ]
} )

webpack output

Version: webpack 4.0.0-beta.0
Child
    Hash: cd8e6279d4dca2229059
    Time: 1738ms
            Asset      Size  Chunks                    Chunk Names
          main.js   735 KiB    main  [emitted]  [big]  main
        vendor.js  1.51 MiB  vendor  [emitted]  [big]  vendor
      main.js.map   830 KiB    main  [emitted]         main
    vendor.js.map  1.75 MiB  vendor  [emitted]         vendor
    Entrypoint main [big] = main.js main.js.map
    Entrypoint vendor [big] = vendor.js vendor.js.map
       [0] multi react prop-types react-dom react-redux redux styled-components gsap invisible-rrs invisible-httprps smooth-scrolling dom-classes 148 bytes {vendor} [built]
       [1] util (ignored) 15 bytes {vendor} [built]
    [./functions/client/large/main.mjs] 1010 bytes {main} [built]
    [./node_modules/webpack/buildin/amd-options.js] (webpack)/buildin/amd-options.js 82 bytes {vendor} [built]
    [./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 509 bytes {vendor} {main} [built]
    [./node_modules/webpack/buildin/harmony-module.js] (webpack)/buildin/harmony-module.js 596 bytes {vendor} {main} [built]
    [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 517 bytes {vendor} [built]
        + 128 hidden modules

    ERROR in ./functions/client/large/main.mjs
    11:33-49 Can't import the named export 'hydrate' from non EcmaScript module (only default export is available)
     @ ./functions/client/large/main.mjs

    ERROR in ./functions/client/large/main.mjs
    14:42-55 "export 'default' (imported as 'Redux') was not found in 'redux'
     @ ./functions/client/large/main.mjs

    ERROR in ./functions/client/large/main.mjs
    14:59-72 "export 'default' (imported as 'Redux') was not found in 'redux'
     @ ./functions/client/large/main.mjs

    ERROR in ./functions/client/large/main.mjs
    16:43-61 "export 'default' (imported as 'ReactRedux') was not found in 'react-redux'
     @ ./functions/client/large/main.mjs

    ERROR in ./functions/client/large/main.mjs
    16:65-83 "export 'default' (imported as 'ReactRedux') was not found in 'react-redux'
     @ ./functions/client/large/main.mjs

    ERROR in ./functions/client/large/main.mjs
    7:36-55 Can't import the named export 'createElement' from non EcmaScript module (only default export is available)
     @ ./functions/client/large/main.mjs

    ERROR in ./functions/client/large/main.mjs
    9:32-47 Can't import the named export 'Component' from non EcmaScript module (only default export is available)
     @ ./functions/client/large/main.mjs

dependencies

"devDependencies": {
    "webpack": "^4.0.0-beta.0",
    "webpack-clean-obsolete-chunks": "^0.3.0",
    "webpack-cli": "^2.0.4"
  },
  "dependencies": {
    "croppie": "^2.5.1",
    "cross-env": "^5.1.1",
    "gsap": "^1.20.3",
    "luxon": "^0.2.7",
    "pino": "^4.10.2",
    "platform": "^1.3.4",
    "prop-types": "^15.6.0",
    "react": "^16.1.0",
    "react-dom": "^16.1.0",
    "react-google-recaptcha": "^0.9.9",
    "react-redux": "^5.0.6",
    "redux": "^4.0.0-beta.1",
    "smooth-scrolling": "^2.3.9",
    "styled-components": "^3.1.6",
    "ua-device": "^0.1.10"
  }

nodejs - 9.5.0

Activity

  1. sokra commented on Feb 9, 2018

    @sokra
    Member

    the redux packages are actual ESM modules but in .js files.

    // redux/package.json
    ...
      "module": "es/index.js",
    ...

    webpack supports this format as ESM, but node.js doesn't.

  2. added 2 commits that reference this issue on Feb 9, 2018
    85ff550
    0f88f8d
  3. sokra commented on Feb 10, 2018

    @sokra
    Member

    fixed in 4.0.0-beta.1

  4. pleunv commented on Feb 23, 2018

    @pleunv

    I'm running into similar issues with graphql-js (imported through graphiql). graphql-js exports both an index.js and index.mjs but with different named exports. I think GraphiQL is expecting the js file but the mjs one is given precedence, leading to a bunch of errors like the following:

    ERROR in ./node_modules/graphql/index.mjs
    88:0-148:42 Can't reexport the named export 'valueFromAST' from non EcmaScript module (only default export is available)
     @ ./node_modules/graphql/index.mjs
     @ ./node_modules/graphiql/dist/utility/introspectionQueries.js
     @ ./node_modules/graphiql/dist/components/GraphiQL.js
     @ ./node_modules/graphiql/dist/index.js
     @ ./src/features/debug/components/GraphiQLView/GraphiQLView.jsx
     @ ./src/features/debug/components/GraphiQLView/index.js
     @ ./src/features/debug/Router.jsx
     @ ./src/features/configuration/Router.jsx
     @ ./src/components/App/App.jsx
     @ ./src/components/App/index.js
     @ ./src/containers/AppContainer.jsx
     @ ./src/index.js
    

    I've confirmed this issue goes away if I remove the index.mjs from the graphql module dir.
    Is this a library issue that should be taken up with graphiql/graphql-js?

    Let me know if you want me to open up a new issue for this.

  5. montogeek commented on Feb 26, 2018

    @montogeek
    Contributor

    @pleunv Please do

  6. pie6k commented on Mar 3, 2018

    @pie6k

    I'm having the same issue as @pleunv .

    ERROR in ./node_modules/graphql/index.mjs
    2:0-49 Can't reexport the named export 'graphql' from non EcmaScript module (only default export is available)
     @ ./node_modules/graphql/index.mjs
     @ ./src/domains/enum/index.ts
     @ ./src/domains/index.ts
     @ ./src/index.ts
    
  7. deftomat commented on Mar 4, 2018

    @deftomat

    The same as @pie6k

  8. jonmanzo commented on Mar 15, 2018

    @jonmanzo

    @pie6k @deftomat
    Workaround that issue with this:

    resolve:{ 
       alias: {
          graphql$: path.resolve(__dirname, 'node_modules/graphql/index.js'),
       }
    }
    
  9. deftomat commented on Mar 16, 2018

    @deftomat

    @jonmanzo
    Thanks. I've temporarily solved it by downgrading a graphql to v0.13.0 as mentioned here.

  10. dashmug commented on Mar 16, 2018

    @dashmug

    @deftomat @jonmanzo's workaround is better as that gives the latest graphql. This way, you can use both latest webpack and latest graphql.

  11. deftomat commented on Mar 16, 2018

    @deftomat

    @dashmug
    Yes, I know. I just don't want to pollute my webpack config as I'm hoping that they will fix it in a few weeks. Then, as I'm using yarn upgrade-interactive --latest from time to time, it will be fixed.
    I'm pretty sure that I will forget to remove alias from webpack config 🙂

  12. jsonnull commented on Mar 16, 2018

    @jsonnull

    @dashmug I don't consider that workaround better because it requires me to add lines of code/configuration to my project, which I have to maintain. I'd much rather subscribe to an issue to learn when there's a permanent fix in graphql. That it is a better workaround in your opinion is perhaps a better phrasing of your sentiment.

  13. jonmanzo commented on Mar 16, 2018

    @jonmanzo

    @jsonnull It's not an issue in GraphQL, but rather Webpack acting as it is intended (and documented
    resolve.mainFields) by prioritizing module over main from the package.json of the dependency based on your target.

    The better webpack config entry (for Node) would be to alter the default prioritization with:

    resolve:{
       mainFields: ["main", "module"]
    }
    
  14. sandangel commented on Mar 18, 2018

    @sandangel

    @jonmanzo thank you. much cleaner than previous suggest when the webpack config file is not in the project root.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions