Compiling more than one Elm project in Webpack

Viewed 156

I'm running a Phoenix server, where the Webpack config file looks like this in parts:

entry: {
  'elm-db': './elm-db/src/Main.elm',
  'elm-ledger': './elm-ledger/src/Main.elm',
},
output: {
  filename: '[name].js',
  path: path.resolve(__dirname, '../priv/static/js'),
  publicPath: '/js/',
},

module: {
  rules: [
    {
      test: /\.elm$/,
      exclude: [/elm-stuff/, /node_modules/],
      use: {
        loader: 'elm-webpack-loader',
        options: {
          cwd: path.resolve(__dirname, 'elm-db'),
          debug: options.mode === "development",
          optimize: options.mode === "production",
        },
      },
    },
  ]
},

I have two Elm projects, elm-db and elm-ledger. I've managed to add elm-db correctly to the config file, and it compiles the project just fine. However, I keep failing to add "elm-ledger" as well. The code above produces a correct elm-db.js file, but compiling elm-ledger.js returns errors, probably because I can't make a correct cwd field. I can't find any documentation on the configuration of cwd.

1 Answers

Not sure if this helps or not but we are doing it with the old version of "elm-webpack-loader": "^6.0.1" (The latest version may have different configurations)


const elmPath = path.resolve(__dirname, './node_modules/.bin/elm');
const srcPaths = [
  path.resolve(__dirname, './app1'),
  path.resolve(__dirname, './app2'),
  path.resolve(__dirname, './app3'),
];

const elmRules = srcPaths.map(featurePath => ({
  test: new RegExp(`^${featurePath}.*elm$`),
  include: featurePath,
  exclude: [/elm-stuff/, /node_modules/],
  use: [
    {
      loader: require.resolve('elm-webpack-loader'),
      options: {
        pathToElm: elmPath,
        cwd: featurePath,
        // other options
      },
    },
  ],
}));

Then you spread that ...elmRules in Webpack config rules

Related