Add support for multiple elm file inputs

This commit is contained in:
Brian Dukes 2023-04-25 08:00:17 -05:00
parent 46b0975d53
commit 16fa3bbf8b
No known key found for this signature in database
GPG Key ID: 3C1CA727E76C0AD1
5 changed files with 80 additions and 13 deletions

View File

@ -27,9 +27,9 @@ export async function run(
let elmFilePath = undefined;
const replacements = null;
let inputFilePath = options.inputFilePath[0];
if (options.inputFilePath[0] == 'make') {
inputFilePath = options.inputFilePath[1];
let inputFilePaths = options.inputFilePath;
if (inputFilePaths[0] == 'make') {
inputFilePaths.shift();
}
const o3Enabled = options.optimizeSpeed;
@ -67,12 +67,14 @@ export async function run(
// process.exit(0)
// }
if (inputFilePath && inputFilePath.endsWith('.js')) {
jsSource = fs.readFileSync(inputFilePath, 'utf8');
if (inputFilePaths.length === 1 && inputFilePaths[0].endsWith('.js')) {
jsSource = fs.readFileSync(inputFilePaths[0], 'utf8');
log('Optimizing existing JS...');
} else if (inputFilePath && inputFilePath.endsWith('.elm')) {
elmFilePath = inputFilePath;
jsSource = compileToStringSync([inputFilePath], {
} else if (
inputFilePaths.every((inputFilePath) => inputFilePath.endsWith('.elm'))
) {
elmFilePath = inputFilePaths;
jsSource = compileToStringSync(inputFilePaths, {
output: 'output/elm.opt.js',
cwd: dirname,
optimize: true,
@ -107,17 +109,37 @@ export async function run(
// This mirrors elm make behavior.
const outputDirectory = path.dirname(options.outputFilePath);
if (
path.dirname(inputFilePath) !== outputDirectory &&
inputFilePaths.every(
(inputFilePath) => path.dirname(inputFilePath) !== outputDirectory
) &&
!fs.existsSync(outputDirectory)
) {
fs.mkdirSync(outputDirectory, { recursive: true });
}
fs.writeFileSync(options.outputFilePath, transformed);
const fileName = path.basename(inputFilePath);
log('Success!');
log('');
log(` ${fileName} ───> ${options.outputFilePath}`);
if (inputFilePaths.length === 1) {
const fileName = path.basename(inputFilePaths[0]);
log(` ${fileName} ───> ${options.outputFilePath}`);
} else {
const maxLength = inputFilePaths
.map((inputFilePath) => path.basename(inputFilePath).length)
.reduce((max, len) => Math.max(max, len), 0);
const fill = maxLength + 4;
let isFirst = true;
for (const inputFilePath of inputFilePaths) {
const fileName = path.basename(inputFilePath) + ' ';
if (isFirst) {
log(` ${fileName.padEnd(fill, '-')}+──> ${options.outputFilePath}`);
isFirst = false;
} else {
log(` ${fileName.padEnd(fill, '-')}+`);
}
}
}
log('');
return options.outputFilePath;
}

View File

@ -36,7 +36,7 @@ export type Options = {
export const transform = async (
_dir: string,
jsSource: string,
elmfile: string | undefined,
elmfiles: string[] | string | undefined,
verbose: boolean,
transforms: Transforms
): Promise<string> => {
@ -54,7 +54,7 @@ export const transform = async (
let source = ts.createSourceFile('elm.js', jsSourceWithoutComments, ts.ScriptTarget.ES2018);
let parsedVariants = primitives;
if (elmfile && transforms.variantShapes) {
if (elmfiles && transforms.variantShapes) {
// const elmSource = fs.readFileSync(elmfile, 'utf8');
// parsedVariants = parseElm({
// author: 'author',

View File

@ -1,6 +1,9 @@
const level1 = require('./dist/elm.js');
const level2 = require('./dist/elm-lvl-2.js');
const level3 = require('./dist/elm-lvl-3.js');
const anotherLevel1 = require('./dist/another-elm.js');
const anotherLevel2 = require('./dist/another-elm-lvl-2.js');
const anotherLevel3 = require('./dist/another-elm-lvl-3.js');
const run = (name, elm) =>
new Promise((resolve, reject) => {
@ -8,6 +11,16 @@ const run = (name, elm) =>
app.ports.onSuccessSend.subscribe(resolve);
app.ports.onFailureSend.subscribe(reject);
})
.then((_) => {
if (!elm.Elm.AnotherTest) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
const anotherApp = elm.Elm.AnotherTest.init({ flags: {} });
anotherApp.ports.onSuccessSend2.subscribe(resolve);
anotherApp.ports.onFailureSend2.subscribe(reject);
});
})
.then((_) => console.info(`${name} -> Success!`))
.catch((reason) => {
console.error(`${name} -> Failing tests: ` + reason);
@ -19,6 +32,9 @@ const main = () => {
run('normal', level1),
run('level-2', level2),
run('level-3', level3),
run('multi-file normal', anotherLevel1),
run('multi-file level-2', anotherLevel2),
run('multi-file level-3', anotherLevel3),
]);
};

View File

@ -7,12 +7,15 @@ set -ex
# Compile normally
elm make src/Tests.elm --output=dist/elm.js --optimize
elm make src/Tests.elm src/AnotherTest.elm --output=dist/another-elm.js --optimize
# Compile with level 2
../../bin/elm-optimize-level-2 --output=dist/elm-lvl-2.js src/Tests.elm
../../bin/elm-optimize-level-2 --output=dist/another-elm-lvl-2.js src/Tests.elm src/AnotherTest.elm
# Compile with level-3
../../bin/elm-optimize-level-2 --output=dist/elm-lvl-3.js -O3 src/Tests.elm
../../bin/elm-optimize-level-2 --output=dist/another-elm-lvl-3.js -O3 src/Tests.elm src/AnotherTest.elm
# Run tests
node index.js

View File

@ -0,0 +1,26 @@
port module AnotherTest exposing (..)
main : Program {} () ()
main =
Platform.worker
{ init =
\json ->
( ()
, if 1 > 0 then
onSuccessSend2 "Pass!"
else
onFailureSend2 "1 should be greater than 0"
)
, update =
\msg model ->
( model, Cmd.none )
, subscriptions = \_ -> Sub.none
}
port onSuccessSend2 : String -> Cmd msg
port onFailureSend2 : String -> Cmd msg