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

support user .babelrc #347

Merged
merged 34 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6a09a3c
WIP on babelrc proof of concept
jaredmcdonald Oct 24, 2016
f0a5d22
remove now-unused babel dependencies from main package.json
jaredmcdonald Oct 24, 2016
0cd4bfa
better solution for resolving all plugins and presets in user .babelrc
jaredmcdonald Oct 24, 2016
3aaf007
parameterize inclusion of babel runtime, and include react inline ele…
Oct 24, 2016
951762a
Merge branch 'master' into babelrc
Oct 25, 2016
fe59ec1
create .babelrc in starter kyt setup
Oct 25, 2016
99cc9a6
Update package.json
jaredmcdonald Nov 24, 2016
728376a
Update package.json
jaredmcdonald Nov 24, 2016
1a20d71
remove unused RHL dependency
jaredmcdonald Nov 24, 2016
fde2c16
Merge branch 'master' into babelrc
jaredmcdonald Nov 24, 2016
0b51740
use var instead of const in packages, these don't necessarily need to…
jaredmcdonald Nov 24, 2016
8ed645f
alpha.0, keywords, files
jaredmcdonald Nov 24, 2016
851331f
add babel-plugin-react-transform-jsx-source, missed in merge earlier
jaredmcdonald Nov 24, 2016
fb67e16
port over work to resolve babel plugins and presets without prefixes
jaredmcdonald Nov 24, 2016
1f1497c
Merge branch 'master' into babelrc
jaredmcdonald Dec 9, 2016
b1ff996
Merge branch 'master' into babelrc
jaredmcdonald Dec 9, 2016
2bcb1f8
alpha.1 babel presets
jaredmcdonald Dec 9, 2016
631ea4f
use prepublished alpha.1 kyt core babel preset
jaredmcdonald Dec 9, 2016
2a0b9cd
starter-kyts should use babel-preset-kyt-react
jaredmcdonald Dec 9, 2016
428543e
add e2e tests that don't pass yet
jaredmcdonald Dec 12, 2016
81cf6b5
Merge branch 'master' into babelrc
jaredmcdonald Dec 12, 2016
1c653e2
e2e tests pass
jaredmcdonald Dec 12, 2016
f1b01ed
webpack should default to babel-preset-kyt-core if no user babelrc ex…
Dec 13, 2016
d0c0c18
jest preprocessor should use kyt-core babel preset if no .babelrc is …
Dec 13, 2016
001e202
es5 the babel-presets further, remove unused .babelrc in kyt-core
Dec 13, 2016
877853a
warn if no user .babelrc
Dec 13, 2016
fbffeac
kyt-core doesn't use these plugin/preset dependencies anymore
Dec 13, 2016
406c0e1
more mocking in tests to suppress errant log warnings
Dec 13, 2016
8ef59f8
fix lint
Dec 13, 2016
f6ad967
log erros and exit if a babel plugin or preset is not resolvable
Dec 14, 2016
e04c035
move npm install for root first lint resolve rules pass in e2e tests …
Dec 14, 2016
7b69ecf
fix tests, unclear why these only started failing on this branch
Dec 14, 2016
18fbc8f
add another line to make the frozen-ness tests more clear
Dec 14, 2016
9c8ab1d
kyt-starter-test is up to date now
Dec 14, 2016
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
5 changes: 4 additions & 1 deletion e2e_tests/tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ describe('KYT CLI', () => {
expect(shell.test('-d', 'node_modules')).toBe(true);
});
it('sets up a starter-kyt', () => {
const setupURL = 'https://github.com/NYTimes/kyt-starter-test.git';
// temporary, revert when https://github.com/NYTimes/kyt-starter-test/pull/1 is merged
const setupURL = 'https://github.com/jaredmcdonald/kyt-starter-test.git';
const output = shell.exec(`node_modules/.bin/kyt-cli setup -r ${setupURL}`);
expect(output.code).toBe(0);
const setupArr = output.stdout.split('\n');
Expand All @@ -54,13 +55,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
Copy link
Contributor

Choose a reason for hiding this comment

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

How come?

Copy link
Member

Choose a reason for hiding this comment

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

I'm also curious of the argument for using < ES2015 in the presets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe this is naive but I figured there's nothing about these presets that really requires them to be used inside kyt, so might as well make them es5 compatible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW these .eslintrc.jsons aren't even honored right now since the root package's lint ignores them (to work around needing to get the eslint config cascade working)

}
}
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": [
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do this for our other packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably!

"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`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe append a time stamp so there's no collisions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

date is set to Date.now() above so I think we should be ok here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh sorry missed the date bit, yeap perfect 👍

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
65 changes: 0 additions & 65 deletions packages/kyt-core/config/__tests__/babel.test.js

This file was deleted.

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...');
});
});
51 changes: 0 additions & 51 deletions packages/kyt-core/config/babel.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/kyt-core/config/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = (rootDir, aliases = {}) => ({
resolveFromUtils('file.stub'),
'^[./a-zA-Z0-9$_-]+\\.(css|scss)$':
resolveFromUtils('style.stub'),
// when this is removed from the base webpack config, we can likely
// remove the runtime and include the polyfill in the test environment
'babel-runtime': require.resolve('babel-plugin-transform-runtime'),
},
aliases
),
Expand Down
Loading