Skip to content

Commit

Permalink
Support presets in liferay-npm-bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-zaera committed Jul 4, 2017
1 parent 11cd9ae commit f647f60
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import fs from 'fs';

export default function({ srcDir, outDir, config }, { pkgJson }) {
const pkgId = `${pkgJson.name}@${pkgJson.version}`;
export default function({ pkg, config }, { pkgJson }) {
const browser = pkgJson.browser;

if (browser) {
if (typeof browser === 'string') {
replaceMainModule(outDir, pkgJson);
replaceMainModule(pkg.dir, pkgJson);
} else {
replaceModules(outDir, pkgJson);
replaceModules(pkg.dir, pkgJson);
}
}
}
Expand Down Expand Up @@ -46,17 +45,23 @@ function replaceFile(pkgId, src, srcName, dest, destName) {
const srcModuleName = srcName.replace('.js', '');
const destModuleName = destName.replace('.js', '');

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
);
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;
}
}
}
2 changes: 1 addition & 1 deletion packages/liferay-npm-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"cp-file": "^4.2.0",
"globby": "^6.1.0",
"minimatch": "^3.0.4",
"liferay-build-tools-util": "^1.0.0",
"read-json-sync": "^1.1.1",
"resolve": "^1.3.3"
}
Expand Down
104 changes: 104 additions & 0 deletions packages/liferay-npm-bundler/src/config.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { getPackageDir } from 'liferay-build-tools-util';
import readJsonSync from 'read-json-sync';
import resolveModule from 'resolve';

let pluginsBaseDir = '.';
let config = loadConfig();

function loadConfig() {
let config = readJsonSync('.npmbundlerrc');

if (config.preset) {
const presetFile = resolveModule.sync(config.preset, {
basedir: '.',
});

config = readJsonSync(presetFile);
pluginsBaseDir = getPackageDir(presetFile);
}

return config;
}

function configRequire(module) {
const pluginFile = resolveModule.sync(module, {
basedir: pluginsBaseDir,
});

return require(pluginFile);
}

export function isDebugBabelActive() {
return config['debug-babel'] || false;
}

export function getExclusions(pkg) {
let exclusions = config.exclude || {};

exclusions = exclusions[pkg.id] || exclusions[pkg.name] || [];

return exclusions;
}

export function loadBabelPlugins(presets, plugins) {
// TOOD: if plugins have config decide what to do with it
return []
.concat(
...presets.map(preset => {
let presetModule;

try {
presetModule = configRequire(preset);
} catch (err) {
presetModule = configRequire(`babel-preset-${preset}`);
}

return presetModule.default().plugins;
})
)
.concat(...plugins);
}

export function getPlugins(phase, pkg) {
const pluginsKey = phase === 'pre' ? 'plugins' : 'post-plugins';

let plugins = [];

if (config[pkg.id] && config[pkg.id][pluginsKey]) {
plugins = config[pkg.id][pluginsKey];
} else if (config['*'] && config['*'][pluginsKey]) {
plugins = config['*'][pluginsKey];
}

return plugins.map(pluginName => {
let pluginConfig = {};

if (Array.isArray(pluginName)) {
pluginConfig = pluginName[1];
pluginName = pluginName[0];
}

const pluginModule = configRequire(
`liferay-npm-bundler-plugin-${pluginName}`
);

return {
run: pluginModule.default,
config: pluginConfig,
};
});

return plugins;
}

export function getBabelConfig(pkg) {
let babelConfig = {};

if (config[pkg.id] && config[pkg.id]['.babelrc']) {
babelConfig = config[pkg.id]['.babelrc'];
} else if (config['*'] && config['*']['.babelrc']) {
babelConfig = config['*']['.babelrc'];
}

return babelConfig;
}
59 changes: 59 additions & 0 deletions packages/liferay-npm-bundler/src/dependencies.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import path from 'path';
import readJsonSync from 'read-json-sync';
import resolveModule from 'resolve';

/**
* Recursively find the dependencies of a package living in a `basedir` and
* return them as a hash of objects where key is the package id and values have
* the following structure:
* {
* id: <package id>, // a package id is a unique `name@version` string
* name: <package name>, // package name (without version, not unique)
* version: <package version>,
* dir: <package dir>
* }
*/
export function getPackageDependencies(basedir) {
var pkgs = {};

var packageJson = readJsonSync(basedir + '/package.json');
var pkgId = packageJson.name + '@' + packageJson.version;

pkgs[pkgId] = {
id: pkgId,
name: packageJson.name,
version: packageJson.version,
dir: basedir,
};

var dependencies = packageJson.dependencies || [];

var dependencyDirs = Object.keys(dependencies).map(function(dependency) {
return resolveDependencyDir(basedir, dependency);
});

dependencyDirs = dependencyDirs.filter(dependencyDir => {
return dependencyDir != null;
});

dependencyDirs.forEach(function(dependencyDir) {
var depPkgs = getPackageDependencies(dependencyDir);

Object.keys(depPkgs).forEach(function(pkgId) {
pkgs[pkgId] = depPkgs[pkgId];
});
});

return pkgs;
}

/**
* Resolves a `dependency` from the context of a specific `packageDir` and
* returns its directory
*/
function resolveDependencyDir(packageDir, dependency) {
var pkgJsonFile = resolveModule.sync(dependency + '/package.json', {
basedir: packageDir,
});
return path.dirname(pkgJsonFile);
}
Loading

0 comments on commit f647f60

Please sign in to comment.