Code Monkey home page Code Monkey logo

resolve's Introduction

resolve

Grunt task to resolve require tags in JavaScript files

The following file app.js requires the file controller.js to be prepended:

//= require "controller.js"

var controller = new controller.Controller({
  container: "#page-container"
});

This is controller.js to be prepended to app.js.

(function (global) {
  global.controller = {
    Controller: function Controller(spec) {
        this.container = $(spec.container);
    }
  };
}(this));

Have grunt-resolve in your package.json

{
  "name": "your-project-name",
  "version": "0.1.2",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-resolve": "~0.1.0"
  }
}

In the Gruntfile.js we put now

grunt.initConfig({
	[...]
	resolve: {
		files: [ "src/app.js" ]
	}
});

grunt.loadNpmTasks('grunt-resolve');

This will create dist/app.js with the concatenated source code. resolve resolves files recursively, normalizes multiple dependencies from and to a given file, skips declared dependencies not found on disk and is robust concerning circular dependencies (does not loop).

Declaring dependencies

In a JavaScript file one or more require lines can be added:

//= require "dep1.js"
//= require "dep2.js"

All require lines of a file are gathered, hoisted to the top of the file and recursively resolved for transitive dependencies.

Multiple files can be listed separated by commas:

//= require "dep1.js, dep2.js"

Dependencies are relative to the source file containing the require line:

//= require "dep1.js, modules/module-1.js, ../common/extend.js"

Grunt task config

The complete configuration options are as follows:

grunt.initConfig({
	resolve: {
		files: [ "src/app.js", "modules/google-analytics.js" ],
		dist: "outputdir",
		exclude: {
			"src/app.js": ["Spec\\.js$", "test-*\\.js$"]
		}
	}
});

resolve's People

Contributors

marcbaechinger avatar dmitriykharchenko avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.