-
-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ResolveLoader distinguished from resolve
- Loading branch information
1 parent
7c713ce
commit 3c90e83
Showing
7 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
6 changes: 6 additions & 0 deletions
6
lib/init/transformations/resolveLoader/__testfixtures__/resolveLoader-0.input.js
This file contains 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,6 @@ | ||
module.exports = { | ||
entry: 'index.js', | ||
output: { | ||
filename: 'bundle.js' | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
lib/init/transformations/resolveLoader/__testfixtures__/resolveLoader-1.input.js
This file contains 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,9 @@ | ||
module.exports = { | ||
entry: 'index.js', | ||
output: { | ||
filename: 'bundle.js' | ||
}, | ||
resolveLoader: { | ||
moduleExtensions: [ '-loader' ] | ||
} | ||
} |
This file contains 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,96 @@ | ||
"use strict"; | ||
|
||
const utils = require("../../../utils/ast-utils"); | ||
|
||
/** | ||
* | ||
* Transform for resolveLoader. Finds the resolveLoader property from yeoman and creates a | ||
* property based on what the user has given us. | ||
* | ||
* @param j — jscodeshift API | ||
* @param ast - jscodeshift API | ||
* @param {any} webpackProperties - transformation object to scaffold | ||
* @param {String} action - action that indicates what to be done to the AST | ||
* @returns ast - jscodeshift API | ||
*/ | ||
|
||
module.exports = function resolveLoaderTransform( | ||
j, | ||
ast, | ||
webpackProperties, | ||
action | ||
) { | ||
function createResolveLoaderProperty(p) { | ||
utils.pushCreateProperty(j, p, "resolveLoader", j.objectExpression([])); | ||
return utils.pushObjectKeys(j, p, webpackProperties, "resolveLoader"); | ||
} | ||
if (webpackProperties) { | ||
if (action === "init" && typeof webpackProperties === "object") { | ||
return ast | ||
.find(j.ObjectExpression) | ||
.filter(p => utils.isAssignment(null, p, createResolveLoaderProperty)); | ||
} else if (action === "init" && webpackProperties.length) { | ||
return ast | ||
.find(j.ObjectExpression) | ||
.filter(p => | ||
utils.isAssignment( | ||
j, | ||
p, | ||
utils.pushCreateProperty, | ||
"resolveLoader", | ||
webpackProperties | ||
) | ||
); | ||
} else if (action === "add") { | ||
const resolveLoaderNode = utils.findRootNodesByName( | ||
j, | ||
ast, | ||
"resolveLoader" | ||
); | ||
if ( | ||
resolveLoaderNode.size() !== 0 && | ||
typeof webpackProperties === "object" | ||
) { | ||
return ast | ||
.find(j.ObjectExpression) | ||
.filter( | ||
p => | ||
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) === | ||
"resolveLoader" | ||
) | ||
.filter(p => { | ||
Object.keys(webpackProperties).forEach(prop => { | ||
utils.checkIfExistsAndAddValue( | ||
j, | ||
p, | ||
prop, | ||
utils.createIdentifierOrLiteral(j, webpackProperties[prop]) | ||
); | ||
}); | ||
return ast; | ||
}); | ||
} else if (resolveLoaderNode.size() !== 0 && webpackProperties.length) { | ||
return ast | ||
.find(j.ObjectExpression) | ||
.filter( | ||
p => | ||
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) === | ||
"resolveLoader" | ||
) | ||
.forEach(p => { | ||
j(p).replaceWith( | ||
utils.createIdentifierOrLiteral(j, webpackProperties) | ||
); | ||
}); | ||
} else { | ||
return resolveLoaderTransform(j, ast, webpackProperties, "init"); | ||
} | ||
} else if (action === "remove") { | ||
// TODO | ||
} else if (action === "update") { | ||
// TODO | ||
} | ||
} else { | ||
return ast; | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
lib/init/transformations/resolveLoader/resolveLoader.test.js
This file contains 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,27 @@ | ||
"use strict"; | ||
|
||
const defineTest = require("../../../utils/defineTest"); | ||
|
||
defineTest( | ||
__dirname, | ||
"resolve", | ||
"resolveLoader-0", | ||
{ | ||
modules: ["'ok'", "mode_nodules"], | ||
mainFields: ["no", "'main'"], | ||
moduleExtensions: ["'-kn'", "ok"] | ||
}, | ||
"init" | ||
); | ||
|
||
defineTest( | ||
__dirname, | ||
"resolve", | ||
"resolveLoader-1", | ||
{ | ||
modules: ["'ok'", "mode_nodules"], | ||
mainFields: ["no", "'main'"], | ||
moduleExtensions: ["'-kn'", "ok"] | ||
}, | ||
"add" | ||
); |
This file contains 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
This file contains 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,3 +1,27 @@ | ||
const webpack = require('webpack'); | ||
module.exports = { | ||
plugins: [] | ||
resolve: { | ||
alias: { | ||
inject: "{{#isdf_eq buildasda 'staasdndalone'}}", | ||
hello: "'worlasdd'", | ||
inject_1: '{{/asd}}', | ||
world: 'asdc' | ||
}, | ||
|
||
aliasFields: ["'as'"], | ||
descriptionFiles: ["'d'", 'e', 'f'], | ||
enforceExtension: true, | ||
extensions: ['ok', "'ho'"], | ||
mainFields: ['ok', "'story'"], | ||
mainFiles: ["'noMainFileHere'", 'niGuess'], | ||
|
||
resolveLoader: { | ||
modules: ["'ok'", 'mode_nodules'], | ||
mainFields: ['no', "'main'"], | ||
moduleExtensions: ["'-kn'", 'ok'] | ||
}, | ||
|
||
plugins: ['somePlugin', "'stringVal'"], | ||
symlinks: false, | ||
} | ||
}; |