babel-plugin-transform-assets
Advanced tools
Comparing version 0.2.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,11 +6,18 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = transformAssets; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _path = require("path"); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
exports.default = transformAssets; | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
var _path = require('path'); | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var defaultOptions = { | ||
@@ -25,4 +32,11 @@ name: '[name].[ext]?[sha512:hash:base64:7]' | ||
var dir = (0, _path.dirname)(filename); | ||
if ((0, _path.isAbsolute)(dir)) return dir; | ||
if (process.env.PWD) return (0, _path.resolve)(process.env.PWD, dir); | ||
if ((0, _path.isAbsolute)(dir)) { | ||
return dir; | ||
} | ||
if (process.env.PWD) { | ||
return (0, _path.resolve)(process.env.PWD, dir); | ||
} | ||
return (0, _path.resolve)(dir); | ||
@@ -34,6 +48,6 @@ } | ||
CallExpression: function CallExpression(path, _ref2) { | ||
var file = _ref2.file; | ||
var opts = _ref2.opts; | ||
var file = _ref2.file, | ||
opts = _ref2.opts; | ||
var currentConfig = _extends({}, defaultOptions, opts); | ||
var currentConfig = _objectSpread({}, defaultOptions, opts); | ||
@@ -45,9 +59,11 @@ if (typeof currentConfig.name !== 'string') { | ||
currentConfig.extensions = currentConfig.extensions || []; | ||
/* eslint-disable global-require */ | ||
require('asset-require-hook')(currentConfig); | ||
/* eslint-enable */ | ||
var _path$node = path.node; | ||
var calleeName = _path$node.callee.name; | ||
var args = _path$node.arguments; | ||
var _path$node = path.node, | ||
calleeName = _path$node.callee.name, | ||
args = _path$node.arguments; | ||
@@ -61,15 +77,16 @@ if (calleeName !== 'require' || !args.length || !t.isStringLiteral(args[0])) { | ||
})) { | ||
var _args = _slicedToArray(args, 1); | ||
var _args = _slicedToArray(args, 1), | ||
filePath = _args[0].value; | ||
var filePath = _args[0].value; | ||
if (!t.isExpressionStatement(path.parent)) { | ||
var from = resolveModulePath(file.opts.filename); | ||
/* eslint-disable global-require, import/no-dynamic-require, new-cap */ | ||
var p = require((0, _path.resolve)(from, filePath)); | ||
if (!t.isVariableDeclarator(path.parent)) { | ||
throw new Error('Found empty import from ' + filePath + '.'); | ||
path.replaceWith(t.StringLiteral(p)); | ||
/* eslint-enable */ | ||
} else { | ||
path.remove(); | ||
} | ||
var from = resolveModulePath(file.opts.filename); | ||
var p = require((0, _path.resolve)(from, filePath)); | ||
path.replaceWith(t.StringLiteral(p)); | ||
} | ||
@@ -76,0 +93,0 @@ } |
{ | ||
"name": "babel-plugin-transform-assets", | ||
"version": "0.2.0", | ||
"version": "1.0.1", | ||
"description": "Transforms importing of asset files at compile time using Babel", | ||
@@ -30,3 +30,3 @@ "main": "lib/index.js", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=9.0.0" | ||
}, | ||
@@ -37,3 +37,3 @@ "scripts": { | ||
"lint": "node node_modules/.bin/eslint src test", | ||
"test": "node node_modules/.bin/mocha --compilers js:babel-core/register 'test/**/*.spec.js'" | ||
"test": "npm run lint && node node_modules/.bin/mocha --compilers js:@babel/register 'test/**/*.spec.js'" | ||
}, | ||
@@ -51,15 +51,19 @@ "repository": { | ||
"devDependencies": { | ||
"babel-cli": "^6.6.0", | ||
"babel-core": "^6.6.0", | ||
"babel-eslint": "^5.0.0", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-stage-2": "^6.5.0", | ||
"@babel/cli": "7.0.0-beta.42", | ||
"@babel/core": "7.0.0-beta.42", | ||
"@babel/preset-es2015": "7.0.0-beta.42", | ||
"@babel/preset-stage-2": "7.0.0-beta.42", | ||
"@babel/register": "^7.0.0-beta.42", | ||
"babel-eslint": "8.2.2", | ||
"chai": "^3.4.1", | ||
"eslint": "^2.2.0", | ||
"eslint-config-rackt": "^1.1.1", | ||
"gulp-babel": "^6.1.2", | ||
"eslint": "^3.8.1", | ||
"eslint-config-airbnb": "^12.0.0", | ||
"eslint-plugin-import": "^1.16.0", | ||
"eslint-plugin-jsx-a11y": "^2.2.3", | ||
"eslint-plugin-react": "^6.4.1", | ||
"gulp-babel": "^8.0.0-beta.2", | ||
"gulp-util": "^3.0.7", | ||
"mocha": "^2.3.4", | ||
"rimraf": "^2.5.2" | ||
"mocha": "^3.1.2", | ||
"rimraf": "^2.5.4" | ||
} | ||
} |
@@ -9,2 +9,16 @@ # babel-plugin-transform-assets | ||
## Example | ||
```js | ||
import file from '../file.txt'; | ||
``` | ||
will be transformed to | ||
```js | ||
var file = 'file.txt?9LDjftP'; | ||
``` | ||
See the spec for [more examples](https://github.com/jmurzy/babel-plugin-transform-assets/blob/master/test/index.spec.js). | ||
## Requirements | ||
@@ -45,5 +59,8 @@ [Babel](https://github.com/babel/babel) v6 or higher. | ||
### Release Notes | ||
### Contributing | ||
* 0.1.0 Minor changes, Update dependencies | ||
* 0.0.1 Initial release | ||
Contributions are very welcome—bug fixes, features, documentation, tests. Just make sure the tests are passing. | ||
### Related Projects | ||
[babel-plugin-css-modules-transform](https://github.com/michalkvasnicak/babel-plugin-css-modules-transform) |
@@ -1,13 +0,20 @@ | ||
import { dirname, isAbsolute, resolve } from 'path' | ||
import { dirname, isAbsolute, resolve } from 'path'; | ||
const defaultOptions = { | ||
name: '[name].[ext]?[sha512:hash:base64:7]' | ||
} | ||
name: '[name].[ext]?[sha512:hash:base64:7]', | ||
}; | ||
export default function transformAssets({ types: t }) { | ||
function resolveModulePath(filename) { | ||
const dir = dirname(filename) | ||
if (isAbsolute(dir)) return dir | ||
if (process.env.PWD) return resolve(process.env.PWD, dir) | ||
return resolve(dir) | ||
const dir = dirname(filename); | ||
if (isAbsolute(dir)) { | ||
return dir; | ||
} | ||
if (process.env.PWD) { | ||
return resolve(process.env.PWD, dir); | ||
} | ||
return resolve(dir); | ||
} | ||
@@ -18,33 +25,45 @@ | ||
CallExpression(path, { file, opts }) { | ||
const currentConfig = { ...defaultOptions, ...opts } | ||
const currentConfig = { | ||
...defaultOptions, | ||
...opts, | ||
}; | ||
if (typeof currentConfig.name !== 'string') { | ||
return | ||
return; | ||
} | ||
currentConfig.extensions = currentConfig.extensions || [] | ||
currentConfig.extensions = currentConfig.extensions || []; | ||
require('asset-require-hook')(currentConfig) | ||
/* eslint-disable global-require */ | ||
require('asset-require-hook')(currentConfig); | ||
/* eslint-enable */ | ||
const { callee: { name: calleeName }, arguments: args } = path.node | ||
const { | ||
callee: { | ||
name: calleeName, | ||
}, | ||
arguments: args, | ||
} = path.node; | ||
if (calleeName !== 'require' || !args.length || !t.isStringLiteral(args[0])) { | ||
return | ||
return; | ||
} | ||
if (currentConfig.extensions.find(ext => args[0].value.endsWith(ext))) { | ||
const [ { value: filePath } ] = args | ||
const [{ value: filePath }] = args; | ||
if (!t.isVariableDeclarator(path.parent)) { | ||
throw new Error(`Found empty import from ${filePath}.`) | ||
if (!t.isExpressionStatement(path.parent)) { | ||
const from = resolveModulePath(file.opts.filename); | ||
/* eslint-disable global-require, import/no-dynamic-require, new-cap */ | ||
const p = require(resolve(from, filePath)); | ||
path.replaceWith(t.StringLiteral(p)); | ||
/* eslint-enable */ | ||
} else { | ||
path.remove(); | ||
} | ||
const from = resolveModulePath(file.opts.filename) | ||
const p = require(resolve(from, filePath)) | ||
path.replaceWith(t.StringLiteral(p)) | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
127948
0
65
6
16
7
120
1