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

Easier way to run transforms on passthrough copy files #344

Closed
Rangoli-Software opened this issue Dec 15, 2018 · 9 comments
Closed

Easier way to run transforms on passthrough copy files #344

Rangoli-Software opened this issue Dec 15, 2018 · 9 comments
Labels
enhancement feature: 🗄 passthrough copy Related to the passthrough file copy feature

Comments

@Rangoli-Software
Copy link

I'm trying to add a transform to minify my "*.js" file. ".js" is listed in the templateFormats, and passThroughFileCopy is true. I've modified the example in Add Filter to create the transform.

eleventyConfig.addTransform("jsmin", function(content, outputPath) {
if( outputPath.endsWith(".js") ) {
let minified = uglifyjs.minify(content);
if ( minified.error ) {
console.log("Uglify JS error: ", minified.error);
}
return minified.code;
}
return content;
});

However, nothing seems to be happening. It looks like transforms are only applied to files that actually have some template processing.

How can I minify my js files?

@okitavera
Copy link

It looks like transforms are only applied to files that actually have some template processing.

Exactly. passthroughFileCopy: true does copy files with non-matching template file extensions directly to your output directory without modification.

So, you can consider processing your js files with external tool like grunt, gulp, webpack, etc, or inlining your js through the addFilter API; especially if you had a small js file and you want to reduce the number of http requests
https://www.11ty.io/docs/quicktips/inline-js/

@zachleat
Copy link
Member

Correct! Upvote #117 which will allow you to do this much easier!

@Rangoli-Software
Copy link
Author

Upvoted. At the moment, I'd prefer to avoid adding any other dependencies like webpack, etc.

@zachleat zachleat changed the title JS minify as a transform Easier way to run transforms on passthrough copy files Dec 17, 2018
@zachleat zachleat added enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. labels Dec 17, 2018
@zachleat
Copy link
Member

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

@jevets
Copy link

jevets commented Apr 19, 2019

@Rangoli-Software

I'm facing a similar problem (but with CSS). I also do not want to add additional dependencies like gulp, grunt, webpack, etc.

For now I'm using a "real" template file (.njk in my case) that simply defines its own permalink and includes a "static" asset file that will be transformed.

It would be nice if files added via addPassthroughCopy() could be transformed via addTransform(). This works for me now, though #117 would cover it too.

The only reason for the include() is so my editor picks up the correct syntax. Otherwise I'd keep the actual CSS right in this file.

// stylesheet.njk
---
permalink: /stylesheet.css
---
{% include "css/main.css" %}
// _includes/css/main.css

body { font-family: 'Comic Sans MS' }
a { color: salmon }
// .eleventy.js

// ...
config.addTransform('doTheCSSWork', (content, outputPath) => {
  if (outputPath.endsWith('.css')) {
    return processTheCSS()
  }
  return content
})

@Ryuno-Ki
Copy link
Contributor

Nice hack!

@ki9us
Copy link

ki9us commented Mar 7, 2020

passthroughFileCopy: true does copy files with non-matching template file extensions directly to your output directory without modification.

OK, but I feel like transforms should work on passthroughFileCopy'd files though. Here is my reasoning:

  1. It's trivial to perform checks in the transform to ensure it only runs on certain files. The example in the docs checks that the extension is .html.
  2. It's not trivial to implement a transform on any other file type, as can be seen by jevet's kludgy workaround, or the suggestion to incorporate an entire second build system like gulp or grunt.

Considering these two points, I just don't see any advantage to the current behavior.

#117 would fix this, to be sure. If #117 were able to extend existing renderers instead of overwriting them, I guess transforms would be obsoleted anyway.

@necolas
Copy link

necolas commented Jan 11, 2021

Is there an active issue to track this feature? I need this too and agree that simply minifying external css/js files shouldn't be this difficult vs working with inline css/js.

@zachleat zachleat added feature: 🗄 passthrough copy Related to the passthrough file copy feature and removed needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. labels Jun 24, 2022
@zachleat zachleat added this to the Eleventy 2.0.0 milestone Jun 24, 2022
@zachleat
Copy link
Member

As #1686 was merged, this will unlock passing a transform callback to recursive-copy. https://github.com/timkendrick/recursive-copy#advanced-options

Shipping with 2.0.0-canary.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature: 🗄 passthrough copy Related to the passthrough file copy feature
Projects
None yet
Development

No branches or pull requests

7 participants