Skip to content

Refactoring, env, closing a bunch of issues #53

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

Merged
merged 28 commits into from
Mar 29, 2017
Merged

Refactoring, env, closing a bunch of issues #53

merged 28 commits into from
Mar 29, 2017

Conversation

denysdovhan
Copy link
Contributor

Aims to fix #52 and #41.

@denysdovhan denysdovhan changed the title Resolve presets WIP: Resolve presets Mar 9, 2017
index.js Outdated

try {
const babelrc = fs.existsSync(babelrcPath);
const packageConfig = JSON.parse(fs.readFileSync(packagePath));
Copy link
Contributor

Choose a reason for hiding this comment

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

require

index.js Outdated
if (!opts.presets) opts.presets = ['latest'];
if (opts.presets.length === 0) delete opts.presets;

// Include 'latest' preset only if:
Copy link
Contributor

Choose a reason for hiding this comment

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

Comments are too length and extra. Code is pretty self-explanatory.

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 better to leave a link to related issues? Just an reminder why this code shouldn't be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, please do.

@denysdovhan denysdovhan changed the title WIP: Resolve presets Resolve presets Mar 10, 2017
index.js Outdated
// * there's no .babelrc file in root directory
// * there's no 'babel' field in package.json
if (!opts.presets) {
const babelrcPath = path.resolve(config.paths.root, '.babelrc');
Copy link
Contributor

Choose a reason for hiding this comment

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

Extensions? Parent dirs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, thought about it.

I don't want to duplicate whole resolution logic from Babel. Maybe we could reuse it somehow?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, it is not in the public API afaik, but it may be worth exposing.

However, I don't see it blocking from merging this PR. Some time later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm gonna dig a bit in Babel's sources. I'll keep you updated.

@shvaikalesh
Copy link
Contributor

One more thing: eslint-config-brunch if you are up to. Thanks.

index.js Outdated
@@ -23,6 +23,17 @@ const prettySyntaxError = err => {
return error;
};

const lookup = (where, filename) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shvaikalesh here's a little bit complex resolution. What do you think?

@loganfsmyth
Copy link
Member

loganfsmyth commented Mar 10, 2017

Generally I think the Babel team as a whole has mixed feelings about enabling plugins/presets by default like this FYI, but it seems like that has already been released as a feature, right?

Given that, the currently-used approach (in babel-loader and babel-register) to resolving the config is the following:

const OptionManager = require('babel-core').OptionManager;

const opts = new OptionManager().init({
    // whatever options you'd pass to Babel normally.
});

// Check both, not just `plugins`.
const hasConfig = 
  (opts.plugins && opts.plugins.length > 0) ||
  (opts.presets && opts.presets.length > 0);

if (!hasConfig) {

}

It's an ugly API but please do not try to hardcode this. If we fix bugs or add new config methods like the newly-landed for 7.x .babelrc.js, your code will just break again.

@denysdovhan
Copy link
Contributor Author

denysdovhan commented Mar 11, 2017

@shvaikalesh reasonable notice from Babel's Slack:

image

Actually, this PR could be easily resolved by removing these lines:

-if (!opts.presets) {
-     opts.presets = ['latest'];
-     // ['env', opts.env]
-}

Unfortunately, it would break backward compat.

@paulmillr
Copy link
Contributor

I don't understand why "having defaults" is a bad idea. Brunch is all about simplicity and no bullshit. We don't want users to write configs when the program can think for them instead.

@denysdovhan denysdovhan changed the title Resolve presets WIP: Resolve presets Mar 11, 2017
@denysdovhan
Copy link
Contributor Author

@paulmillr okay. Anyway, I'm gonna find a better solutions. Will update my PR soon.

@loganfsmyth don't we run the risk using some kind of internal APIs? Probably, you would like to change Babel's internal API and then we will find ourselves in the situation when our users get broken builds.

@loganfsmyth
Copy link
Member

don't we run the risk using some kind of internal APIs? Probably, you would like to change Babel's internal API and then we will find ourselves in the situation when our users get broken builds.

I'm not sure what you mean? The point of the OptionManager API is to get just the data you're asking about.

@shvaikalesh
Copy link
Contributor

Agreed with Paul here. We should have cool defaults, like babel-preset-env. Related: #54

@denysdovhan
Copy link
Contributor Author

I'm not sure what you mean?

@loganfsmyth OptionManager is an internal API, isn't it? That means you wouldn't warn us when you change that API.

@loganfsmyth
Copy link
Member

It definitely won't break in 6.x and I'm planning to specifically leave a dummy class of export class OptionManager { init(opts){ ...call internal api... } } in 7.x for backward-compatibility because we want the transition from 6.x to 7.x to be as seamless as possible for people.

It's used by babel-register and babel-loader, which are both independent of babel-core, so any change to it at this point would also break those, which is a no-go for us.

test.js Outdated
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected))
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
}, error => assert(!error));
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected))
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
}, error => assert(!error));
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected))
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated

return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected))
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated

return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected))
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
it('should properly link to source file in source maps', () =>
sourceMapPlugin.compile({data: content, path})
.then(result => {
JSON.parse(result.map).should.not.throw;
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
);

it('should properly link to source file in source maps', () =>
sourceMapPlugin.compile({data: content, path})
Copy link
Contributor

Choose a reason for hiding this comment

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

return

test.js Outdated
JSON.parse(result.map).should.not.throw;
JSON.parse(result.map).sources.should.contain(path);
})
.catch(should.not.throw)
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
}, error => assert(!error));
return plugin.compile({data: content, path: 'file.js'})
.then(result => JSON.parse(result.map).should.not.throw)
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
done();
}, error => assert(!error));
return plugin.compile({data: content, path: 'file.js'})
.then(result => JSON.parse(result.map).should.not.throw)
Copy link
Contributor

@shvaikalesh shvaikalesh Mar 21, 2017

Choose a reason for hiding this comment

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

should.not.throw is extra

test.js Outdated
}, error => assert(!error));
return plugin.compile({data: content, path: 'vendor/file.js'})
.then(result => result.data.should.be.equal(content))
.catch(should.not.throw);
Copy link
Contributor

Choose a reason for hiding this comment

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

extra

test.js Outdated
it('should be an object', function() {
assert(plugin);
});
it('should have #compile method', () =>
Copy link
Contributor

Choose a reason for hiding this comment

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

why return Assertion to mocha?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does Mocha care about what is returned?

Anyway, yeah, that's not a good practice. Gonna add curly brackets around it.

@denysdovhan
Copy link
Contributor Author

Now babel-brunch uses env, detects UglifyJS and sets corresponding target if it's needed. At this point, it should be enough to fix #52, #54 and #41.

@shvaikalesh @paulmillr what do you think about targets options for babel-brunch proposed by @yavorsky? (#53 (comment))

index.js Outdated
try {
const pkgPath = path.resolve(pkgFile);
const isProduction = process.env.NODE_ENV === 'production';
const isUglify = 'uglify-js-brunch' in require(pkgPath).devDependencies;
Copy link
Contributor

Choose a reason for hiding this comment

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

require('uglify-js-brunch')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

neat

@denysdovhan denysdovhan changed the title WIP: Refactoring, env, closing a bunch of issues Refactoring, env, closing a bunch of issues Mar 27, 2017
@denysdovhan
Copy link
Contributor Author

Done, I think. Can be merged.

index.js Outdated
if (!this.options.presets) {
const babelConfig = new babel.OptionManager().init(this.options);
const hasConfig =
babelConfig.presets && babelConfig.presets.length > 0 ||
Copy link
Contributor

Choose a reason for hiding this comment

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

JS is not Ruby. 0 is falsy.

Choose a reason for hiding this comment

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

JS is not Ruby. 0 is falsy.

Correct me if I'm wrong, and it's not immediately clear, but this should work because @denysdovhan is checking length > 0.

I suggest wrapping the comparison check in parens so it is more obvious that you're using a comparison operator.

const hasConfig =
  babelConfig.presets && (babelConfig.presets.length > 0) ||
  babelConfig.plugins && (babelConfig.plugins.length > 0);

Copy link
Contributor Author

@denysdovhan denysdovhan Mar 29, 2017

Choose a reason for hiding this comment

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

@millerized @shvaikalesh is right. Doesn't matter. This should work too:

const hasConfig =
  babelConfig.presets && babelConfig.presets.length ||
  babelConfig.plugins && babelConfig.plugins.length;

@millerized
Copy link

Hi @denysdovhan. I would offer to help contribute and get this past the finish line, but it looks like you are almost there.

I am looking forward to using brunch with env support – great work! 🍻

@paulmillr paulmillr merged commit df0e92b into babel:master Mar 29, 2017
@webdif webdif mentioned this pull request Aug 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

transform-class-properties in .babelrc results in error
6 participants