liferay-npm-bundler-plugin-replace-browser-modules
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -21,6 +21,6 @@ 'use strict'; | ||
var pkg = { | ||
id: 'package@1.0.0', | ||
name: 'package', | ||
version: '1.0.0', | ||
dir: process.cwd() + '/packages/' + 'liferay-npm-bundler-plugin-replace-browser-modules/src/__tests__' | ||
id: 'package@1.0.0', | ||
name: 'package', | ||
version: '1.0.0', | ||
dir: process.cwd() + '/packages/' + 'liferay-npm-bundler-plugin-replace-browser-modules/src/__tests__' | ||
}; | ||
@@ -33,5 +33,5 @@ | ||
beforeEach(function () { | ||
pkgFiles.forEach(function (file) { | ||
_cpFile2.default.sync(pkg.dir + '/sources/' + file, pkg.dir + '/' + file); | ||
}); | ||
pkgFiles.forEach(function (file) { | ||
_cpFile2.default.sync(pkg.dir + '/sources/' + file, pkg.dir + '/' + file); | ||
}); | ||
}); | ||
@@ -41,89 +41,89 @@ | ||
afterEach(function () { | ||
pkgFiles.forEach(function (file) { | ||
try { | ||
fs.unlink(pkg.dir + '/' + file); | ||
} catch (err) {} | ||
}); | ||
pkgFiles.forEach(function (file) { | ||
try { | ||
fs.unlink(pkg.dir + '/' + file); | ||
} catch (err) {} | ||
}); | ||
}); | ||
it('replaces main file with browser file', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
browser: 'test-browser.js.file' | ||
}; | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
browser: 'test-browser.js.file' | ||
}; | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
}); | ||
it('works with unpkg field too', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
unpkg: 'test-browser.js.file' | ||
}; | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
unpkg: 'test-browser.js.file' | ||
}; | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
}); | ||
it('works with jsdelivr field too', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
jsdelivr: 'test-browser.js.file' | ||
}; | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
jsdelivr: 'test-browser.js.file' | ||
}; | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-main.js.file').toString()).toMatchSnapshot(); | ||
}); | ||
it('replaces server files with browser files', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
browser: { | ||
'test-browser.js.file': 'test.js.file', | ||
'test-browser-2.js.file': 'test-2.js.file' | ||
} | ||
}; | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
browser: { | ||
'test-browser.js.file': 'test.js.file', | ||
'test-browser-2.js.file': 'test-2.js.file' | ||
} | ||
}; | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
expect(fs.readFileSync(pkg.dir + '/test-browser.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-browser-2.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-browser.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-browser-2.js.file').toString()).toMatchSnapshot(); | ||
}); | ||
it('does replace ignored modules with empty objects', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: { | ||
'test-browser.js.file': false | ||
} | ||
}; | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: { | ||
'test-browser.js.file': false | ||
} | ||
}; | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
expect(fs.readFileSync(pkg.dir + '/test-browser.js.file').toString()).toMatchSnapshot(); | ||
expect(fs.readFileSync(pkg.dir + '/test-browser.js.file').toString()).toMatchSnapshot(); | ||
}); | ||
it('does not throw on inexistent files', function () { | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: 'non-existent-file.js' | ||
}; | ||
var pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: 'non-existent-file.js' | ||
}; | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
(0, _index2.default)({ pkg: pkg }, { pkgJson: pkgJson }); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = function (_ref, _ref2) { | ||
var pkg = _ref.pkg, | ||
config = _ref.config; | ||
var pkgJson = _ref2.pkgJson; | ||
var pkg = _ref.pkg, | ||
config = _ref.config; | ||
var pkgJson = _ref2.pkgJson; | ||
var browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
var browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
if (browser) { | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson); | ||
} | ||
} | ||
if (browser) { | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson); | ||
} | ||
} | ||
}; | ||
@@ -41,9 +41,9 @@ | ||
function replaceMainModule(pkgDir, browser, pkgJson) { | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
var main = pkgJson.main || 'index.js'; | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
var main = pkgJson.main || 'index.js'; | ||
var src = _path2.default.join(pkgDir, browser); | ||
var dest = _path2.default.join(pkgDir, main); | ||
var src = _path2.default.join(pkgDir, browser); | ||
var dest = _path2.default.join(pkgDir, main); | ||
replaceFile(pkgId, src, browser, dest, main); | ||
replaceFile(pkgId, src, browser, dest, main); | ||
} | ||
@@ -64,16 +64,16 @@ | ||
function replaceModules(pkgDir, browser, pkgJson) { | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
Object.keys(browser).forEach(function (from) { | ||
var to = browser[from]; | ||
var dest = _path2.default.join(pkgDir, from); | ||
Object.keys(browser).forEach(function (from) { | ||
var to = browser[from]; | ||
var dest = _path2.default.join(pkgDir, from); | ||
if (to == false) { | ||
ignoreFile(dest); | ||
} else { | ||
var src = _path2.default.join(pkgDir, to); | ||
if (to == false) { | ||
ignoreFile(dest); | ||
} else { | ||
var src = _path2.default.join(pkgDir, to); | ||
replaceFile(pkgId, src, to, dest, from); | ||
} | ||
}); | ||
replaceFile(pkgId, src, to, dest, from); | ||
} | ||
}); | ||
} | ||
@@ -91,15 +91,15 @@ | ||
function replaceFile(pkgId, src, srcName, dest, destName) { | ||
var srcModuleName = srcName.replace('.js', ''); | ||
var destModuleName = destName.replace('.js', ''); | ||
var srcModuleName = srcName.replace('.js', ''); | ||
var destModuleName = destName.replace('.js', ''); | ||
try { | ||
var contents = _fs2.default.readFileSync(src).toString(); | ||
contents = contents.replace('\'' + pkgId + '/' + srcModuleName + '\'', '\'' + pkgId + '/' + destModuleName + '\''); | ||
try { | ||
var contents = _fs2.default.readFileSync(src).toString(); | ||
contents = contents.replace('\'' + pkgId + '/' + srcModuleName + '\'', '\'' + pkgId + '/' + destModuleName + '\''); | ||
_fs2.default.writeFileSync(dest, '/* Module replaced with ' + srcName + ' by liferay-npm-bundler-plugin-replace-browser-modules */\n' + contents); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
_fs2.default.writeFileSync(dest, '/* Module replaced with ' + srcName + ' by liferay-npm-bundler-plugin-replace-browser-modules */\n' + contents); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
} | ||
@@ -113,4 +113,4 @@ | ||
function ignoreFile(file) { | ||
_fs2.default.writeFileSync(file, '/* Module ignored by ' + 'liferay-npm-bundler-plugin-replace-browser-modules */\n'); | ||
_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.2.1", | ||
"version": "1.2.2", | ||
"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", |
@@ -7,8 +7,8 @@ import cpFile from 'cp-file'; | ||
const pkg = { | ||
id: 'package@1.0.0', | ||
name: 'package', | ||
version: '1.0.0', | ||
dir: | ||
`${process.cwd()}/packages/` + | ||
`liferay-npm-bundler-plugin-replace-browser-modules/src/__tests__`, | ||
id: 'package@1.0.0', | ||
name: 'package', | ||
version: '1.0.0', | ||
dir: | ||
`${process.cwd()}/packages/` + | ||
`liferay-npm-bundler-plugin-replace-browser-modules/src/__tests__`, | ||
}; | ||
@@ -18,7 +18,7 @@ | ||
const pkgFiles = [ | ||
'test-2.js.file', | ||
'test-browser-2.js.file', | ||
'test-browser.js.file', | ||
'test-main.js.file', | ||
'test.js.file', | ||
'test-2.js.file', | ||
'test-browser-2.js.file', | ||
'test-browser.js.file', | ||
'test-main.js.file', | ||
'test.js.file', | ||
]; | ||
@@ -28,5 +28,5 @@ | ||
beforeEach(() => { | ||
pkgFiles.forEach(file => { | ||
cpFile.sync(`${pkg.dir}/sources/${file}`, `${pkg.dir}/${file}`); | ||
}); | ||
pkgFiles.forEach(file => { | ||
cpFile.sync(`${pkg.dir}/sources/${file}`, `${pkg.dir}/${file}`); | ||
}); | ||
}); | ||
@@ -36,100 +36,100 @@ | ||
afterEach(() => { | ||
pkgFiles.forEach(file => { | ||
try { | ||
fs.unlink(`${pkg.dir}/${file}`); | ||
} catch (err) {} | ||
}); | ||
pkgFiles.forEach(file => { | ||
try { | ||
fs.unlink(`${pkg.dir}/${file}`); | ||
} catch (err) {} | ||
}); | ||
}); | ||
it('replaces main file with browser file', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
browser: 'test-browser.js.file', | ||
}; | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
browser: 'test-browser.js.file', | ||
}; | ||
plugin({ pkg }, { pkgJson }); | ||
plugin({pkg}, {pkgJson}); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-main.js.file`).toString(), | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-main.js.file`).toString() | ||
).toMatchSnapshot(); | ||
}); | ||
it('works with unpkg field too', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
unpkg: 'test-browser.js.file', | ||
}; | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
unpkg: 'test-browser.js.file', | ||
}; | ||
plugin({ pkg }, { pkgJson }); | ||
plugin({pkg}, {pkgJson}); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-main.js.file`).toString(), | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-main.js.file`).toString() | ||
).toMatchSnapshot(); | ||
}); | ||
it('works with jsdelivr field too', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
jsdelivr: 'test-browser.js.file', | ||
}; | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'test-main.js.file', | ||
jsdelivr: 'test-browser.js.file', | ||
}; | ||
plugin({ pkg }, { pkgJson }); | ||
plugin({pkg}, {pkgJson}); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-main.js.file`).toString(), | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-main.js.file`).toString() | ||
).toMatchSnapshot(); | ||
}); | ||
it('replaces server files with browser files', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
browser: { | ||
'test-browser.js.file': 'test.js.file', | ||
'test-browser-2.js.file': 'test-2.js.file', | ||
}, | ||
}; | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
browser: { | ||
'test-browser.js.file': 'test.js.file', | ||
'test-browser-2.js.file': 'test-2.js.file', | ||
}, | ||
}; | ||
plugin({ pkg }, { pkgJson }); | ||
plugin({pkg}, {pkgJson}); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-browser.js.file`).toString(), | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-browser-2.js.file`).toString(), | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-browser.js.file`).toString() | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-browser-2.js.file`).toString() | ||
).toMatchSnapshot(); | ||
}); | ||
it('does replace ignored modules with empty objects', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: { | ||
'test-browser.js.file': false, | ||
}, | ||
}; | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: { | ||
'test-browser.js.file': false, | ||
}, | ||
}; | ||
plugin({ pkg }, { pkgJson }); | ||
plugin({pkg}, {pkgJson}); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-browser.js.file`).toString(), | ||
).toMatchSnapshot(); | ||
expect( | ||
fs.readFileSync(`${pkg.dir}/test-browser.js.file`).toString() | ||
).toMatchSnapshot(); | ||
}); | ||
it('does not throw on inexistent files', () => { | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: 'non-existent-file.js', | ||
}; | ||
const pkgJson = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
main: 'main.js', | ||
browser: 'non-existent-file.js', | ||
}; | ||
plugin({ pkg }, { pkgJson }); | ||
plugin({pkg}, {pkgJson}); | ||
}); |
100
src/index.js
@@ -7,12 +7,12 @@ import fs from 'fs'; | ||
*/ | ||
export default function({ pkg, config }, { pkgJson }) { | ||
const browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
export default function({pkg, config}, {pkgJson}) { | ||
const browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
if (browser) { | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson); | ||
} | ||
} | ||
if (browser) { | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson); | ||
} | ||
} | ||
} | ||
@@ -28,9 +28,9 @@ | ||
function replaceMainModule(pkgDir, browser, pkgJson) { | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
const main = pkgJson.main || 'index.js'; | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
const main = pkgJson.main || 'index.js'; | ||
const src = path.join(pkgDir, browser); | ||
const dest = path.join(pkgDir, main); | ||
const src = path.join(pkgDir, browser); | ||
const dest = path.join(pkgDir, main); | ||
replaceFile(pkgId, src, browser, dest, main); | ||
replaceFile(pkgId, src, browser, dest, main); | ||
} | ||
@@ -46,16 +46,16 @@ | ||
function replaceModules(pkgDir, browser, pkgJson) { | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
Object.keys(browser).forEach(from => { | ||
const to = browser[from]; | ||
const dest = path.join(pkgDir, from); | ||
Object.keys(browser).forEach(from => { | ||
const to = browser[from]; | ||
const dest = path.join(pkgDir, from); | ||
if (to == false) { | ||
ignoreFile(dest); | ||
} else { | ||
const src = path.join(pkgDir, to); | ||
if (to == false) { | ||
ignoreFile(dest); | ||
} else { | ||
const src = path.join(pkgDir, to); | ||
replaceFile(pkgId, src, to, dest, from); | ||
} | ||
}); | ||
replaceFile(pkgId, src, to, dest, from); | ||
} | ||
}); | ||
} | ||
@@ -73,24 +73,24 @@ | ||
function replaceFile(pkgId, src, srcName, dest, destName) { | ||
const srcModuleName = srcName.replace('.js', ''); | ||
const destModuleName = destName.replace('.js', ''); | ||
const srcModuleName = srcName.replace('.js', ''); | ||
const destModuleName = destName.replace('.js', ''); | ||
try { | ||
let contents = fs.readFileSync(src).toString(); | ||
contents = contents.replace( | ||
`'${pkgId}/${srcModuleName}'`, | ||
`'${pkgId}/${destModuleName}'`, | ||
); | ||
try { | ||
let contents = fs.readFileSync(src).toString(); | ||
contents = contents.replace( | ||
`'${pkgId}/${srcModuleName}'`, | ||
`'${pkgId}/${destModuleName}'` | ||
); | ||
fs.writeFileSync( | ||
dest, | ||
'/* Module replaced with ' + | ||
srcName + | ||
' by liferay-npm-bundler-plugin-replace-browser-modules */\n' + | ||
contents, | ||
); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
fs.writeFileSync( | ||
dest, | ||
'/* Module replaced with ' + | ||
srcName + | ||
' by liferay-npm-bundler-plugin-replace-browser-modules */\n' + | ||
contents | ||
); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
} | ||
@@ -104,7 +104,7 @@ | ||
function ignoreFile(file) { | ||
fs.writeFileSync( | ||
file, | ||
'/* Module ignored by ' + | ||
'liferay-npm-bundler-plugin-replace-browser-modules */\n', | ||
); | ||
fs.writeFileSync( | ||
file, | ||
'/* Module ignored by ' + | ||
'liferay-npm-bundler-plugin-replace-browser-modules */\n' | ||
); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
28778
0