Skip to content

Commit

Permalink
ast for devServer (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed Oct 22, 2017
1 parent 2c7c0b4 commit 8892c95
Show file tree
Hide file tree
Showing 8 changed files with 1,529 additions and 1,903 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`devServer transforms correctly using "devServer-0" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}
}
"
`;

exports[`devServer transforms correctly using "devServer-1" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
devServer: someVar
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
}
}
40 changes: 40 additions & 0 deletions lib/creator/transformations/devServer/devServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";

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

/*
*
* Transform for devServer. Finds the devServer property from yeoman and creates a
* property based on what the user has given us.
*
* @param j — jscodeshift API
* @param ast - jscodeshift API
* @param { Object } webpackProperties - Object containing transformation rules
* @returns ast - jscodeshift API
*/

module.exports = function(j, ast, webpackProperties) {
function createDevServerProperty(p) {
utils.pushCreateProperty(j, p, "devServer", j.objectExpression([]));
return utils.pushObjectKeys(j, p, webpackProperties, "devServer");
}
if (webpackProperties && typeof webpackProperties === "object") {
return ast
.find(j.ObjectExpression)
.filter(p => utils.isAssignment(null, p, createDevServerProperty));
} else if (webpackProperties && webpackProperties.length) {
return ast
.find(j.ObjectExpression)
.filter(p =>
utils.isAssignment(
j,
p,
utils.pushCreateProperty,
"devServer",
webpackProperties
)
);
} else {
return ast;
}
};
10 changes: 10 additions & 0 deletions lib/creator/transformations/devServer/devServer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

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

defineTest(__dirname, "devServer", "devServer-0",{
contentBase: "path.join(__dirname, 'dist')",
compress: true,
port: 9000
});
defineTest(__dirname, "devServer", "devServer-1", "someVar");
4 changes: 3 additions & 1 deletion lib/creator/transformations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const mergeTransform = require("./other/merge");
const moduleTransform = require("./module/module");
const pluginsTransform = require("./plugins/plugins");
const topScopeTransform = require("./top-scope/top-scope");
const devServerTransform = require("./devServer/devServer");

/*
* @function runTransform
Expand Down Expand Up @@ -58,7 +59,8 @@ const transformsObject = {
moduleTransform,
pluginsTransform,
topScopeTransform,
mergeTransform
mergeTransform,
devServerTransform
};

module.exports = function runTransform(webpackProperties) {
Expand Down
Loading

0 comments on commit 8892c95

Please sign in to comment.