liferay-npm-bundler-plugin-replace-browser-modules
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _pluginLogger = require('liferay-npm-build-tools-common/lib/plugin-logger'); | ||
var _pluginLogger2 = _interopRequireDefault(_pluginLogger); | ||
var _index = require('../index'); | ||
@@ -31,4 +35,7 @@ | ||
var log = void 0; | ||
// Restore package status prior to running each test | ||
beforeEach(function () { | ||
log = new _pluginLogger2.default(); | ||
pkgFiles.forEach(function (file) { | ||
@@ -48,2 +55,15 @@ _cpFile2.default.sync(pkg.dir + '/sources/' + file, pkg.dir + '/' + file); | ||
it('logs results correctly', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
browser: 'test-browser.js.file' | ||
}; | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
expect(log.messages).toMatchSnapshot(); | ||
}); | ||
it('replaces main file with browser file', function () { | ||
@@ -57,3 +77,3 @@ var pkgJson = { | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
@@ -71,3 +91,3 @@ expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
@@ -85,3 +105,3 @@ expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
@@ -101,3 +121,3 @@ expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
@@ -118,3 +138,3 @@ expect(fs.readFileSync(pkg.dir + '/test-browser.js.file').toString()).toMatchSnapshot(); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
@@ -132,4 +152,4 @@ expect(fs.readFileSync(pkg.dir + '/test-browser.js.file').toString()).toMatchSnapshot(); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg, log: log }, { pkgJson: pkgJson }); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
@@ -8,3 +8,4 @@ 'use strict'; | ||
exports.default = function (_ref, _ref2) { | ||
var pkg = _ref.pkg; | ||
var pkg = _ref.pkg, | ||
log = _ref.log; | ||
var pkgJson = _ref2.pkgJson; | ||
@@ -16,6 +17,8 @@ | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson); | ||
replaceMainModule(pkg.dir, browser, pkgJson, log); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson); | ||
replaceModules(pkg.dir, browser, pkgJson, log); | ||
} | ||
} else { | ||
log.info('replace-browser-modules', 'No browser modules found'); | ||
} | ||
@@ -39,5 +42,6 @@ }; | ||
* @param {Object} pkgJson package.json contents | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceMainModule(pkgDir, browser, pkgJson) { | ||
function replaceMainModule(pkgDir, browser, pkgJson, log) { | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
@@ -49,3 +53,3 @@ var main = pkgJson.main || 'index.js'; | ||
replaceFile(pkgId, src, browser, dest, main); | ||
replaceFile(pkgId, src, browser, dest, main, log); | ||
} | ||
@@ -58,2 +62,3 @@ | ||
* @param {Object} pkgJson package.json contents | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
@@ -66,3 +71,3 @@ */ | ||
*/ | ||
function replaceModules(pkgDir, browser, pkgJson) { | ||
function replaceModules(pkgDir, browser, pkgJson, log) { | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
@@ -75,7 +80,7 @@ | ||
if (to == false) { | ||
ignoreFile(dest); | ||
ignoreFile(dest, from, log); | ||
} else { | ||
var src = _path2.default.join(pkgDir, to); | ||
replaceFile(pkgId, src, to, dest, from); | ||
replaceFile(pkgId, src, to, dest, from, log); | ||
} | ||
@@ -92,10 +97,14 @@ }); | ||
* @param {String} destName the name of the destination file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceFile(pkgId, src, srcName, dest, destName) { | ||
function replaceFile(pkgId, src, srcName, dest, destName, log) { | ||
var srcModuleName = srcName.replace('.js', ''); | ||
var destModuleName = destName.replace('.js', ''); | ||
log.info('replace-browser-modules', 'Replacing module ' + destName + ' with module ' + srcName); | ||
try { | ||
var contents = _fs2.default.readFileSync(src).toString(); | ||
contents = contents.replace('\'' + pkgId + '/' + srcModuleName + '\'', '\'' + pkgId + '/' + destModuleName + '\''); | ||
@@ -114,7 +123,11 @@ | ||
* @param {String} file path to file to be ignored | ||
* @param {String} fileName the name of the file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function ignoreFile(file) { | ||
function ignoreFile(file, fileName, log) { | ||
log.info('replace-browser-modules', 'Emptying module ' + fileName + ' because it is server-only'); | ||
_fs2.default.writeFileSync(file, '/* Module ignored by ' + 'liferay-npm-bundler-plugin-replace-browser-modules */\n'); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "liferay-npm-bundler-plugin-replace-browser-modules", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "A liferay-npm-bundler plugin to replace files listed under the browser/module entry of package.json files.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
import cpFile from 'cp-file'; | ||
import * as fs from 'fs'; | ||
import PluginLogger from 'liferay-npm-build-tools-common/lib/plugin-logger'; | ||
import plugin from '../index'; | ||
@@ -24,4 +25,7 @@ | ||
let log; | ||
// Restore package status prior to running each test | ||
beforeEach(() => { | ||
log = new PluginLogger(); | ||
pkgFiles.forEach(file => { | ||
@@ -41,2 +45,15 @@ cpFile.sync(`${pkg.dir}/sources/${file}`, `${pkg.dir}/${file}`); | ||
it('logs results correctly', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
browser: 'test-browser.js.file', | ||
}; | ||
plugin({pkg, log}, {pkgJson}); | ||
expect(log.messages).toMatchSnapshot(); | ||
}); | ||
it('replaces main file with browser file', () => { | ||
@@ -50,3 +67,3 @@ const pkgJson = { | ||
plugin({pkg}, {pkgJson}); | ||
plugin({pkg, log}, {pkgJson}); | ||
@@ -66,3 +83,3 @@ expect( | ||
plugin({pkg}, {pkgJson}); | ||
plugin({pkg, log}, {pkgJson}); | ||
@@ -82,3 +99,3 @@ expect( | ||
plugin({pkg}, {pkgJson}); | ||
plugin({pkg, log}, {pkgJson}); | ||
@@ -100,3 +117,3 @@ expect( | ||
plugin({pkg}, {pkgJson}); | ||
plugin({pkg, log}, {pkgJson}); | ||
@@ -121,3 +138,3 @@ expect( | ||
plugin({pkg}, {pkgJson}); | ||
plugin({pkg, log}, {pkgJson}); | ||
@@ -137,3 +154,3 @@ expect( | ||
plugin({pkg}, {pkgJson}); | ||
plugin({pkg, log}, {pkgJson}); | ||
}); |
@@ -7,3 +7,3 @@ import fs from 'fs'; | ||
*/ | ||
export default function({pkg}, {pkgJson}) { | ||
export default function({pkg, log}, {pkgJson}) { | ||
const browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
@@ -13,6 +13,8 @@ | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson); | ||
replaceMainModule(pkg.dir, browser, pkgJson, log); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson); | ||
replaceModules(pkg.dir, browser, pkgJson, log); | ||
} | ||
} else { | ||
log.info('replace-browser-modules', 'No browser modules found'); | ||
} | ||
@@ -26,5 +28,6 @@ } | ||
* @param {Object} pkgJson package.json contents | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceMainModule(pkgDir, browser, pkgJson) { | ||
function replaceMainModule(pkgDir, browser, pkgJson, log) { | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
@@ -36,3 +39,3 @@ const main = pkgJson.main || 'index.js'; | ||
replaceFile(pkgId, src, browser, dest, main); | ||
replaceFile(pkgId, src, browser, dest, main, log); | ||
} | ||
@@ -45,5 +48,6 @@ | ||
* @param {Object} pkgJson package.json contents | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceModules(pkgDir, browser, pkgJson) { | ||
function replaceModules(pkgDir, browser, pkgJson, log) { | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
@@ -56,7 +60,7 @@ | ||
if (to == false) { | ||
ignoreFile(dest); | ||
ignoreFile(dest, from, log); | ||
} else { | ||
const src = path.join(pkgDir, to); | ||
replaceFile(pkgId, src, to, dest, from); | ||
replaceFile(pkgId, src, to, dest, from, log); | ||
} | ||
@@ -73,10 +77,17 @@ }); | ||
* @param {String} destName the name of the destination file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceFile(pkgId, src, srcName, dest, destName) { | ||
function replaceFile(pkgId, src, srcName, dest, destName, log) { | ||
const srcModuleName = srcName.replace('.js', ''); | ||
const destModuleName = destName.replace('.js', ''); | ||
log.info( | ||
'replace-browser-modules', | ||
`Replacing module ${destName} with module ${srcName}` | ||
); | ||
try { | ||
let contents = fs.readFileSync(src).toString(); | ||
contents = contents.replace( | ||
@@ -104,5 +115,12 @@ `'${pkgId}/${srcModuleName}'`, | ||
* @param {String} file path to file to be ignored | ||
* @param {String} fileName the name of the file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function ignoreFile(file) { | ||
function ignoreFile(file, fileName, log) { | ||
log.info( | ||
'replace-browser-modules', | ||
`Emptying module ${fileName} because it is server-only` | ||
); | ||
fs.writeFileSync( | ||
@@ -109,0 +127,0 @@ file, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32580
438