Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browserize

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browserize - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

cli.js

31

index.js
const fs = require('fs-extra')
const path = require('path')
module.exports = function browserize(options) {
const defaultFilePath = 'index'
const defaultInputExtension = '.js'
const defaultOutputExtension = '.mjs'
module.exports = function browserize(options = {}) {
const output = browserizeFiles(options)

@@ -10,6 +15,13 @@ writeFile(output, options)

//
function writeFile(data, {
output: filePath = 'browserized.js',
output: filePath = defaultFilePath,
encoding = 'utf8',
}) {
if (filePath && !path.extname(filePath)) {
filePath = filePath + defaultOutputExtension
}
fs.outputFileSync(filePath, data, { encoding })

@@ -19,5 +31,13 @@ }

function browserizeFiles({
default: defaultPath = path.resolve('index.js'),
default: defaultPath = defaultFilePath,
named: namedPath,
}) {
if (defaultPath && !path.extname(defaultPath)) {
defaultPath = defaultPath + defaultInputExtension
}
if (namedPath && !path.extname(namedPath)) {
namedPath = namedPath + defaultInputExtension
}
if (defaultPath && namedPath) {

@@ -40,2 +60,3 @@ const defaultExport = browserizeDefault(defaultPath)

throw new Error('Expect at least one input file')

@@ -45,3 +66,3 @@ }

function browserizeDefault(filePath) {
const defaultExportParts = fs.readFileSync(filePath).toString().split(/^module.exports[ \t\n]*=[ \t\n]*/m)
const defaultExportParts = fs.readFileSync(filePath).toString().split(/^module\.exports[ \t\n]*=[ \t\n]*/m)

@@ -56,3 +77,3 @@ if (defaultExportParts.length !== 2) {

function browserizeNamed(filePath) {
const namedExportsParts = fs.readFileSync(filePath).toString().split(/^(?:module\.)?exports[ \t\n]*=[ \t\n]*\{/m)
const namedExportsParts = fs.readFileSync(filePath).toString().split(/^module\.exports[ \t\n]*=[ \t\n]*\{/m)

@@ -59,0 +80,0 @@ if (namedExportsParts.length !== 2) {

12

package.json
{
"name": "browserize",
"description": "Converts simple node.js modules into ES6 modules",
"version": "0.0.2",
"version": "0.0.3",
"author": "rasenplanscher",
"license": "MIT",
"main": "index.js",
"bin": "cli.js",
"files": [
"cli.js"
],
"scripts": {

@@ -12,6 +16,8 @@ "test": "ava"

"devDependencies": {
"ava": "^1.2.0"
"ava": "^1.2.0",
"proxyquire": "^2.1.0"
},
"dependencies": {
"fs-extra": "^7.0.1"
"fs-extra": "^7.0.1",
"minimist": "^1.2.0"
},

@@ -18,0 +24,0 @@ "homepage": "https://github.com/rasenplanscher/browserize#readme",

# browserize
Converts simple node.js modules into ES6 modules.
+ [What it is](#what-it-is)
+ [What it does](#what-it-does)
+ [What it does not](#what-it-does-not)
+ [When to use](#when-to-use)
+ [When not to use](#when-not-to-use)
+ ["But browserize does almost what I need"](#but-browserize-does-almost-what-i-need)
+ [How to use it](#how-to-use-it)
+ [CLI](#cli)
+ [node API](#node-api)
+ [Requirements](#requirements)
----
# What it is
+ [What it does](#what-it-does)
+ [What it does not](#what-it-does-not)
+ [When to use](#when-to-use)
+ [When not to use](#when-not-to-use)
+ ["But browserize does almost what I need"](#but-browserize-does-almost-what-i-need)
## What it does
`browserize` turns this:
```js
module.exports = function defaultExport() {}
```
into this:
```js
export default function defaultExport() {}
```
## What it does not
`browserize` does not:
+ check if the result will run in a browser
+ transform `require`s into `import`s
+ bundle dependencies àla Webpack/Rollup
+ transpile anything other than JavaScript, like CoffeeScript (it might work by coincidence, but there's no support for that)
## When to use
`browserize` is made for small packages without dependencies that should run both in node.js and in the browser.
## When not to use
If your package has any dependency, it's probably complex enough to warrant babel, webpack, or some such. Use that instead.
If you need to transpile anything, like CoffeScript or TypeScript, your tooling for that should cover you.
## "But browserize does almost what I need"
Open an issue, and let's talk about it 😉
# How to use it
+ [CLI](#cli)
+ [node API](#node-api)
+ [Requirements](#requirements)
## CLI
```bash
npx browserize [--no-default|-x] [[--default|-d] index.js] [[--named|-n] helpers.js] [[--output|-o] index.mjs]
```
### Three examples
#### The simplest form
```sh
npx browserize
```
This reads `index.js` and writes the equivalent `index.mjs`, and that's it.
#### Adding named exports
```sh
npm browserize -n helper-functions
```
This reads `index.js` and `helper-functions.js`, then transforms concatenates them, and finally writes the result to `index.mjs`.
#### The most complex case `browserize` covers
```sh
npx browserize class-name.jsx helper-functions.js dist/browser-magic.js
```
This includes named exports and sets custom paths for everything.
## node API
`browserize` takes an options object with three optional entries:
+ `default`: the file where the default export is found, defaults to `index.js`
+ `named`: where to find the named exports, defaults to `null`
+ `output`: where to write the ESM file, defaults to `index.mjs`
And that is it.
### Two examples
#### The simplest form
```js
const browserize = require('browserize')
browserize()
```
This reads `index.js` and writes the equivalent `index.mjs`, and that's it.
#### The most complex case `browserize` covers
```js
const browserize = require('browserize')
browserize({
default: 'class-name.jsx',
named: 'helper-functions.js',
output: 'dist/browser-magic.js',
})
```
This includes named exports and sets custom paths for everything.
## Requirements
`browserize` is a simple tool and has a few simple requirements:
### Each source file must contain exactly one assignment to `module.exports`
#### Good
```js
module.exports = class DefaultExport {}
```
```js
module.exports = {
key1: helper1,
key2: helper2,
}
```
#### Bad
```js
exports.key1 = helper1
exports.key2 = helper2
```
While valid, `browserize` does not know how to transform this.
```js
module.exports = export1
module.exports = export2
```
This is not useful anyway.
```js
window.myStuff = class DefaultExport {}
```
This is not a module.
### The default export must be declared without a newline between the assignment operator and the exported item
#### Good
```js
module.exports = class DefaultExport {}
```
```js
module.exports = class DefaultExport {
}
```
#### Bad
```js
module.exports =
class DefaultExport {}
```
While this is valid in node.js, it will lead to an invalid ESM file.
### The named exports must be declared as an object literal
#### Good
```js
module.exports = { helper1, helper2 }
```
```js
module.exports = {
key1: helper1,
key2: helper2,
}
```
#### Bad
```js
module.exports.key1 = helper1
module.exports.key2 = helper2
```
While this is valid in node.js, `browserize` does not understand it.
This is too complex, and has no real benefit over the object literal.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc