elm-optimize-level-2 icon indicating copy to clipboard operation
elm-optimize-level-2 copied to clipboard

Usage with webpack

Open hpate-triller opened this issue 4 years ago • 3 comments
trafficstars

Greetings, and thank you for such a cool project!

I was looking at adding this to our codebase at work, and we are using https://github.com/elm-community/elm-webpack-loader, and I wanted get your thoughts on using it with webpack, and what the right direction is.

At the moment, I have it as a webpack loader in our project, like

// config/elmOptimizeLoader.js
const Transform = require("../node_modules/elm-optimize-level-2/dist/transform");
const { toolDefaults } = require("../node_modules/elm-optimize-level-2/dist/types");

module.exports = async function elmOptimizeLoader(source, map) {
  const callback = this.async();
  const transformed = await Transform.transform("", source, undefined, false, toolDefaults);
  callback(null, transformed, map);
};

And then in the webpack.config.js it sets up the loader.

{
  ...
  resolveLoader: {
    alias: {
      "elm-optimize-level-2": path.join(__dirname, "config/elmOptimizeLoader"),
    },
  },
  module: {
    rules: [
      ...,
      {
        test: /\.elm$/,
        exclude: [/elm-stuff/, /node_modules/],
        use: [ "elm-optimize-level-2", elmWebpackLoader(mode)]
      },
    ]
  }

The part that feels weird is requiring the files from dist in order to run the transform directly on the stringified source code that webpack passes in, rather than being able to use an export from the project itself, like

const elmOptimize = require("elm-optimize-level-2")

module.exports = async function elmOptimizeLoader(source, map) {
  const callback = this.async();
  const transformed = await elmOptimize(source);
  callback(null, transformed, map);
};

hpate-triller avatar Jan 21 '21 21:01 hpate-triller

Very interested in this as well!

Augustin82 avatar Jan 26 '21 16:01 Augustin82

Hello! Missed the original notification so just saw this.

I'm not really experienced with webpack, so not sure of your constraints, but I think of elm-optimize-level-2 as a drop-in replacement for elm make --optimize

Can't you just call the elm-optimize-level-2 as you would elm make?

The project isn't really intended to be depended on by importing the distributed js. Those js files may change their interfaces or maybe this project won't even be js-based in the future.

mdgriffith avatar Feb 14 '21 16:02 mdgriffith

@hpate-triller how do you feel about turning this into a Webpack plugin?

: )

Birowsky avatar Mar 23 '21 08:03 Birowsky