diff --git a/lib/creator/transformations/node/__snapshots__/node.test.js.snap b/lib/creator/transformations/node/__snapshots__/node.test.js.snap new file mode 100644 index 00000000000..0046bc33d63 --- /dev/null +++ b/lib/creator/transformations/node/__snapshots__/node.test.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`node transforms correctly using "node-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + node: { + console: false, + global: true, + process: true, + Buffer: true, + __filename: mock, + __dirname: mock, + setImmediate: true + } +} +" +`; diff --git a/lib/creator/transformations/node/__testfixtures__/node-0.input.js b/lib/creator/transformations/node/__testfixtures__/node-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/node/__testfixtures__/node-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/node/node.test.js b/lib/creator/transformations/node/node.test.js new file mode 100644 index 00000000000..bd1b0e0484b --- /dev/null +++ b/lib/creator/transformations/node/node.test.js @@ -0,0 +1,11 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'node', 'node-0', {node : { + console: false, + global: true, + process: true, + Buffer: true, + __filename: 'mock', + __dirname: 'mock', + setImmediate: true +}}); diff --git a/lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap b/lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap new file mode 100644 index 00000000000..6fbe0c9fe35 --- /dev/null +++ b/lib/creator/transformations/plugins/__snapshots__/plugins.test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`plugins transforms correctly using "plugins-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + plugins: [ + new webpack.optimize.CommonsChunkPlugin({name:'vendor',filename:'vendor-[hash].min.js'}) + ] +} +" +`; diff --git a/lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js b/lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/plugins/__testfixtures__/plugins-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/plugins/plugins.test.js b/lib/creator/transformations/plugins/plugins.test.js new file mode 100644 index 00000000000..5825f2a52dd --- /dev/null +++ b/lib/creator/transformations/plugins/plugins.test.js @@ -0,0 +1,5 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'plugins', 'plugins-0', {plugins : [ + 'new webpack.optimize.CommonsChunkPlugin({name:' + '\'' + 'vendor' + '\'' + ',filename:' + '\'' + 'vendor' + '-[hash].min.js\'})' +]}); diff --git a/lib/creator/transformations/target/__snapshots__/target.test.js.snap b/lib/creator/transformations/target/__snapshots__/target.test.js.snap new file mode 100644 index 00000000000..663e3b899af --- /dev/null +++ b/lib/creator/transformations/target/__snapshots__/target.test.js.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`target transforms correctly using "target-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + target: async-node +} +" +`; + +exports[`target transforms correctly using "target-1" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + target: 'node', + target: async-node +} +" +`; diff --git a/lib/creator/transformations/target/__testfixtures__/target-0.input.js b/lib/creator/transformations/target/__testfixtures__/target-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/target/__testfixtures__/target-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/target/__testfixtures__/target-1.input.js b/lib/creator/transformations/target/__testfixtures__/target-1.input.js new file mode 100644 index 00000000000..c8e83f95936 --- /dev/null +++ b/lib/creator/transformations/target/__testfixtures__/target-1.input.js @@ -0,0 +1,7 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + target: 'node' +} diff --git a/lib/creator/transformations/target/target.test.js b/lib/creator/transformations/target/target.test.js new file mode 100644 index 00000000000..f8760bb98dd --- /dev/null +++ b/lib/creator/transformations/target/target.test.js @@ -0,0 +1,4 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'target', 'target-0', {target : 'async-node'}); +defineTest(__dirname, 'target', 'target-1', {target : 'async-node'}); diff --git a/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap new file mode 100644 index 00000000000..70131f1c10c --- /dev/null +++ b/lib/creator/transformations/watch/__snapshots__/watch.test.js.snap @@ -0,0 +1,49 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`watch transforms correctly using "watch-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: true +} +" +`; + +exports[`watch transforms correctly using "watch-0" data 2`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false +} +" +`; + +exports[`watch transforms correctly using "watch-1" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + watch: true, + watchOptions: {} +} +" +`; + +exports[`watch transforms correctly using "watch-1" data 2`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + watch: false, + watchOptions: {} +} +" +`; diff --git a/lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap b/lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap new file mode 100644 index 00000000000..9f9b0088461 --- /dev/null +++ b/lib/creator/transformations/watch/__snapshots__/watchOptions.test.js.snap @@ -0,0 +1,64 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`watchOptions transforms correctly using "watch-0" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + } +} +" +`; + +exports[`watchOptions transforms correctly using "watch-1" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false, + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + }, + + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + } +} +" +`; + +exports[`watchOptions transforms correctly using "watch-2" data 1`] = ` +"module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + + watch: false, + watchOptions: { + aggregateTimeout: 320, + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + }, + + watchOptions: { + aggregateTimeout: 300, + poll: 1000, + ignored: /node_modules/ + } +} +" +`; diff --git a/lib/creator/transformations/watch/__testfixtures__/watch-0.input.js b/lib/creator/transformations/watch/__testfixtures__/watch-0.input.js new file mode 100644 index 00000000000..ea0822c2484 --- /dev/null +++ b/lib/creator/transformations/watch/__testfixtures__/watch-0.input.js @@ -0,0 +1,6 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + } +} diff --git a/lib/creator/transformations/watch/__testfixtures__/watch-1.input.js b/lib/creator/transformations/watch/__testfixtures__/watch-1.input.js new file mode 100644 index 00000000000..503bb1d4a71 --- /dev/null +++ b/lib/creator/transformations/watch/__testfixtures__/watch-1.input.js @@ -0,0 +1,8 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + watch: false, + watchOptions: {} +} diff --git a/lib/creator/transformations/watch/__testfixtures__/watch-2.input.js b/lib/creator/transformations/watch/__testfixtures__/watch-2.input.js new file mode 100644 index 00000000000..1d94f02f15f --- /dev/null +++ b/lib/creator/transformations/watch/__testfixtures__/watch-2.input.js @@ -0,0 +1,10 @@ +module.exports = { + entry: 'index.js', + output: { + filename: 'bundle.js' + }, + watch: false, + watchOptions: { + aggregateTimeout: 320 + } +} diff --git a/lib/creator/transformations/watch/watch.js b/lib/creator/transformations/watch/watch.js index 2e23c0c2616..188b34f59cf 100644 --- a/lib/creator/transformations/watch/watch.js +++ b/lib/creator/transformations/watch/watch.js @@ -1,11 +1,11 @@ const isAssignment = require('../../utils/is-assignment'); +const transformUtils = require('../../../transformations/utils'); module.exports = function(j, ast, yeomanConfig) { const webpackProperties = yeomanConfig.webpackOptions; function createWatchProperty(p) { - return p.value.properties.push( - j.property('init', j.identifier('watch'), j.literal(webpackProperties['watch'])) - ); + const propValue = transformUtils.createIdentifierOrLiteral(j, webpackProperties['watch']); + transformUtils.checkIfExistsAndAddValue(j, p, 'watch', propValue); } if(typeof(webpackProperties['watch']) === 'boolean') { return ast.find(j.ObjectExpression).filter(p => isAssignment(p, createWatchProperty)); @@ -13,3 +13,5 @@ module.exports = function(j, ast, yeomanConfig) { return ast; } }; + + diff --git a/lib/creator/transformations/watch/watch.test.js b/lib/creator/transformations/watch/watch.test.js new file mode 100644 index 00000000000..1b5e1e195c3 --- /dev/null +++ b/lib/creator/transformations/watch/watch.test.js @@ -0,0 +1,6 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'watch', 'watch-0', {watch : true}); +defineTest(__dirname, 'watch', 'watch-0', {watch : false}); +defineTest(__dirname, 'watch', 'watch-1', {watch : true}); +defineTest(__dirname, 'watch', 'watch-1', {watch : false}); diff --git a/lib/creator/transformations/watch/watchOptions.test.js b/lib/creator/transformations/watch/watchOptions.test.js new file mode 100644 index 00000000000..c457bfbb388 --- /dev/null +++ b/lib/creator/transformations/watch/watchOptions.test.js @@ -0,0 +1,19 @@ +const defineTest = require('../../../transformations/defineTest'); + +defineTest(__dirname, 'watchOptions', 'watch-0', {watchOptions : { + aggregateTimeout: 300, + poll: 1000, + ignored: '/node_modules/' +}}); + +defineTest(__dirname, 'watchOptions', 'watch-1', {watchOptions : { + aggregateTimeout: 300, + poll: 1000, + ignored: '/node_modules/' +}}); + +defineTest(__dirname, 'watchOptions', 'watch-2', {watchOptions : { + aggregateTimeout: 300, + poll: 1000, + ignored: '/node_modules/' +}}); diff --git a/lib/transformations/__snapshots__/utils.test.js.snap b/lib/transformations/__snapshots__/utils.test.js.snap index ac742027a35..400542763b9 100644 --- a/lib/transformations/__snapshots__/utils.test.js.snap +++ b/lib/transformations/__snapshots__/utils.test.js.snap @@ -1,5 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`utils checkIfExistsAndAddValue should create new prop if none exist 1`] = ` +" + module.exports = { + entry: 'index.js', + externals: \\"React\\" + } + " +`; + +exports[`utils checkIfExistsAndAddValue should override prop if it exists 1`] = ` +" + module.exports = { + entry: \\"app.js\\" + } + " +`; + exports[`utils createIdentifierOrLiteral should create basic literal 1`] = `"'stringLiteral'"`; exports[`utils createIdentifierOrLiteral should create boolean 1`] = `"true"`; diff --git a/lib/transformations/utils.js b/lib/transformations/utils.js index cccfff5d57f..46b21c788ab 100644 --- a/lib/transformations/utils.js +++ b/lib/transformations/utils.js @@ -271,6 +271,29 @@ function getRequire(j, constName, packagePath) { ]); } +/* +* @function checkIfExistsAndAddValue +* +* If a prop exists, it overrides it, else it creates a new one +* @param j — jscodeshift API +* @param { Node } node - objectexpression to check +* @param { string } key - Key of the property +* @param { string } value - computed value of the property +* @returns - nothing +*/ + +function checkIfExistsAndAddValue(j, node, key, value) { + const existingProp = node.value.properties.filter(prop => prop.key.name === key); + let prop; + if (existingProp.length > 0){ + prop = existingProp[0]; + prop.value = value; + } else { + prop = j.property('init', j.identifier(key), value); + node.value.properties.push(prop); + } +} + module.exports = { safeTraverse, createProperty, @@ -282,5 +305,6 @@ module.exports = { createLiteral, createIdentifierOrLiteral, findObjWithOneOfKeys, - getRequire + getRequire, + checkIfExistsAndAddValue }; diff --git a/lib/transformations/utils.test.js b/lib/transformations/utils.test.js index c98d9444a0a..7b0b3d8bdfd 100644 --- a/lib/transformations/utils.test.js +++ b/lib/transformations/utils.test.js @@ -176,4 +176,25 @@ var a = { plugs: [] } expect(j(require).toSource()).toMatchSnapshot(); }); }); + + describe('checkIfExistsAndAddValue', () => { + it('should create new prop if none exist', () => { + const ast = j(` + module.exports = { + entry: 'index.js' + } + `); + ast.find(j.ObjectExpression).forEach(node => utils.checkIfExistsAndAddValue(j, node, 'externals', j.literal('React'))); + expect(ast.toSource()).toMatchSnapshot(); + }); + it('should override prop if it exists', () => { + const ast = j(` + module.exports = { + entry: 'index.js' + } + `); + ast.find(j.ObjectExpression).forEach(node => utils.checkIfExistsAndAddValue(j, node, 'entry', j.literal('app.js'))); + expect(ast.toSource()).toMatchSnapshot(); + }); + }); });