Skip to content

Commit

Permalink
Merge pull request #91 from mattiasw/custom-build-v2
Browse files Browse the repository at this point in the history
Custom build v2
  • Loading branch information
mattiasw authored Apr 6, 2020
1 parent 9d9f5d1 commit 41f46ef
Show file tree
Hide file tree
Showing 20 changed files with 913 additions and 489 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/**/*
dist
webpack.config.js
12 changes: 8 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 12.x
- name: Use Node.js 10.x
uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 10.x
- run: npm ci
- name: Test npm package with minimal requirements
run: |
Expand All @@ -43,14 +43,16 @@ jobs:
PATH=$(echo "$PATH" | sed -e 's/:\/usr\/local\/bin://'):/usr/local/bin/npm:/usr/local/bin/node
npm init -y
npm install ../$PACKAGE
node -p "const {writeFileSync} = require('fs'); const packageJson = require('./package.json'); packageJson.exifreader = {include: {jpeg: true, exif: true}}; writeFileSync('package.json', JSON.stringify(packageJson));"
npm rebuild exifreader
package-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 12.x
- name: Use Node.js 10.x
uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 10.x
- run: npm ci
- name: Test npm package with minimal requirements
run: |
Expand All @@ -63,3 +65,5 @@ jobs:
$env:Path = $arrPath -join ';'
npm init -y
npm install ..\exifreader-$($PackageVersion).tgz
node -p "const {writeFileSync} = require('fs'); const packageJson = require('./package.json'); packageJson.exifreader = {include: {jpeg: true, exif: true}}; writeFileSync('package.json', JSON.stringify(packageJson));"
npm rebuild exifreader
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ easily be used from Webpack, RequireJS, Browserify, Node etc. Since it is
written using ES2015+, you can also import the ES module directly from your own
ES2015+ project.

The included bundle has all functionality built in, but you can easily make a
custom build that suits your project's needs. See below for instructions. NOTE:
This functionality is in beta. Please file an issue if there are any problems.

**Notes for exif-js users**

If you come here from the popular but now dead exif-js package, please let me
Expand Down Expand Up @@ -139,6 +143,69 @@ fs.writeFileSync('/path/to/new/thumbnail.jpg', Buffer.from(tags['Thumbnail'].ima

See [examples/](examples/) directory for more details.

### Configure a custom build

**NOTE:** This functionality is in beta but should work fine. Please file an
issue if you're having problems or ideas on how to make it better.

This is for npm users that use the built file. To specify what functionality you
want you can either use include pattern (start with an empty set and include) or
exclude pattern (start with full functionality and exclude). If an include
pattern is set, excludes will not be used.

The configuration is added to your project's `package.json` file.

**Example 1:** Only include JPEG files and Exif tags (this makes the bundle
almost half the size of the full one (non-gzipped)):

```javascript
"exifreader": {
"include": {
"jpeg": true,
"exif": true
}
}
```

**Example 2:** Exclude XMP tags:

```javascript
"exifreader": {
"exclude": {
"xmp": true
}
}
```

Then, if you didn't install ExifReader yet, just run `npm install exifreader`.
Otherwise you have to re-build the library:

```bash
npm rebuild exifreader
```

After that the new bundle is here: `node_modules/exifreader/dist/exif-reader.js`

If you're using the include pattern config, remember to include everything you
want to use. If you want `xmp` and don't specify any file types, you will get
"Invalid image format", and if you specify `jpeg` but don't mention any tag
types no tags will be found.

Possible modules to include or exclude:

| Module | Description |
| ----------- | -------------------------------------------------------------- |
| `jpeg` | JPEG images. |
| `tiff` | TIFF images. |
| `png` | PNG images. |
| `heic` | HEIC/HEIF images. |
| `file` | JPEG image width, height etc. |
| `exif` | Regular Exif tags. If excluded, will also exclude `thumbnail`. |
| `iptc` | IPTC tags. |
| `xmp` | XMP tags. |
| `icc` | ICC color profile tags. |
| `thumbnail` | JPEG thumbnail. Needs `exif`. |

Notes
-----

Expand Down Expand Up @@ -217,6 +284,8 @@ npm run coverage
Known Limitations
-----------------

- For PNG files, only uncompressed XMP tags are currently supported.
- For HEIC files, only Exif tags are currently supported.
- The descriptions for UserComment, GPSProcessingMethod and GPSAreaInformation
are missing for other encodings than ASCII.

Expand Down Expand Up @@ -246,8 +315,11 @@ case is covered.
Changelog
---------

- **April 2020**:
- Add functionality to create a custom build to reduce bundle size.
- **March 2020**:
- Add support for PNG images.
- Add support for thumbnails in JPEGs.
- Major update to version 3.0. However, the actual change is quite small,
albeit a breaking one if you use that functionality (`.value` on
rational tags). Rational values are now kept in their original
Expand Down
7 changes: 7 additions & 0 deletions bin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"node": true
},
"rules": {
}
}
30 changes: 30 additions & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require('path');
const {execSync} = require('child_process');
const dependentHasExifReaderConfig = require('./findDependentConfig');

process.chdir(path.join(__dirname, '..'));

if (!process.argv.includes('--only-with-config') || checkConfig()) {
execSync('webpack', {stdio: 'inherit'});
}

function checkConfig() {
if (dependentHasExifReaderConfig()) {
if (!isDependenciesInstalled()) {
console.log('Installing ExifReader custom build dependencies...');
execSync('npm install --no-optional --no-package-lock');
console.log('Done.');
}
return true;
}
return false;
}

function isDependenciesInstalled() {
try {
execSync('npm ls webpack');
return true;
} catch (error) {
return false;
}
}
17 changes: 17 additions & 0 deletions bin/findDependentConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path');

module.exports = getConfigFromParent.bind(null, path.join(__dirname, '..'));

function getConfigFromParent(directory) {
const parentDirectory = path.join(directory, '..');
if (parentDirectory === directory) {
return false;
}

try {
const packageJson = require(path.join(parentDirectory, 'package.json'));
return packageJson.exifreader;
} catch (error) {
return getConfigFromParent(parentDirectory);
}
}
2 changes: 1 addition & 1 deletion dist/exif-reader.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/exif-reader.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
"license": "MPL-2.0",
"main": "dist/exif-reader.js",
"module": "src/exif-reader.js",
"sideEffects": false,
"files": [
"bin/",
"dist/",
"src/",
"exif-reader.d.ts"
"babel.config.json",
"exif-reader.d.ts",
"webpack.config.js"
],
"dependencies": {},
"types": "./exif-reader.d.ts",
Expand All @@ -32,15 +36,17 @@
"mocha": "^7.1.1",
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1",
"string-replace-loader": "^2.2.0",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.6"
},
"scripts": {
"build": "webpack",
"build": "node bin/build.js",
"coverage": "nyc npm test",
"lint": "eslint .",
"precommit": "npm-run-all lint test",
"test": "cross-env BABEL_ENV=test mocha --require @babel/register"
"test": "cross-env BABEL_ENV=test mocha --require @babel/register",
"postinstall": "node bin/build.js --only-with-config"
},
"nyc": {
"check-coverage": true,
Expand Down
16 changes: 16 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

export default {
USE_FILE: true,
USE_EXIF: true,
USE_IPTC: true,
USE_XMP: true,
USE_ICC: true,
USE_THUMBNAIL: true,
USE_TIFF: true,
USE_JPEG: true,
USE_PNG: true,
USE_HEIC: true
};
20 changes: 12 additions & 8 deletions src/exif-reader.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* ExifReader
* http://github.com/mattiasw/exifreader
* Copyright (C) 2011-2018 Mattias Wallander <[email protected]>
* Copyright (C) 2011-2020 Mattias Wallander <[email protected]>
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import {objectAssign} from './utils';
import DataViewWrapper from './dataview';
import Constants from './constants';
import ImageHeader from './image-header';
import Tags from './tags';
import FileTags from './file-tags';
Expand Down Expand Up @@ -45,7 +46,7 @@ export function loadView(dataView, options = {expanded: false}) {

const {fileDataOffset, tiffHeaderOffset, iptcDataOffset, xmpChunks, iccChunks, pngHeaderOffset} = ImageHeader.parseAppMarkers(dataView);

if (hasFileData(fileDataOffset)) {
if (Constants.USE_JPEG && Constants.USE_FILE && hasFileData(fileDataOffset)) {
foundMetaData = true;
const readTags = FileTags.read(dataView, fileDataOffset);
if (options.expanded) {
Expand All @@ -54,7 +55,7 @@ export function loadView(dataView, options = {expanded: false}) {
tags = objectAssign({}, tags, readTags);
}
}
if (hasExifData(tiffHeaderOffset)) {
if (Constants.USE_EXIF && hasExifData(tiffHeaderOffset)) {
foundMetaData = true;
const {Thumbnail: thumbnailTags, ...readTags} = Tags.read(dataView, tiffHeaderOffset);
if (options.expanded) {
Expand All @@ -66,7 +67,7 @@ export function loadView(dataView, options = {expanded: false}) {
tags.Thumbnail = thumbnailTags;
}
}
if (hasIptcData(iptcDataOffset)) {
if (Constants.USE_JPEG && Constants.USE_IPTC && hasIptcData(iptcDataOffset)) {
foundMetaData = true;
const readTags = IptcTags.read(dataView, iptcDataOffset);
if (options.expanded) {
Expand All @@ -75,7 +76,7 @@ export function loadView(dataView, options = {expanded: false}) {
tags = objectAssign({}, tags, readTags);
}
}
if (hasXmpData(xmpChunks)) {
if (Constants.USE_XMP && hasXmpData(xmpChunks)) {
foundMetaData = true;
const readTags = XmpTags.read(dataView, xmpChunks);
if (options.expanded) {
Expand All @@ -84,7 +85,7 @@ export function loadView(dataView, options = {expanded: false}) {
tags = objectAssign({}, tags, readTags);
}
}
if (hasIccData(iccChunks)) {
if (Constants.USE_JPEG && Constants.USE_ICC && hasIccData(iccChunks)) {
foundMetaData = true;
const readTags = IccTags.read(dataView, iccChunks);
if (options.expanded) {
Expand All @@ -93,7 +94,7 @@ export function loadView(dataView, options = {expanded: false}) {
tags = objectAssign({}, tags, readTags);
}
}
if (hasPngFileData(pngHeaderOffset)) {
if (Constants.USE_PNG && hasPngFileData(pngHeaderOffset)) {
foundMetaData = true;
const readTags = PngFileTags.read(dataView, pngHeaderOffset);
if (options.expanded) {
Expand All @@ -103,7 +104,10 @@ export function loadView(dataView, options = {expanded: false}) {
}
}

const thumbnail = Thumbnail.get(dataView, tags.Thumbnail, tiffHeaderOffset);
const thumbnail = Constants.USE_JPEG
&& Constants.USE_EXIF
&& Constants.USE_THUMBNAIL
&& Thumbnail.get(dataView, tags.Thumbnail, tiffHeaderOffset);
if (thumbnail) {
tags.Thumbnail = thumbnail;
foundMetaData = true;
Expand Down
Loading

0 comments on commit 41f46ef

Please sign in to comment.