rollup-plugin-cjs-es
Advanced tools
Comparing version 0.1.1 to 0.2.0
49
index.js
@@ -6,2 +6,3 @@ const path = require("path"); | ||
const {createFilter} = require("rollup-pluginutils"); | ||
const resolve = require("resolve"); | ||
@@ -22,34 +23,26 @@ const {wrapImport, unwrapImport} = require("./lib/transform"); | ||
// normalize map key to absolute path | ||
[options.importStyle, options.exportStyle].forEach(map => { | ||
if (typeof map === "object") { | ||
for (const key of Object.keys(map)) { | ||
const newKey = require.resolve(path.resolve(key)); | ||
if (newKey !== key) { | ||
map[newKey] = map[key]; | ||
delete map[key]; | ||
} | ||
if (typeof options.exportType === "object") { | ||
for (const key of Object.keys(options.exportType)) { | ||
const newKey = resolve.sync(key, {basedir: process.cwd()}); | ||
if (newKey !== key) { | ||
options.exportType[newKey] = options.exportType[key]; | ||
delete options.exportType[key]; | ||
} | ||
} | ||
}); | ||
} | ||
function getPreferStyle(type, id, requireId) { | ||
const preferStyle = options[type + "Style"]; | ||
if (typeof preferStyle === "string") { | ||
return preferStyle; | ||
function getExportType(id, importer) { | ||
if (!options.exportType) { | ||
return; | ||
} | ||
if (typeof preferStyle === "function") { | ||
return preferStyle(id, requireId); | ||
if (typeof options.exportType === "string") { | ||
return options.exportType; | ||
} | ||
if (typeof preferStyle === "object") { | ||
if (typeof preferStyle[id] === "string") { | ||
return preferStyle[id]; | ||
} | ||
if (typeof preferStyle[id] === "function") { | ||
return preferStyle[id](requireId); | ||
} | ||
if (typeof preferStyle[id] === "object") { | ||
return preferStyle[id][requireId]; | ||
} | ||
if (importer) { | ||
id = resolve.sync(id, { | ||
basedir: path.dirname(importer) | ||
}); | ||
} | ||
return typeof options.exportType === "function" ? | ||
options.exportType(id, importer) : options.exportType[id]; | ||
} | ||
@@ -94,4 +87,4 @@ | ||
sourceMap: options.sourceMap, | ||
importStyle: requireId => getPreferStyle("import", id, requireId), | ||
exportStyle: () => getPreferStyle("export", id), | ||
importStyle: requireId => getExportType(requireId, id), | ||
exportStyle: () => getExportType(id), | ||
hoist: options.hoist, | ||
@@ -98,0 +91,0 @@ dynamicImport: options.dynamicImport |
{ | ||
"name": "rollup-plugin-cjs-es", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Convert CommonJS module into ES module", | ||
@@ -13,2 +13,5 @@ "keywords": [ | ||
"main": "index.js", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
@@ -28,6 +31,7 @@ "test": "eslint **/*.js && mocha", | ||
"dependencies": { | ||
"cjs-es": "^0.3.0", | ||
"cjs-es": "^0.3.1", | ||
"estree-walker": "^0.5.1", | ||
"magic-string": "^0.24.0", | ||
"merge-source-map": "^1.1.0", | ||
"resolve": "^1.7.1", | ||
"rollup-pluginutils": "^2.0.1" | ||
@@ -34,0 +38,0 @@ }, |
rollup-plugin-cjs-es | ||
==================== | ||
[![Build Status](https://travis-ci.org/eight04/rollup-plugin-cjs-es.svg?branch=master)](https://travis-ci.org/eight04/rollup-plugin-cjs-es) | ||
Convert CommonJS module into ES module. Powered by [cjs-es](https://github.com/eight04/cjs-es). | ||
@@ -29,4 +31,3 @@ | ||
exclude: [], | ||
importStyle: "default", | ||
exportStyle: "default", | ||
exportType: "default", | ||
sourceMap: true, | ||
@@ -171,6 +172,4 @@ splitCode: true, | ||
To fix it, [mark the require as `// default`](https://github.com/eight04/cjs-es#import-style) or use the `importStyle` option. | ||
To fix it, [mark the require as `// default`](https://github.com/eight04/cjs-es#import-style), or use `exportType` option to tell the plugin that *foo.js* uses default export. | ||
However, adding comments manually could be a problem if they are mixed everywhere. In this case, you may want to set both `importStyle` and `exportStyle` to `"default"` so the plugin uses default import/export everywhere. | ||
### Dynamic import() problem with default export | ||
@@ -289,3 +288,3 @@ | ||
*bundled with `importStyle: "default"` and `exportStyle: "default"`* | ||
*bundled with `exportType: "default"`* | ||
```js | ||
@@ -317,3 +316,3 @@ var foo = { | ||
- `importer`: `string`. The module ID which is being transformed. It is usually an absolute path. | ||
- `importee`: `string`. The id inside `require()` function. | ||
- `importee`: `string`. The require ID inside `require()` function. | ||
@@ -326,39 +325,25 @@ The return value should be a `boolean`. | ||
* `dynamicImport`: `boolean`. If true then enable [dynamic import transformer](https://github.com/eight04/cjs-es#dynamic-import). Default: `false`. | ||
* `importStyle`: `string|object|function`. Change the importStyle option for cjs-es. | ||
* `exportType`: `string|object|function`. Tell the plugin what type of the export does the module use. | ||
If `importStyle` is a function, it receives 2 arguments: | ||
If `exportType` is a function, it receives 2 arguments: | ||
- `importer`: `string`. The module ID which is being transformed. | ||
- `importee`: `string`. The id inside `require()` function. | ||
- `modulId`: `string`. The ID of the module. | ||
- `importer`: `null|string`. If the module is imported by an importer, this would be the ID of the importer module. | ||
The return value should be `"named"` or `"default"`. | ||
The return value should be the type of export for `moduleId`. | ||
If `importStyle` is an object, it is a 2 depth map. For example: | ||
If `exportType` is an object, it is a `"path/to/module": type` map. | ||
```js | ||
importStyle: { | ||
"path/to/moduleA": "default", // set importStyle to "default" for moduleA | ||
"path/to/moduleB": { | ||
"./foo": "default" // set importStyle to "default" for moduleB and | ||
} // only when requiring `./foo` module. | ||
} | ||
``` | ||
Default: `"named"`. | ||
* `exportStyle`: `string|object|function`. Change the exportStyle option for cjs-es. | ||
If `exportStyle` is a function, it receives 1 argument: | ||
- `exporter`: `string`. The module ID which is being transformed. | ||
If `exportStyle` is an object, it is a `"path/to/module": "value"` map. | ||
Default: `"named"`. | ||
Changelog | ||
--------- | ||
* 0.2.0 (Apr 28, 2018) | ||
- Add: `exportType` option. | ||
- Drop: `importStyle`, `exportStyle` option. | ||
* 0.1.0 (Apr 27, 2018) | ||
- Initial releast. |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
1
16397
7
5
209
345
+ Addedresolve@^1.7.1
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
Updatedcjs-es@^0.3.1