Skip to content

Commit 8b81c6d

Browse files
gc-skyscannermungodewar
authored andcommitted
[SEARCHEXP-1358]Handle relative path and resolve to their real path (#164)
* handle relative path * remove console.log * handle relative path * unformat * update version * add unit test * revert change log --------- Co-authored-by: gc.zhu <[email protected]>
1 parent e5dd57a commit 8b81c6d

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"env": {
3+
"jest": true
4+
}
5+
}

packages/react-scripts/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ npm start
2727
- **`css.html` & `js.html`**: New files in the `build/` output folder. These are html partials that include `<script />` and `<link />` references to the various static assets output by webpack. Useful if automatic chunking is turned on and you don't want to worry about order.
2828
- A bunch of configuration options via `"backpack-react-scripts"` field in `package.json`:
2929
- `crossOriginLoading`: Modify the default behaviour, see [docs](https://webpack.js.org/configuration/output/#output-crossoriginloading).
30-
- `babelIncludePrefixes`: An array of module name prefixes to opt into babel compilation. Includes `["@skyscanner/bpk-", "bpk-", "saddlebag-"]` by default.
30+
- `babelIncludePrefixes`: An array of module name prefixes to opt into babel compilation, including local import module, e.g. `"../common"`. Includes `["@skyscanner/bpk-", "bpk-", "saddlebag-"]` by default.
3131
- `enableAutomaticChunking`: Boolean, opt in to automatic chunking of vendor, common and app code.
3232
- `vendorsChunkRegex`: String, Regex for picking what goes into the `vendors` chunk. See `cacheGroups` in webpack docs. Dependent on `enableAutomaticChunking` being enabled
3333
- `amdExcludes`: Array of module names to exclude from AMD parsing. Incldues `["lodash"]` by default.

packages/react-scripts/backpack-addons/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Our react scripts fork includes a number of custom configuration items in order
44

55
| Feature | Description | Default Value |
66
|:---|:--|:---|
7-
| **babelIncludePrefixes** | Array of module name prefixes to opt into babel compilation. <br> Default includes **@skyscanner/bpk-, bpk- and saddlebag-** packages by default to be compiled | **[@skyscanner/bpk-, bpk- and saddlebag-]** |
7+
| **babelIncludePrefixes** | Array of module name prefixes to opt into babel compilation, including local import module, e.g. `"../common"`. <br> Default includes **@skyscanner/bpk-, bpk- and saddlebag-** packages by default to be compiled | **[@skyscanner/bpk-, bpk- and saddlebag-]** |
88
| **sriEnabled** | Determines if Subresource Intergrity is used during build to add an integrity hash for files. <br> The SRI is a security feature to enable browsers to verify the files they fetch are unmodified. <br> If enabled crossOriginLoading value is overriden with anonymous to allow output to have integrity value <br> See [webpack subresource integrity docs](https://github.com/waysact/webpack-subresource-integrity/blob/master/README.md) | **false** (this is currently the default in the future security may want to make it true by default but pending them still trying things about) |
99
| **crossOriginLoading** | Option to enable cross origin loading of chunks to modify the default webpack behaviour of being false. <br> Docs: https://webpack.js.org/configuration/output/#outputcrossoriginloading | **false** |
1010
| **ignoreCssWarnings** | Provides the ablity to supress CSS ordering warnings when its safe and ordering is not of a concern on the output <br> See [mini css extract plugin docs](https://github.com/webpack-contrib/mini-css-extract-plugin#remove-order-warnings) | **false** - by default we should care about order as it can sometimes have an output impact |

packages/react-scripts/backpack-addons/babelIncludePrefixes.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
"use strict";
22

33
const paths = require("../config/paths");
4+
const path = require('path');
45
const appPackageJson = require(paths.appPackageJson);
56
const bpkReactScriptsConfig = appPackageJson["backpack-react-scripts"] || {};
67
const customModuleRegexes = bpkReactScriptsConfig.babelIncludePrefixes
7-
? bpkReactScriptsConfig.babelIncludePrefixes.map(
8-
(prefix) => new RegExp(`node_modules[\\/]${prefix}`)
9-
)
8+
? bpkReactScriptsConfig.babelIncludePrefixes.map(prefix => {
9+
if (prefix && /^(\.|\/)/.test(prefix)) {
10+
// if the prefixes starts with '.' or '/', e.g. '../common' './common' '/common'
11+
// That means it is a relative path which doesn't need to be in the node_modules folder
12+
return path.resolve(paths.appPath, prefix);
13+
}
14+
return new RegExp(`node_modules[\\/]${prefix}`);
15+
})
1016
: [];
1117

1218
// Backpack / saddlebag node module regexes
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const getBabelIncludePrefixes = require('../backpack-addons/babelIncludePrefixes');
3+
4+
jest.mock('../config/paths', () => ({
5+
appSrc: '/Users/xxx/backpack-react-scripts/client/src',
6+
appPackageJson: './test/mockPackage.json',
7+
appPath: '/Users/xxx/backpack-react-scripts/client',
8+
}));
9+
10+
describe('babelIncludePrefixes', () => {
11+
test('should handler babelIncludePrefixes correctly', () => {
12+
const babelIncludePrefixes = getBabelIncludePrefixes();
13+
14+
expect(babelIncludePrefixes).toEqual([
15+
'/Users/xxx/backpack-react-scripts/client/src',
16+
/node_modules[\\/]bpk-/,
17+
/node_modules[\\/]saddlebag-/,
18+
/node_modules[\\/]@skyscanner[\\/]bpk-/,
19+
/node_modules[\\/]@skyscanner[\\/]backpack-web/,
20+
new RegExp('node_modules[\\/]@skyscanner'),
21+
'/Users/xxx/backpack-react-scripts/client/common',
22+
'/Users/xxx/backpack-react-scripts/common',
23+
'/server',
24+
]);
25+
});
26+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "test",
3+
"version": "1.0.0",
4+
"backpack-react-scripts": {
5+
"babelIncludePrefixes": ["@skyscanner", "./common", "../common", "/server"]
6+
}
7+
}

0 commit comments

Comments
 (0)