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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propshaft ESM Modules chunks not resolved correctly (works with sprockets-rails) #48

Closed
navidemad opened this issue Dec 10, 2021 · 7 comments

Comments

@navidemad
Copy link

application.js:
Screenshot 2021-12-10 at 16 41 36

app/views/layouts/application.html.erb:
Screenshot 2021-12-10 at 16 46 20

Google Chrome console / networks tab
Screenshot 2021-12-10 at 16 42 55
Screenshot 2021-12-10 at 16 42 45

Scenarios:

Gemfile: Propshaft uncommented, sprockets-rails commented
ESBUILD_MODULES=1 ./bin/dev
Output: 馃敶

Gemfile: Propshaft commented, sprockets-rails uncommented
ESBUILD_MODULES=1 ./bin/dev
Output: 馃煝

Gemfile: Propshaft commented, sprockets-rails uncommented
./bin/dev
Output: 馃煝

Gemfile: Propshaft uncommented, sprockets-rails commented
./bin/dev
Output: 馃煝

Minimalist application to reproduce the issue: https://github.com/navidemad/PropshaftApp

@brenogazzola
Copy link
Collaborator

If you precompile your assets you will probably see that sprockets breaks too, because it only adds digests to the file names after precompilation.

I think we are going to need a jscompiler like we have for csscompiler to solve this.

@thanosbellos
Copy link

Hi

By changing the regex in asset.rb to match the one in the this sprockets pull request

and using the following esbuild configuration from the sample repo above:

"use strict";

import esbuild from "esbuild";
import path from "path";
import glob from "glob";

const __dirname = path.resolve();

var entryPoints = glob
  .sync(path.resolve(__dirname, `app/javascript/*.js`))
  .reduce(function (map, filepath) {
    map[path.basename(filepath, ".js")] = [filepath];
    return map;
  }, {});

const esbuildOptions = {
  bundle: true,
  logLevel: "debug",
  entryPoints: entryPoints,
  format: "esm",
  outdir: path.resolve(__dirname, "app/assets/builds/js/modern"),
  chunkNames: '[name]-[hash].digested',
  splitting: typeof process.env.ESBUILD_MODULES != "undefined",
  treeShaking: true,
  sourcemap: false,
  minify: false,
  metafile: false,
  platform: "browser",
  watch: true,
};

esbuild
  .build(esbuildOptions)
  .catch((e) => {
    compilationError(e.message);
    process.exit(1);
  });

everything seems to be working normally:
Screenshot 2021-12-23 at 7 18 59 PM

@brenogazzola
Copy link
Collaborator

@thanosbellos Thanks for the heads up. #55 is up.

@thanosbellos
Copy link

Thanks a lot for your work. This is the only thing, that kept me from upgrading to propshaft and Rails 7.0. I hope that the sprockets pull request, is also merged into the master so that any legacy app can use the same configuration.

Thanks again.

@brenogazzola
Copy link
Collaborator

@navidemad #55 was merged. Using the chunkNames config demonstrated by @thanosbellos should be enough to get chunks working in your app. Could you check if everything is ok, so we can close this issue and the other PR?

@navidemad
Copy link
Author

@brenogazzola Confirmed, it's working well when I specify in my esbuild config:
chunkNames: '[name]-[hash].digested',

What about #49, does it still necessary ?

@brenogazzola
Copy link
Collaborator

brenogazzola commented Dec 28, 2021

Nope. I've closed that one and left a note. If it's working well, then I'll consider this one solved. Thanks for the help

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

Successfully merging a pull request may close this issue.

3 participants