Skip to content

Commit

Permalink
Build Step for the CLI (#190)
Browse files Browse the repository at this point in the history
* chore: Use prettier (#173)

* build: Add webpack lint settings and style code with eslint --fix

We now use the webpack eslint settings that make
sense in the cli. Also use the new --fix of eslint
to automatically beautify the code

* Add additional .eslintrc to /bin to remove unrelated eslint errors

* build: Add jest-cli as dev dependency

* style: fix lintin errors and coding style according to webpack settings

* build: update dependencies and add package-lock to repo

* feat: Require quotes only in property names that require it

* chore: Add again removed settings by merge conflict solving

* tests: Fix failing test

* chore: Use husky instead of pre-commit

pre-commit package stopped working. See observing/pre-commit#113

husky also requires less boilerplate in the package.json

* [Feature] Added support to Flow inside transformations (#174)

* [FLOW] Implemented interfaces and flow types inside the transformations

* [FLOW] first commit

* Added flwo to two other files

* Reviewed interfaces

* Created different intefaces for expressions

* Fixed rest of trasformations

* More developments

* Updated code after code review

* Fixed failing tests

* Removed yarn file

* Applied linting

* Updated packages

* Applied lint style to the imports due to code review

* docs: Documentation for migrate (#175)

* initial draft of migrate documentation

* formatting

* formatting

* added summary of migrate changes

* maintain consistent usage of user through documentation

* Feature/generate loader (#183)

* Add template files for loader yeoman generator

* Create yeoman generator for a webpack loader project

* Add tests for loader-generator

* Add `mkdirp` dependency for loader-generator

* Add function to create yeoman env and run loader-generator

* Add `generate-loader` command to webpack-cli

* Copy loader templates from proper directory

* Add template files for plugin generator

* Create yeoman generator for webpack plugins

* Add function to create yeoman env and run plugin generator

* Add cli command to generate plugin

* Register generate- commands in yargs

* Add template files for loader examples and tests

* Copy loader test and example template files in generator

* Add template files for plugin examples and tests

* Copy plugin test and example template files in generator

* Refactor generator file copying, switch .includes with .indexOf in CLI arg parsing

* Change `indexOf('foo') > -1` to `indexOf('foo') >= 0`

* Factor out generator copy utilities into separate module

* Rewrite generators using a function that returns a customized generator class

* Fix linting errors

* Remove //eslint-disable lines from template files

* Add unit tests for CLI and add flow compilation (#186)

* add unit tests for everything and flow compilation

* add travis builds

* add cli to signature & add ignore to trash code

* port to jest

* remove redundant cli command

* Feature/generate loader (#183)

* Add template files for loader yeoman generator

* Create yeoman generator for a webpack loader project

* Add tests for loader-generator

* Add `mkdirp` dependency for loader-generator

* Add function to create yeoman env and run loader-generator

* Add `generate-loader` command to webpack-cli

* Copy loader templates from proper directory

* Add template files for plugin generator

* Create yeoman generator for webpack plugins

* Add function to create yeoman env and run plugin generator

* Add cli command to generate plugin

* Register generate- commands in yargs

* Add template files for loader examples and tests

* Copy loader test and example template files in generator

* Add template files for plugin examples and tests

* Copy plugin test and example template files in generator

* Refactor generator file copying, switch .includes with .indexOf in CLI arg parsing

* Change `indexOf('foo') > -1` to `indexOf('foo') >= 0`

* Factor out generator copy utilities into separate module

* Rewrite generators using a function that returns a customized generator class

* Fix linting errors

* Remove //eslint-disable lines from template files

* add unit tests for everything and flow compilation

* add travis builds

* add cli to signature & add ignore to trash code

* port to jest

* remove redundant cli command

* rebase against master

* ast for devServer (#185)

* feat: replace yarn backup w/ normal yarn (#160)

* feat: replace yarn backup w/ normal yarn

* upgrade npm & remove yarn

* Use yarn if available (#189)

* bump version to 1.3.4

* update package.json

* 1.3.5

* prepublish -> postinstall

* 1.3.6

* temp fix to prepublish issue

* 1.3.7

* add propper post install

* 1.3.8

* re-add manual build step

* 1.3.9

* refactor

* bump version

* disable published pkg

* add ignore patterns

* add more ignore patterns for npm
  • Loading branch information
evenstensberg authored Oct 22, 2017
1 parent 84b2c38 commit 2eb4953
Show file tree
Hide file tree
Showing 177 changed files with 6,890 additions and 451 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015", "stage-3"]
"presets": ["es2015", "stage-3", "flow"]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/__testfixtures__/*
coverage
test
dist
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
"root": true,
"plugins": ["node"],
"extends": ["eslint:recommended", "plugin:node/recommended"],
"plugins": ["node", "flowtype"],
"extends": ["eslint:recommended", "plugin:node/recommended", "plugin:flowtype/recommended"],
"env": {
"node": true,
"es6": true,
Expand Down
11 changes: 11 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]
flow-typed

[options]
module.system.node.resolve_dirname=./lib/
module.system.node.resolve_dirname=node_modules
experimental.const_params=false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ yarn-error.log
# Jest Coverage
coverage

# Distribution Build
dist

# Test Compilation
test/js/*
15 changes: 15 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test
coverage
lib
.travis.yml
.eslintrc*
.fitcommitjsrc.json
.vscode
.editorconfig
.eslintignore
__mocks__
__testfixtures__
*.test.js
*.input.js
types.js
*.spec.js
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ before_script:
- bash <(curl -s https://codecov.io/bash)

script:
- npm run prepublish
- npm run lint
- npm run test
32 changes: 14 additions & 18 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@ var resolveCwd = require("resolve-cwd");
// Local version replace global one
var localCLI = resolveCwd.silent("webpack-cli/bin/webpack");

if (process.argv.slice(2).indexOf("init") >= 0) {
var initPkgs =
process.argv.slice(2).length === 1 ? [] : [process.argv.slice(2).pop()];
//eslint-disable-next-line
return require("../lib/initialize")(initPkgs);
} else if (process.argv.slice(2).indexOf("migrate") >= 0) {
var filePaths =
process.argv.slice(2).length === 1 ? [] : [process.argv.slice(2).pop()];
if (!filePaths.length) {
throw new Error("Please specify a path to your webpack config");
}
var inputConfigPath = path.resolve(process.cwd(), filePaths[0]);
//eslint-disable-next-line
return require("../lib/migrate.js")(inputConfigPath, inputConfigPath);
} else if (process.argv.slice(2).indexOf("generate-loader") >= 0) {
return require("../lib/generate-loader/index.js")();
} else if (process.argv.slice(2).indexOf("generate-plugin") >= 0) {
return require("../lib/generate-plugin/index.js")();
const NON_COMPILATION_ARGS = [
"init",
"migrate",
"generate-loader",
"generate-plugin"
];

const NON_COMPILATION_CMD = process.argv.find(arg => {
return NON_COMPILATION_ARGS.find(a => a === arg);
});

if (NON_COMPILATION_CMD) {
// eslint-disable-next-line
return require("../dist/index")(NON_COMPILATION_CMD, process.argv);
}

if (localCLI && path.relative(localCLI, __filename) !== "") {
Expand Down
66 changes: 66 additions & 0 deletions dist/creator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";

const yeoman = require("yeoman-environment");
const Generator = require("yeoman-generator");
const path = require("path");
const defaultGenerator = require("./yeoman/webpack-generator");
const WebpackAdapter = require("./yeoman/webpack-adapter");
const runTransform = require("./transformations/index");

/*
* @function creator
*
* Runs yeoman and runs the transformations based on the object
* built up from an author/user
*
* @param { String } options - An path to the given generator
* @returns { Function } runTransform - Run transformations based on yeoman prompt
*/

function creator(options) {
let env = yeoman.createEnv("webpack", null, new WebpackAdapter());
const generatorName = options
? replaceGeneratorName(path.basename(options[0]))
: "webpack-default-generator";
if (options) {
const WebpackGenerator = class extends Generator {
initializing() {
options.forEach(path => {
return this.composeWith(require.resolve(path));
});
}
};
env.registerStub(WebpackGenerator, generatorName);
} else {
env.registerStub(defaultGenerator, "webpack-default-generator");
}

env.run(generatorName).on("end", () => {
if (generatorName !== "webpack-default-generator") {
//HACK / FIXME
env = env.options.env;
return runTransform(env.configuration);
} else {
return runTransform(env.getArgument("configuration"));
}
});
}

/*
* @function replaceGeneratorName
*
* Replaces the webpack-addons pattern with the end of the addons name merged
* with 'generator'
*
* @param { String } name - name of the generator
* @returns { String } name - replaced pattern of the name
*/

function replaceGeneratorName(name) {
return name.replace(/(webpack-addons)?([^:]+)(:.*)?/g, "generator$2");
}

module.exports = {
creator,
replaceGeneratorName
};
10 changes: 10 additions & 0 deletions dist/creator/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

const replaceGeneratorName = require("./index").replaceGeneratorName;

describe("replaceGeneratorName", () => {
it("should replace a pattern of an addon", () => {
const generatorName = replaceGeneratorName("webpack-addons-thefox");
expect(generatorName).toEqual("generator-thefox");
});
});
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'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
}
}
32 changes: 32 additions & 0 deletions dist/creator/transformations/context/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

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

/*
*
* Transform for context. Finds the context 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) {
if (webpackProperties) {
return ast
.find(j.ObjectExpression)
.filter(p =>
utils.isAssignment(
j,
p,
utils.pushCreateProperty,
"context",
webpackProperties
)
);
} else {
return ast;
}
};
7 changes: 7 additions & 0 deletions dist/creator/transformations/context/context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

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

defineTest(__dirname, "context", "context-0", "path.resolve(__dirname, 'app')");
defineTest(__dirname, "context", "context-1", "'./some/fake/path'");
defineTest(__dirname, "context", "context-2", "contextVariable");
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 dist/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 dist/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");
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'
}
}
32 changes: 32 additions & 0 deletions dist/creator/transformations/devtool/devtool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

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

/*
*
* Transform for devtool. Finds the devtool 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) {
if (webpackProperties) {
return ast
.find(j.ObjectExpression)
.filter(p =>
utils.isAssignment(
j,
p,
utils.pushCreateProperty,
"devtool",
webpackProperties
)
);
} else {
return ast;
}
};
8 changes: 8 additions & 0 deletions dist/creator/transformations/devtool/devtool.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

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

defineTest(__dirname, "devtool", "devtool-0", "'source-map'");
defineTest(__dirname, "devtool", "devtool-0", "myVariable");
defineTest(__dirname, "devtool", "devtool-1", "'cheap-module-source-map'");
defineTest(__dirname, "devtool", "devtool-1", "false");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Loading

0 comments on commit 2eb4953

Please sign in to comment.