-
Notifications
You must be signed in to change notification settings - Fork 414
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
Conversation
There was a problem hiding this 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 }, |
There was a problem hiding this comment.
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.
Line 28 in 62da50e
map.setSourceContent( |
bin/linaria.js
Outdated
'', | ||
'Options:', | ||
' --help, -h Show help [boolean]', | ||
' --sourceMaps, -m Generate source maps [boolean]', |
There was a problem hiding this comment.
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]', |
There was a problem hiding this comment.
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)), |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
@thymikee can you check this? any feedback? |
There was a problem hiding this 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. |
There was a problem hiding this comment.
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
.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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
src/node.js
Outdated
} | ||
return { | ||
...output, | ||
[filename]: { css, map: map || null }, |
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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]', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing brackets?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
Would be good to update it per the new |
9e2e71a
to
296d0c9
Compare
I rebased the PR against master since there were conflicts. |
45523b7
to
fd4af7b
Compare
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 Please check and merge when you have time :) |
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
371fff8
to
fed3aa4
Compare
#214
Let's decide what options we want to support in CLI and over experience, then I'll add the docs.