babel-plugin-styled-components
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -21,2 +21,6 @@ 'use strict'; | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _hash = require('../utils/hash'); | ||
@@ -53,6 +57,8 @@ | ||
var getDisplayName = function getDisplayName(path, file) { | ||
var getDisplayName = function getDisplayName(path, state) { | ||
var file = state.file; | ||
var componentName = (0, _getName2.default)(path); | ||
if (file) { | ||
return componentName ? `${ blockName(file) }__${ componentName }` : blockName(file); | ||
return componentName ? `${blockName(file)}__${componentName}` : blockName(file); | ||
} else { | ||
@@ -63,15 +69,53 @@ return componentName; | ||
var id = 0; | ||
var findModuleRoot = function findModuleRoot(filename) { | ||
if (!filename) { | ||
return null; | ||
} | ||
var dir = _path2.default.dirname(filename); | ||
if (_fs2.default.existsSync(_path2.default.join(dir, 'package.json'))) { | ||
return dir; | ||
} else if (dir !== filename) { | ||
return findModuleRoot(dir); | ||
} else { | ||
return null; | ||
} | ||
}; | ||
var getComponentId = function getComponentId(displayName) { | ||
// Prefix the identifier with a character if no displayName exists because CSS classes cannot start with a number | ||
return `${ displayName || 's' }-${ (0, _hash2.default)(`${ id }${ displayName }`) }`; | ||
var FILE_HASH = 'styled-components-file-hash'; | ||
var COMPONENT_POSITION = 'styled-components-component-position'; | ||
var getFileHash = function getFileHash(state) { | ||
var file = state.file; | ||
// hash calculation is costly due to fs operations, so we'll cache it per file. | ||
if (file.get(FILE_HASH)) { | ||
return file.get(FILE_HASH); | ||
} | ||
var filename = file.opts.filename; | ||
// find module root directory | ||
var moduleRoot = findModuleRoot(filename); | ||
var filePath = moduleRoot && _path2.default.relative(moduleRoot, filename).replace(_path2.default.sep, '/'); | ||
var moduleName = moduleRoot && JSON.parse(_fs2.default.readFileSync(_path2.default.join(moduleRoot, 'package.json'))).name; | ||
var code = file.code; | ||
var fileHash = (0, _hash2.default)([moduleName, filePath, code].join('')); | ||
file.set(FILE_HASH, fileHash); | ||
return fileHash; | ||
}; | ||
var getNextId = function getNextId(state) { | ||
var id = state.file.get(COMPONENT_POSITION) || 0; | ||
state.file.set(COMPONENT_POSITION, id + 1); | ||
return id; | ||
}; | ||
var getComponentId = function getComponentId(state) { | ||
// Prefix the identifier with a character because CSS classes cannot start with a number | ||
return `${getFileHash(state).replace(/^(\d)/, 's$1')}-${getNextId(state)}`; | ||
}; | ||
exports.default = function (path, state) { | ||
if ((0, _detectors.isStyled)(path.node.tag, state)) { | ||
var displayName = getDisplayName(path, (0, _options.useFileName)(state) && state.file); | ||
id++; | ||
addConfig(path, (0, _options.useDisplayName)(state) && displayName, (0, _options.useSSR)(state) && getComponentId(displayName)); | ||
addConfig(path, (0, _options.useDisplayName)(state) && getDisplayName(path, (0, _options.useFileName)(state) && state), (0, _options.useSSR)(state) && getComponentId(state)); | ||
} | ||
}; |
@@ -17,2 +17,16 @@ 'use strict'; | ||
var minify = function minify(linebreak) { | ||
var regex = new RegExp(linebreak + '\\s*', 'g'); | ||
return function (code) { | ||
return code.split(regex).filter(function (line) { | ||
return line.length > 0; | ||
}).map(function (line) { | ||
return line.indexOf('//') === -1 ? line : line + '\n'; | ||
}).join(''); | ||
}; | ||
}; | ||
var minifyRaw = minify('(?:\\\\r|\\\\n|\\r|\\n)'); | ||
var minifyCooked = minify('[\\r\\n]'); | ||
exports.default = function (path, state) { | ||
@@ -29,4 +43,4 @@ if ((0, _options.useMinify)(state) && ((0, _detectors.isStyled)(path.node.tag, state) || (0, _detectors.isHelper)(path.node.tag, state))) { | ||
element.value.raw = element.value.raw.replace(/(\\r|\\n|\r|\n)\s*/g, ''); | ||
element.value.cooked = element.value.cooked.replace(/[\r\n]\s*/g, ''); | ||
element.value.raw = minifyRaw(element.value.raw); | ||
element.value.cooked = minifyCooked(element.value.cooked); | ||
} | ||
@@ -33,0 +47,0 @@ } catch (err) { |
{ | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"name": "babel-plugin-styled-components", | ||
@@ -4,0 +4,0 @@ "description": "Improve the debugging experience and add server-side rendering support to styled-components", |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19857
321
0
1