browserize
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
const browserize = require('.'); | ||
const fs = require('./fs'); | ||
@@ -26,3 +26,3 @@ let defaultPath, namedPath, outputPath | ||
browserize({ | ||
fs({ | ||
default: defaultPath, | ||
@@ -29,0 +29,0 @@ named: namedPath, |
136
index.js
@@ -1,90 +0,86 @@ | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
module.exports = function browserize({ main, named }) { | ||
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\\((?:'[^']+'|"[^"]+")\\)`)) | ||
const defaultFilePath = 'index' | ||
const defaultInputExtension = '.js' | ||
const defaultOutputExtension = '.mjs' | ||
return `${mainExport}\n;;\n${deduped(namedExport, mainExport)}` | ||
} | ||
module.exports = function browserize(options = {}) { | ||
const output = browserizeFiles(options) | ||
writeFile(output, options) | ||
} | ||
// | ||
function writeFile(data, { | ||
default: defaultPath = defaultFilePath, | ||
named: namedPath, | ||
output: filePath, | ||
encoding = 'utf8', | ||
}) { | ||
if (!filePath) { | ||
if (!defaultPath) { | ||
defaultPath = namedPath | ||
} | ||
filePath = path.join( | ||
path.dirname(defaultPath), | ||
path.basename( | ||
defaultPath, | ||
path.extname(defaultPath) | ||
) | ||
) | ||
if (named) { | ||
return browserizeNamed(named) | ||
} | ||
if (filePath && !path.extname(filePath)) { | ||
filePath = filePath + defaultOutputExtension | ||
if (main) { | ||
return browserizeMain(main) | ||
} | ||
fs.outputFileSync(filePath, data, { encoding }) | ||
throw new Error('Expect at least one input file') | ||
} | ||
function browserizeFiles({ | ||
default: defaultPath = defaultFilePath, | ||
named: namedPath, | ||
}) { | ||
if (defaultPath && !path.extname(defaultPath)) { | ||
defaultPath = defaultPath + defaultInputExtension | ||
} | ||
if (namedPath && !path.extname(namedPath)) { | ||
namedPath = namedPath + defaultInputExtension | ||
} | ||
function deduped(named, main) { | ||
return named | ||
.replace( | ||
/\bconst +(\w+)\s*=\s*([^\n]+);?\n/g, | ||
(match, name, value) => { | ||
const sameValue = new RegExp( | ||
`\\bconst +${ | ||
name | ||
}\\s*=\\s*${ | ||
value.replace(/([(.)])/g,'\\$1') | ||
}` | ||
) | ||
const anyValue = new RegExp( | ||
`\\bconst +${ | ||
name | ||
}\\b` | ||
) | ||
if (sameValue.test(main)) { | ||
return '' | ||
} | ||
if (defaultPath && namedPath) { | ||
const defaultExport = browserizeDefault(defaultPath) | ||
const defaultName = defaultExport.match(/(?:export default )(?:class +|function +|const +)?(\w+)/)[1] | ||
if (anyValue.test(main)) { | ||
throw new Error(`constant ${name} has different values`) | ||
} | ||
const namedExport = browserizeNamed(namedPath).replace(new RegExp(`^const +${defaultName} *= *require\\((['"])[^\\1]+\\1\\)`), '') | ||
return match | ||
} | ||
) | ||
.replace( | ||
/\b(?:let|var) +(\w+)(\s*=)?/g, | ||
(match, name, assignment) => { | ||
const variable = new RegExp( | ||
`\\b(?:let|var) +${ | ||
name | ||
}\\b` | ||
) | ||
return `${defaultExport}\n;;\n${namedExport}` | ||
} | ||
if (variable.test(main)) { | ||
if (assignment) { | ||
return `${name} =` | ||
} | ||
if (namedPath) { | ||
return browserizeNamed(namedPath) | ||
} | ||
return '' | ||
} | ||
if (defaultPath) { | ||
return browserizeDefault(defaultPath) | ||
} | ||
throw new Error('Expect at least one input file') | ||
return match | ||
} | ||
) | ||
} | ||
function browserizeDefault(filePath) { | ||
const defaultExportParts = fs.readFileSync(filePath).toString().split(/^module\.exports[ \t\n]*=[ \t\n]*/m) | ||
function browserizeMain(data) { | ||
const mainExportParts = data.split(/^module\.exports[ \t\n]*=[ \t\n]*/m) | ||
if (defaultExportParts.length !== 2) { | ||
if (mainExportParts.length !== 2) { | ||
throw new Error('Expect exactly one export in default export file') | ||
} | ||
return `${defaultExportParts[0]}export default ${defaultExportParts[1]}` | ||
return `${mainExportParts[0]}export default ${mainExportParts[1]}` | ||
} | ||
function browserizeNamed(filePath) { | ||
const namedExportsParts = fs.readFileSync(filePath).toString().split(/^module\.exports[ \t\n]*=[ \t\n]*\{/m) | ||
function browserizeNamed(data, defaultExport) { | ||
const namedExportsParts = data.split(/^module\.exports[ \t\n]*=[ \t\n]*\{/m) | ||
@@ -95,3 +91,9 @@ if (namedExportsParts.length !== 2) { | ||
return `${namedExportsParts[0]}export {${namedExportsParts[1]}` | ||
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": "1.0.2", | ||
"version": "2.0.0", | ||
"author": "rasenplanscher", | ||
@@ -10,3 +10,4 @@ "license": "MIT", | ||
"files": [ | ||
"cli.js" | ||
"cli.js", | ||
"fs.js" | ||
], | ||
@@ -13,0 +14,0 @@ "scripts": { |
@@ -64,14 +64,20 @@ # browserize | ||
## node API | ||
`browserize` takes an options object with three optional entries: | ||
+ `default`: the file where the default export is found, defaults to `index.js` | ||
You can import either `browserize` or `browserize/fs`, depending on how you will use it. | ||
`browserize` takes an options object with two optional entries: | ||
+ `main`: a string containing the main/default export | ||
+ `named`: a string containing the named exports | ||
`browserize/fs` takes an options object with three optional entries: | ||
+ `main`: the file where the main/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 the `default` or `named` filename with the extension `.mjs` | ||
+ `output`: where to write the ESM file, defaults to the `main` or `named` filename with the extension `.mjs` | ||
And that is it. | ||
### Two examples | ||
### Examples | ||
#### The simplest form | ||
```js | ||
const browserize = require('browserize') | ||
browserize() | ||
const browserizeFS = require('browserize/fs') | ||
browserizeFS() | ||
``` | ||
@@ -81,6 +87,18 @@ This reads `index.js` and writes the equivalent `index.mjs`, and that's it. | ||
#### The most complex case `browserize` covers | ||
#### Handling in-memory files | ||
```js | ||
const fs = require('fs-extra') | ||
const browserize = require('browserize') | ||
const main = fs.readFileSync('main.js').toString() | ||
browserize({ main }) | ||
``` | ||
Turns the content of `main.js` into its ESM version. | ||
This is mainly useful if you want to integrate with a build setup using in-memory files, like `gulp`. | ||
#### The most complex case `browserize/fs` covers | ||
```js | ||
const browserize = require('browserize') | ||
browserize({ | ||
@@ -101,6 +119,6 @@ default: 'class-name.jsx', | ||
The CLI passes the given arguments through to the underlying node API. | ||
The CLI passes the given arguments through to the underlying node API, and works through `browserize/fs`. | ||
### Three examples | ||
### Examples | ||
#### The simplest form | ||
@@ -107,0 +125,0 @@ ```sh |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11169
6
152
215