• Stars
    star
    532
  • Rank 83,377 (Top 2 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Webpack loader for the Elm programming language.

Elm loader Version Travis build Status

Webpack loader for the Elm programming language.

It is aware of Elm dependencies and tracks them. This means that in watch mode, if you require an Elm module from a Webpack entry point, not only will that .elm file be watched for changes, but any other Elm modules it imports will be watched for changes as well.

Installation

$ npm install --save elm-webpack-loader

Usage

Documentation: rules

webpack.config.js:

module.exports = {
  module: {
    rules: [{
      test: /\.elm$/,
      exclude: [/elm-stuff/, /node_modules/],
      use: {
        loader: 'elm-webpack-loader',
        options: {}
      }
    }]
  }
};

See the examples section below for the complete webpack configuration.

Options

cwd (default null) Recommended

You can add cwd=elmSource to the loader:

var elmSource = __dirname + '/elm/path/in/project'
  ...
  use: {
    loader: 'elm-webpack-loader',
    options: {
      cwd: elmSource
    }
  }
  ...

cwd should be set to the same directory as your elm.json file. You can use this to specify a custom location within your project for your elm files. Note, this will cause the compiler to look for all elm source files in the specified directory. This approach is recommended as it allows the compile to watch elm.json as well as every file in the source directories.

Optimize (default is true in production mode)

Set this to true to compile bundle in optimized mode. See https://elm-lang.org/0.19.0/optimize for more information.

  ...
  use: {
    loader: 'elm-webpack-loader',
    options: {
      optimize: true
    }
  }
  ...

Debug (default is true in development mode)

Set this to true to enable Elm's time traveling debugger.

  ...
  use: {
    loader: 'elm-webpack-loader',
    options: {
      debug: true
    }
  }
  ...

RuntimeOptions (default undefined)

This allows you to control aspects of how elm make runs with GHC Runtime Options.

The 0.19 version of elm make supports a limited set of those options, the most useful of which is for profiling a build. To profile a build use the settings runtimeOptions: '-s', which will print out information on how much time is spent in mutations, in the garbage collector, etc.

Note: Using the flags below requires building a new elm make binary with -rtsopts enabled!

If you notice your build spending a lot of time in the garbage collector, you can likely optimize it with some additional flags to give it more memory, e.g. -A128M -H128M -n8m.

  ...
  use: {
    loader: 'elm-webpack-loader',
    options: {
      runtimeOptions: ['-A128M', '-H128M', '-n8m']
    }
  }
  ...

Files (default - path to 'required' file)

elm make allows you to specify multiple modules to be combined into a single bundle

elm make Main.elm Path/To/OtherModule.elm --output=combined.js

The files option allows you to do the same within webpack

module: {
  loaders: [
    {
      test: /\.elm$/,
      exclude: [/elm-stuff/, /node_modules/],
      loader: 'elm-webpack-loader',
      options: {
        files: [
          path.resolve(__dirname, "path/to/Main.elm"),
          path.resolve(__dirname, "Path/To/OtherModule.elm")
        ]
      }
    }
  ]
}

(Note: It's only possible to pass array options when using the object style of loader configuration.)

You're then able to use this with

import Elm from "./elm/Main";

Elm.Main.init({node: document.getElementById("main")});
Elm.Path.To.OtherModule.init({node: document.getElementById("other")});
Hot module reloading

Hot module reloading is supported by installing elm-hot-webpack-loader and adding it to your list of loaders. It should look something like this:

module: {
  rules: [
    {
      test: /\.elm$/,
      exclude: [/elm-stuff/, /node_modules/],
      use: [
        { loader: 'elm-hot-webpack-loader' },
        { loader: 'elm-webpack-loader' }
      ]
    }
  ]
}

IMPORTANT: elm-hot-webpack-loader must be placed in the list immediately before elm-webpack-loader.

Upstream options

All options are sent down as an options object to node-elm-compiler. For example, you can explicitly pick the local elm binary by setting the option pathToElm:

  ...
  use: {
    loader: 'elm-webpack-loader',
    options: {
      pathToElm: 'node_modules/.bin/elm'
    }
  }
  ...

For a list all possible options, consult the source.

Notes

Example

You can find an example in the example folder. To run:

npm install
npm run build

You can have webpack watch for changes with: npm run watch

You can run the webpack dev server with: npm run dev

For a full featured example project that uses elm-webpack-loader see pmdesgn/elm-webpack-starter .

noParse

Webpack can complain about precompiled files (files compiled by elm make). You can silence this warning with noParse. You can see it in use in the example.

  module: {
    rules: [...],
    noParse: [/.elm$/]
  }

Revisions

8.0.0

  • Fix watching when using the dev server. Use compiler.watching instead of compiler.options.watch as the latter doesn't work with the dev server.

7.0.0

  • Logs build output directly to stdout to retain formatting.
  • Remove stack trace for errors, as they're never relevant.
  • optimize and debug flags are now set by default depending on the webpack mode.
  • Removed several options which provide little benefit.
  • Reduced number of dependencies.

6.0.0

  • Elm is now a peer dependency.
  • Documentation fixes.

5.0.0

  • Support for Elm 0.19, drops support for Elm 0.18.

4.3.1

  • Fix a bug where maxInstances might end up being higher than expected

4.3.0

  • Set maxInstances to 1
  • Patch watching behaviour
  • Add forceWatch to force watch mode

4.2.0

Make live reloading work more reliably

4.1.0

Added maxInstances for limiting of instances

4.0.0

Watching is now done based on elm-package.json, faster startup time via @eeue56

3.1.0

Add support for --debug via node-elm-compiler

3.0.6

Allow version bumps of node-elm-compiler.

3.0.5

Upgrade to latest node-elm-compiler, which fixes some dependency tracking issues.

3.0.4

Fix potential race condition between dependency checking and compilation.

3.0.3

Use node-elm-compiler 4.0.1+ for important bugfix.

3.0.2

Use node-elm-compiler 4.0.0+

3.0.1

Pass a real error object to webpack on failures.

3.0.0

Support Elm 0.17, and remove obsolete appendExport option.

2.0.0

Change warn to be a pass-through compiler flag rather than a way to specify logging behavior.

1.0.0

Initial stable release.

More Repositories

1

elm-webpack-starter

Boilerplate for developing Elm apps on Webpack
JavaScript
862
star
2

elm-test

moved to elm-explorations/test
Elm
340
star
3

js-integration-examples

Examples of common uses of ports and web components
Elm
138
star
4

list-extra

Convenience functions for working with List.
Elm
135
star
5

webgl

Moved to elm-explorations/webgl
Elm
131
star
6

parser-combinators

A parser combinator library for Elm.
Elm
104
star
7

elm-datepicker

A reusable date picker component in Elm.
Elm
96
star
8

elm-route

A library for parsing routes.
Elm
69
star
9

elm-time

A pure Elm date and time library.
Elm
67
star
10

typed-svg

Typed SVG library written for Elm
Elm
60
star
11

maybe-extra

Convenience functions for working with Maybe.
Elm
51
star
12

json-extra

Convenience functions for working with Json.
Elm
37
star
13

string-extra

String helper functions for Elm.
Elm
34
star
14

elm-compiler-docs

Repo where to write down documentation and guides for the elm-compiler
Ruby
33
star
15

guidelines

guidelines for *-extra contributors
33
star
16

html-extra

Additional functions for working with Html.
Elm
31
star
17

random-extra

Extra functions for the core Random library.
Elm
28
star
18

elm-list-extra

DEPRECATED; use elm-community/list-extra
Elm
17
star
19

html-test-runner

DEPRECATED - Run elm-test suites in the browser
Elm
16
star
20

basics-extra

Additional basic functions for Elm.
Elm
15
star
21

material-icons

Material Icons in Elm.
Elm
14
star
22

linear-algebra

Fast Linear Algebra for Elm.
JavaScript
14
star
23

result-extra

Convenience functions for working with Result.
Elm
13
star
24

dict-extra

A library with extra functions for the dictionary type in elm core.
Elm
13
star
25

elm-json-extra

DEPRECATED. Moved to elm-community/json-extra =>
Elm
10
star
26

array-extra

convenience functions for working with Array
Elm
9
star
27

slack

A meta repo for all things Slack-related
9
star
28

lazy-list

Lazy list implementation in Elm.
Elm
8
star
29

discussions

A repo for holding Elm community discussion rules and metadata
7
star
30

list-split

Split lists into chunks.
Elm
5
star
31

elm-lazy-list

Elm
4
star
32

Elm.tmLanguage

Python
2
star
33

code-confusion-catalog

Community-written documents exploring proposed language additions.
1
star
34

travis-ci

1
star