Skip to content
Snippets Groups Projects
Commit dc185350 authored by Jure's avatar Jure
Browse files

Add 'packages/pubsweet-theme-plugin/' from commit 'c1535e4c'

git-subtree-dir: packages/pubsweet-theme-plugin
git-subtree-mainline: 0e021bd5
git-subtree-split: c1535e4c
parents 0e021bd5 c1535e4c
No related branches found
No related tags found
No related merge requests found
const path = require('path')
const moduleName = component => {
const namepart = component
.replace(
/([A-Z])/g,
l => `-${l.toLowerCase()}`
)
return `pubsweet-component${namepart}`
}
function ThemePlugin (theme) {
this.theme = theme || 'default'
}
const componentName = module => module
.replace('pubsweet-component-', '')
.split('-')
.map(w => `${w.slice(0, 1).toUpperCase()}${w.slice(1)}`)
.join('')
ThemePlugin.prototype.apply = function (resolver) {
var theme = this.theme
resolver.plugin('resolve', function (request, callback) {
var fs = resolver.fileSystem
const self = this
self.cache = {}
const requestJson = JSON.stringify({
path: request.path,
request: request.request
})
const done = () => {
self.cache[requestJson] = true
return callback()
}
if (self.cache[requestJson] === true) {
return callback()
} else if (self.cache[requestJson] !== undefined) {
return self.doResolve('resolve', self.cache[requestJson], 'using path: ' + pathWithTheme, callback)
}
if (theme === 'default') return done()
const themeModule = moduleName(theme)
if (/\.local\.scss$/.test(request.request)) {
var extension = '.local.scss'
var pathWithoutFiletype = path.dirname(request.request) + '/' + path.basename(request.request, extension)
} else if (request.request && path.extname(request.request) === '.scss') {
extension = '.scss'
pathWithoutFiletype = path.dirname(request.request) + '/' + path.basename(request.request, extension)
} else {
self.cache[requestJson] = true
return callback()
}
var folders = request.path.split('/')
var componentModule = folders.pop()
if (!(/pubsweet-component/.test(componentModule))) return done()
var pathWithTheme = path
.resolve(
request.path,
'..',
themeModule,
componentName(componentModule),
pathWithoutFiletype
) + extension
var pathWithoutTheme = path
.resolve(
request.path,
pathWithoutFiletype
) + extension
fs.stat(pathWithoutTheme, (err, stats) => {
if (err) return done()
fs.stat(pathWithTheme, (err, stats) => {
if (err) return callback()
var obj = {
path: path.dirname(pathWithTheme),
request: request.request,
query: request.query,
directory: request.directory
}
self.cache[requestJson] = obj
self.doResolve('resolve', obj, 'using path: ' + pathWithTheme, callback)
})
})
})
}
module.exports = ThemePlugin
{
"name": "pubsweet-theme-plugin",
"version": "0.0.1",
"description": "A plugin for Webpack 2, used by PubSweet for themes support.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://gitlab.coko.foundation/pubsweet/pubsweet-theme-plugin"
},
"keywords": [
"themes",
"webpack",
"plugin"
],
"author": "Collaborative Knowledge Foundation",
"license": "MIT"
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment