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

feat: add node API and cli #227

Merged
merged 4 commits into from
Jan 7, 2019
Merged

feat: add node API and cli #227

merged 4 commits into from
Jan 7, 2019

Conversation

zamotany
Copy link
Contributor

#214

Let's decide what options we want to support in CLI and over experience, then I'll add the docs.

@callstack-bot
Copy link

callstack-bot commented Sep 23, 2018

Hey @zamotany, thank you for your pull request 🤗.
The coverage report for this branch can be viewed here.

Copy link
Member

@satya164 satya164 left a comment

Choose a reason for hiding this comment

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

I think in a library, I would usually want to:

  • generate a single CSS file, we should just be able concat all CSS files for this
  • when generating multiple CSS files, I would want to insert require statements to the JS files so the CSS is actually picked up, we would probably want to accept the directory where the built JS files are, to insert require statements

src/node.js Outdated
const { css, map } = transform(filename, code, sourceMaps);
return {
...output,
[filename]: { css, map: map || null },
Copy link
Member

Choose a reason for hiding this comment

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

Also need to set the source content for the source maps so it knows what's in the source JS file. e.g.

map.setSourceContent(

bin/linaria.js Outdated
'',
'Options:',
' --help, -h Show help [boolean]',
' --sourceMaps, -m Generate source maps [boolean]',
Copy link
Member

Choose a reason for hiding this comment

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

Maybe -s is nicer. Other CLIs such as Babel use -s to denote source maps.

bin/linaria.js Outdated
'Options:',
' --help, -h Show help [boolean]',
' --sourceMaps, -m Generate source maps [boolean]',
' --outDir, -o Output directory [string]',
Copy link
Member

Choose a reason for hiding this comment

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

Camelcase isn't common in CLIs, so probably --source-maps and --out-dir is better instead of --sourceMaps and --outDir

bin/linaria.js Outdated
mkdir.sync(outDir);
Object.keys(styles).forEach(filename => {
fs.writeFileSync(
path.join(outDir, path.basename(filename)),
Copy link
Member

Choose a reason for hiding this comment

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

We would probably also want to preserve the directory structure, otherwise, CSS files may overwrite each other when 2 JS files have the same name

bin/linaria.js Outdated

mkdir.sync(outDir);
Object.keys(styles).forEach(filename => {
fs.writeFileSync(
Copy link
Member

Choose a reason for hiding this comment

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

We should check if there is any CSS first before writing the file, and if CSS is empty, skip writing

bin/linaria.js Outdated
if (styles[filename].map) {
fs.writeFileSync(
path.join(outDir, `${path.basename(filename)}.map`),
styles[filename].map
Copy link
Member

Choose a reason for hiding this comment

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

Probably should also add a sourceMapURL in the CSS file.

@zamotany zamotany requested a review from thymikee September 27, 2018 15:49
@zamotany
Copy link
Contributor Author

@thymikee can you check this? any feedback?

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

LGTM but I'm checking on a phone :D

bin/linaria.js Outdated

fs.writeFileSync(cssOutput, cssContent);

// Add require to original JS file to import extracted CSS.
Copy link
Member

Choose a reason for hiding this comment

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

We don't want to add require to the original source files. We should accept a path to the directory where the user has built JS files and insert them there.

bin/linaria.js Outdated Show resolved Hide resolved
bin/linaria.js Outdated Show resolved Hide resolved
bin/linaria.js Outdated
.replace(path.extname(jsFilename), '.css');
const cssOutput = path.join(outDir, folderStructure, cssFilename);

// Skip writing if the file already exists and the new CSS code is empty.
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if we should skip writing if CSS file exists. If I change something in the JS and run it again, I'd expect the CSS files to be updated. If we skip it, they'll never be updated unless you delete the existing files first manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please ready carefully: Skip writing if the file already exists and the new CSS code is empty

It was done per your request:

We should check if there is any CSS first before writing the file, and if CSS is empty, skip writing

Copy link
Member

Choose a reason for hiding this comment

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

I didn't mean if the file already exists, I said if there is any CSS i.e. if there's any CSS text.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok got it

bin/linaria.js Outdated
const cssOutput = path.join(outDir, folderStructure, cssFilename);

// Skip writing if the file already exists and the new CSS code is empty.
if (!fs.existsSync(cssOutput) || styles[jsFilename].css) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of checking styles[jsFilename].css here, the node API should avoid adding the file into the result if there's no CSS

bin/linaria.js Outdated Show resolved Hide resolved
src/node.js Outdated
}
return {
...output,
[filename]: { css, map: map || null },
Copy link
Member

Choose a reason for hiding this comment

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

Probably should return map.toString() instead of the generator instance

bin/linaria.js Outdated
' --config, -c Path to Babel config file string]',
'',
'Example:',
' linaria -m ./file1.js ./file2.js',
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be updated

bin/linaria.js Outdated
' --source-maps, -s Generate source maps [boolean]',
' --require-css, -r Require CSS in original JS file [boolean]',
' --out-dir, -o Output directory [string]',
' --config, -c Path to Babel config file string]',
Copy link
Member

Choose a reason for hiding this comment

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

missing brackets?

Copy link
Member

Choose a reason for hiding this comment

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

also should mention which options are required and throw if not provided. I'd recommend using something like yargs to make it easier instead of writing it manually https://github.com/yargs/yargs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

None of the options are required. Only arguments are.

Copy link
Member

Choose a reason for hiding this comment

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

@zamotany --out-dir should be required, no? otherwise would be too easy to pollute working directory with css files by mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be, I think babel for example doesn't require you to provide out dir. Or maybe we should set some default like build? or css_dist?

Copy link
Member

Choose a reason for hiding this comment

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

maybe extracted_css or something. but maybe better just to require it. I'd want to change it anyways.

@satya164 satya164 mentioned this pull request Sep 28, 2018
12 tasks
@satya164
Copy link
Member

Would be good to update it per the new transform function. Also maybe we should expose transform for the public API and the cli for working with files?

@satya164 satya164 force-pushed the feat/node-api-and-cli branch 4 times, most recently from 9e2e71a to 296d0c9 Compare December 16, 2018 00:50
@satya164
Copy link
Member

I rebased the PR against master since there were conflicts.

@satya164 satya164 force-pushed the feat/node-api-and-cli branch 2 times, most recently from 45523b7 to fd4af7b Compare January 6, 2019 02:36
@satya164
Copy link
Member

satya164 commented Jan 6, 2019

Hey, I fixed the behavior of inserting require statements (it was supposed to insert them in a given directory with compiled JS files, not the source files).

I also replaced commander with yargs because there was barely any validation of the arguments in commander. yargs also seems easier to use and the parsed arguments are typed, which is a plus.

Please check and merge when you have time :)

src/cli.js Outdated Show resolved Hide resolved
src/cli.js Outdated Show resolved Hide resolved
refactor: replace commander with yargs

yargs has better validation options and seems easier to use than commander. the parsed args are typed as well, which is a bonus

fix: pass options when evaluating a component

chore: release 1.0.0-beta.4

Update src/cli.js

Co-Authored-By: zamotany <[email protected]>
fix typo

Co-Authored-By: zamotany <[email protected]>
feat: support configuring linaria with a config
file

The babel preset is used in several ways:
- babel config
- webpack loader
- rollup plugin
- stylelint preprocessor

Having a config file makes it possible to configure it in one place instead of configuring separately for each tool.

Merge branch 'master' into feat/node-api-and-cli
@zamotany zamotany force-pushed the feat/node-api-and-cli branch from 371fff8 to fed3aa4 Compare January 7, 2019 12:20
@satya164 satya164 merged commit 8ace1f3 into master Jan 7, 2019
@satya164 satya164 deleted the feat/node-api-and-cli branch January 7, 2019 12:34
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.

4 participants