{"size": 2576, "ext": "js", "max_stars_repo_path": "lib/module-dependencies.js", "max_stars_repo_name": "arabsight/fusebox-aurelia-plugin", "max_stars_repo_stars_event_min_datetime": "2017-12-02T14:01:27.000Z", "max_stars_repo_stars_event_max_datetime": "2017-12-05T17:08:16.000Z", "max_issues_repo_path": "lib/module-dependencies.js", "max_issues_repo_name": "arabsight/fusebox-aurelia-plugin", "max_issues_repo_issues_event_min_datetime": "2018-01-05T21:07:25.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-09T17:02:43.000Z", "max_forks_repo_path": "lib/module-dependencies.js", "max_forks_repo_name": "arabsight/fusebox-aurelia-plugin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 28.6222222222, "max_line_length": 93, "alphanum_fraction": 0.5927795031}
const { parseWithBabylon, getModuleNames } = require('./analysis');
const {
    isNodeModule,
    isUserModule,
    getModuleName,
    addViewByConvention
} = require('./utils');

class ModuleDependenciesPlugin {
    constructor(opts) {
        this.test = /\.js$/;

        this.options = Object.assign({
            pal: 'browser',
            aureliaApp: 'main',
            aureliaConfig: ['standard', 'developmentLogging']
        }, opts);
    }

    transform(file) {
        if (file.context.useCache && file.loadFromCache()) {
            return;
        }

        if (file.info.fuseBoxPath.endsWith('aurelia-bootstrapper.js')) {
            file.addStringDependency(`aurelia-pal-${this.options.pal}`);
        }

        if (file.info.fuseBoxPath.endsWith('aurelia-framework.js')) {
            let plugins = getFrameworkPlugins(this.options.aureliaConfig);
            plugins.forEach(p => file.addStringDependency(p));
        }

        file.loadContents();
        if (!file.analysis.ast) {
            file.analysis.loadAst(parseWithBabylon(file.contents));
        }

        const modules = getModuleNames(file.analysis.ast);

        if (modules.length > 0) {
            modules.forEach(id => this.addDependency(id, file));
        }
    }

    addDependency(id, file) {
        if (isNodeModule(id, file)) {

            file.addStringDependency(id);

            if (id.startsWith('.')) {
                addViewByConvention(id, file);
            }

        } else if (isUserModule(id, file)) {

            file.addStringDependency(id.startsWith('.') ? id : `~/${id}`);
            addViewByConvention(id, file);

        } else {
            throw new Error(`Can't resolve module: ${id}`);
        }
    }
}

const configModuleNames = {
    'defaultBindingLanguage': 'aurelia-templating-binding',
    'router': 'aurelia-templating-router',
    'history': 'aurelia-history-browser',
    'defaultResources': 'aurelia-templating-resources',
    'eventAggregator': 'aurelia-event-aggregator',
    'developmentLogging': 'aurelia-logging-console',
};

function getFrameworkPlugins(config) {
    if (!config) return undefined;
    if (!Array.isArray(config)) config = [config];

    let i = config.indexOf('standard');
    if (i >= 0) { config.splice(i, 1, 'basic', 'history', 'router'); }
    i = config.indexOf('basic');
    if (i >= 0) {
        config.splice(i, 1, 'defaultBindingLanguage', 'defaultResources', 'eventAggregator');
    }

    return config.map(c => configModuleNames[c]);
}

module.exports = (opts) => new ModuleDependenciesPlugin(opts);
