How to import js files dynamically at compile time as input for browserify?

- 1 answer

Ad

In our project structure we have a directory containing a number of js files. We may add or remove any of these files later. Currently there is a main.js in which we import each file and create a map (file name as the key and the class defined in the file as the value).

Example:

Validator1.js

class Validator1 {
    constructor() {
        this.payRegex = /^[0-9][0-9][0-9]\/[A-Z,0-9][A-Z,0-9]*$/;
    }
    validate(obj) {
        //do something
    }
}

export default Validator1;

In main.js

import Validator1 from 'validator1.js';

import NoopValidator from './noop.js';

var validatorMap = {};
validatorMap['validator1'] = new Validator1;

validatorMap['DEFAULT'] = new NoopValidator;

We give this file to browserify to create a bundle.js. Now as I said there are a number of files in that folder and we want to generate this file at compile time with maven.

  • Is there any way other than creating a maven plugin to do this?
  • We are using EMAScript6
Ad

Answer

Ad

There isn't a built-in way to do this, but a Browserify Transform could handle the task. You could write one yourself, but I think Bulkify might do what you want already, depending on what format you want the result in.

See:

Ad
source: stackoverflow.com
Ad