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

clean-css ignore comments throw errors when source map is built #1088

Closed
dougludlow opened this issue Oct 17, 2019 · 1 comment
Closed

clean-css ignore comments throw errors when source map is built #1088

dougludlow opened this issue Oct 17, 2019 · 1 comment
Labels

Comments

@dougludlow
Copy link

dougludlow commented Oct 17, 2019

Possibly related to #1042. I noticed this while running a build with Angular CLI (ng build --prod --source-map). I was able to reproduce the issue with clean-css-cli and have provided details below.

Environment

  • clean-css version: 4.2.1
  • node.js version: v10.16.2
  • operating system: macOS Majave 10.14.6

Reproducible code

echo "/* clean-css ignore:start */a { margin: 0px; }/* clean-css ignore:end */" | npx [email protected] --source-map -o ~/Desktop/style.css

Error

/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/apply-source-maps.js:72
    metadata = scope[2][0];
                       ^

TypeError: Cannot read property '0' of undefined
    at findTokenSource (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/apply-source-maps.js:72:24)
    at doApplySourceMaps (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/apply-source-maps.js:46:14)
    at applySourceMaps (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/apply-source-maps.js:33:5)
    at Object.callback (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:25:12)
    at doInlineImports (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:200:25)
    at inline (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:178:10)
    at fromStyles (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:151:5)
    at fromString (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:48:10)
    at doReadSources (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:33:12)
    at readSources (/Users/doug.ludlow/.npm/_npx/99367/lib/node_modules/clean-css-cli/node_modules/clean-css/lib/reader/read-sources.js:24:10)

Possible solution

While debugging this issue, I found that code surrounded with /* clean-css ignore:<start|end> */ comments have a token type of raw (Token.RAW) and resemble a shape like this:

[ 'raw', 'a { margin: 0px; }', [ [ 1, 28, undefined ] ] ]

As such, the following line of code could probably be modified as follows:

https://github.com/jakubpawlowicz/clean-css/blob/3b2d0d23acf7582516a34f1f32e4dc84a830351a/lib/reader/apply-source-maps.js#L68

- if (token[0] == Token.AT_RULE || token[0] == Token.COMMENT) {
+ if (token[0] == Token.AT_RULE || token[0] == Token.COMMENT || token[0] == Token.RAW) {

However, I'm not exactly aware of the implications related to that associated change. Let me know if that change makes sense and if you'd like me to open a pull request.

@jakubpawlowicz
Copy link
Collaborator

Hey @dougludlow - I have just pushed a fix based on your suggestion. That was it! Many thanks for debugging it to that point!
The change will land in v5 soon #1135

Sorry for not taking care of this patch earlier, but I'm catching up with clean-css maintenance only now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants