JavaScript dependency management

后端 未结 6 1946
清歌不尽
清歌不尽 2020-12-08 08:06

I am currently maintaining a large number of JS files and the dependency issue is growing over my head. Right now I have each function in a separate file and I manually main

6条回答
  •  忘掉有多难
    2020-12-08 08:31

    It would be nice to have a tool that can automatically detect those dependencies for you and choose how they are loaded. The best solutions today are a bit cruder though. I created a dependency manager for my particular needs that I want to add to the list (Pyramid Dependency Manager). It has some key features which solve some unique use cases.

    1. Handles other files (including inserting html for views...yes, you can separate your views during development)
    2. Combines the files for you in javascript when you are ready for release (no need to install external tools)
    3. Has a generic include for all html pages. You only have to update one file when a dependency gets added, removed, renamed, etc

    Some sample code to show how it works during development.

    File: dependencyLoader.js

    //Set up file dependencies
    Pyramid.newDependency({
        name: 'standard',
        files: [
        'standardResources/jquery.1.6.1.min.js'
        ]
    });
    
    Pyramid.newDependency({
    name:'lookAndFeel',
    files: [
        'styles.css',
        'customStyles.css',
        'applyStyles.js'
        ]
    });
    
    Pyramid.newDependency({
    name:'main',
    files: [
        'createNamespace.js',
        'views/buttonView.view', //contains just html code for a jquery.tmpl template
        'models/person.js',
        'init.js'
        ],
        dependencies: ['standard','lookAndFeel']
    });
    

    Html Files

    
        
        
        
    
    

    It does require you to maintain a single file to manage dependencies. I am thinking about creating a program that can automatically generate the loader file for you based on includes in the header but since it handles many different types of dependencies, maintaining them in one file might actually be better.

提交回复
热议问题