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

fix terser webpack plugin compatibility #436

Merged
merged 6 commits into from
Feb 13, 2019

Conversation

Bobgy
Copy link
Contributor

@Bobgy Bobgy commented Jan 29, 2019

closes #426

@Bobgy
Copy link
Contributor Author

Bobgy commented Jan 29, 2019

I didn't update tests, but test is pulling in latest webpack by default. Seeing CIs passing should already prove the new version works with latest webpack without changing fixtures a little bit.

src/misc/get-uglify-plugin.js Outdated Show resolved Hide resolved
@Bobgy
Copy link
Contributor Author

Bobgy commented Feb 1, 2019

New behavior:

  1. when terser-webpack-plugin exists, it will use it instead of uglify-webpack-plugin
  2. when using terser-webpack-plugin, it will pass the same options from uglify-webpack-plugin to it

Copy link
Collaborator

@GGAlanSmithee GGAlanSmithee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments, mostly on syntax, a small logical change, happy with this PR overall though.

tests/legacy/fixtures/sw-minify-false/webpack.config.js Outdated Show resolved Hide resolved
tests/legacy/fixtures/sw-minify-auto/webpack.config.js Outdated Show resolved Hide resolved
src/service-worker.js Outdated Show resolved Hide resolved
@@ -82,28 +82,28 @@ export default class ServiceWorker {
};

if (this.minify === true) {
if (!UglifyJsPlugin) {
throw new Error('OfflinePlugin: uglifyjs-webpack-plugin is required to preform a minification')
if (!makeUglifyJsPlugin) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think makeUglifyJsPlugin !== null would be more explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to makeUglifyJsPlugin == null. I think that's what you mean here

@@ -1,5 +1,11 @@
const webpack = require('webpack');
let UglifyJsPlugin;
let isUsingTerser = false;

try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try {
  UglifyJsPlugin = require('terser-webpack-plugin');
  isUsingTerser = true;
} catch (e) {}

try {
  UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
} catch (e) {}

if (!UglifyJsPlugin) {
  try {
    UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  } catch (e) {}
}

Regarding this logic, depending on what optimization libs are present, terser-webpack-plugin will not be used. For instance, I do not know under what circumstances webpack.optimize.UglifyJsPlugin will be available, but given the current implementation, terser-webpack-plugin will have the least presidence, is this correct? Maybe explore a different solution, maybe something like:

try {
  UglifyJsPlugin = require('terser-webpack-plugin');
  isUsingTerser = true;

  if (!UglifyJsPlugin) {
    UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
  }

  if (!UglifyJsPlugin) {
    UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  }
} catch (e) {}

?

Alternatively, wrap this in a function with early exits. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, you are totally right. Let me fix accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't pull out the try catch blocks, because it was meant for catching exceptions thrown by require as expected for each require.

src/misc/get-uglify-plugin.js Outdated Show resolved Hide resolved
@GGAlanSmithee
Copy link
Collaborator

@NekR do you have time to check this? Will require a new version and npm deploy!

@Bobgy
Copy link
Contributor Author

Bobgy commented Feb 2, 2019

@GGAlanSmithee Thank you for the detailed CR. I will address the issues.

Copy link
Contributor Author

@Bobgy Bobgy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed issues accordingly. Let me know if there's anything else you've got some concerns

@@ -82,28 +82,28 @@ export default class ServiceWorker {
};

if (this.minify === true) {
if (!UglifyJsPlugin) {
throw new Error('OfflinePlugin: uglifyjs-webpack-plugin is required to preform a minification')
if (!makeUglifyJsPlugin) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to makeUglifyJsPlugin == null. I think that's what you mean here

src/service-worker.js Outdated Show resolved Hide resolved
@@ -1,5 +1,11 @@
const webpack = require('webpack');
let UglifyJsPlugin;
let isUsingTerser = false;

try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, you are totally right. Let me fix accordingly

@@ -1,5 +1,11 @@
const webpack = require('webpack');
let UglifyJsPlugin;
let isUsingTerser = false;

try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't pull out the try catch blocks, because it was meant for catching exceptions thrown by require as expected for each require.

@Bobgy
Copy link
Contributor Author

Bobgy commented Feb 3, 2019

it seems CI is failing on the same code that passed before. I don't know why. I will debug soon

@GGAlanSmithee
Copy link
Collaborator

@Bobgy Thanks for your continued work. I see that you reverted your last commit, so I guess there is still something you want to address? Will await further comments from you.

@Bobgy
Copy link
Contributor Author

Bobgy commented Feb 9, 2019

@GGAlanSmithee I made the revert just to retry CI and it was still failing. It seemed that latest webpack was broken at that time. Now I just removed the revert and CI is passing again. Please review if you have time.

It seems I messed up a little bit when rebasing. Fixing it right now.

@Bobgy
Copy link
Contributor Author

Bobgy commented Feb 9, 2019

OK, all issues fixed and CI is passing again

Copy link
Collaborator

@GGAlanSmithee GGAlanSmithee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bobgy

Thanks for your continued work on this PR, almost ready to merge this now. I've requested some additional changes, since it's the best time to do these now before this get merged. It's mostly a name change to signify that we might not be using UglifyJsPlugin, and some minor code change.

If possible, please do these changes. Othervise let me know if you don't have time and I can do a follow-up PR.

Thanks!

docs/FAQ.md Outdated Show resolved Hide resolved
lib/misc/get-uglify-plugin.js Outdated Show resolved Hide resolved
src/misc/get-uglify-plugin.js Outdated Show resolved Hide resolved
src/service-worker.js Outdated Show resolved Hide resolved
src/service-worker.js Outdated Show resolved Hide resolved
@Bobgy
Copy link
Contributor Author

Bobgy commented Feb 13, 2019

@GGAlanSmithee Thank you for your thoughtful comments.

@GGAlanSmithee GGAlanSmithee merged commit 66cde2c into NekR:master Feb 13, 2019
@GGAlanSmithee
Copy link
Collaborator

image

So green 💯

@Bobgy Bobgy deleted the terser_compatibility branch November 7, 2019 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for terser-webpack-plugin (webpack@4.26.0+)
2 participants