-
-
Notifications
You must be signed in to change notification settings - Fork 633
Update extractTextPlugin transformation #390
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
Merged
ematipico
merged 6 commits into
webpack:master
from
bitpshr:feature/v3-v4-extract-migrate
Apr 18, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e58f6b
ast(migrate): update extractTextPlugin transformation v3 -> v4
bitpshr f7a8969
ast(migrate): update extractTextPlugin transformation rules
bitpshr 48f0ddf
ast(migrate): add utilities to ast-utils
bitpshr 2f4d8ef
ast(migrate): add more jsdoc for ast-utils
bitpshr 6f3402d
ast(migrate): add more jsdoc for ast-utils
bitpshr 9d04a70
ast(migrate): extract logic into ast-utils
bitpshr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 56 additions & 8 deletions
64
lib/migrate/extractTextPlugin/__snapshots__/extractTextPlugin.test.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/migrate/extractTextPlugin/__testfixtures__/extractTextPlugin-0.input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | ||
|
||
module.export = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: ExtractTextPlugin.extract({ | ||
use: 'css-loader', | ||
fallback: ['style-loader', { loader: 'test-object-loader' }] | ||
}) | ||
} | ||
] | ||
}, | ||
plugins: [ | ||
new Foo(), | ||
new ExtractTextPlugin({ | ||
id: 'foo', | ||
filename: 'foo.css', | ||
allChunks: true, | ||
disable: false, | ||
ignoreOrder: true | ||
}) | ||
] | ||
} |
25 changes: 25 additions & 0 deletions
25
lib/migrate/extractTextPlugin/__testfixtures__/extractTextPlugin-1.input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | ||
|
||
module.export = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: ['css-hot-loader'].concat(ExtractTextPlugin.extract({ | ||
use: 'css-loader', | ||
fallback: 'style-loader' | ||
})) | ||
} | ||
] | ||
}, | ||
plugins: [ | ||
new Foo(), | ||
new ExtractTextPlugin({ | ||
id: 'foo', | ||
filename: 'foo.css', | ||
allChunks: true, | ||
disable: false, | ||
ignoreOrder: true | ||
}) | ||
] | ||
} |
30 changes: 30 additions & 0 deletions
30
lib/migrate/extractTextPlugin/__testfixtures__/extractTextPlugin-2.input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | ||
|
||
module.export = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: [ | ||
'foo-loader', | ||
ExtractTextPlugin.extract({ | ||
use: 'css-loader', | ||
fallback: 'style-loader', | ||
publicPath: 'public' | ||
}), | ||
'bar-loader' | ||
] | ||
} | ||
] | ||
}, | ||
plugins: [ | ||
new Foo(), | ||
new ExtractTextPlugin({ | ||
id: 'foo', | ||
filename: 'foo.css', | ||
allChunks: true, | ||
disable: false, | ||
ignoreOrder: true | ||
}) | ||
] | ||
} |
16 changes: 0 additions & 16 deletions
16
lib/migrate/extractTextPlugin/__testfixtures__/extractTextPlugin.input.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,97 @@ | ||
const utils = require("../../utils/ast-utils"); | ||
const { | ||
addConditionalLoader, | ||
addRequire, | ||
createPluginByName, | ||
findLoaderFunction, | ||
getLoaderOptionValue, | ||
getPluginOptionValue, | ||
pathToArray, | ||
removePluginByName, | ||
removeRequire | ||
} = require("../../utils/ast-utils"); | ||
|
||
/** | ||
* | ||
* Check whether `node` is the invocation of the plugin denoted by `pluginName` | ||
* | ||
* @param {Object} j - jscodeshift top-level import | ||
* @param {Node} node - ast node to check | ||
* @param {String} pluginName - name of the plugin | ||
* @returns {Boolean} isPluginInvocation - whether `node` is the invocation of the plugin denoted by `pluginName` | ||
*/ | ||
|
||
function findInvocation(j, node, pluginName) { | ||
let found = false; | ||
found = | ||
j(node) | ||
.find(j.MemberExpression) | ||
.filter(p => p.get("object").value.name === pluginName) | ||
.size() > 0; | ||
return found; | ||
} | ||
|
||
/** | ||
* | ||
* Transform for ExtractTextPlugin arguments. Consolidates arguments into single options object. | ||
* Transform for ExtractTextPlugin. Replaces it with MiniCssExtractPlugin and translates | ||
* configuration where possible. | ||
* | ||
* @param {Object} j - jscodeshift top-level import | ||
* @param {Node} ast - jscodeshift ast to transform | ||
* @returns {Node} ast - jscodeshift ast | ||
*/ | ||
module.exports = function transformExtractTextPlugin(j, ast) { | ||
const devLoaders = []; | ||
const prodLoaders = []; | ||
|
||
let elements; | ||
let loaderIndex; | ||
let loaderPath; | ||
let newElements; | ||
let pluginExists; | ||
let requireExists; | ||
|
||
const filename = getPluginOptionValue(j, ast, "ExtractTextPlugin", "filename"); | ||
const publicPath = getLoaderOptionValue(j, ast, "ExtractTextPlugin", "publicPath"); | ||
const currentFallbackLoader = getLoaderOptionValue(j, ast, "ExtractTextPlugin", "fallback"); | ||
const currentLoader = getLoaderOptionValue(j, ast, "ExtractTextPlugin", "use"); | ||
|
||
module.exports = function(j, ast) { | ||
const changeArguments = function(p) { | ||
const args = p.value.arguments; | ||
|
||
const literalArgs = args.filter(p => utils.isType(p, "Literal")); | ||
if (literalArgs && literalArgs.length > 1) { | ||
const newArgs = j.objectExpression( | ||
literalArgs.map((p, index) => | ||
utils.createProperty(j, index === 0 ? "fallback" : "use", p.value) | ||
) | ||
); | ||
p.value.arguments = [newArgs]; | ||
const loaderOptions = filename || publicPath ? {} : null; | ||
filename && Object.assign(loaderOptions, { filename }); | ||
publicPath && Object.assign(loaderOptions, { publicPath: publicPath.value }); | ||
|
||
// Remove require for old plugin | ||
requireExists = removeRequire(j, ast, "ExtractTextPlugin"); | ||
|
||
// Add require for new plugin | ||
requireExists && addRequire(j, ast, "MiniCssExtractPlugin", "mini-css-extract-plugin"); | ||
|
||
// Remove old plugin | ||
pluginExists = removePluginByName(j, ast, "ExtractTextPlugin"); | ||
|
||
// Add new plugin | ||
pluginExists && createPluginByName(j, ast, "MiniCssExtractPlugin", loaderOptions); | ||
|
||
// Remove old loader | ||
const loaderCallPath = findLoaderFunction(j, ast, "ExtractTextPlugin"); | ||
if (loaderCallPath) { | ||
loaderPath = loaderCallPath.parent.value; | ||
loaderPath.elements && (elements = [...loaderPath.elements]); | ||
j(loaderCallPath).remove(); | ||
} | ||
|
||
// Build loader arrays | ||
currentLoader && pathToArray(currentLoader, switchedLoader => { | ||
const loader = j.memberExpression(j.identifier("MiniCssExtractPlugin"), j.identifier("loader"), false); | ||
prodLoaders.push(loader, ...switchedLoader); | ||
currentFallbackLoader && pathToArray(currentFallbackLoader, switchedFallbackLoader => { | ||
devLoaders.push(...switchedFallbackLoader, ...switchedLoader); | ||
}); | ||
}); | ||
|
||
// Add new loader | ||
const loader = currentFallbackLoader ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be another util, |
||
addConditionalLoader(j, prodLoaders, devLoaders) : | ||
j.arrayExpression(prodLoaders); | ||
|
||
if (loaderPath) { | ||
switch (loaderPath.type) { | ||
case "CallExpression": | ||
loaderPath.arguments = [loader]; | ||
break; | ||
case "Property": | ||
loaderPath.value = loader; | ||
break; | ||
case "ArrayExpression": | ||
loaderIndex = elements.findIndex(e => e.callee && e.callee.object.name === "ExtractTextPlugin"); | ||
newElements = [ | ||
...elements.slice(0, loaderIndex), | ||
loader, | ||
...elements.slice(loaderIndex + 1) | ||
]; | ||
loaderPath.elements = newElements; | ||
break; | ||
} | ||
return p; | ||
}; | ||
const name = utils.findVariableToPlugin( | ||
j, | ||
ast, | ||
"extract-text-webpack-plugin" | ||
); | ||
if (!name) return ast; | ||
|
||
return ast | ||
.find(j.CallExpression) | ||
.filter(p => findInvocation(j, p, name)) | ||
.forEach(changeArguments); | ||
} | ||
|
||
return ast; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you extract these into more functions that could be reused?