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

ast(cli): Recursively parse AST #341

Merged
merged 18 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
23 changes: 23 additions & 0 deletions .yo-rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Copy link
Member Author

Choose a reason for hiding this comment

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

Ups, forgot this!

"webpack-addons-demo": {
"configuration": {
"dev": {
"webpackOptions": {
"entry": "ok",
"output": {
"filename": "'[name].js'"
},
"context": "path.join(__dirname, \"src\")",
"plugins": [
"new webpack.optimize.CommonsChunkPlugin({name:'k',filename:'k-[hash].min.js'})"
]
},
"topScope": [
"const path = require(\"path\")",
"const webpack = require(\"webpack\")"
],
"configName": "pengwings"
}
}
}
}
37 changes: 37 additions & 0 deletions lib/ast/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

const utils = require("../utils/ast-utils");

module.exports = function astTransform(j, ast, value, action, key) {
const node = utils.findRootNodesByName(j, ast, key);
if (node.size() !== 0) {
// push to existing key
} else {
// get module.exports prop
const root = ast
.find(j.ObjectExpression)
.filter(p => {
return (
utils.safeTraverse(p, [
"parentPath",
"value",
"left",
"object",
"name"
]) === "module" &&
utils.safeTraverse(p, [
"parentPath",
"value",
"left",
"property",
"name"
]) === "exports"
);
})
.filter(p => p.value.properties);

return root.forEach(p => {
utils.addProperty(j, p, key, value);
});
}
};
3 changes: 2 additions & 1 deletion lib/generate-plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const yeoman = require("yeoman-environment");
const PluginGenerator = require("../generators/plugin-generator").PluginGenerator;
const PluginGenerator = require("../generators/plugin-generator")
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be destructured :)

const { PluginGenerator } = require("../generators/plugin-generator")

Copy link
Member Author

Choose a reason for hiding this comment

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

Im not done

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah and in the meantime we give feedback while you develop :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha, just don't want you to do extra work 🙌🤙

.PluginGenerator;

/**
* Runs a yeoman generator to create a new webpack plugin project
Expand Down
7 changes: 5 additions & 2 deletions lib/generators/init-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ module.exports = class InitGenerator extends Generator {
Confirm("prodConfirm", "Are you going to use this in production?")
]);
})
.then(prodConfirmAnswer => this.isProd = prodConfirmAnswer["prodConfirm"])
.then(
prodConfirmAnswer => (this.isProd = prodConfirmAnswer["prodConfirm"])
)
.then(() => {
return this.prompt([
Confirm("babelConfirm", "Will you be using ES2015?")
Expand Down Expand Up @@ -383,7 +385,7 @@ module.exports = class InitGenerator extends Generator {
done();
});
}

/*
installPlugins() {
const asyncNamePrompt = this.async();
const defaultName = this.isProd ? "prod" : "config";
Expand All @@ -408,4 +410,5 @@ module.exports = class InitGenerator extends Generator {
writing() {
this.config.set("configuration", this.configuration);
}
*/
};
91 changes: 6 additions & 85 deletions lib/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,8 @@ const chalk = require("chalk");
const pEachSeries = require("p-each-series");

const runPrettier = require("../utils/run-prettier");

const entryTransform = require("./transformations/entry/entry");
const outputTransform = require("./transformations/output/output");
const contextTransform = require("./transformations/context/context");
const resolveTransform = require("./transformations/resolve/resolve");
const devtoolTransform = require("./transformations/devtool/devtool");
const targetTransform = require("./transformations/target/target");
const watchTransform = require("./transformations/watch/watch");
const watchOptionsTransform = require("./transformations/watch/watchOptions");
const externalsTransform = require("./transformations/externals/externals");
const nodeTransform = require("./transformations/node/node");
const performanceTransform = require("./transformations/performance/performance");
const statsTransform = require("./transformations/stats/stats");
const amdTransform = require("./transformations/other/amd");
const bailTransform = require("./transformations/other/bail");
const cacheTransform = require("./transformations/other/cache");
const profileTransform = require("./transformations/other/profile");
const mergeTransform = require("./transformations/other/merge");
const parallelismTransform = require("./transformations/other/parallelism");
const recordsInputPathTransform = require("./transformations/other/recordsInputPath");
const recordsOutputPathTransform = require("./transformations/other/recordsOutputPath");
const recordsPathTransform = require("./transformations/other/recordsPath");
const moduleTransform = require("./transformations/module/module");
const pluginsTransform = require("./transformations/plugins/plugins");
const topScopeTransform = require("./transformations/top-scope/top-scope");
const devServerTransform = require("./transformations/devServer/devServer");
const modeTransform = require("./transformations/mode/mode");
const resolveLoaderTransform = require("./transformations/resolveLoader/resolveLoader");

const transformsObject = {
entryTransform,
outputTransform,
contextTransform,
resolveTransform,
devtoolTransform,
targetTransform,
watchTransform,
watchOptionsTransform,
externalsTransform,
nodeTransform,
performanceTransform,
statsTransform,
amdTransform,
bailTransform,
cacheTransform,
profileTransform,
moduleTransform,
pluginsTransform,
topScopeTransform,
mergeTransform,
devServerTransform,
modeTransform,
parallelismTransform,
recordsInputPathTransform,
recordsOutputPathTransform,
recordsPathTransform,
resolveLoaderTransform
};
const astTransform = require("../ast");
const propTypes = require("../utils/prop-types");

/**
*
Expand All @@ -75,27 +19,8 @@ const transformsObject = {
* @returns {Object} - An Object with the transformations to be run
*/

function mapOptionsToTransform(transformObject, config) {
return Object.keys(transformObject)
.map(transformKey => {
const stringVal = transformKey.substr(
0,
transformKey.indexOf("Transform")
);
if (Object.keys(config.webpackOptions).length) {
if (config.webpackOptions[stringVal]) {
return [
transformObject[transformKey],
config.webpackOptions[stringVal]
];
} else {
return [transformObject[transformKey], config[stringVal]];
}
} else {
return [transformObject[transformKey]];
}
})
.filter(e => e[1]);
function mapOptionsToTransform(config) {
return Object.keys(config.webpackOptions).filter(k => propTypes.has(k));
}

/**
Expand All @@ -117,7 +42,7 @@ module.exports = function runTransform(webpackProperties, action) {

webpackConfig.forEach(scaffoldPiece => {
const config = webpackProperties[scaffoldPiece];
const transformations = mapOptionsToTransform(transformsObject, config);
const transformations = mapOptionsToTransform(config);
const ast = j(
initActionNotDefined
? webpackProperties.configFile
Expand All @@ -126,11 +51,7 @@ module.exports = function runTransform(webpackProperties, action) {
const transformAction = action || null;

return pEachSeries(transformations, f => {
if (!f[1]) {
return f[0](j, ast, transformAction);
} else {
return f[0](j, ast, f[1], transformAction);
}
return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
})
.then(_ => {
let configurationName;
Expand Down
8 changes: 7 additions & 1 deletion lib/init/transformations/top-scope/top-scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

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

defineTest(__dirname, "top-scope", "top-scope-0", ["const test = 'me';"], "init");
defineTest(
__dirname,
"top-scope",
"top-scope-0",
["const test = 'me';"],
"init"
);
defineTest(
__dirname,
"top-scope",
Expand Down
Loading