npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

importer

v0.7.2

Published

File importing for CoffeeScript and JavaScript

Downloads

45

Readme

Importer

Importer adds an #import statement to JavaScript based languages including CoffeeScript that works like #include in C-based languages. It compiles files into JavaScript, concatenates them together in the places you've defined, generates source maps, and manages recompilation for only those files that have changed, speeding up builds for large projects.

#import "name"
#import "another.coffee"
#import "somefile.js"
    
# some code using the imported files here...

In JavaScript, the //import directive can be used instead of #import.

Features

  • Import statements can be placed anywhere and the dependency source code will replace it.
  • Compiling CoffeeScript and JavaScript source files are included out of the box. You can add more to the importer.extensions object.
  • Support for generating source maps.
  • Support for framework or library dependencies in a search path as well as relative paths.
  • File extensions are optional and will be automatically resolved if not included.
  • Files will only be included once in the resulting code, regardless of how many times a file is imported.
  • If used as a server, only modified files will be recompiled on subsequent requests.
  • Can be used to run the compiled code directly on the command line or required in a Node module.

Command line usage

When installed with npm install importer -g, a command line tool called importer will be made available.

  1. To start a server to host your compiled code, run importer mainfile.coffee --port 8080
  2. To output to a file, run importer mainfile.coffee main.js
  3. To compile and execute, run importer mainfile.coffee

The command line options include:

-p, --port        Port to start server on       
-f, --frameworks  Path to frameworks directory    [default: "./frameworks"]
-m, --minify      Minifies the output JavaScript  [boolean]
-s, --source-map  Whether to output a source map  [boolean]

Node module usage

importer = require 'importer'
pkg = importer.createPackage './path/to/main/file',
    frameworkPath: '/path/to/frameworks'
    sync: false               # whether compilation should be synchronous (default: false)
    sourceMap: 'out.js.map'   # filename/url of the output sourcemap (default: null)
    minify: false             # whether to minify the output with UglifyJS
        
# if asynchronous...
pkg.build (err, result) ->
    # result is an object containing 
    # {code: 'compiled js', map: 'sourcemap if requested'}
        
# if synchronous...
try
    result = pkg.build()
catch err
    # do something
        
# to load and run the result as a node module...
moduleExports = pkg.require()

# or, without creating a package
moduleExports = importer.require './path/to/main/file',
    frameworkPath: '/path/to/frameworks'

Connect/Express middleware

# options supports all options documented above, plus the `url`
# attribute giving the route to use to access the compiled JS.
# Defaults to `"/#{path.basename(main, path.extname(main))}.js"`
# Sourcemaps are automatically generated at "#{url}.js.map" unless
# you turn them off by setting the `sourceMap` option to `false`.
app.use importer.middleware('main.coffee', options)

Adding additional languages

Currently, importing CoffeeScript and JavaScript files are supported but you can extend that to other languages that compile to JavaScript by adding an entry to the importer.extensions object.

importer.extensions['.lua'] = (code, generateSourceMap) -> 
    return lua.compile(code)

If a language compiler supporting source maps is used, you should first check the generateSourceMap option to be sure that they are desired by the user, and if so, return an object containing {code: 'compiled js', map: 'sourcemap'}. Otherwise, return a string.

License

The importer module is licensed under the MIT license.