-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support presets in liferay-npm-bundler
- Loading branch information
1 parent
11cd9ae
commit f647f60
Showing
5 changed files
with
265 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.