exifreader 4.40.5 → 4.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,8 +12,8 @@ that contain metadata, **well under 1 ms per file** for typical phone JPEGs
12
12
  and **over 95% less bandwidth**.
13
13
 
14
14
  ExifReader is highly and easily configurable and the resulting bundle can be as
15
- small as **~4 KiB** (gzipped) if you're only interested in a few tags (e.g. date
16
- and/or GPS values).
15
+ small as **~9 KiB** (Brotli-compressed) if you're only interested in a few tags
16
+ (e.g. date and/or GPS values).
17
17
 
18
18
  ExifReader supports module formats ESM, AMD, CommonJS, and globals and can
19
19
  therefore easily be used from Webpack, RequireJS, Browserify, Node etc.
@@ -786,33 +786,52 @@ tags (resulting bundle will be ~19 % of a gzipped full build):
786
786
  }
787
787
  ```
788
788
 
789
- Then, if you didn't install ExifReader yet, run `npm install exifreader`.
790
- Otherwise you have to rebuild the library:
789
+ Then build the custom bundle. If you haven't installed ExifReader yet:
791
790
 
792
791
  ```bash
793
- npm rebuild exifreader
792
+ npm install exifreader
793
+ npx exifreader build
794
794
  ```
795
795
 
796
- With yarn 2+:
797
-
798
- From my experience, you need a `node_modules` folder for the rebuild command to
799
- work with yarn 2+. If you don't have it, run `yarn config set nodeLinker
800
- node-modules` and then run `yarn`. Then you can try to rebuild:
796
+ If it is already installed, or you just changed the configuration, run:
801
797
 
802
798
  ```bash
803
- yarn rebuild exifreader
799
+ npx exifreader build
804
800
  ```
805
801
 
806
- With yarn 1:
807
- ```bash
808
- yarn add exifreader
802
+ This reads the `exifreader` configuration from your `package.json`, rebuilds the
803
+ library, and writes the result to `node_modules/exifreader/dist/exif-reader.js`.
804
+ It works the same way with npm, yarn, and pnpm.
805
+
806
+ **Re-run after every install.** A fresh `npm install`, `npm ci`, or upgrade
807
+ restores the full bundle, so run `npx exifreader build` again afterwards. The
808
+ most reliable approach is to wire it into your own build step, for example (in
809
+ your own `package.json`):
810
+
811
+ ```json
812
+ "scripts": {
813
+ "prebuild": "exifreader build"
814
+ }
809
815
  ```
810
816
 
811
- After that the new bundle is here: `node_modules/exifreader/dist/exif-reader.js`
817
+ **Deprecated: automatic rebuild on install.** Older versions rebuilt the library
818
+ automatically through an npm `postinstall` script, so a plain install or `npm
819
+ rebuild exifreader` (or `yarn rebuild exifreader` with yarn 2+, or `yarn add
820
+ exifreader` with yarn 1) produced the custom bundle. This still works in v4 but
821
+ is deprecated and will be removed in v5, because npm is phasing out automatic
822
+ install scripts. Use `npx exifreader build` instead, which also avoids the yarn
823
+ 2+ `node_modules` and `nodeLinker` caveats.
812
824
 
813
825
  If you are using `vite`, you will need to [clear the dependency cache](https://vitejs.dev/guide/dep-pre-bundling.html#file-system-cache)
814
826
  after a rebuild.
815
827
 
828
+ **Advanced and monorepos.** Instead of the `package.json` configuration you can
829
+ pass the same `include`/`exclude` object as JSON in the `EXIFREADER_CUSTOM_BUILD`
830
+ environment variable, which takes priority over `package.json`. In a monorepo,
831
+ `npx exifreader build` reads the configuration from the package you run it in,
832
+ but a hoisted `node_modules/exifreader` is shared by every workspace package, so
833
+ there is only one custom bundle and the last build wins.
834
+
816
835
  If you're using the include pattern config, remember to include everything you
817
836
  want to use. If you want `xmp` and don't specify any file types, you will get
818
837
  "Invalid image format", and if you specify `jpeg` but don't mention any tag
package/babel.config.json CHANGED
@@ -1,14 +1,9 @@
1
1
  {
2
+ "presets": [
3
+ "@babel/env"
4
+ ],
2
5
  "env": {
3
- "development": {
4
- "presets": [
5
- "@babel/env"
6
- ]
7
- },
8
6
  "test": {
9
- "presets": [
10
- "@babel/env"
11
- ],
12
7
  "plugins": [
13
8
  "rewire"
14
9
  ]
package/bin/build.js CHANGED
@@ -4,64 +4,94 @@
4
4
 
5
5
  const path = require('path');
6
6
  const fs = require('fs');
7
+ const os = require('os');
7
8
  const {execSync} = require('child_process');
8
9
  const dependentHasExifReaderConfig = require('./findDependentConfig');
9
10
 
10
11
  const EXIFREADER_ROOT_DIR = path.join(__dirname, '..');
11
- process.chdir(EXIFREADER_ROOT_DIR);
12
12
 
13
- if (!process.argv.includes('--only-with-config') || checkConfig()) {
14
- execSync(`npx -p ${getPackage('webpack-cli')} -p ${getPackage('webpack')} webpack`, {stdio: 'inherit'});
13
+ module.exports = {runBuild, hasDependentConfig};
14
+
15
+ if (require.main === module) {
16
+ // With --only-with-config (postinstall) only build when a dependent has a
17
+ // config. Without it (`npm run build`) always build.
18
+ if (!process.argv.includes('--only-with-config') || hasDependentConfig()) {
19
+ runBuild();
20
+ }
15
21
  }
16
22
 
17
- function checkConfig() {
18
- if (dependentHasExifReaderConfig()) {
19
- console.log('Installing ExifReader custom build dependencies...'); // eslint-disable-line no-console
20
- const packages = [
21
- '@babel/core',
22
- '@babel/preset-env',
23
- '@babel/register',
24
- 'babel-loader',
25
- 'cross-env',
26
- 'string-replace-loader',
27
- 'terser-webpack-plugin'
28
- ].map((_package) => getPackage(_package));
29
- const tmpDir = path.join(EXIFREADER_ROOT_DIR, '__tmp');
30
- try {
31
- initTmpDir(tmpDir);
32
- execSync(`npm install --production --loglevel=error --no-optional --no-package-lock --no-save ${packages.join(' ')}`, {stdio: 'inherit'});
33
- console.log('Done.'); // eslint-disable-line no-console
34
- } catch (error) {
35
- console.error('Could not install requirements for a custom build:', error); // eslint-disable-line no-console
36
- process.exit(1);
37
- } finally {
38
- cleanUpTmpDir(tmpDir);
23
+ /**
24
+ * Run the webpack build, installing the build toolchain first when a custom
25
+ * build is requested.
26
+ *
27
+ * @param {{config?: object}} [options] When `config` is given (the `exifreader`
28
+ * config object), it is passed to webpack via the EXIFREADER_CUSTOM_BUILD env
29
+ * var. Otherwise webpack resolves the config from package.json itself.
30
+ */
31
+ function runBuild(options) {
32
+ options = options || {};
33
+ process.chdir(EXIFREADER_ROOT_DIR);
34
+
35
+ // Pin the build env regardless of the caller's shell: NODE_ENV=production
36
+ // keeps webpack in production mode, and BABEL_ENV is only belt-and-suspenders
37
+ // against a stray BABEL_ENV=test adding the test-only rewire plugin.
38
+ const env = {...process.env, BABEL_ENV: 'production', NODE_ENV: 'production'};
39
+ if (options.config) {
40
+ env.EXIFREADER_CUSTOM_BUILD = JSON.stringify(options.config);
41
+ }
42
+
43
+ let tmpDir;
44
+ try {
45
+ if (env.EXIFREADER_CUSTOM_BUILD || hasDependentConfig()) {
46
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'exifreader-build-'));
47
+ installCustomBuildDependencies(tmpDir);
48
+ const buildModules = path.join(tmpDir, 'node_modules');
49
+ // Point webpack at the isolated toolchain. NODE_PATH (read at child
50
+ // startup) covers webpack.config.js's require('terser-webpack-plugin')
51
+ // and Babel's @babel/preset-env lookup. EXIFREADER_BUILD_MODULES feeds
52
+ // resolveLoader.modules for the webpack loaders.
53
+ env.EXIFREADER_BUILD_MODULES = buildModules;
54
+ env.NODE_PATH = env.NODE_PATH ? buildModules + path.delimiter + env.NODE_PATH : buildModules;
55
+ }
56
+
57
+ execSync(`npx -p ${getPackage('webpack-cli')} -p ${getPackage('webpack')} webpack`, {stdio: 'inherit', env});
58
+ } finally {
59
+ if (tmpDir) {
60
+ try {
61
+ fs.rmSync(tmpDir, {recursive: true, force: true});
62
+ } catch (error) {
63
+ // Best-effort: a locked file (e.g. Windows AV/watcher) during
64
+ // cleanup must not mask the build result. os.tmpdir() is reaped
65
+ // by the OS anyway.
66
+ }
39
67
  }
40
- return true;
41
68
  }
42
- return false;
43
69
  }
44
70
 
45
- function getPackage(name) {
46
- const version = require(path.join(EXIFREADER_ROOT_DIR, 'package.json')).devDependencies[name].replace(/^\^/, '');
47
- return `${name}@${version}`;
71
+ function hasDependentConfig() {
72
+ return !!dependentHasExifReaderConfig();
48
73
  }
49
74
 
50
- function initTmpDir(tmpDir) {
51
- const nodeModulesDir = path.join(EXIFREADER_ROOT_DIR, 'node_modules');
52
- fs.mkdirSync(tmpDir, {recursive: true});
53
- if (fs.existsSync(nodeModulesDir)) {
54
- fs.renameSync(nodeModulesDir, path.join(tmpDir, 'node_modules'));
55
- }
56
- process.chdir(tmpDir);
57
- execSync('npm init -y', {stdio: 'inherit'});
75
+ function installCustomBuildDependencies(tmpDir) {
76
+ console.log('Installing ExifReader custom build dependencies...'); // eslint-disable-line no-console
77
+ const packages = [
78
+ '@babel/core',
79
+ '@babel/preset-env',
80
+ '@babel/register',
81
+ 'babel-loader',
82
+ 'cross-env',
83
+ 'string-replace-loader',
84
+ 'terser-webpack-plugin'
85
+ ].map((_package) => getPackage(_package));
86
+
87
+ // npm init only creates a package.json to anchor the install to tmpDir, so
88
+ // silence its package.json dump (stderr is kept for real errors).
89
+ execSync('npm init -y', {cwd: tmpDir, stdio: ['ignore', 'ignore', 'inherit']});
90
+ execSync(`npm install --production --loglevel=error --no-optional --no-package-lock --no-save ${packages.join(' ')}`, {cwd: tmpDir, stdio: 'inherit'});
91
+ console.log('Done.'); // eslint-disable-line no-console
58
92
  }
59
93
 
60
- function cleanUpTmpDir(tmpDir) {
61
- const nodeModulesDir = path.join(tmpDir, 'node_modules');
62
- if (fs.existsSync(nodeModulesDir)) {
63
- fs.renameSync(nodeModulesDir, path.join(EXIFREADER_ROOT_DIR, 'node_modules'));
64
- }
65
- process.chdir(EXIFREADER_ROOT_DIR);
66
- fs.rmSync(tmpDir, {recursive: true, force: true});
94
+ function getPackage(name) {
95
+ const version = require(path.join(EXIFREADER_ROOT_DIR, 'package.json')).devDependencies[name].replace(/^\^/, '');
96
+ return `${name}@${version}`;
67
97
  }
package/bin/cli.js ADDED
@@ -0,0 +1,165 @@
1
+ #!/usr/bin/env node
2
+
3
+ /* This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
6
+
7
+ /* eslint-disable no-console */
8
+
9
+ const path = require('path');
10
+ const fs = require('fs');
11
+
12
+ const EXIFREADER_ROOT_DIR = path.join(__dirname, '..');
13
+
14
+ module.exports = {parseArgs, run, checkLocalInstall};
15
+
16
+ // build.js is required lazily (only when run) so importing this file in unit
17
+ // tests does not pull its integration-tested build logic into coverage.
18
+ if (require.main === module) {
19
+ run(process.argv, {
20
+ cwd: () => process.cwd(),
21
+ env: process.env,
22
+ version: () => require(path.join(EXIFREADER_ROOT_DIR, 'package.json')).version,
23
+ resolveConfig: (directory) => require('./findDependentConfig').forCli(directory),
24
+ checkLocalInstall,
25
+ build: (options) => require('./build').runBuild(options),
26
+ log: console.log,
27
+ error: console.error,
28
+ exit: (code) => process.exit(code)
29
+ });
30
+ }
31
+
32
+ function run(argv, deps) {
33
+ const args = parseArgs(argv);
34
+
35
+ if (args.version) {
36
+ deps.log(deps.version());
37
+ deps.exit(0);
38
+ return;
39
+ }
40
+
41
+ if (args.help) {
42
+ deps.log(helpText());
43
+ deps.exit(0);
44
+ return;
45
+ }
46
+
47
+ if (args.command === 'build') {
48
+ runBuildCommand(deps);
49
+ return;
50
+ }
51
+
52
+ if (args.command) {
53
+ deps.error(`Unknown command: ${args.command}`);
54
+ }
55
+ deps.log(helpText());
56
+ deps.exit(1);
57
+ }
58
+
59
+ function parseArgs(argv) {
60
+ const args = argv.slice(2);
61
+ return {
62
+ command: args.filter((arg) => !arg.startsWith('-'))[0],
63
+ help: args.includes('-h') || args.includes('--help'),
64
+ version: args.includes('-v') || args.includes('--version')
65
+ };
66
+ }
67
+
68
+ function helpText() {
69
+ return 'Usage: npx exifreader <command>\n'
70
+ + '\n'
71
+ + 'Commands:\n'
72
+ + ' build Rebuild dist/exif-reader.js using the "exifreader" custom build\n'
73
+ + ' configuration (include/exclude) in your project\'s package.json.\n'
74
+ + '\n'
75
+ + 'Options:\n'
76
+ + ' -h, --help Show this help.\n'
77
+ + ' -v, --version Show the exifreader version.\n'
78
+ + '\n'
79
+ + 'Run this after installing exifreader and adding an "exifreader" section to your\n'
80
+ + 'package.json. It replaces the deprecated automatic postinstall rebuild.';
81
+ }
82
+
83
+ function runBuildCommand(deps) {
84
+ const cwd = deps.cwd();
85
+ const install = deps.checkLocalInstall(cwd);
86
+ if (!install.ok) {
87
+ deps.error(install.message);
88
+ deps.exit(1);
89
+ return;
90
+ }
91
+
92
+ // An explicit EXIFREADER_CUSTOM_BUILD env var overrides package.json. Webpack
93
+ // reads it directly, so skip resolution and build.
94
+ if (deps.env.EXIFREADER_CUSTOM_BUILD) {
95
+ deps.build();
96
+ return;
97
+ }
98
+
99
+ const config = deps.resolveConfig(cwd);
100
+ if (!config) {
101
+ deps.error(noConfigMessage());
102
+ deps.exit(1);
103
+ return;
104
+ }
105
+
106
+ deps.log(`Building a custom exifreader bundle at ${install.distPath}`);
107
+ deps.build({config});
108
+ }
109
+
110
+ function noConfigMessage() {
111
+ return 'No ExifReader custom build configuration found. Add an "exifreader" section with '
112
+ + '"include" or "exclude" to your project\'s package.json, then run "npx exifreader build" '
113
+ + 'again. See https://github.com/mattiasw/ExifReader#configure-a-custom-build';
114
+ }
115
+
116
+ function checkLocalInstall(cwd, options) {
117
+ options = options || {};
118
+ const resolve = options.resolve || defaultResolve;
119
+ const realpath = options.realpath || fs.realpathSync;
120
+ const rootDir = options.rootDir || EXIFREADER_ROOT_DIR;
121
+ const isPnp = options.isPnp !== undefined ? options.isPnp : !!process.versions.pnp;
122
+
123
+ let resolvedDir;
124
+ try {
125
+ resolvedDir = path.dirname(resolve(cwd));
126
+ } catch (error) {
127
+ return {ok: false, message: notInstalledMessage(isPnp)};
128
+ }
129
+
130
+ const running = safeRealpath(realpath, rootDir);
131
+ const installed = safeRealpath(realpath, resolvedDir);
132
+ if (running !== installed) {
133
+ return {ok: false, message: mismatchMessage(installed)};
134
+ }
135
+
136
+ return {ok: true, distPath: path.join(installed, 'dist', 'exif-reader.js')};
137
+ }
138
+
139
+ function defaultResolve(cwd) {
140
+ return require.resolve('exifreader/package.json', {paths: [cwd]});
141
+ }
142
+
143
+ function notInstalledMessage(isPnp) {
144
+ if (isPnp) {
145
+ return 'exifreader could not be resolved under Yarn Plug\'n\'Play. Custom builds require a '
146
+ + 'node_modules install: set nodeLinker: node-modules in .yarnrc.yml, run yarn, then '
147
+ + 'run "npx exifreader build".';
148
+ }
149
+ return 'exifreader is not installed in this project. Run "npm install exifreader" first, then '
150
+ + '"npx exifreader build".';
151
+ }
152
+
153
+ function safeRealpath(realpath, target) {
154
+ try {
155
+ return realpath(target);
156
+ } catch (error) {
157
+ return target;
158
+ }
159
+ }
160
+
161
+ function mismatchMessage(installedDir) {
162
+ return 'A different exifreader copy is installed in this project than the one being run '
163
+ + `(installed at ${installedDir}). This usually means you ran "npx exifreader@<version>" with `
164
+ + 'a version other than the installed one. Run "npx exifreader build" without a version pin.';
165
+ }
@@ -4,18 +4,59 @@
4
4
 
5
5
  const path = require('path');
6
6
 
7
- module.exports = getConfigFromParent.bind(null, path.join(__dirname, '..'));
7
+ const INSTALL_DIR = path.join(__dirname, '..');
8
8
 
9
- function getConfigFromParent(directory) {
10
- const parentDirectory = path.join(directory, '..');
11
- if (parentDirectory === directory) {
12
- return false;
9
+ // Default export, used by the postinstall path and webpack.config.js. Walks up
10
+ // from the install location and returns the first package.json's `exifreader`
11
+ // value. The options arg is for tests only (inject a reader / install dir).
12
+ module.exports = findFromInstallLocation;
13
+ module.exports.forCli = forCli;
14
+
15
+ function findFromInstallLocation(options) {
16
+ return walkUp(path.join(installDirFrom(options), '..'), readerFrom(options));
17
+ }
18
+
19
+ // Config resolution for `npx exifreader build`: prefer the project the user is
20
+ // in by walking up from the start dir and using the first package.json found,
21
+ // falling back to the install-location walk only when it has no `exifreader`
22
+ // key. Using the first package.json avoids grabbing a stray ancestor's config.
23
+ function forCli(startDir, options) {
24
+ const read = readerFrom(options);
25
+ return nearestConfig(startDir, read)
26
+ || walkUp(path.join(installDirFrom(options), '..'), read)
27
+ || false;
28
+ }
29
+
30
+ function installDirFrom(options) {
31
+ return (options && options.installDir) || INSTALL_DIR;
32
+ }
33
+
34
+ function readerFrom(options) {
35
+ return (options && options.readPackageJson) || requirePackageJson;
36
+ }
37
+
38
+ function walkUp(directory, read) {
39
+ const packageJson = read(directory);
40
+ if (packageJson !== undefined) {
41
+ return packageJson.exifreader;
13
42
  }
43
+ const parent = path.join(directory, '..');
44
+ return parent === directory ? false : walkUp(parent, read);
45
+ }
14
46
 
47
+ function nearestConfig(directory, read) {
48
+ const packageJson = read(directory);
49
+ if (packageJson !== undefined) {
50
+ return packageJson.exifreader || false;
51
+ }
52
+ const parent = path.join(directory, '..');
53
+ return parent === directory ? false : nearestConfig(parent, read);
54
+ }
55
+
56
+ function requirePackageJson(directory) {
15
57
  try {
16
- const packageJson = require(path.join(parentDirectory, 'package.json'));
17
- return packageJson.exifreader;
58
+ return require(path.join(directory, 'package.json'));
18
59
  } catch (error) {
19
- return getConfigFromParent(parentDirectory);
60
+ return undefined;
20
61
  }
21
62
  }