@financial-times/dotcom-build-images 2.6.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 +61 -0
- package/dist/node/index.js +40 -0
- package/package.json +39 -0
- package/src/index.ts +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# @financial-times/dotcom-build-images
|
|
2
|
+
|
|
3
|
+
This package exports a Webpack plugin to provide support for static image assets in our asset pipeline.
|
|
4
|
+
The plugin will copy all image files contained in the `client/` directory, and any of its subdirectories, into the configured public directory.
|
|
5
|
+
|
|
6
|
+
## Getting started
|
|
7
|
+
|
|
8
|
+
This package is compatible with Node 12+ and is distributed on npm.
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install --save-dev @financial-times/dotcom-build-images
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
After installing the module you must add it to the list of plugins in your project's `webpack.config.js` configuration file:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
const { PageKitImagesPlugin } = require('@financial-times/dotcom-build-base');
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
plugins: [
|
|
21
|
+
new PageKitImagesPlugin()
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
## Scope
|
|
26
|
+
|
|
27
|
+
The directory path for each image will be retained e.g.
|
|
28
|
+
|
|
29
|
+
`image.jpg` -> `image.jpg`
|
|
30
|
+
`images/image.jpg` -> `images/image.jpg`
|
|
31
|
+
`images/subdir/image.jpg` -> `images/subdir/image.jpg`
|
|
32
|
+
|
|
33
|
+
The public image paths are automatically added to the generated `manifest.json`, meaning that they're available via the `dotcom-server-asset-loader` e.g.
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
assetLoader.getPublicURL('images/subdir/image.jpg')
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The image file names are hashed in production, e.g. `image.76f59deb1275.jpg`, to allow us to set longer cache times.
|
|
40
|
+
|
|
41
|
+
The plugin can be configured to look in a custom location for the images by passing in the relevant base path:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
new PageKitImagesPlugin({ basePath: path.join(__dirname, '/path', '/to', '/images') })
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Options
|
|
48
|
+
|
|
49
|
+
| Option | Type | Default | Description |
|
|
50
|
+
|------------|---------|---------------|---------------------------------|
|
|
51
|
+
| `basePath` | String | `"./client/"` | Directory to look in for images |
|
|
52
|
+
|
|
53
|
+
## Caveats
|
|
54
|
+
|
|
55
|
+
- This works by creating a new "fake" entry point and will output a useless
|
|
56
|
+
JS file - this was necessary because the manifest plugin used by Page Kit
|
|
57
|
+
doesn't recognise the type of dependencies added by Webpack's copy plugin
|
|
58
|
+
(so you wouldn't be able to access images via the asset loader, boo!)
|
|
59
|
+
|
|
60
|
+
- It is possible to use the copy plugin, but that would mean swapping the
|
|
61
|
+
manifest plugin: https://gist.github.com/i-like-robots/9a460b55bcc34a38866b9cc163787639
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PageKitImagesPlugin = void 0;
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const glob = require('glob');
|
|
6
|
+
const MultiEntryPlugin = require('webpack/lib/MultiEntryPlugin');
|
|
7
|
+
class PageKitImagesPlugin {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.options = {
|
|
10
|
+
basePath: './client',
|
|
11
|
+
...options
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
apply(compiler) {
|
|
15
|
+
const imageFiles = glob.sync('**/*.{png,jpg,jpeg,gif,webp,ico,svg}', {
|
|
16
|
+
absolute: true,
|
|
17
|
+
cwd: path.resolve(this.options.basePath)
|
|
18
|
+
});
|
|
19
|
+
new MultiEntryPlugin(compiler.context, imageFiles, '__images__').apply(compiler);
|
|
20
|
+
const isDevMode = compiler.options.mode === 'development';
|
|
21
|
+
const outputFileName = isDevMode ? '[name].[ext]' : '[name].[contenthash:12].[ext]';
|
|
22
|
+
compiler.options.module.rules.push({
|
|
23
|
+
test: /\.(png|jpe?g|gif|webp|ico|svg)$/,
|
|
24
|
+
use: [
|
|
25
|
+
{
|
|
26
|
+
loader: require.resolve('file-loader'),
|
|
27
|
+
options: {
|
|
28
|
+
name: (resourcePath) => {
|
|
29
|
+
const dirname = path.dirname(resourcePath);
|
|
30
|
+
const relativePath = path.relative(this.options.basePath, dirname);
|
|
31
|
+
// retain relative directory structure
|
|
32
|
+
return path.join(relativePath, outputFileName);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.PageKitImagesPlugin = PageKitImagesPlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@financial-times/dotcom-build-images",
|
|
3
|
+
"version": "2.6.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/node/index.js",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
+
"tsc": "../../node_modules/.bin/tsc --incremental",
|
|
10
|
+
"clean": "npm run clean:dist && npm run clean:node_modules",
|
|
11
|
+
"clean:dist": "rm -rf dist",
|
|
12
|
+
"clean:node_modules": "rm -rf node_modules",
|
|
13
|
+
"build": "npm run build:node",
|
|
14
|
+
"build:node": "npm run tsc -- --module commonjs --outDir ./dist/node",
|
|
15
|
+
"dev": "npm run build:node -- --watch"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"webpack": "^4.39.2"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"file-loader": "^6.0.0",
|
|
25
|
+
"glob": "^7.1.6"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/webpack": "^4.41.7"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">= 12.0.0"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"repository": "https://github.com/Financial-Times/dotcom-page-kit.git",
|
|
36
|
+
"directory": "packages/dotcom-build-images"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/Financial-Times/dotcom-page-kit/tree/HEAD/packages/dotcom-build-images"
|
|
39
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const glob = require('glob')
|
|
3
|
+
const MultiEntryPlugin = require('webpack/lib/MultiEntryPlugin')
|
|
4
|
+
|
|
5
|
+
export class PageKitImagesPlugin {
|
|
6
|
+
options: { basePath: string }
|
|
7
|
+
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.options = {
|
|
10
|
+
basePath: './client',
|
|
11
|
+
...options
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
apply(compiler) {
|
|
16
|
+
const imageFiles = glob.sync('**/*.{png,jpg,jpeg,gif,webp,ico,svg}', {
|
|
17
|
+
absolute: true,
|
|
18
|
+
cwd: path.resolve(this.options.basePath)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
new MultiEntryPlugin(compiler.context, imageFiles, '__images__').apply(compiler)
|
|
22
|
+
|
|
23
|
+
const isDevMode = compiler.options.mode === 'development'
|
|
24
|
+
const outputFileName = isDevMode ? '[name].[ext]' : '[name].[contenthash:12].[ext]'
|
|
25
|
+
|
|
26
|
+
compiler.options.module.rules.push({
|
|
27
|
+
test: /\.(png|jpe?g|gif|webp|ico|svg)$/,
|
|
28
|
+
use: [
|
|
29
|
+
{
|
|
30
|
+
loader: require.resolve('file-loader'),
|
|
31
|
+
options: {
|
|
32
|
+
name: (resourcePath) => {
|
|
33
|
+
const dirname = path.dirname(resourcePath)
|
|
34
|
+
const relativePath = path.relative(this.options.basePath, dirname)
|
|
35
|
+
|
|
36
|
+
// retain relative directory structure
|
|
37
|
+
return path.join(relativePath, outputFileName)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
}
|