GithubHelp home page GithubHelp logo

ascjs's Introduction

ascjs

License: ISC Build Status Coverage Status donate Greenkeeper badge

ES2015 to CommonJS import/export transformer


Looking for a CommonJS minimalistic bundler ?

Fully based on ascjs, asbundle is a no-brainer to create out of the box browser compatible bundles. Don't miss it out!


This module does one thing only: it loosely transpiles ES2015 import/export statements into valid CommonJS in order to fix the only part of Node that's incompatible with modern JS.

How to

You can use ascjs as binary utility or as module.

npm install -g ascjs

# to see what you can do
ascjs --help

As executable, you can use ascjs to output, or save, some code content.

ascjs code
ascjs --ignore=a.js,b.js sourceFile
ascjs --no-default
ascjs sourceFile
ascjs sourceFile destFile

# folders are recursively parsed
# destFolder is mandatory
ascjs sourceFolder destFolder

You can also use it via pipe operator.

echo code | ascjs
cat source.js | ascjs | uglifyjs -o dest.js

As module, you can require it and use it to convert ESM to CJS.

const ascjs = require('ascjs');

ascjs('import "test";');
// require("test");

Features

  • extremely lightweight, based on babylon for performance and reliability, it transforms only imports/exports ignoring everything else
  • produces modern JavaScript, you are in charge of extra transformations if needed
  • indentation, spaces, semi or no-semi are preserved: beautiful source code remains beautiful
  • uses same Babel convention, resolving export default ... intent as exports.default
  • you can finally write .js code and transform it for Node only before publishing on npm
  • you could write .mjs modules and transform them into CommonJS for Browserify or other bundlers as target

Constrains

  • live bindings for exported values are not preserved. You need to delegate in scope eventual changes
  • dynamic import(...) is untouched. If you write that, let Webpack handle it for you later on
  • there is no magic whatsoever in module names resolution, what you write in ESM is what you get as CJS

Flags

  • --ignore=... a comma separated paths to ignore parsing
  • --no-default remove the __esModule flag and export default via module.exports =

Example

This module can transform the following ES2015+ code

import func, {a, b} from './module.js';
import * as tmp from 'other';

const val = 123;

export default function test() {
  console.log('ascjs');
};

export {func, val};

into the following one:

'use strict';
const func = (m => m.__esModule ? m.default : m)(require('./module.js'));
const {a, b} = require('./module.js');
const tmp = require('other');

const val = 123;

function test() {
  console.log('ascjs');
}
Object.defineProperty(exports, '__esModule', {value: true}).default = test;

exports.func = func;
exports.val = val;

ascjs's People

Contributors

greenkeeper[bot] avatar webreflection avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ascjs's Issues

Parse `import.meta` and replace it with an object

If import.meta is found in the wild the equivalent CJS will likely break.

This module should ensure that import.meta is translated as {url: __filename}, taking into account Windows shenanigans with paths.

Namespace semantics

The following lines are not equivalent because namespace objects do not contain default exports.

import * as tmp from 'other';
const tmp = require('other');

It would be possible to add some conditional handling for the case where 'other' is found to be an ES module. Let me know if you are interested in PRs for this kind of thing.

Error when transforming export * from relative module path.

rimraf cjs/* && ascjs esm cjs

/Users/bret/littlstar/sdk-js/node_modules/ascjs/index.js:137
      }${specifier.local.name}${EOL}`;
                         ^

TypeError: Cannot read property 'name' of undefined
    at /Users/bret/littlstar/sdk-js/node_modules/ascjs/index.js:137:26
    at Array.forEach (<anonymous>)
    at Object.ExportNamedDeclaration (/Users/bret/littlstar/sdk-js/node_modules/ascjs/index.js:132:21)
    at /Users/bret/littlstar/sdk-js/node_modules/ascjs/index.js:171:37
    at Array.forEach (<anonymous>)
    at parse (/Users/bret/littlstar/sdk-js/node_modules/ascjs/index.js:169:23)
    at ascjs (/Users/bret/littlstar/sdk-js/node_modules/ascjs/bin.js:6:18)
    at /Users/bret/littlstar/sdk-js/node_modules/ascjs/bin.js:99:25

It throws when I try to transform this file:

export * as commands from './commands/index.js'

Reverting to 4.0.2 (and lower) and the transform continues to work. Not a show stopper on my end, work arounds aplenty.

Digging into what change might have caused this, and what that change changed.

ReferenceError: path is not defined

ascjs --ignore=main.mjs src/ lib/

 | /packages/asciidoctor/project/node_modules/ascjs/bin.js:15
 |           .map(file => path.resolve(__dirname, file))
 |                             ^
 | 
 | ReferenceError: path is not defined
 |     at ignore.push.apply.arg.slice.replace.split.map.file packages/asciidoctor/(project/node_modules/ascjs/bin.js:15:29)
 |     at Array.map (<anonymous>)
 |     at options.forEach.arg packages/asciidoctor/(project/node_modules/ascjs/bin.js:15:12)
 |     at Array.forEach (<anonymous>)
 |     at Object.<anonymous> packages/asciidoctor/(project/node_modules/ascjs/bin.js:10:9)
 |     at Module._compile (module.js:653:30)
 |     at Object.Module._extensions..js (module.js:664:10)
 |     at Module.load (module.js:566:32)
 |     at tryModuleLoad (module.js:506:12)
 |     at Function.Module._load (module.js:498:3)

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

switch to `@babel/parser`

The babylon project has been archived, hence it'd be wise to switch into @babel/parser instead, as that's the one updated.

However, there are various hcanges I've no idea about, so I'll need to investigate what changed, what breaks, what works, etc etc.

Don't touch actual CommonJS files

Use case, you have ES Modules files in src, but there you also have src/cli.js which is actual real cjs file with shebang.

I propose to skip such files that don't have import/exports, so just copy them without transforming them.

Say you have this src/cli.js

#!/usr/bin/env node

'use strict';

const proc = require('process');

// ... cli code

it gets transformed to invalid bin file

'use strict';
#!/usr/bin/env node

'use strict';

const proc = require('process');

// ... rest

Watch flag

Problem: when I develop an esm module back-ported to cjs with ascjs, I sometimes link the module to a local consumer module that is running in cjs mode. When I modify the esm module, I forget to re-compile the cjs version and end up pulling out my hair when it doesn't change things like I expect, and then eventually remember to recompile. (Has happened 2-3 time now 😅)

Possible solution: add a watch flag to ascjs. This way, when working on the esm portion, any file changes will trigger a cjs rebuild automatically. That way if I have the module linked to cjs consumers, I don't have to manually trigger rebuilds.

Would you be interested in that addition and do you have any thoughts on how you would like that done?

An in-range update of coveralls is breaking the build 🚨

The devDependency coveralls was updated from 3.0.8 to 3.0.9.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

coveralls is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • coverage/coveralls: First build on greenkeeper/coveralls-3.0.9 at 100.0% (Details).
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for Maintenance, dependency refinement
  • removed Istanbul, mocha dependencies
  • reverted cobertura-parse support (package dependency archived)
  • removed snyk (automated security updates from GitHub via Dependabot enabled)
  • improved Windows support
Commits

The new version differs by 15 commits.

  • 2e2b108 bump version
  • 50913ed Remove the now unused istanbul and mocha-lcov-reporter devDependencies
  • 1bceeff Revert #243.
  • 4aa11a2 Remove snyk.
  • 33eccc3 Revert "CI: use npm ci on Node.js >=8."
  • 236529b Update logger.js
  • 3a90b07 fix equality operator in logger.js
  • bbe2de5 Update package.json
  • 07ef879 Fix logger regression.
  • f58f8b9 README.md: Add GitHub Actions CI info.
  • 0488f10 Tweak README.md
  • be79dab CI: use npm ci on Node.js >=8.
  • ef51945 CI: Add Windows testing and lint
  • c202346 Fix Windows tests again.
  • 3d82534 use Coveralls GitHub action

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Regression in 6.0.2

LinkeDOM can't build anymore with latest ascjs ... which is a bummer ... all good up to 6.0.1

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.