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

babel-plugin-add-module-metadata

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-add-module-metadata - npm Package Compare versions

Comparing version 2.13.2 to 2.13.3-alpha.6

LICENSE.md

244

lib/index.js

@@ -1,51 +0,44 @@

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (_ref) {
var t = _ref.types;
return {
visitor: {
Identifier: {
exit: function exit(path, state) {
maybeAddEsModuleFlagCase2(t, path, state);
}
},
StringLiteral: {
exit: function exit(path, state) {
maybeAddEsModuleFlagCase1(t, path, state);
}
}
}
};
"use strict";
/**
* © 2017 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var _babelIpc = require('liferay-npm-build-tools-common/lib/babel-ipc');
var babelIpc = _interopRequireWildcard(_babelIpc);
var _packages = require('liferay-npm-build-tools-common/lib/packages');
var pkgs = _interopRequireWildcard(_packages);
var _pluginLogger = require('liferay-npm-build-tools-common/lib/plugin-logger');
var _pluginLogger2 = _interopRequireDefault(_pluginLogger);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _readJsonSync = require('read-json-sync');
var _readJsonSync2 = _interopRequireDefault(_readJsonSync);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const babelIpc = __importStar(require("liferay-npm-build-tools-common/lib/babel-ipc"));
const babelUtil = __importStar(require("liferay-npm-build-tools-common/lib/babel-util"));
const plugin_logger_1 = __importDefault(require("liferay-npm-build-tools-common/lib/plugin-logger"));
const path_1 = __importDefault(require("path"));
const read_json_sync_1 = __importDefault(require("read-json-sync"));
/**
* @return {object} a babel visitor
*/
function default_1({ types: t }) {
return {
visitor: {
Identifier: {
exit(path, state) {
maybeAddEsModuleFlagCase2(t, path, state);
},
},
StringLiteral: {
exit(path, state) {
maybeAddEsModuleFlagCase1(t, path, state);
},
},
},
};
}
exports.default = default_1;
/**
* Add `esModule` flag when `__esModule` is being assigned true through

@@ -58,45 +51,33 @@ * `Object.defineProperty()`.

*/
/**
* © 2017 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
function maybeAddEsModuleFlagCase1(t, path, state) {
var node = path.node;
if (node.value !== '__esModule') {
return false;
}
var parent = path.parent;
if (!t.isCallExpression(parent)) {
return false;
}
var callee = parent.callee;
if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.object) || callee.object.name !== 'Object' || !t.isIdentifier(callee.property) || callee.property.name !== 'defineProperty') {
return false;
}
var args = parent.arguments;
if (args.length != 3) {
return false;
}
if (!t.isIdentifier(args[0]) || args[0].name !== 'exports' || !t.isStringLiteral(args[1]) || args[1].value !== '__esModule' || !t.isObjectExpression(args[2])) {
return false;
}
addEsModuleFlag(state);
return true;
const { node } = path;
if (node.value !== '__esModule') {
return false;
}
const { parent } = path;
if (!t.isCallExpression(parent)) {
return false;
}
const { callee } = parent;
if (!t.isMemberExpression(callee) ||
!t.isIdentifier(callee.object) ||
callee.object.name !== 'Object' ||
!t.isIdentifier(callee.property) ||
callee.property.name !== 'defineProperty') {
return false;
}
const args = parent.arguments;
if (args.length != 3) {
return false;
}
if (!t.isIdentifier(args[0]) ||
args[0].name !== 'exports' ||
!t.isStringLiteral(args[1]) ||
args[1].value !== '__esModule' ||
!t.isObjectExpression(args[2])) {
return false;
}
addEsModuleFlag(state);
return true;
}
/**

@@ -110,27 +91,14 @@ * Add `esModule` flag when `__esModule` is being assigned true through

*/
/**
* @return {object} a babel visitor
*/
function maybeAddEsModuleFlagCase2(t, path, state) {
var node = path.node;
if (node.name !== '__esModule') {
return false;
}
var parent = path.parent;
if (!t.isMemberExpression(parent) || !parent.object === 'exports') {
return false;
}
addEsModuleFlag(state);
return true;
const { node } = path;
if (node.name !== '__esModule') {
return false;
}
const { parent } = path;
if (!t.isMemberExpression(parent) || !parent.object === 'exports') {
return false;
}
addEsModuleFlag(state);
return true;
}
/**

@@ -141,35 +109,21 @@ * Add `esModule` flag to current JS module.

function addEsModuleFlag(state) {
if (state.esModuleFlagAdded) {
return;
}
var filenameRelative = state.file.opts.filenameRelative;
var filenameAbsolute = _path2.default.resolve(filenameRelative);
var pkgDir = pkgs.getPackageDir(filenameRelative);
var pkgJson = (0, _readJsonSync2.default)(_path2.default.join(pkgDir, 'package.json'));
var _babelIpc$get = babelIpc.get(state),
rootPkgJson = _babelIpc$get.rootPkgJson;
var pkgId = pkgJson.name === rootPkgJson.name ? '/' : pkgJson.name + '@' + pkgJson.version;
var _babelIpc$get2 = babelIpc.get(state),
manifest = _babelIpc$get2.manifest;
manifest.addModuleFlags(pkgId, _path2.default.relative(pkgDir, filenameAbsolute), {
esModule: true
});
state.esModuleFlagAdded = true;
var _babelIpc$get3 = babelIpc.get(state, function () {
return {
log: new _pluginLogger2.default()
};
}),
log = _babelIpc$get3.log;
log.info('add-module-metadata', "Added 'esModule' flag");
if (state.esModuleFlagAdded) {
return;
}
const { filename } = state.file.opts;
const pkgDir = babelUtil.getPackageDir(filename);
const pkgJson = read_json_sync_1.default(path_1.default.join(pkgDir, 'package.json'));
const { rootPkgJson } = babelIpc.get(state);
const pkgId = pkgJson.name === rootPkgJson.name
? '/'
: `${pkgJson.name}@${pkgJson.version}`;
const { manifest } = babelIpc.get(state);
manifest.addModuleFlags(pkgId, path_1.default.relative(pkgDir, filename), {
esModule: true,
});
state.esModuleFlagAdded = true;
const { log } = babelIpc.get(state, () => ({
log: new plugin_logger_1.default(),
}));
log.info('add-module-metadata', "Added 'esModule' flag");
}
//# sourceMappingURL=index.js.map
{
"name": "babel-plugin-add-module-metadata",
"version": "2.13.2",
"version": "2.13.3-alpha.6+ae09044",
"description": "A Babel plugin to add AMD modules' metadata to the manifest.json file.",
"main": "lib/index.js",
"scripts": {
"build": "babel --source-maps -D -d lib src",
"prepublish": "npm run build"
"copyfiles": "node ../../scripts/copyfiles.js",
"build": "tsc && yarn copyfiles",
"prepublish": "yarn build"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-es2015": "^6.24.1"
"dependencies": {
"liferay-npm-build-tools-common": "2.13.3-alpha.6+ae09044",
"read-json-sync": "^2.0.1"
},
"dependencies": {
"liferay-npm-build-tools-common": "2.13.2",
"read-json-sync": "^1.1.1"
}
"gitHead": "ae09044beb216743cb2f1e2273aa6e055716ce64"
}

@@ -12,2 +12,3 @@ /**

import PkgDesc from 'liferay-npm-build-tools-common/lib/pkg-desc';
import project from 'liferay-npm-build-tools-common/lib/project';
import path from 'path';

@@ -17,46 +18,53 @@

const pkgJson = require(path.join(__dirname, '../../package.json'));
const pkgDesc = new PkgDesc(pkgJson.name, pkgJson.version, null);
const prjDirPath = path.join(__dirname, '__fixtures__', 'a-project');
const filenameRelative = path.join('path', 'to', 'module.js');
const filename = path.join(prjDirPath, filenameRelative);
const prjPkgDesc = new PkgDesc('a-project', '1.0.0', null);
let logger, manifest;
const case1Source = `
Object.defineProperty(exports, "__esModule", {
value: true
});
beforeEach(() => {
babelIpc.set(__filename, {
log: (logger = new PluginLogger()),
manifest: (manifest = new Manifest()),
rootPkgJson: pkgJson,
});
var x = require('./x');
console.log('x is', x);
module.exports = 'Here is x: ' + x;
`;
const case2Source = `
Object.defineProperty(exports, "__esModule", {
value: true
});
var x = require('./x');
console.log('x is', x);
module.exports = 'Here is x: ' + x;
`;
beforeAll(() => {
project.loadFrom(prjDirPath);
});
describe('esModule flag', () => {
const case1Source = `
Object.defineProperty(exports, "__esModule", {
value: true
});
describe('plugin feature tests', () => {
let logger, manifest;
var x = require('./x');
console.log('x is', x);
module.exports = 'Here is x: ' + x;
`;
const case2Source = `
Object.defineProperty(exports, "__esModule", {
value: true
});
beforeEach(() => {
babelIpc.set(filenameRelative, {
log: (logger = new PluginLogger()),
manifest: (manifest = new Manifest()),
rootPkgJson: project.pkgJson,
});
});
var x = require('./x');
console.log('x is', x);
module.exports = 'Here is x: ' + x;
`;
it('is added in case 1', () => {
it('esModule flag is added in case 1', () => {
const source = case1Source;
babel.transform(source, {
filenameRelative: __filename,
filename,
filenameRelative,
plugins: [plugin],
});
const pkg = manifest.getPackage(pkgDesc);
const pkg = manifest.getPackage(prjPkgDesc);
const {flags} = pkg.modules['src/__tests__/index.test.js'];
const {flags} = pkg.modules[filenameRelative];

@@ -66,3 +74,3 @@ expect(flags.esModule).toBe(true);

it('is added in case 2', () => {
it('esModule flag is added in case 2', () => {
const source = `

@@ -77,9 +85,10 @@ module.exports.__esModule = true;

babel.transform(source, {
filenameRelative: __filename,
filename,
filenameRelative,
plugins: [plugin],
});
const pkg = manifest.getPackage(pkgDesc);
const pkg = manifest.getPackage(prjPkgDesc);
const {flags} = pkg.modules['src/__tests__/index.test.js'];
const {flags} = pkg.modules[filenameRelative];

@@ -93,7 +102,14 @@ expect(flags.esModule).toBe(true);

babel.transform(source, {
filenameRelative: __filename,
filename,
filenameRelative,
plugins: [plugin],
});
expect(logger.messages).toMatchSnapshot();
expect(logger.messages).toEqual([
{
level: 'info',
source: 'add-module-metadata',
things: ["Added 'esModule' flag"],
},
]);
});

@@ -105,8 +121,15 @@

babel.transform(source, {
filenameRelative: __filename,
filename,
filenameRelative,
plugins: [plugin],
});
expect(logger.messages).toMatchSnapshot();
expect(logger.messages).toEqual([
{
level: 'info',
source: 'add-module-metadata',
things: ["Added 'esModule' flag"],
},
]);
});
});

@@ -8,3 +8,3 @@ /**

import * as babelIpc from 'liferay-npm-build-tools-common/lib/babel-ipc';
import * as pkgs from 'liferay-npm-build-tools-common/lib/packages';
import * as babelUtil from 'liferay-npm-build-tools-common/lib/babel-util';
import PluginLogger from 'liferay-npm-build-tools-common/lib/plugin-logger';

@@ -123,5 +123,4 @@ import npath from 'path';

const {filenameRelative} = state.file.opts;
const filenameAbsolute = npath.resolve(filenameRelative);
const pkgDir = pkgs.getPackageDir(filenameRelative);
const {filename} = state.file.opts;
const pkgDir = babelUtil.getPackageDir(filename);
const pkgJson = readJsonSync(npath.join(pkgDir, 'package.json'));

@@ -138,3 +137,3 @@

manifest.addModuleFlags(pkgId, npath.relative(pkgDir, filenameAbsolute), {
manifest.addModuleFlags(pkgId, npath.relative(pkgDir, filename), {
esModule: true,

@@ -141,0 +140,0 @@ });

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