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

WIP: Add PostCSS's SugarSS recognition by default #1689

Closed
wants to merge 6 commits into from

Conversation

DoumanAsh
Copy link

Personally I find SugarSS is pretty nice simplified version of CSS syntax.

Since its addition is configured by setting corresponding parse in PostCSS's config, I wonder if it would be ok to add its extension by default?

@Kompwu
Copy link

Kompwu commented Jul 9, 2018

@DoumanAsh Can you help me ?
I'm trying to get .sss bundled...

@DoumanAsh
Copy link
Author

@Kompwu Well as far as I understand currently you'd need to register this extension to be recognized as CSS asset:

const Bundler = require('parcel-bundler');

let bundler = new Bundler('input.js');
bundler.addAssetType('.sss', './assets/CSSAsset');

If I understand this API correctly(I'm currently decided to use plain CSS with nesting support

@Kompwu
Copy link

Kompwu commented Jul 9, 2018

@DoumanAsh Thanks,
How can i make the same thing without js ?
Edit: @DeMoorJasper Can you help me ?

@Kompwu Kompwu mentioned this pull request Jul 9, 2018
@DoumanAsh
Copy link
Author

@Kompwu Sorry I'm not sure as I'm new to parcel myself

@Kompwu
Copy link

Kompwu commented Jul 10, 2018

@DeMoorJasper Can you merge it pls ?

@DeMoorJasper
Copy link
Member

It would be nice if the sugarSS asset actually installed and configured postCSS for the user, this way it works zero-config, you can probably create an asset that runs sss through postCSS and then passes it to css. (Hope this makes any sense, lemme know if you need any help)

@DoumanAsh
Copy link
Author

@DeMoorJasper Ok, I'll try to approach with this suggestion. But what should we do if user already has config?

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jul 11, 2018

@DoumanAsh You can probably load the PostCSS config and check for SugarSS and just forward it to CSS if it's already installed and configured I guess?

Copy link

@Kompwu Kompwu left a comment

Choose a reason for hiding this comment

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

You should load the PostCSS configuration and check for SugarSS & transpile it to CSS if it's already installed and configured.
If not, configure PostCSS & SugarSS & transpile it to CSS.

@DoumanAsh
Copy link
Author

I tried to give it a go by extending existing CSS asset, but it makes me think that it is rather difficult to do.

The problem is not only to load postcss config, but there is also parse method that requires you to specify syntax so I'd need to load sugarss in two places it seems

@Kompwu
Copy link

Kompwu commented Jul 15, 2018

@DoumanAsh Do what you can and we'll help you with everything else.

@Kompwu
Copy link

Kompwu commented Jul 17, 2018

@DoumanAsh How's going?

@DoumanAsh
Copy link
Author

@Kompwu Sorry, I've been a bit busy lately, I'll try to resume working on it as soon as I have time

@DoumanAsh
Copy link
Author

Currently I have issue with PostCSS API usage as it seems to cause errors when I attempt to parse .sss:
```CssSyntaxError: Missed semicolon````

Though I did set let root = postcss.parse(code, {from: this.name, to: this.name, syntax: sugarss}) corresponding parser.

I was thinking that I could parse SugarSSS into CSS and then rely on existing CSSAsset but it seems I misunderstood the API as it ignores provided by me syntax.

@Kompwu
Copy link

Kompwu commented Jul 29, 2018

@DoumanAsh Any news ?
@devongovett Hey, can you help us ?

@DoumanAsh
Copy link
Author

@Kompwu Sorry, recently I'm busy with other things and didn't look into PostCSS

@Kompwu
Copy link

Kompwu commented Aug 6, 2018

@DoumanAsh How's going ?

@DoumanAsh
Copy link
Author

@Kompwu Sorry I'm not actively using parcel as of the moment and kinda busy so I'm not planning to resume work on it until need arise again.

I don't mind someone to carry on, or leave issue open so that me or someone else could continue later on.

@Kompwu
Copy link

Kompwu commented Aug 9, 2018

@ai Hi, can you take a look ?

}

parse(code) {
let root = postcss.parse(code, {from: this.name, to: this.name, syntax: sugarss});
Copy link

Choose a reason for hiding this comment

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

syntax => parser.

We need to set SugarSS only as input language (parser) and keep output language (stringifier) as default CSS

Copy link
Author

Choose a reason for hiding this comment

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

I think I also tried it initially, but for some reason postcss still parsed code as if it was CSS

Copy link

Choose a reason for hiding this comment

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

I will try to run tests after Def Con

Copy link

Choose a reason for hiding this comment

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

I can try if you want.

Copy link

Choose a reason for hiding this comment

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

Yeap, it should be parser:.

Copy link

Choose a reason for hiding this comment

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

So all that remains is to automate the configuration?

Copy link
Member

@DeMoorJasper DeMoorJasper left a comment

Choose a reason for hiding this comment

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

Just locking this for merge untill this is finished

@DeMoorJasper DeMoorJasper changed the title Add PostCSS's SugarSS recognition by default WIP: Add PostCSS's SugarSS recognition by default Aug 11, 2018
@Kompwu
Copy link

Kompwu commented Aug 18, 2018

@DeMoorJasper Are you sure automating configuration is a good thing?
I don't think Parcel should do it.

@DeMoorJasper
Copy link
Member

@Kompwu We are not automating config, we're adding a default?

I just thought it might be useful to actually have something with extension sugarSS be transpiled to css by default (it should still read the config and potentially even just pass along the sugarss file to postCSS without any transforms if it's configured). The rest of the PostCSS transforms will still apply as far as I know...

@Kompwu
Copy link

Kompwu commented Aug 19, 2018

@DoumanAsh Can you finish?
All that remains is to add a default configuration if there is none.

@DoumanAsh
Copy link
Author

@Kompwu To be honest I'm not sure why, but even when I place parser, it still fails in simple integration test.

The current commit also makes sugarss required dependency, would it be possible to make it optional?

@DeMoorJasper
Copy link
Member

How does it add it as a dependency, you could use localRequire, that's how it's supposed to be used.

}

parse(code) {
let root = postcss.parse(code, {from: this.name, to: this.name, parser: sugarss});
Copy link
Member

Choose a reason for hiding this comment

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

let sugarss = await localRequire('sugarss')

@@ -0,0 +1,35 @@
const postcss = require('postcss');
const sugarss = require('sugarss');
Copy link
Member

Choose a reason for hiding this comment

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

should be a local require and a dev dependency for testing...

}
}

class CSSAst {
Copy link
Member

Choose a reason for hiding this comment

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

Split this up into a seperate file, so both CSS and SugarSS Asset use it

if (config.parser === undefined && asset.options.css_parser !== undefined) {
const name = asset.options.css_parser;

if (!(typeof name === "string")) throw "Internal Error: Invalid PostCSS parser type. Should be string";
Copy link
Member

Choose a reason for hiding this comment

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

typeof name !== "string"


if (!(typeof name === "string")) throw "Internal Error: Invalid PostCSS parser type. Should be string";

let parser = await localRequire(name, asset.name).catch(() => require(name));
Copy link
Member

Choose a reason for hiding this comment

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

why does it catch with a require, that's what localRequire already does?

localRequire's should just throw if it fails, because require is just the same thing. (Unless you want to require something from inside parcel, but that should happen inside the transform not be a fallback as this could have unwanted side-effects)


class SSSAsset extends CSSAsset {
constructor(name, options) {
options.css_parser = "sugarss";
Copy link
Member

Choose a reason for hiding this comment

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

Why is this necessary?

Copy link
Author

Choose a reason for hiding this comment

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

I wanted to propagate to transform to automatically load parser, if it is necessary.

But not sure if it is good idea

Copy link
Member

Choose a reason for hiding this comment

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

You already do a localRequire in transform so not sure why this is necessary at all.

@DoumanAsh
Copy link
Author

Thanks for review, I applied majority of comments.

I'm not sure though how to automate configuration for PostCSS though.

My simple integration test is still failing me:

  sugarss
    1) should correctly parse SugarSS asset


  0 passing (478ms)
  1 failing

  1) sugarss
       should correctly parse SugarSS asset:
     CssSyntaxError: Missed semicolon
      at Input.error (node_modules\postcss\lib\input.js:119:22)
      at Parser.checkMissedSemicolon (node_modules\postcss\lib\parser.js:544:26)
      at Parser.decl (node_modules\postcss\lib\parser.js:269:50)
      at Parser.other (node_modules\postcss\lib\parser.js:168:18)
      at Parser.parse (node_modules\postcss\lib\parser.js:84:26)
      at Function.parse (node_modules\postcss\lib\parse.js:24:16)
      at SSSAsset.parse (src\assets\SSSAsset.js:16:24)

@@ -0,0 +1,18 @@
class CSSAst {
Copy link
Member

Choose a reason for hiding this comment

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

Rename the file to CSSAst, as js and html also have an ast...

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking that it would be better for future to have common source of asts?

Copy link
Member

Choose a reason for hiding this comment

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

In the future every asset will have it's own package so it will be seperate anyways

}
}

module.exports.CSSAst = CSSAst;
Copy link
Member

Choose a reason for hiding this comment

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

Just make this the default export module.exports = CSSAst;

@DeMoorJasper
Copy link
Member

You could check if the postCSS config already has a sugarss transform setup and skip this transform entirely if it has. But in the test it probably shouldn't have any postCSS config. Otherwise you're basically just testing postCSS

@DeMoorJasper
Copy link
Member

I've changed some stuff around and it should be working better now. You might want to cleanup and debug it a bit more as it's not actually working right now, and def not clean

@DeMoorJasper
Copy link
Member

Gonna open up a new PR that bases of off this one, with fixed code

@DeMoorJasper DeMoorJasper mentioned this pull request Aug 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants