Code Monkey home page Code Monkey logo

source-map-url's Introduction

Overview Build Status

browser support

Tools for working with sourceMappingURL comments.

var sourceMappingURL = require("source-map-url")

var code = [
  "!function(){...}();",
  "/*# sourceMappingURL=foo.js.map */"
].join("\n")

sourceMappingURL.get(code)
// foo.js.map

code = sourceMappingURL.set(code, "/other/file.js.map")
// !function(){...}();
// /*# sourceMappingURL=/other/file.js.map */

code = sourceMappingURL.insertBefore(code, "\n// License: MIT")
// !function(){...}();
// // License: MIT
// /*# sourceMappingURL=/other/file.js.map */

code = sourceMappingURL.remove(code)
// !function(){...}();
// // License: MIT

sourceMappingURL.get(code)
// null

Installation

  • npm install source-map-url
  • bower install source-map-url
  • component install lydell/source-map-url

Works with CommonJS, AMD and browser globals, through UMD.

Usage

sourceMappingURL.get(code)

Returns the url of the sourceMappingURL comment in code. Returns null if there is no such comment. Note that the url can be the empty string and that both the empty string and null are falsy. Consider using if (url === null) {} rather than if (url) {} if you need to tell those two cases apart.

sourceMappingURL.set(code, url [, commentSyntax])

Updates the sourceMappingURL comment in code to use url. Creates such a comment if there is none. Returns the updated code.

The commentSyntax argument is optional. It is an array. The first element of it defines how a comment starts, while the second element defines how it ends. The default value is ["/*", " */"]. /**/ comments were chosen as default in favor of // comments, because ideally they work with both JavaScript and CSS. This way, you don’t have to think about what type of code you’re working with. However, Chrome sadly does not support /**/ comments in JavaScript. So currently, you need to use .set(code, url, ["//"]) for JavaScript that needs source map support in Chrome. A bug has been filed about this.

sourceMappingURL.remove(code)

Removes the sourceMappingURL comment in code. Does nothing if there is no such comment. Returns the updated code.

sourceMappingURL.insertBefore(code, string)

Inserts string before the sourceMappingURL comment in code. Appends string to code if there is no such comment.

Lets you append something to a file without worrying about breaking the sourceMappingURL comment (which needs to be at the end of the file).

sourceMappingURL.regex

The regex that is used to match sourceMappingURL comments. It matches both // and /**/ comments, thus supporting both JavaScript and CSS.

sourceMappingURL.SourceMappingURL(commentSyntax)

Lets you create a new SourceMappingURL instance, using commentSyntax as default comment syntax for the set method.

For example, if you mainly work with JavaScript code that needs to have source map support in Chrome, you could use it like this:

var SourceMappingURL = require("source-map-url").SourceMappingURL
var sourceMappingURL = new SourceMappingURL(["//"])

Tests

Start by running npm test, which lints the code and runs the test suite in Node.js.

To run the tests in a browser, run testling (npm install -g testling) or testling -u.

License

The X11 (“MIT”) License.

source-map-url's People

Contributors

lydell avatar

Stargazers

blommaerts john avatar

Watchers

Jaeho Shin avatar James Cloos avatar  avatar blommaerts john 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.