serby / compact Goto Github PK
View Code? Open in Web Editor NEWA simple JavaScript compacting middleware for express
A simple JavaScript compacting middleware for express
This means some JS libs can't be used with compact safely, e.g Jade. See mishoo/UglifyJS#361
When I have multiple scripts and I use compactJsHtml()
in debug mode, I often get
Uncaught SyntaxError: Unexpected end of input
in my browser console for one or more scripts. It turns out that the script hasn't been completely copied from src
to dest
before the page is served.
I see that the copying is being done using util.pump, which has been deprecated in favor of stream.pipe. So that may fix the issue.
Looks good, but is there a way to have it regenerate each time the underlying code changes? Looks like I have to restart the server each time. But I'm new to node and so maybe I'm "doing it wrong."
Thx
Because there's only one compactJsHtml
function, compact doesn't allow you to create, say, one set of JS files to be concatenated in the <head>
and another set to be concatenated at the end of the <body>
.
How about an API that looks like this:
// Express configuration
app.use(compact.middleware({
head: ['namespace1'],
foot: ['namespace2']
});
// Jade template
html
head
!= compact.head.html()
body
block content
!= compact.foot.html()
The ThrowError code is missing from should lib.
If you clone the latest version of should.js and copy the should.js lib file over the top of the exciting one. The Tests run fine.
But installing it from npm does not seem to bring in the lastest version.
Just a quick heads up that anyone wanting to use this on Express 3.x needs to change line 201 in compact.js as app.helpers are not used anymore. You simply have to change it to app.locals
I would create a pull request but it seems trivial for one simple word change
It would be nice to have a feature in compact that lets you reference third party scripts.. e.g jQuery on Google's CDN, Google Maps API, various share widgets (Twitter, +1, Facebook).
e.g:
compact.addNamespace('find-us')
.addThirdPartyJs('http://maps.google.com/maps/api/js?sensor=true')
.addJs('/js/my-map.js');
Output:
<script src='http://maps.google.com/maps/api/js?sensor=true'></script>
<script src="/js/compact/find-us.js"></script>
Similar functionality is easily achievable with Jade template inheritance, but I think it would be better if all of the frontend JS was organised in one place. What do you think?
The slashes in the script tag become \ in windows and FF doesn't correct it
It seems to work after changing filename in line 255 to
filename.split(path.sep).join('/')
Trying to migrate from minj and I encountered this issue...
Every time I request a page in the browser, the timestamp of the compacted js file(s) is updated. This was causing nodemon to restart the server on each request. I've added the compated directory to my .npmignore
which prevents this issue, but I am confused as to why the timestamp is updated, and don't think it should be.
I'm adding scripts (using addJs()) to my namespace, but in the compacted file, the order isn't maintained. Example:
compact.addNamespace('edit');
compact.ns.edit
.addJs('ace/ace.js')
.addJs('ace/mode-html.js')
.addJs('ace/mode-css.js')
.addJs('ace/mode-javascript.js')
.addJs('edit.js');
The scripts are added to the compacted file in this order: mode-html.js -> mode-css.js -> mode-javascripts.js -> edit.js -> ace.js
I was wondering if there is any way to fix this? Thanks.
The files (from different locations) with the same name get compiled to the same place, so the last one gets served. Perhaps need some timestamp or uid string in the filename or something?
The helpers compactJsHtml()
and compactJs()
are attached to app.helpers
, which means that they exist for the duration of the application, not the request (as expected).
Here is a problem that is causes:
app.js
app.get('/', routes.index); // No JS on page
app.get('/edit', compact.js(['edit']), routes.edit); // `edit` JS on the page
layout.jade
(inherited by index and edit views)
//...
if (compactJsHtml !== 'undefined')
!=compactJsHtml()
//...
/
then the page is output as desired: no JS./edit
then the page is output as desired: edit
JS/
output the edit
JS because compactJsHtml
is still defined from the request to/edit
path.existsSync is now called fs.existsSync
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.