xbmono
xbmono

Reputation: 2316

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

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.

Upvotes: 4

Views: 729

Answers (1)

CodingWithSpike
CodingWithSpike

Reputation: 43718

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:

Upvotes: 3

Related Questions