GithubHelp home page GithubHelp logo

stralsi / amd-to-common Goto Github PK

View Code? Open in Web Editor NEW

This project forked from willyham/amd-to-common

0.0 2.0 0.0 16 KB

Convert requireJS AMD style defines to requireJS commonJS style defines

JavaScript 100.00%

amd-to-common's Introduction

amd-to-common

Convert requireJS AMD style defines to requireJS commonJS style defines.

Install

npm install -g amd-to-common

Usage:

amd-to-common [file/directory/glob] [--exclude=]

Important: This /will/ rewrite your files. Please make sure they are checked in to some kind of source control before running amd-to-common.

What does it do?

RequireJS has a secondary, little known import style. When writing a module, most people use something like this:

define([
  'underscore',
], function(_){
  'use strict';

  return {
    invoke: _.noop
  };

});

However, there is a much simpler and more beautiful syntax which is resembles commonJS:

define(function(require, exports, module){
  'use strict';
  
  var _ = require('underscore');

  module.exports = {
    invoke: _.noop
  };

});

There are a few advantages to using this syntax. It removes the unweildy array and huge dependecy list construct and also gives you a much simpler task if you're planning on converting to something commonJS compliant like Browserify.

Why not use browserify-ftw?

browserify-ftw is an awesome project which aims to convert your project to browserify in basically one shot. Sometimes, especially for large webapps, this isn't ideal. On a large project, we wanted a half-way house so that we could write in CommonJS style whilst still preparing to move to browserify.

How does it work?

amd-to-common uses esprima to parse your JS files into an AST. It then analyses the content of the file, and does some really gnarly string replacements in order to rewrite to CommonJS style. Ideally, it would modify the AST and use something like escodegen to rewrite the source, but as most projects have very different spacing styles, this would probably anger a lot of people.

Is it safe?

Most likely, but I'm sure there are some edge cases. If it makes you feel better, I've used this to convert a pretty complex (~200 module) web app with very few no problems.

amd-to-common's People

Contributors

willyham avatar janza avatar

Watchers

James Cloos avatar  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.