Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

ES Module Support #1884

Open
Jesspu opened this issue Feb 18, 2022 · 10 comments
Open

ES Module Support #1884

Jesspu opened this issue Feb 18, 2022 · 10 comments

Comments

@Jesspu
Copy link

Jesspu commented Feb 18, 2022

Hey,

Recently, some of the dependencies of brunch plugins we use were converted to ES modules. That means that to update these, the plugins needed to also be converted to ES modules. However, brunch did not work with ES modules.

I took a crack at it, and was able to convert brunch to work with both commonjs and es modules. So far I have converted two of our plugins to ESM, while leaving the remaining as commonjs with no issues. The two that I have moved to ESM are imagemin-brunch and replacer-brunch. (we have our own forks of these plugins, as it was faster to manage and patch bugs ourselves, but they are largely unchanged from the current versions).

Is this something that you would be interested in a PR?

If this is not something that is currently in the future scope of brunch, please feel free to just close this issue. Just through I would share as it seems the move to ESM is progressing.

Obviously, this is not as good as native ESM support, and more of stop gap to get ES modules working with brunch today. Converting brunch to an ES module would require a lot more work.

The code in this gist shows the method changed in the plugins.js file, the patch is denoted with enclosing comment blocks.
https://gist.github.com/Jesspu/4bf38ea7db5b1e394449855967978a6c

Also, here is an example of what a brunch plugin looks like when converted to ESM (there may be a better way to to do this, but it has worked). https://gist.github.com/Jesspu/f30930cc4c492ff71565ca207214c7ca

I love brunch, and really appreciate all the work that has been put into it.

@paulmillr
Copy link
Contributor

Hey @Jesspu

Thanks for the kind words! Unfortunately i'm focusing on other projects at the moment, and there is no space for Brunch. You can consider it deprecated. Unless someone wants to contribute.

I'd keep this issue open in any case.

@Jesspu
Copy link
Author

Jesspu commented Feb 18, 2022

Okay, no problem appreciate your response and all the work on brunch over the years.

We are actually still using brunch for our new frontend apps built in svelte, and it works great. I have had to maintain most of our plugins, but with the way brunch is set up, it is pretty trivial.

Sad that the sun is setting on brunch, but I will probably keep using it until I am no longer able to do so.

If anyone else finds this issue, and has questions about converting brunch plugins to ESM, or using ESM in brunch, let me know and I will try to help.

@reubano
Copy link
Contributor

reubano commented Mar 24, 2022

Here's how I handled it for @observablehq/plot. Ugly, but works...

package.json

{
  ...
  "dependencies": {
    "@babel/runtime": "^7.7.4",
    "@observablehq/plot": "^0.4.2",
    "@popperjs/core": "^2.10.1",
    "bootstrap": "^5.1.0",
    "brunch": "^4.0.0",
    "coffeescript": "^2.5.1",
    "lodash": "^4.17.21",
    "mithril": "^2.0.4",
  },
  "devDependencies": {
    "@babel/core": "^7.17.8",
    "@babel/plugin-transform-runtime": "^7.17.0",
    "@babel/preset-env": "^7.16.11",
    "auto-reload-brunch": "^2.7.1",
    "babel-brunch": "^7.0.1",
    "clean-css-brunch": "^3.0.0",
    "coffee-script-brunch": "^4.0.0",
  }
}

brunch-config.js

// Generated by CoffeeScript 2.4.1

module.exports = {
  // http://brunch.io/docs/config
  plugins: {
    babel: {
      presets: ['@babel/preset-env'],
      plugins: [
        [
          "@babel/plugin-transform-runtime",
          {
            "absoluteRuntime": false,
            "version": "^7.7.4"
          }
        ]
      ],
      ignore: [
        /^node_modules\/bootstrap/,
        /^node_modules\/mithril/,
        /^node_modules\/lodash/,
        /^node_modules\/@babel\/runtime/,
      ]
    }
  },
  npm: {
    compilers: ['babel-brunch'],
    globals: {
      popper: "@popperjs/core",
      _: "lodash",
      Plot: "@observablehq/plot",
      babelInteropRequireDefault: "@babel/runtime/helpers/interopRequireDefault",
      babelGet: "@babel/runtime/helpers/get",
      babelInherits: "@babel/runtime/helpers/inherits",
      babelSlicedToArray: "@babel/runtime/helpers/slicedToArray",
      babelObjectSpread2: "@babel/runtime/helpers/objectSpread2",
      babelClassCallCheck: "@babel/runtime/helpers/classCallCheck",
      babelCreateClass: "@babel/runtime/helpers/createClass",
      babelPossibleConstructorReturn: "@babel/runtime/helpers/possibleConstructorReturn",
      babelGetPrototypeOf: "@babel/runtime/helpers/getPrototypeOf",
      babelObjectWithoutProperties: "@babel/runtime/helpers/objectWithoutProperties",
      babelToConsumableArray: "@babel/runtime/helpers/toConsumableArray",
      babelAssertThisInitialized: "@babel/runtime/helpers/assertThisInitialized",
      babelDefineProperty: "@babel/runtime/helpers/defineProperty",
      babelInteropRequireDefault: "@babel/runtime/helpers/interopRequireDefault",
      babelTypeof: "@babel/runtime/helpers/typeof",
      babelWrapNativeSuper: "@babel/runtime/helpers/wrapNativeSuper",
      babelRegenerator: "@babel/runtime/regenerator",
      bootstrap: "bootstrap",
    }
  },
  server: {
    hostname: "0.0.0.0",
    port: 3333
  },
  files: {
    javascripts: {
      joinTo: {
        "javascripts/app.js": /^app/,
        "javascripts/vendor.js": /^(?!app)/
      }
    },
    stylesheets: {
      joinTo: {
        "stylesheets/app.css": /^app\/.*custom\.css/,
        "stylesheets/vendor.css": /^(?!app)/
      }
    }
  },
  modules: {
    autoRequire: {
      "javascripts/app.js": ["initialize"]
    }
  },
  sourceMaps: "absoluteUrl"
};

view.coffee

m = require 'mithril'

module.exports =
  oncreate: (vnode) ->
    attrs = vnode.attrs
    data = attrs.collection.list

    graph = Plot.plot
      y: {grid: true}
      marks: [Plot.line(data, {x: "date", y: "value"})]

    vnode.dom.appendChild graph

  view: (vnode) -> m 'span.chart'

@reubano
Copy link
Contributor

reubano commented Mar 24, 2022

BTW @paulmillr, really hope you get more time for brunch. Don't know what I'd do if I was forced to go back to rollup or Webpack.

@Jesspu
Copy link
Author

Jesspu commented Mar 24, 2022

Very cool, thanks for sharing.

I am actually close to porting brunch itself to an ES module. That will allow it to use CJS and ESM packages throughout without any issues.

Once it's done and validated, I will share it as well. It will also be shipped to the @firstfleet/brunch npm package (fork of brunch that currently has my esm plugin support).

I hope this will at least future proof brunch a bit so I can keep using it.

@reubano
Copy link
Contributor

reubano commented Mar 24, 2022

Cool! I'm sure they'd appreciate a PR over here too.

@paulmillr
Copy link
Contributor

@reubano hard to do find more time being at war, but I also want to keep Brunch up and running. Just need more help.

@reubano
Copy link
Contributor

reubano commented Mar 27, 2022

@reubano hard to do find more time being at war, but I also want to keep Brunch up and running. Just need more help.

Thoughts and prayers to you and yours!! 🙏🏽

@boehs
Copy link
Contributor

boehs commented Mar 27, 2022

Just stumbled upon brunch, I also really want it to stay alive. Anything the community can do?

@paulmillr, do stay safe!

@Jesspu
Copy link
Author

Jesspu commented Mar 27, 2022 via email

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

No branches or pull requests

4 participants