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

Add NoEmitOnErrorsPlugin transformation #399

Merged
merged 3 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const uglifyJsPluginTransform = require("./uglifyJsPlugin/uglifyJsPlugin");
const loaderOptionsPluginTransform = require("./loaderOptionsPlugin/loaderOptionsPlugin");
const bannerPluginTransform = require("./bannerPlugin/bannerPlugin");
const extractTextPluginTransform = require("./extractTextPlugin/extractTextPlugin");
const noEmitOnErrorsPluginTransform = require("./noEmitOnErrorsPlugin/noEmitOnErrorsPlugin");
const removeDeprecatedPluginsTransform = require("./removeDeprecatedPlugins/removeDeprecatedPlugins");

const transformsObject = {
Expand All @@ -19,6 +20,7 @@ const transformsObject = {
loaderOptionsPluginTransform,
bannerPluginTransform,
extractTextPluginTransform,
noEmitOnErrorsPluginTransform,
removeDeprecatedPluginsTransform
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`noEmitOnErrorsPlugin transforms correctly using "noEmitOnErrorsPlugin-0" data 1`] = `
"module.export = {
optimizations: {
noEmitOnErrors: true
}
}
"
`;

exports[`noEmitOnErrorsPlugin transforms correctly using "noEmitOnErrorsPlugin-1" data 1`] = `
"module.export = {
optimizations: {
splitChunks: false,
noEmitOnErrors: true
},
plugins: [new Foo()]
}
"
`;

exports[`noEmitOnErrorsPlugin transforms correctly using "noEmitOnErrorsPlugin-2" data 1`] = `
"module.export = {
optimizations: {
noEmitOnErrors: true
},
plugins: [new Foo()]
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style = space
indent_size = 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.export = {
plugins: [
new webpack.NoEmitOnErrorsPlugin()
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.export = {
optimizations: {
splitChunks: false
},
plugins: [
new Foo(),
new webpack.NoEmitOnErrorsPlugin()
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.export = {
optimizations: {
noEmitOnErrors: false
},
plugins: [
new Foo(),
new webpack.NoEmitOnErrorsPlugin()
]
}
55 changes: 55 additions & 0 deletions lib/migrate/noEmitOnErrorsPlugin/noEmitOnErrorsPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const {
findPluginsByName,
safeTraverse
} = require("../../utils/ast-utils");

/**
*
* Transform for NoEmitOnErrorsPlugin. If found, removes the
* plugin and sets optimizations.noEmitOnErrors to true
*
* @param {Object} j - jscodeshift top-level import
* @param {Node} ast - jscodeshift ast to transform
* @returns {Node} ast - jscodeshift ast
*/
module.exports = function(j, ast) {
let rootPath;

// Remove old plugin
findPluginsByName(j, ast, ["webpack.NoEmitOnErrorsPlugin"])
Copy link
Contributor

@ematipico ematipico Apr 12, 2018

Choose a reason for hiding this comment

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

I noticed that this function is basically used also in the other PRs. I think it's worth creating on single function that does this. Regarding the rootPath, we could change the logic:

const removed = findAndRemovePlugin(j, ast, ["webpack.NoEmitOnErrorsPlugin"]);
// if removed, means that was found
if (removed) {
  const rootPath = safeTraverse(path, ["parent", "parent", "parent", "value"]) // need to change the depth of the path obviusly
}

Ideally you could create on single PR to tackle all the transformers in one go so they can use the same utility all together. Or we could change one PR, merge it to master and you will rebase the others accordingly. What do you think? What's best for you

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll combine them all into this PR if that works for you. Seems like the easiest approach. On it now, thanks for the review.

.filter(path => safeTraverse(path, ["parent", "value"]))
.forEach(path => {
rootPath = safeTraverse(path, ["parent", "parent", "parent", "value"]);
const arrayPath = path.parent.value;
if (arrayPath.elements && arrayPath.elements.length === 1) {
j(path.parent.parent).remove();
} else {
j(path).remove();
}
});

// Set new optimizations option
if (rootPath) {
const optimizationsExist = ast.find(j.Property).filter(path => path.node.key.name === "optimizations").size() > 0;

if (optimizationsExist) {
rootPath.properties.filter(path => path.key.name === "optimizations")
.forEach(path => {
const newProperties = path.value.properties.filter(path => path.key.name !== "noEmitOnErrors");
newProperties.push(j.objectProperty(j.identifier("noEmitOnErrors"), j.booleanLiteral(true)));
path.value.properties = newProperties;
});
} else {
rootPath.properties.push(
j.objectProperty(
j.identifier("optimizations"),
j.objectExpression([
j.objectProperty(j.identifier("noEmitOnErrors"), j.booleanLiteral(true))
])
)
);
}
}

return ast;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

const defineTest = require("../../utils/defineTest");

defineTest(__dirname, "noEmitOnErrorsPlugin", "noEmitOnErrorsPlugin-0");
defineTest(__dirname, "noEmitOnErrorsPlugin", "noEmitOnErrorsPlugin-1");
defineTest(__dirname, "noEmitOnErrorsPlugin", "noEmitOnErrorsPlugin-2");