babel-plugin-transform-assets-import-to-string
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"parser": "babel-eslint", | ||
"env": { | ||
"browser": true, | ||
"commonjs": true, | ||
"es6": true, | ||
"node": true, | ||
"mocha" : true | ||
"mocha": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"sourceType": "module", | ||
"ecmaFeatures": { | ||
"experimentalObjectRestSpread": true, | ||
"jsx": true | ||
} | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"prettier" | ||
], | ||
"extends": [ | ||
"eslint:recommended" | ||
"eslint:recommended", | ||
"plugin:prettier/recommended" | ||
] | ||
} |
@@ -7,6 +7,3 @@ 'use strict'; | ||
exports.defaultOptions = undefined; | ||
exports.transformImportsInline = transformImportsInline; | ||
var _path = require('path'); | ||
var _transform = require('./transform'); | ||
@@ -18,38 +15,59 @@ | ||
var defaultOptions = exports.defaultOptions = { | ||
flatten: false, | ||
extensions: ['.gif', '.jpeg', '.jpg', '.png', '.svg'] | ||
const defaultOptions = { | ||
baseUri: '', | ||
extensions: ['.gif', '.jpeg', '.jpg', '.png', '.svg'], | ||
flatten: false | ||
}; | ||
var applyTransform = function applyTransform(p, t, state, value, calleeName) { | ||
var ext = (0, _path.extname)(value); | ||
var options = Object.assign({}, defaultOptions, state.opts); | ||
function isRequireStatement(p) { | ||
const callee = p.get('callee'); | ||
return callee.isIdentifier() && callee.equals('name', 'require'); | ||
} | ||
if (options.extensions && options.extensions.indexOf(ext) >= 0) { | ||
var dir = (0, _path.dirname)((0, _path.resolve)(state.file.opts.filename)); | ||
var absPath = (0, _path.resolve)(dir, value); | ||
(0, _transform2.default)(p, t, options, absPath, calleeName); | ||
function isValidArgument(p) { | ||
const arg = p.get('arguments')[0]; | ||
return arg && arg.isStringLiteral(); | ||
} | ||
function initOptions(cache, state) { | ||
if (cache) { | ||
return cache; | ||
} | ||
}; | ||
function transformImportsInline(_ref) { | ||
var t = _ref.types; | ||
return Object.assign({}, defaultOptions, state.opts); | ||
} | ||
function transformAssets({ types: t }) { | ||
return { | ||
pre() { | ||
this.optionCache = null; | ||
}, | ||
post() { | ||
this.optionCache = null; | ||
}, | ||
visitor: { | ||
ImportDeclaration: function ImportDeclaration(p, state) { | ||
applyTransform(p, t, state, p.node.source.value, 'import'); | ||
ImportDeclaration(p, state) { | ||
this.optionCache = initOptions(this.optionCache, state); | ||
(0, _transform2.default)({ | ||
path: p, | ||
types: t, | ||
filename: state.file.opts.filename, | ||
value: p.node.source.value, | ||
callee: 'import' | ||
}, this.optionCache); | ||
}, | ||
CallExpression: function CallExpression(p, state) { | ||
var callee = p.get('callee'); | ||
if (!callee.isIdentifier() || !callee.equals('name', 'require')) { | ||
return; | ||
} | ||
CallExpression(p, state) { | ||
if (isRequireStatement(p) && isValidArgument(p)) { | ||
const arg = p.get('arguments')[0]; | ||
this.optionCache = initOptions(this.optionCache, state); | ||
var arg = p.get('arguments')[0]; | ||
if (!arg || !arg.isStringLiteral()) { | ||
return; | ||
(0, _transform2.default)({ | ||
path: p, | ||
types: t, | ||
filename: state.file.opts.filename, | ||
value: arg.node.value, | ||
callee: 'require' | ||
}, this.optionCache); | ||
} | ||
applyTransform(p, t, state, arg.node.value, 'require'); | ||
} | ||
@@ -60,2 +78,3 @@ } | ||
exports.default = transformImportsInline; | ||
exports.defaultOptions = defaultOptions; | ||
exports.default = transformAssets; |
@@ -7,10 +7,2 @@ 'use strict'; | ||
var _crypto = require('crypto'); | ||
var _crypto2 = _interopRequireDefault(_crypto); | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _path = require('path'); | ||
@@ -20,46 +12,32 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _filePath = require('./steps/filePath'); | ||
function getHash(str) { | ||
return _crypto2.default.createHash('sha1').update(str, 'utf8').digest('hex').slice(0, 8); | ||
} | ||
var _filePath2 = _interopRequireDefault(_filePath); | ||
function getFile(absPath, baseDir, uri, flatten) { | ||
var file = absPath.split(baseDir || _path2.default.sep).pop(); | ||
var _fileHash = require('./steps/fileHash'); | ||
if (!baseDir) { | ||
return uri ? '/' + file : file; | ||
} | ||
var _fileHash2 = _interopRequireDefault(_fileHash); | ||
var fileName = flatten ? _path2.default.basename(file) : file; | ||
var _replaceNode = require('./steps/replaceNode'); | ||
return _path2.default.join(baseDir, fileName).replace(/\\/g, '/').replace(/\/\/g/, '/'); | ||
} | ||
var _replaceNode2 = _interopRequireDefault(_replaceNode); | ||
var getVariableName = function getVariableName(p) { | ||
if (p.node.specifiers && p.node.specifiers[0] && p.node.specifiers[0].local) { | ||
return p.node.specifiers[0].local.name; | ||
} | ||
}; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = function (p, t, opts, absPath, calleeName) { | ||
var file = getFile(absPath, opts.baseDir, opts.baseUri, opts.flatten); | ||
var hash = ''; | ||
function transform(scope, options) { | ||
const ext = _path2.default.extname(scope.value); | ||
if (opts.hash === 1) { | ||
var content = _fs2.default.readFileSync(absPath, 'utf8').trim(); | ||
hash = '?' + getHash(content); | ||
if (!options.extensions || options.extensions.indexOf(ext) < 0) { | ||
return; | ||
} | ||
var uri = '' + (opts.baseUri || '') + file + hash; | ||
const dir = _path2.default.dirname(_path2.default.resolve(scope.filename)); | ||
const absPath = _path2.default.resolve(dir, scope.value); | ||
if (calleeName === 'require') { | ||
p.replaceWith(t.StringLiteral(uri)); | ||
return; | ||
} | ||
let fp = (0, _filePath2.default)(absPath, options); | ||
fp = (0, _fileHash2.default)(fp, absPath, options); | ||
var variableName = getVariableName(p); | ||
if (variableName) { | ||
p.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(variableName), t.stringLiteral(uri))])); | ||
} | ||
}; | ||
(0, _replaceNode2.default)(scope, `${options.baseUri}${fp}`); | ||
} | ||
exports.default = transform; |
{ | ||
"name": "babel-plugin-transform-assets-import-to-string", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Babel plugin that transforms image assets import and requires to urls / cdn", | ||
@@ -43,12 +43,14 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-eslint": "^8.0.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-register": "^6.24.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-register": "^6.26.0", | ||
"chai": "^4.0.0", | ||
"coveralls": "^3.0.0", | ||
"cross-env": "^5.0.0", | ||
"eslint": "^4.1.0", | ||
"mocha": "^4.0.0", | ||
"nyc": "^11.0.2", | ||
"cross-env": "^5.1.5", | ||
"eslint": "^4.19.1", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-plugin-prettier": "^2.6.0", | ||
"mocha": "^5.0.0", | ||
"nyc": "^11.8.0", | ||
"prettier": "^1.12.1", | ||
"rimraf": "^2.6.1" | ||
@@ -55,0 +57,0 @@ }, |
# babel-plugin-transform-assets-import-to-string | ||
> Babel plugin that transforms image assets import and requires to urls / cdn | ||
@@ -9,7 +10,7 @@ | ||
- [About](#about) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [via babelrc](#via-babelrc) | ||
- [via Node API](#via-node-api) | ||
* [About](#about) | ||
* [Installation](#installation) | ||
* [Usage](#usage) | ||
* [via babelrc](#via-babelrc) | ||
* [via Node API](#via-node-api) | ||
@@ -51,8 +52,14 @@ ## About | ||
### via .babelrc | ||
```json | ||
{ | ||
"plugins": [["transform-assets-import-to-string", { | ||
"baseDir": "/assets", | ||
"baseUri": "http://your.cdn.address" | ||
}]] | ||
"plugins": [ | ||
[ | ||
"transform-assets-import-to-string", | ||
{ | ||
"baseDir": "/assets", | ||
"baseUri": "http://your.cdn.address" | ||
} | ||
] | ||
] | ||
} | ||
@@ -64,7 +71,12 @@ ``` | ||
```js | ||
require("babel-core").transform("code", { | ||
plugins: [["transform-assets-import-to-string", { | ||
"baseDir": "/assets", | ||
"baseUri": "http://your.cdn.address" | ||
}]] | ||
require('babel-core').transform('code', { | ||
plugins: [ | ||
[ | ||
'transform-assets-import-to-string', | ||
{ | ||
baseDir: '/assets', | ||
baseUri: 'http://your.cdn.address' | ||
} | ||
] | ||
] | ||
}); | ||
@@ -75,3 +87,3 @@ ``` | ||
__Note:__ leading `.` (dot) is required. | ||
**Note:** leading `.` (dot) is required. | ||
@@ -81,5 +93,8 @@ ```json | ||
"plugins": [ | ||
["transform-assets-import-to-string", { | ||
"extensions": [".jpg", ".png"] | ||
}] | ||
[ | ||
"transform-assets-import-to-string", | ||
{ | ||
"extensions": [".jpg", ".png"] | ||
} | ||
] | ||
] | ||
@@ -95,4 +110,3 @@ } | ||
[circle-link]: https://circleci.com/gh/yeojz/babel-plugin-transform-assets-import-to-string | ||
[npm-badge]: https://img.shields.io/npm/v/babel-plugin-transform-assets-import-to-string.svg?style=flat-square | ||
[npm-link]: https://www.npmjs.com/package/babel-plugin-transform-assets-import-to-string |
Sorry, the diff of this file is not supported yet
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
329677
17
6885
107
13
1