Skip to content

Commit

Permalink
perf(module): optimize plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Apr 2, 2018
1 parent 2e167f8 commit b7998c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
30 changes: 17 additions & 13 deletions lib/module/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { resolve, join, basename } = require('path')
const { existsSync, readdirSync } = require('fs')
const merge = require('lodash/merge')
const uniq = require('lodash/uniq')
const defaults = require('./defaults')

const libRoot = resolve(__dirname, '..')
Expand All @@ -16,16 +17,18 @@ module.exports = function (moduleOptions) {
copyCore.call(this, options)

// Process and normalize strategies
const { strategies, schemes } = processStrategies.call(this, options)
const { strategies, strategyScheme } = processStrategies.call(this, options)
delete options.strategies

// Set defaultStrategy
if (!options.defaultStrategy) {
if (!options.defaultStrategy && strategies.length) {
options.defaultStrategy = strategies[0]._name
} else {
console.warn('no strategy defined!')
}

// Copy plugin
copyPlugin.call(this, { options, strategies, schemes })
copyPlugin.call(this, { options, strategies, strategyScheme })
}

function validateOptions (options) {
Expand Down Expand Up @@ -73,15 +76,16 @@ function copyCore (options) {
}
}

function copyPlugin ({ options, strategies, schemes }) {
function copyPlugin ({ options, strategies, strategyScheme }) {
// Copy auth plugin
const { dst } = this.addTemplate({
src: resolve(libRoot, 'module', 'plugin.js'),
fileName: join('auth', 'plugin.js'),
options: {
options,
schemes,
strategies
strategies,
uniqueSchemes: uniq([...strategyScheme.values()]),
strategyScheme
}
})

Expand All @@ -96,7 +100,7 @@ function copyPlugin ({ options, strategies, schemes }) {

function processStrategies (options) {
const strategies = []
const schemes = {}
const strategyScheme = new Map()

for (const name in options.strategies) {
if (!options.strategies[name]) {
Expand All @@ -118,11 +122,9 @@ function processStrategies (options) {

// Try to apply provider
const provider = resolveProvider.call(this, strategy._provider)

if (typeof provider === 'function') {
provider.call(this, strategy, { name })
strategy._provider = provider.name || ''
} else {
delete strategy._provider
}

// Guess _scheme property
Expand All @@ -145,15 +147,17 @@ function processStrategies (options) {
fileName: join('auth', 'schemes', schemeName)
})

schemes[schemeName] = '.' + join('/', 'schemes', basename(schemeSrc))
strategy._scheme = schemeName
// Remove unnecessary fields
delete strategy._scheme
delete strategy._provider

strategyScheme.set(strategy, '.' + join('/', 'schemes', basename(schemeSrc)))
strategies.push(strategy)
}

return {
strategies,
schemes
strategyScheme
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/module/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Auth from './auth'
import './middleware'

// Active chemes
<%= Object.keys(options.schemes).map(scheme => `import ${'scheme_' + hash(scheme)} from '${options.schemes[scheme]}'`).join('\n') %>
<%= options.uniqueSchemes.map(path =>`import ${'scheme_' + hash(path)} from '${path}'`).join('\n') %>

export default function (ctx, inject) {
// Options
Expand All @@ -16,9 +16,15 @@ export default function (ctx, inject) {
inject('auth', $auth)

// Register strategies
<% for (let strategy of options.strategies) { %>
$auth.registerStrategy('<%= strategy._name %>', new <%='scheme_' + hash(strategy._scheme) %> ($auth, <%= JSON.stringify(strategy) %>))
<% } %>

<%=
options.strategies.map(strategy => {
const scheme = 'scheme_' + hash(options.strategyScheme.get(strategy))
const schemeOptions = JSON.stringify(strategy)
const name = strategy._name
return `// ${name}\n $auth.registerStrategy('${name}', new(${scheme}, ${schemeOptions}))`
}).join('\n\n ')
%>

// Initialize auth
return $auth.init().catch(error => {
Expand Down

0 comments on commit b7998c6

Please sign in to comment.