madsflensted / elm-brunch

Brunch plugin to compile Elm code
MIT License
74 stars 31 forks source link

Race condition in brunch compile #26

Open OvermindDL1 opened 7 years ago

OvermindDL1 commented 7 years ago

I am having the elm-brunch plugin compile a variety of main apps into a single file, which is output to a brunch watched directory of other javascript, which then all gets compiled into one single javascript file. However brunch does not seem to pick up on Elm changes immediately as it seems to start compiling while Elm does and Elm finishes writing 'after' Brunch has already started compiling the files, so I have to save a file twice with ten seconds or so between them to have it compile twice.

Information

  plugins: {
    babel: {
      presets: ['es2015'],
      plugins: ["transform-object-rest-spread"],
      // Do not use ES6 compiler in vendor code
      ignore: [/web\/static\/vendor/],
      compact: false
    },
    elmBrunch: {
      elmFolder: '.',
      mainModules: [
        'web/elm/MessengerApp.elm',
        'web/elm/ExampleElmApp.elm'
      ],
      outputFolder: 'web/static/js',
      outputFile: 'elm.js',
      makeParameters: ['--warn']
    }
  },
  modules: {
    autoRequire: {
      "js/app.js": ["web/static/js/app"]
    }
  },
  paths: {
    watched: [
      "web/elm",
      "web/static",
      "test/static"
    ],

    public: "priv/static"
  },
  files: {
    javascripts: {
      joinTo: "js/app.js",
    },
    templates: {
      joinTo: "js/app.js"
    }
  },
{
  "version": "0.1.0",
  "summary": "Test",
  "repository": "https://github.com/user/project.git",
  "license": "BSD3",
  "source-directories": [
    "web/elm"
  ],
  "exposed-modules": [],
  "dependencies": {
    "elm-lang/core": "4.0.2 <= v < 5.0.0",
    "elm-lang/html": "1.1.0 <= v < 2.0.0",
  },
  "elm-version": "0.17.1 <= v < 0.18.0",
madsflensted commented 7 years ago

Yes there have been a couple of different issues related to this. I am not sure they will ever be a perfect solution to this, but one way that might work is to wait (async) for the elm to finish, then read the resulting js file and pass it on from the compilation step, instead of relying on brunch reading the output of elm compiler.

OvermindDL1 commented 7 years ago

Ah, so setting it up as a brunch compiler instead of a pre-processing step, that might work yeah. Just remember that at least on Windows the file it unreadable for up to a few seconds after elm finishes while windows finishes writing it to disk. This might be the main cause of brunch not picking up the new changes properly actually, but if the elm compiler could output to a file and to stdout (or just to stdout and you write the file out) then that should sidestep the issue well.