GitXplorerGitXplorer
k

static-file-loader

public
4 stars
6 forks
0 issues

Commits

List of commits on branch master.
Unverified
67cc06ece920bdbf85420bf172825c88a00246f6

v0.6.0

ddoug-wade committed 6 years ago
Verified
fd32fc54ebeed8dd8b670b444246ad87d035282f

Merge pull request #12 from Bobgy/webpack4

ddoug-wade committed 7 years ago
Unverified
afafa5a686d6fb0727d87fa20872ac227d51a7d9

update readme

committed 7 years ago
Unverified
bd2b0116ab679bc306174887c9f63dc1c1cedf56

code clean up && remove node 4 support in travis

committed 7 years ago
Unverified
f98666fbf739856ec5b96677616c02a38582b9e7

add webpack 4 as peer dependency

committed 7 years ago
Unverified
9f35a26f16d11c1ffda2a0193a93c5e0e0aad578

fix integration test

committed 7 years ago

README

The README file for this repository.

static-file-loader

Build Status

Drop-in replacement for file-loader with the only difference: static-file-loader stores a map of original file names to file names with hashes in the compilation stats:

{
  "/Users/koss/src/kossnocorp/static-file-loader/test/fixtures/static/a.gif": "/980d0a50ac153b475e9fb1b8ffe22619.gif",
  "/Users/koss/src/kossnocorp/static-file-loader/test/fixtures/static/b.gif": "/980d0a50ac153b475e9fb1b8ffe22619.gif",
  "/Users/koss/src/kossnocorp/static-file-loader/test/fixtures/static/c.gif": "/980d0a50ac153b475e9fb1b8ffe22619.gif"
}

It's helpful if you want to use webpack to pre-build static files and then build HTML template using the paths map.

Installation

npm install static-file-loader file-loader --save-dev

Example

In an entry:

require.context('!!static-file-loader!./static', true, /.+/)

// ...

Template:

<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8' />
    <link rel='shortcut icon' href='<%= staticPath('favicon.png') %>' />
  </head>
  <body>
    <!-- ... -->
  </body>
</html>

Development server:

// ...

var staticFilesPath = path.join(process.cwd(), 'static')
var publicPath = webpackConfig.output.publicPath

express()
  .use('/', express.static(staticFilesPath))
  .get('*', function(req, res) {
    var html = template({
      staticPath: function(staticFilePath) {
        return path.join(publicPath, staticFilePath)
      }
    })
    res.send(html)
  })

Production build script:

// ...

var staticMapKey = require('static-file-loader').key
var staticFilesPath = path.join(process.cwd(), 'assets')
var publicPath = webpackConfig.output.publicPath

webpack(webpackConfig).run(function(err, stats) {
  var staticMap = stats.compilation[staticMapKey]
  var html = template({
    staticPath: function(staticFilePath) {
      var processedStaticFilePath = staticMap[path.join(staticFilesPath, staticFilePath)]
      return path.join(publicPath, processedStaticFilePath)
    }
  })

  // ...
})

License

MIT