Skip to content
This repository was archived by the owner on Jun 19, 2018. It is now read-only.

Commit acbad80

Browse files
committed
feat: include MagentoResolver and docs
1 parent a00a52a commit acbad80

File tree

7 files changed

+89
-26
lines changed

7 files changed

+89
-26
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ a Peregrine app to a Magento backend and a
2222
* [`PWADevServer`](docs/PWADevServer.md) -- Configures your system settings and
2323
* [`ServiceWorkerPlugin`](docs/ServiceWorkerPlugin.md) -- Creates
2424
a ServiceWorker with different settings based on dev scenarios
25+
* [`MagentoResolver`](docs/MagentoResolver.md) -- Configures Webpack to resolve
26+
modules and assets in Magento PWA themes.

Diff for: docs/MagentoResolver.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## MagentoResolver
2+
3+
Adapter for configuring Webpack to resolve assets according to Magento PWA conventions.
4+
5+
### Usage
6+
7+
In `webpack.config.js`:
8+
9+
```js
10+
const buildpack = require('@magento/pwa-buildpack');
11+
const MagentoResolver = buildpack.Webpack.MagentoResolver;
12+
13+
module.exports = async env => {
14+
const config {
15+
/* webpack entry, output, rules, etc */
16+
17+
18+
resolve: await MagentoResolver.configure({
19+
paths: {
20+
root: __dirname
21+
}
22+
})
23+
24+
};
25+
26+
return config;
27+
}
28+
```
29+
30+
31+
- ⚠️ `MagentoResolver.configure()` is async and returns a Promise, so a Webpack
32+
config that uses it must use the [Exporting a Promise configuration type](https://webpack.js.org/configuration/configuration-types/#exporting-a-promise).
33+
The newer `async/await` syntax looks cleaner than using Promises directly.
34+
35+
### Purpose
36+
37+
Generates a configuration for use in the [`resolve` property of Webpack config](https://webpack.js.org/configuration/resolve/).
38+
Describes how to traverse the filesystem structure for assets required in source
39+
files.
40+
41+
Currently, `MagentoResolver` does very little, but it's likely that the Magento
42+
development environment will require custom resolution rules in the future; this
43+
utility sets the precedent of the API for delivering those rules.
44+
45+
### API
46+
47+
`MagentoResolver` has only one method: `.configure(options)`. It returns a Promise
48+
for an object that can be assigned to the `resolve` property in Webpack config
49+
objects.
50+
51+
#### `MagentoResolver.configure(options: ResolverOptions): Promise<resolve>`
52+
53+
#### `options`
54+
55+
- `paths: object`: **Required.** Local absolute paths to theme folders.
56+
- `root`: Absolute path to the root directory of the theme.

Diff for: src/webpack/MagentoResolver.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const optionsValidator = require('../util/options-validator');
2+
class MagentoResolver {
3+
static validateConfig = optionsValidator('MagentoResolver', {
4+
'paths.root': 'string'
5+
});
6+
static async configure(options) {
7+
this.validateConfig('.configure()', options);
8+
return {
9+
modules: [options.paths.root, 'node_modules'],
10+
mainFiles: ['index'],
11+
extensions: ['.js']
12+
};
13+
}
14+
}
15+
module.exports = MagentoResolver;

Diff for: src/webpack/Resolver.js

-10
This file was deleted.

Diff for: src/webpack/__tests__/MagentoResolver.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const MagentoResolver = require('../MagentoResolver');
2+
test('static configure() produces a webpack resolver config', async () => {
3+
await expect(
4+
MagentoResolver.configure({ paths: { root: 'fakeRoot' } })
5+
).resolves.toEqual({
6+
modules: ['fakeRoot', 'node_modules'],
7+
mainFiles: ['index'],
8+
extensions: ['.js']
9+
});
10+
});
11+
test('static configure() throws if required paths are missing', async () => {
12+
await expect(
13+
MagentoResolver.configure({ paths: { root: false } })
14+
).rejects.toThrow('paths.root must be of type string');
15+
});

Diff for: src/webpack/__tests__/resolver.spec.js

-15
This file was deleted.

Diff for: src/webpack/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
MagentoRootComponentsPlugin: require('./plugins/MagentoRootComponentsPlugin'),
33
ServiceWorkerPlugin: require('./plugins/ServiceWorkerPlugin'),
4-
Resolver: require('./Resolver'),
4+
MagentoResolver: require('./MagentoResolver'),
55
PWADevServer: require('./PWADevServer')
66
};

0 commit comments

Comments
 (0)