Elm-Compiler Package

Hi!

I’m writing a build-plugin package to compile elm to js. It only supports one Main.elm to one js-output file for now. The problem: when I change one elm file

  • all files get compiled first
  • than the one file that I changed get compiled

The elm compiler is fast enough(<200ms) for the currently small codebase. But when the app gets bigger I would like to avoid unnecessary compile cycles.

package.js:

Package.describe({
    summary: 'compile elm to js',
    version: '1.0.0',
    name: 'private:elm',
});

Package.registerBuildPlugin({
    name: 'elm',
    sources: ['plugin.js'],
    npmDependencies: {
        'node-elm-compiler': '5.0.3',
    },
});

Package.onUse((api) => {
    api.versionsFrom('1.8.1');
    api.use('isobuild:compiler-plugin@1.0.0');
});

plugin.js:

const { compileToStringSync } = Npm.require('node-elm-compiler');

Plugin.registerCompiler(
    {
        extensions: ['elm'],
        archMatching: 'web',
    },
    () => new ElmCompiler(),
);

ElmCompiler = class ElmCompiler {
    processFilesForTarget(files) {
        const mainFiles = files.filter(
            (file) => file.getBasename() === 'Main.elm',
        );

        if (mainFiles.length === 1) {
            mainFiles.forEach((file) => {
                const path = file.getPathInPackage();
                file.addJavaScript({
                    data: compileToStringSync(path, {}),
                    hash: file.getSourceHash(),
                    path: `${path}.js`,
                });
            });
        }
    }
};
1 Like

You probably want to have your class extend the CachingCompiler (or MultiFileCachingCompiler if you want to support imports). See: https://github.com/meteor/meteor/tree/master/packages/caching-compiler

Specifically you’ll want to add your own methods for compileOneFile and compileOneFileLater