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 2.0.1 to 2.0.2

101

index.js

@@ -1,24 +0,77 @@

module.exports = function browserize({ main, named }) {
module.exports = function browserize({ main, named, imports={} }) {
if (main && named) {
const mainExport = browserizeMain(main)
const mainName = mainExport.match(/export +default +(?:class +|function +|const +)?(\w+)/)[1]
const namedExport = browserizeNamed(named, new RegExp(`^const +${mainName} *= *require\\((?:'[^']+'|"[^"]+")\\)`))
return `${mainExport}\n;;\n${deduped(namedExport, mainExport)}`
return interpolateImports(
browserizeBoth(main, named),
imports,
)
}
if (named) {
return browserizeNamed(named)
return interpolateImports(
browserizeNamed(named),
imports,
)
}
if (main) {
return browserizeMain(main)
return interpolateImports(
browserizeMain(main),
imports,
)
}
throw new Error('Expect at least one input file')
}
function deduped(named, main) {
function browserizeBoth(main, named) {
const mainExport = browserizeMain(main)
const mainName = mainExport.match(/export +default +(?:class +|function +|const +)?(\w+)/)[1]
const namedExport = browserizeNamed(named, new RegExp(`^const +${mainName} *= *require\\((?:'[^']+'|"[^"]+")\\)`))
return `${mainExport}\n;;\n${deduplicate(namedExport, mainExport)}`
}
function browserizeMain(file) {
const mainExportParts = file.split(/^module\.exports[ \t\n]*=[ \t\n]*/m)
if (mainExportParts.length !== 2) {
throw new Error('Expect exactly one export in default export file')
}
return `${mainExportParts[0]}export default ${mainExportParts[1]}`
}
function browserizeNamed(file, defaultExport) {
const namedExportsParts = file.split(/^module\.exports[ \t\n]*=[ \t\n]*\{/m)
if (namedExportsParts.length !== 2) {
throw new Error('Expect exactly one grouped export in named exports file')
}
let browserizedContent = `${namedExportsParts[0]}export {${namedExportsParts[1]}`
if (defaultExport) {
browserizedContent = browserizedContent.replace(defaultExport, '')
}
return browserizedContent
}
//
function interpolateImports(baseFile, imports) {
return Object.entries(imports).reduce((file, [path, data]) => (
file.replace(
/require\((['"])(\..+?)\1\)/g,
(_, __, requirePath) => JSON.stringify(data)
)
), baseFile)
}
function deduplicate(named, main) {
return named

@@ -73,27 +126,1 @@ .replace(

}
function browserizeMain(data) {
const mainExportParts = data.split(/^module\.exports[ \t\n]*=[ \t\n]*/m)
if (mainExportParts.length !== 2) {
throw new Error('Expect exactly one export in default export file')
}
return `${mainExportParts[0]}export default ${mainExportParts[1]}`
}
function browserizeNamed(data, defaultExport) {
const namedExportsParts = data.split(/^module\.exports[ \t\n]*=[ \t\n]*\{/m)
if (namedExportsParts.length !== 2) {
throw new Error('Expect exactly one grouped export in named exports file')
}
let browserizedContent = `${namedExportsParts[0]}export {${namedExportsParts[1]}`
if (defaultExport) {
browserizedContent = browserizedContent.replace(defaultExport, '')
}
return browserizedContent
}
{
"name": "browserize",
"description": "Converts simple node.js modules into ES6 modules",
"version": "2.0.1",
"version": "2.0.2",
"author": "rasenplanscher",

@@ -6,0 +6,0 @@ "license": "MIT",

@@ -29,11 +29,100 @@ # browserize

```js
module.exports = function defaultExport() {}
module.exports = function main() {}
```
```js
const extra = 'EXTRA',
module.exports = {
extra,
}
```
into this:
```js
export default function defaultExport() {}
export default function main() {}
const extra = 'EXTRA',
export {
extra,
}
```
### Recognizes references to `main`
`browserize` turns this:
```js
module.exports = function main() { return common }
```
```js
const main = require('./main')
module.exports = {
extra: main,
}
```
into this:
```js
const common = 'CONSTANT'
export default function main() { return common }
export {
extra: main,
}
```
This relies
### Merges constants and Variables
`browserize` turns this:
```js
const common = 'CONSTANT'
module.exports = function main() { return common }
```
```js
const common = 'CONSTANT'
module.exports = {
extra: common,
}
```
into this:
```js
const common = 'CONSTANT'
export default function main() { return common }
export {
extra: common,
}
```
and this:
```js
let common = 'VARIABLE'
module.exports = function main() { return common }
```
```js
let common = 'VARIABLE'
module.exports = {
extra: common,
}
```
into this:
```js
let common = 'VARIABLE'
export default function main() { return common }
common = 'VARIABLE'
export {
extra: common,
}
```
**NOTE:** Since `browserize` cannot know if `common` gets changed in `main.js`, it leaves the assignment in place. If the variable is not assigned in the second file, the declaration is removed.
## What it does not

@@ -63,9 +152,13 @@ `browserize` does not:

**NOTE:** If you want to interpolate imports, you need to use the node API.
This feature is currently not available for `browserize/fs` and consequently for the CLI.
## node API
You can import either `browserize` or `browserize/fs`, depending on how you will use it.
`browserize` takes an options object with two optional entries:
`browserize` takes an options object with three optional entries:
+ `main`: a string containing the main/default export
+ `named`: a string containing the named exports
+ `imports`: a key/value store that maps import paths to replacement values

@@ -114,2 +207,22 @@ `browserize/fs` takes an options object with three optional entries:

#### Replacing imports
```js
const fs = require('fs-extra')
const browserize = require('browserize')
const main = fs.readFileSync('main.js').toString()
browserize({
main,
imports: {
'./constant': 'CONSTANT',
}
})
```
Using this feature, you can extract constants for common use in node files and still have an ESM file without dependencies.
**IMPORTANT**: The keys are matched verbatim, so `imports:{'./x':'X'}` will do nothing for `require('./x.js')`.
**NOTE**: This only works for simple values, like strings and arrays, not functions or classes.
## CLI

@@ -151,2 +264,3 @@ ```bash

### Each source file must contain exactly one assignment to `module.exports`

@@ -182,2 +296,3 @@ #### Good

### The default export must be declared without a newline between the assignment operator and the exported item

@@ -200,2 +315,3 @@ #### Good

### The named exports must be declared as an object literal

@@ -208,4 +324,4 @@ #### Good

module.exports = {
key1: helper1,
key2: helper2,
helper1,
helper2,
}

@@ -216,4 +332,4 @@ ```

```js
module.exports.key1 = helper1
module.exports.key2 = helper2
module.exports.helper1 = helper1
module.exports.helper2 = helper2
```

@@ -223,1 +339,26 @@ 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.
### The named exports must use shorthand syntax
#### Good
```js
module.exports = {
helper1,
helper2,
}
```
#### Bad
```js
module.exports = {
helper1: helper1,
helper2: helper2,
}
```
```js
module.exports = {
key1: helper1,
key2: helper2,
}
```
While this is valid in node.js, it will lead to an invalid ESM file.
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