npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

manage-htaccess

v1.0.0

Published

Enable or disable statement blocks / features within .htaccess based on some state.

Downloads

5

Readme

manage-htaccess


Enable or disable statement blocks / features within .htaccess based on some state from node.js

Example of use case

Can be used when developing stuff on non-webapp-dedicated LAMP server whlist using Webpack build system / module bundler and webpack-dev-server for hot reloading.

  • When building for deploy: files for js, css, images, fonts etc. are spewed out in filesystem (i.e., http://devserver.tld/path/to/app/assets/site.js)
  • When building for development: webpack-dev-server hot reloading is used - js is served by node.js on some port, css is inlined within js, images are somewhere in between (i.e., http://devserver.tld:1234/site.js)

This means that paths change (HTML asking for js, CSS asking for images etc.). A really simple proxying (or in this case just url rewriting) does the job.

This hack comments or uncomments tagged blocks in .htaccess based on what environment (target) combinations webpack is run for.

Usage

/**
 * Htaccess managing function
 * Reference:
 * @param  {Array} Array containing objects that describe tags, their state
 * @param  {String} Path to .htaccess file
 * @param  {String} Special string (.htaccess comment) that is used for enabling/disabling blocks
 * @return
 */

An example for webpack.config.js

'use strict';

// development by default
const production = process.env.NODE_ENV === 'production';
const staging = process.env.NODE_ENV === 'staging';

const webpackMyHtaccess = require('manage-htaccess');

webpackMyHtaccess(
  [
    {
      tag: 'DUMMY',
      enabled: staging,
    },
    {
      tag: 'MYAWESOMETAG',
      enabled: !production,
      attributes: {
        port: 1234
      }
    },
    {
      tag: 'MYOTHERTAG',
      enabled: staging,
    }
  ],
  path.join(__dirname, '.htaccess'), // optional
  '#%!' // optional
);

let config = {
  entry: {
    site: path.join(__dirname, 'src/site.js')
  },
  output: {
    path: path.join(__dirname, 'abc/def'),
    publicPath: 'http://devserver.tld/path/to/abc/def/',
    filename: "[name].js"
  }
};

config.module = {
	....
};

config.plugins = [
	...
];

module.exports = config;

Sample .htaccess config

<IfModule mod_rewrite.c>
    
    RewriteEngine On
    
#%!<MYAWESOMETAG>
    # Proxy assets in memory to webpack-dev-server when developing
    # Disable during production
    RewriteRule ^abc\/def/(.+) http://devserver.tld:1234/path/to/abc/def/$1 [P]
#%!</MYAWESOMETAG>

</IfModule>

#%!<MYOTHERTAG>
    # Whatever
#%!</MYOTHERTAG>

Example HTML/PHP file assumed to be at abc. In this configuration public/site.js will be found both on static deploy as well as hotreloading webpack-dev-server based on wether content of MYAWESOMETAG is disabled/enabled in .htaccess.

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>APP</title>
  <link rel="stylesheet" type="text/css" href="public/site.css">
</head>
<body>
  <div class="app"></div>
  <script async src="public/site.js"></script>
</body>
</html>

##TODO

This is really hackish straight forward way for a very specific issue. Make it smarter? Don't parse existing .htaccess file, but make it as a part of building system?