Skip to content

Commit

Permalink
support user .babelrc (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmcdonald authored and ccpricenytimes committed Dec 14, 2016
1 parent b0e9e4a commit d803c3e
Show file tree
Hide file tree
Showing 29 changed files with 460 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- 6.0
script: cd packages/kyt-core && npm install && cd ../kyt-cli && npm install && cd ../kyt-utils && npm install && cd ../.. && npm run lint && npm install && npm test && npm run e2e
script: npm install && cd packages/kyt-core && npm install && cd ../kyt-cli && npm install && cd ../kyt-utils && npm install && cd ../.. && npm run lint && npm test && npm run e2e
2 changes: 2 additions & 0 deletions e2e_tests/tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ describe('KYT CLI', () => {
expect(setupArr.includes('👍 Created .stylelintrc.json file')).toBe(true);
expect(setupArr.includes('👍 Created kyt.config.js file')).toBe(true);
expect(setupArr.includes('👍 Created .editorconfig file')).toBe(true);
expect(setupArr.includes('👍 Created .babelrc')).toBe(true);
expect(setupArr.includes('👍 Created .gitignore file')).toBe(true);
expect(setupArr.includes('👍 Created src directory')).toBe(true);
});
it('sets up with the correct files', () => {
expect(shell.test('-d', 'src')).toBe(true);
expect(shell.test('-f', 'kyt.config.js')).toBe(true);
expect(shell.test('-f', '.editorconfig')).toBe(true);
expect(shell.test('-f', '.babelrc')).toBe(true);
expect(shell.test('-f', '.eslintrc.json')).toBe(true);
expect(shell.test('-f', '.stylelintrc.json')).toBe(true);
expect(shell.test('-f', 'prototype.js')).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test-watch": "jest --watch",
"test-coverage": "jest --coverage",
"e2e": "jest --config ./e2e_tests/jest.config.json --verbose --no-cache",
"lint": "packages/kyt-core/node_modules/.bin/eslint --config packages/kyt-core/.eslintrc.json --ignore-pattern **/node_modules --ignore-pattern packages/starter-kyts ./"
"lint": "packages/kyt-core/node_modules/.bin/eslint --config packages/kyt-core/.eslintrc.json --ignore-pattern **/node_modules --ignore-pattern packages/babel-presets --ignore-pattern packages/starter-kyts ./"
},
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions packages/babel-presets/babel-preset-kyt-core/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-var": 0
}
}
22 changes: 22 additions & 0 deletions packages/babel-presets/babel-preset-kyt-core/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var babelPresetLatest = require('babel-preset-latest');
var babelTransformRuntime = require('babel-plugin-transform-runtime');
var babelTransformModules = require('babel-plugin-transform-es2015-modules-commonjs');

module.exports = function(context, opts) {
opts = opts || {};
return {
// modules are handled by webpack, don't transform them
presets: [[babelPresetLatest, { modules: false }]],

// provide the ability to opt into babel-plugin-transform-runtime inclusion
plugins: [opts.includeRuntime === true && babelTransformRuntime].filter(Boolean),

env: {
test: {
plugins: [
[babelTransformModules, { loose: true }],
],
},
},
};
};
15 changes: 15 additions & 0 deletions packages/babel-presets/babel-preset-kyt-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "babel-preset-kyt-core",
"version": "0.1.0-alpha.1",
"description": "an opinionated babel preset, best used with kyt",
"main": "lib/index.js",
"author": "NYTimes",
"license": "Apache-2.0",
"dependencies": {
"babel-plugin-transform-es2015-modules-commonjs": "6.16.0",
"babel-plugin-transform-runtime": "6.15.0",
"babel-preset-latest": "6.16.0"
},
"keywords": ["babel", "babel-preset", "kyt"],
"files": ["lib"]
}
5 changes: 5 additions & 0 deletions packages/babel-presets/babel-preset-kyt-react/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-var": 0
}
}
31 changes: 31 additions & 0 deletions packages/babel-presets/babel-preset-kyt-react/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var babelPresetReact = require('babel-preset-react');
var reactRemovePropTypes = require('babel-plugin-transform-react-remove-prop-types');
var reactTransformConstant = require('babel-plugin-transform-react-constant-elements');
var reactTransformInline = require('babel-plugin-transform-react-inline-elements');
var reactTransformJsxSource = require('babel-plugin-transform-react-jsx-source');
var babelPresetKytCore = require('babel-preset-kyt-core');

module.exports = function(context, opts) {
opts = opts || {};
return {
presets: [
babelPresetReact,
// pass options through to core preset
[babelPresetKytCore, opts.coreOptions || {}],
],
env: {
development: {
plugins: [
reactTransformJsxSource,
],
},
production: {
plugins: [
reactRemovePropTypes,
reactTransformConstant,
reactTransformInline,
],
},
},
};
};
25 changes: 25 additions & 0 deletions packages/babel-presets/babel-preset-kyt-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "babel-preset-kyt-react",
"version": "0.1.0-alpha.1",
"description": "an opinionated babel preset for react apps, best used with kyt",
"main": "lib/index.js",
"author": "NYTimes",
"license": "Apache-2.0",
"dependencies": {
"babel-plugin-transform-react-constant-elements": "6.9.1",
"babel-plugin-transform-react-inline-elements": "^6.8.0",
"babel-plugin-transform-react-jsx-source": "6.9.0",
"babel-plugin-transform-react-remove-prop-types": "0.2.10",
"babel-preset-kyt-core": "0.1.0-alpha.1",
"babel-preset-react": "6.16.0"
},
"keywords": [
"babel",
"babel-preset",
"kyt",
"react"
],
"files": [
"lib"
]
}
17 changes: 17 additions & 0 deletions packages/kyt-cli/cli/actions/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = (flags, args) => {
userKytConfigPath,
userNodeModulesPath,
userPackageJSONPath,
userBabelrcPath,
} = require('kyt-utils/paths')(); // eslint-disable-line

const date = Date.now();
Expand Down Expand Up @@ -282,6 +283,17 @@ module.exports = (flags, args) => {
logger.task('Created .editorconfig file');
};

const createBabelrc = () => {
// back up existing .babelrc, if it exists
if (shell.test('-f', userBabelrcPath)) {
const mvTo = path.join(userRootPath, `.babelrc-${date}.bak`);
shell.mv(userBabelrcPath, mvTo);
logger.info(`Backed up current .babelrc to ${mvTo}`);
}
shell.cp(`${tmpDir}/.babelrc`, userBabelrcPath);
logger.task('Created .babelrc');
};

// Copies the starter kyt kyt.config.js
// to the user's base directory.
const createKytConfig = () => {
Expand Down Expand Up @@ -389,11 +401,16 @@ module.exports = (flags, args) => {
logger.log(error);
bailProcess();
}
if (!args.repository) {
// temporary - get the right version of the starter-kyts
shell.exec('cd .kyt-tmp && git checkout babelrc');
}
// eslint-disable-next-line global-require,import/no-dynamic-require
tempPackageJSON = require(`${tmpDir}/package.json`);
updateUserPackageJSON(false);
installUserDependencies();
createESLintFile();
createBabelrc();
createStylelintFile();
createEditorconfigLink();
createKytConfig();
Expand Down
19 changes: 0 additions & 19 deletions packages/kyt-core/.babelrc

This file was deleted.

1 change: 1 addition & 0 deletions packages/kyt-core/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage
**/node_modules
2 changes: 1 addition & 1 deletion packages/kyt-core/cli/actions/__tests__/proto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jest.mock('kyt-utils/logger');
jest.mock('shelljs');

const listen = jest.fn();
jest.setMock('webpack-dev-server', () => ({ listen }));
jest.setMock('webpack-dev-server', jest.fn(() => ({ listen })));

const config = {
modifyWebpackConfig: jest.fn(c => c),
Expand Down
65 changes: 0 additions & 65 deletions packages/kyt-core/config/__tests__/babel.test.js

This file was deleted.

23 changes: 12 additions & 11 deletions packages/kyt-core/config/__tests__/jest.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const jest = require('../jest');
// can't call this `jest` because that's a global in tests
const jestConfig = require('../jest');

it('jest() returns a jest config', () => {
it('jestConfig() returns a jest config', () => {
const rootDir = 'rootDir';
const jestConfig = jest(rootDir);
const config = jestConfig(rootDir);

expect(typeof jestConfig).toBe('object');
expect(jestConfig.moduleNameMapper).toBeDefined();
expect(jestConfig.scriptPreprocessor).toBeDefined();
expect(jestConfig.testPathIgnorePatterns).toBeDefined();
expect(jestConfig.testEnvironment).toBeDefined();
expect(jestConfig.testRegex).toBeDefined();
expect(jestConfig.collectCoverageFrom).toBeDefined();
expect(jestConfig.rootDir).toBe(rootDir);
expect(typeof config).toBe('object');
expect(config.moduleNameMapper).toBeDefined();
expect(config.scriptPreprocessor).toBeDefined();
expect(config.testPathIgnorePatterns).toBeDefined();
expect(config.testEnvironment).toBeDefined();
expect(config.testRegex).toBeDefined();
expect(config.collectCoverageFrom).toBeDefined();
expect(config.rootDir).toBe(rootDir);
});
33 changes: 33 additions & 0 deletions packages/kyt-core/config/__tests__/webpack.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
const shell = {
test: jest.fn(),
};

const logger = {
warn: jest.fn(),
};

jest.setMock('shelljs', shell);
jest.setMock('kyt-utils/logger', logger);

const devClientConfig = require('../webpack.dev.client');
const devServerConfig = require('../webpack.dev.server');
const prodClientConfig = require('../webpack.prod.client');
const prodServerConfig = require('../webpack.prod.server');
const baseConfig = require('../webpack.base');

describe('webpack.dev.client', () => {
it('has babel-polyfill as first entry in entry.main array', () => {
Expand Down Expand Up @@ -30,3 +42,24 @@ describe('webpack.prod.server', () => {
expect(config.entry.main[0]).toBe('babel-polyfill');
});
});

describe('webpack.base', () => {
beforeEach(() => {
logger.warn.mockClear();
});
it('doesn\'t set up a babel preset if a .babelrc exists', () => {
shell.test.mockImplementationOnce(() => true);
const config = baseConfig({ clientURL: {}, publicPath: '/' });
const babelLoader = config.module.rules.find(({ loader }) => loader === 'babel-loader');
expect(babelLoader.options.presets).toBeUndefined();
expect(logger.warn).not.toHaveBeenCalled();
});
it('sets up kyt-core babel preset if a .babelrc exists', () => {
shell.test.mockImplementationOnce(() => false);
const config = baseConfig({ clientURL: {}, publicPath: '/' });
const babelLoader = config.module.rules.find(({ loader }) => loader === 'babel-loader');
expect(babelLoader.options.presets.length).toBe(1);
expect(babelLoader.options.presets[0]).toMatch(/babel-preset-kyt-core/);
expect(logger.warn).toHaveBeenCalledWith('No user .babelrc found. Using kyt default babel preset...');
});
});
Loading

0 comments on commit d803c3e

Please sign in to comment.