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

Include assets that get collapsed to a data uri string #2

Open
mtscout6 opened this issue Jan 24, 2015 · 4 comments
Open

Include assets that get collapsed to a data uri string #2

mtscout6 opened this issue Jan 24, 2015 · 4 comments

Comments

@mtscout6
Copy link
Member

They are currently omitted altogether, it would be nice to output that data uri into the asset json file.

@schovi
Copy link

schovi commented Aug 16, 2015

👍 Looking for that really long!

@mtscout6
Copy link
Member Author

@schovi I haven't had time to do this myself, but if you wanted to submit a pull request for it I'd happily review it and bring it in.

@schovi
Copy link

schovi commented Aug 25, 2015

@mtscout6 I made it works in my project

I had to improve your module function ExtractAssets

function ExtractAssets(modules, requestShortener, publicPath) {
  var emitted = true;
  var assets = modules
    .map(m => {
      let name = m.readableIdentifier(requestShortener)

      if(name.slice(0,2) != "./")
        return

      name = name.slice(2, name.length)

      const assets = Object.keys(m.assets || {})

      let asset;

      if (assets.length) {
        asset = assets[0];

      } else if(m._source) {
        const source = m._source.source()
        asset = source.slice(18, source.length - 1)
      }

      if(!asset)
        return

      return {
        name: name,
        asset: asset
      };

    }).filter(m => {
      return m !== undefined;
    }).reduce((acc, m) => {
        acc[m.name] = url.resolve(publicPath, m.asset);
      return acc;
    }, {});

  return [emitted, assets];
}

As you can see there is some stupid condition to filter out assets i dont want. Otherwise stats.json is really huge and parsing takes some.

Then in my server initializer I have following

import addModulePath from 'app-module-path';

////// Fix Webpack assets
// Add assets load path
const assetsPath = path.join(__dirname, 'assets/')

addModulePath.addPath(assetsPath);

// To ignore webpack custom loaders on server.
['css', 'less', 'sass', 'styl'].forEach((ext) => {
  require.extensions['.' + ext] = () => {}
})

// Override assets requiring
let assetCache;

const getAssets = () => {
  if(assetCache)
    return assetCache

  console.log("Getting assets from webpack 'build/assets.json'")
  assetCache = JSON.parse(fs.readFileSync(path.join(__dirname, 'build/assets.json')))

  return assetCache
}

// Overiding requiring of assets
['jpg', 'jpeg', 'png', 'gif', ...].forEach((ext) => {
  require.extensions['.' + ext] = (require, path) => {
    const file = path.replace(assetsPath, "")
    require.exports = getAssets().assets[file]
  }
})

Now I can load assets if they are just url or base64 on server side. It is ugly, but it is mine and it works :)

@mtscout6
Copy link
Member Author

If you want to start with a pull request we can go from there. That will give me better diffing tools to see what's changed.

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

No branches or pull requests

2 participants