mdgriffith / elm-optimize-level-2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Usage with webpack

hpate-triller opened this issue · comments

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);
};

Very interested in this as well!

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.

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

: )