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

Support "documents" as assets out of the box #508

Open
G-Rath opened this issue Oct 25, 2023 · 1 comment
Open

Support "documents" as assets out of the box #508

G-Rath opened this issue Oct 25, 2023 · 1 comment

Comments

@G-Rath
Copy link
Contributor

G-Rath commented Oct 25, 2023

I recently discovered there's a little bit of work required to support serving "document" assets such as PDFs via Shakapacker that allows nested file paths - effectively the config ended up being:

'use strict';

const Dotenv = require('dotenv-webpack');
const { dirname } = require('path');
const {
  config: { source_path: sourcePath },
  generateWebpackConfig
} = require('shakapacker');

const options = {
  module: {
    rules: [
      {
        test: /\.(pdf)$/,
        type: 'asset/resource',
        generator: {
          filename: pathData => {
            const folders = dirname(pathData.filename)
              .replace(`${sourcePath}`, '')
              .split('/')
              .filter(Boolean);

            const foldersWithStatic = ['static', ...folders].join('/');

            return `${foldersWithStatic}/[name]-[hash][ext][query]`;
          }
        }
      }
    ]
  },
  plugins: [new Dotenv()]
};

module.exports = generateWebpackConfig(options);

This is actually the exact configuration Shakapacker uses internally except they have a different test which is static - the key thing is the filename part.

Say we have a file at app/frontend/documents/forms/declaration_form.pdf:

  • without filename we'd have to do asset_pack_path("declaration_form.pdf") and we'd get something like packs/123abc.pdf
  • with filename we are able to do asset_pack_path("documents/forms/declaration_form.pdf") and we'd get something like packs/static/documents/forms/declaration_form-123abc.pdf

We don't use these kind of assets in most apps, but it feels easy enough to at least have this documented or included as a code comment in our default webpack config to save people some time.

@lukeify
Copy link

lukeify commented Nov 2, 2023

Discussed at Ruby Guild on 3 November 2023 and needs more exploration. One concern raised was that it may become mostly unused in most of our apps and adds some complexity to our webpacker setup.

One step is to explore removing sprockets. Ticket to be created for that.

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

No branches or pull requests

2 participants