1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | const path_1 = require("path");
|
8 |
|
9 | const loader_utils_1 = require("loader-utils");
|
10 |
|
11 | module.exports = function sectionFocusLoader() {};
|
12 |
|
13 | module.exports.pitch = function pitch() {
|
14 | const {
|
15 | resourcePath
|
16 | } = this;
|
17 | const {
|
18 | focus
|
19 | } = loader_utils_1.getOptions(this);
|
20 |
|
21 | if (focus == null || focus.length === 0) {
|
22 | throw new Error('You attempted to use the section-focus-loader without setting any focused sections. Make sure you pass the `focus` option and run webpack again.');
|
23 | }
|
24 |
|
25 | const sectionOrSections = focus.length > 1 ? 'sections' : 'section';
|
26 | return `
|
27 | // section-focus-loader
|
28 |
|
29 | var React = require('react');
|
30 |
|
31 | function FocusedComponent() {
|
32 | return React.createElement('div', {style: {padding: '20px', fontSize: '20px', lineHeight: '24px'}},
|
33 | React.createElement('p', {}, 'This component has been removed because you are focusing on the ${focusString(focus)} ${sectionOrSections}.'),
|
34 | React.createElement('p', {style: {marginTop: '20px'}},
|
35 | 'To work on this section, restart your server without the ',
|
36 | React.createElement('code', {}, '--focus'),
|
37 | ' flag or with ',
|
38 | React.createElement('code', {}, '--focus ${sectionGroupName(resourcePath)}')
|
39 | )
|
40 | );
|
41 | }
|
42 |
|
43 | module.exports = new Proxy({}, {
|
44 | get: (_, prop) => {
|
45 | if (prop === '__esModule') {
|
46 | return true;
|
47 | }
|
48 |
|
49 | if (prop === 'then') {
|
50 | return Promise.resolve(FocusedComponent);
|
51 | }
|
52 |
|
53 | return FocusedComponent;
|
54 | },
|
55 | });
|
56 | `;
|
57 | };
|
58 |
|
59 | function sectionGroupName(file) {
|
60 | const parts = file.split(path_1.sep);
|
61 | return parts[parts.findIndex(part => part === 'sections') + 1];
|
62 | }
|
63 |
|
64 | function focusString(sections) {
|
65 | if (sections.length > 2) {
|
66 | return `${sections.slice(0, sections.length - 1).join(', ')}, and ${sections[sections.length - 1]}`;
|
67 | } else if (sections.length > 1) {
|
68 | return `${sections[0]} and ${sections[1]}`;
|
69 | } else {
|
70 | return sections[0];
|
71 | }
|
72 | } |
\ | No newline at end of file |