Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Error building with Webpack 2's UglifyJsPlugin #192

Closed
insin opened this issue Sep 15, 2016 · 13 comments
Closed

Error building with Webpack 2's UglifyJsPlugin #192

insin opened this issue Sep 15, 2016 · 13 comments

Comments

@insin
Copy link

insin commented Sep 15, 2016

I'm trying to run a --preact build while testing nwb's Webpack 2 upgrade and getting the following error:

ERROR  in vendor.db2641fb.js from UglifyJs
SyntaxError: Unexpected token: punc ()) [./~/preact-compat/src/index.js:31,0][ve
ndor.db2641fb.js:1557,24]

jsnext:main and module are pointing to src/index.js, which contains some untranspiled ES6 (which UglifyJS can't currently handle) - it's my understanding that the current approach which needs to be used with these package.json configs is to create a build which transpiles everything except ES6 module syntax back to ES5 and point to that.

@insin insin changed the title Error building with Webpack 2 Error building with Webpack 2's UglifyJsPlugin Sep 15, 2016
@developit
Copy link
Member

I think there is another open issue for this from @piuccio. Personally I think it's silly to only transpile ES Modules, we should be transpiring es2016 in its entirety since that's the spec :(

That said I do want to support this and I'm not wanting anyone to use es2015 in prod just because of me. We should merge that PR I think.

@ctrlplusb
Copy link
Contributor

FYI, temporary workaround in webpack config is to remove the 'jsnext:main' option from the mainFields. i.e.

    ...
    resolve: {
      mainFields: ['main', 'web'],
      ...

@piuccio
Copy link

piuccio commented Oct 5, 2016

I made it work turning on babel on node_modules/preact

@ctrlplusb
Copy link
Contributor

Aha thats much better! I assume you meant preact-compat though, and not preact. 😉

For clarities' sake, an example:

        {
          test: /\.jsx?$/,
          loader: 'babel-loader',
          include: [
            path.resolve(appRootPath, './src'),
            path.resolve(appRootPath, './node_modules/preact-compat'),
          ],
          query: {
            presets: [
              // JSX
              'react',
              // All the latest JS goodies, except for ES6 modules which
              // webpack has native support for.
              ['latest', { modules: false }],
            ],
          },
        },

@developit
Copy link
Member

@ctrlplusb going to test this out with Webpack 2 now :)

@piuccio
Copy link

piuccio commented Oct 5, 2016

Yeah something like that. I actually included all node modules and excluded the ones that don't work.

You might have to include proptypes as well, can't remember, sorry

@ctrlplusb
Copy link
Contributor

@developit 100% working. I have the config in my repo with all the latest goodies. :)

@astericky
Copy link

wow! Thanks for this solution @ctrlplusb . I still don't quite understand why it works yet but thanks.

@BANG88
Copy link

BANG88 commented Nov 22, 2016

Another option is using the build version,I try to replace React with Preact-Compat have the same issue as @insin , But i use TypeScript instead of babel ,It works well and my config is:

'alias': {
      'react': 'preact-compat/dist/preact-compat',
      'react-dom': 'preact-compat/dist/preact-compat'
 }

@rubencodes
Copy link

I came across this as well—solved using the "include" syntax mentioned above. I think it would be worthwhile to document this under Usage with Webpack—I could submit a pull request if it's helpful!

@developit
Copy link
Member

Readme would be okay, but I'd prefer to not require a workaround. Need to produce an ES Modules bundle and point modules at it.

@developit
Copy link
Member

FWIW I showed another solution to this on Twitter - it involves running babel-loader on modules from node_modules that declare jsnext:main/modules entrypoints. I'd still like to pre-bundle preact and preact-compat though, there is a size savings there.

@developit
Copy link
Member

Just realized this is a duplicate of #155, which was closed perhaps a little prematurely. Let's move discussion there :)

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants