@cnbritain/merlin-sass-custom-importer
Advanced tools
Comparing version 2.0.1 to 2.0.2
117
lib/index.js
@@ -0,1 +1,3 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-env node */ | ||
'use strict'; | ||
@@ -7,13 +9,12 @@ | ||
const COMPONENTS_DIR = path.normalize('node_modules'); | ||
const LOGGER = { | ||
'enabled': false, | ||
'log': function(mode, ...args){ | ||
if(!this.enabled) return; | ||
'log': function (mode, ...args) { | ||
if (!this.enabled) return; | ||
this[mode](...args); | ||
}, | ||
'SKIP': function(...args){ | ||
'SKIP': function (...args) { | ||
console.log(chalk.underline.red(...args)); | ||
}, | ||
'LOAD': function(...args){ | ||
'LOAD': function (...args) { | ||
console.log(chalk.underline.green(...args)); | ||
@@ -26,12 +27,15 @@ } | ||
file: 'sass/wireframe/wireframe', | ||
contains: function(url){ | ||
contains: function (url) { | ||
return url.endsWith(this.key); | ||
}, | ||
resolve: function(url, previous){ | ||
resolve: function (url, previous) { | ||
let previousDir = getComponentDirFromPath(previous); | ||
if(!previousDir){ | ||
if (!previousDir) { | ||
previousDir = process.cwd(); | ||
} | ||
const baseUrl = url.substr(0, url.length - this.length); | ||
return path.resolve(previousDir, 'node_modules', baseUrl, this.file); | ||
const moduleDir = path.dirname(require.resolve(baseUrl + '/package.json')); | ||
return path.resolve(moduleDir, this.file); | ||
} | ||
@@ -42,12 +46,15 @@ }; | ||
length: 6, | ||
contains: function(url){ | ||
contains: function (url) { | ||
return url.endsWith(this.key); | ||
}, | ||
resolve: function(url, previous, theme){ | ||
resolve: function (url, previous, theme) { | ||
let previousDir = getComponentDirFromPath(previous); | ||
if(!previousDir){ | ||
if (!previousDir) { | ||
previousDir = process.cwd(); | ||
} | ||
const baseUrl = url.substr(0, url.length - this.length); | ||
return path.resolve(previousDir, 'node_modules', baseUrl, `sass/${theme}/${theme}.scss`); | ||
const moduleDir = path.dirname(require.resolve(baseUrl + '/package.json')); | ||
return path.resolve(moduleDir, `sass/${theme}/${theme}.scss`); | ||
} | ||
@@ -58,10 +65,10 @@ }; | ||
length: null, | ||
contains: function(url){ | ||
contains: function (url) { | ||
return BRAND_THEMES.some((brand) => url.endsWith(`:${brand}`)); | ||
}, | ||
resolve: function(url, previous){ | ||
resolve: function (url, previous) { | ||
let theme = null; | ||
let len = BRAND_THEMES.length; | ||
while(len--){ | ||
if(url.endsWith(`:${BRAND_THEMES[len]}`)){ | ||
while (len--) { | ||
if (url.endsWith(`:${BRAND_THEMES[len]}`)) { | ||
theme = BRAND_THEMES[len]; | ||
@@ -73,7 +80,14 @@ break; | ||
let previousDir = getComponentDirFromPath(previous); | ||
if(!previousDir){ | ||
if (!previousDir) { | ||
previousDir = process.cwd(); | ||
} | ||
const baseUrl = url.substr(0, url.length - theme.length - 1); | ||
return path.resolve(previousDir, 'node_modules', baseUrl, `sass/${theme}/${theme}.scss`); | ||
// use node's module resolution to find the package | ||
// using package.json is a bit of a hack but any valid | ||
// module should have it... | ||
const moduleDir = path.dirname(require.resolve(baseUrl + '/package.json')); | ||
return path.resolve(moduleDir, `sass/${theme}/${theme}.scss`); | ||
} | ||
@@ -83,20 +97,19 @@ }; | ||
const BRAND_THEMES = [ | ||
"bystander", | ||
"glamour", | ||
"gq", | ||
"missvogue", | ||
"tatler", | ||
"traveller", | ||
"vogue", | ||
"wired", | ||
"houseandgarden", | ||
"brides" | ||
'bystander', | ||
'glamour', | ||
'gq', | ||
'missvogue', | ||
'tatler', | ||
'traveller', | ||
'vogue', | ||
'wired', | ||
'houseandgarden', | ||
'brides' | ||
]; | ||
module.exports = function(merlinConfig={}, scopeName=null){ | ||
module.exports = function (merlinConfig = {}, scopeName = null) { | ||
const SESSION = new Set(); | ||
SESSION.SKIPPED_ITEMS = 0; | ||
SESSION.LOADED_ITEMS = 0; | ||
SESSION.reset = function sessionReset(){ | ||
SESSION.reset = function sessionReset() { | ||
SESSION.clear(); | ||
@@ -109,3 +122,3 @@ SESSION.SKIPPED_ITEMS = 0; | ||
function importer(url, previous, done){ | ||
function importer(url, previous, done) { | ||
const realPrevious = fs.realpathSync(previous); | ||
@@ -115,6 +128,6 @@ let sassUrl = null; | ||
// Check if path begins with @, update url to be component based | ||
if(url.startsWith('@')){ | ||
if (url.startsWith('@')) { | ||
sassUrl = resolveComponentTheme(url, realPrevious, merlinConfig); | ||
// Resolve url normally, relatively | ||
// Resolve url normally, relatively | ||
} else { | ||
@@ -126,13 +139,12 @@ let urlParts = path.normalize(realPrevious).split(path.sep); | ||
// sassUrl = correctSassPartials(sassUrl); | ||
try { | ||
sassUrl = correctRealPath(sassUrl); | ||
} catch(err) { | ||
} catch (err) { | ||
return err; | ||
} | ||
if(SESSION.has(sassUrl)){ | ||
if (SESSION.has(sassUrl)) { | ||
LOGGER.log('SKIP', `SASS${scope}::Skipping ${sassUrl}`); | ||
SESSION.SKIPPED_ITEMS++; | ||
if(done){ | ||
if (done) { | ||
return done({ contents: '' }); | ||
@@ -147,3 +159,3 @@ } else { | ||
SESSION.add(sassUrl); | ||
if(done){ | ||
if (done) { | ||
return done({ file: sassUrl }); | ||
@@ -162,13 +174,4 @@ } else { | ||
function correctSassPartials(url){ | ||
const urlPieces = url.split(path.sep); | ||
let filename = urlPieces.pop(); | ||
if(filename.startsWith('_')){ | ||
filename = filename.slice(1); | ||
} | ||
return path.resolve(urlPieces.join(path.sep), filename); | ||
} | ||
function correctRealPath(url){ | ||
if(url.endsWith('.scss')){ | ||
function correctRealPath(url) { | ||
if (url.endsWith('.scss')) { | ||
return fs.realpathSync(url); | ||
@@ -180,10 +183,10 @@ } else { | ||
function resolveComponentTheme(url, previous, merlinConfig){ | ||
function resolveComponentTheme(url, previous, merlinConfig) { | ||
// Check if we're using our keywords - theme, wireframe. If so, resolve | ||
// the sass location | ||
if(WIREFRAME_KEY.contains(url)){ | ||
if (WIREFRAME_KEY.contains(url)) { | ||
return WIREFRAME_KEY.resolve(url, previous); | ||
} else if(THEME_KEY.contains(url)){ | ||
} else if (THEME_KEY.contains(url)) { | ||
return THEME_KEY.resolve(url, previous, merlinConfig.currentTheme); | ||
} else if(BRAND_KEY.contains(url)){ | ||
} else if (BRAND_KEY.contains(url)) { | ||
return BRAND_KEY.resolve(url, previous); | ||
@@ -196,5 +199,5 @@ } else { | ||
const RE_COMPONENT_DIR = new RegExp(`(.+packages${path.sep}merlin-[-\\w]+${path.sep})`); | ||
function getComponentDirFromPath(p){ | ||
function getComponentDirFromPath(p) { | ||
const matches = RE_COMPONENT_DIR.exec(p); | ||
if(matches){ | ||
if (matches) { | ||
return matches[1]; | ||
@@ -201,0 +204,0 @@ } |
{ | ||
"name": "@cnbritain/merlin-sass-custom-importer", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "", | ||
@@ -12,3 +12,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "810f385fa1ca0f2472da4211776add986980a778" | ||
"gitHead": "61921e706ea1ee9e1c71cf399d09773265304d8a" | ||
} |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7424
168
5