Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 786bd6c

Browse files
author
Alexander Vakrilov
authored
fix: don't include partial scss files in bundle (#988)
* fix: don't include partial scss files in bundle * fix: imporve regex after review
1 parent cf74631 commit 786bd6c

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

.vscode/launch.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js",
99
"args": [
1010
"--config=jasmine-config/jasmine.json"
11-
]
11+
],
12+
"preLaunchTask": "npm:tsc"
1213
},
1314
{
1415
"type": "node",

bundle-config-loader.spec.ts

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import bundleConfigLoader from "./bundle-config-loader";
2+
3+
const defaultLoaderOptions = {
4+
angular: false,
5+
loadCss: true,
6+
unitTesting: false,
7+
};
8+
9+
const defaultTestFiles = {
10+
"./app-root.xml": true,
11+
"./app-root.land.xml": true,
12+
"./app.ts": true,
13+
"./app.css": true,
14+
"./app.android.scss": true,
15+
"./app.ios.scss": true,
16+
"./app.land.css": true,
17+
"./components/my-component.css": true,
18+
"./components/my-component.land.ts": true,
19+
"./components/my-component.ts": true,
20+
"./components/my-component.land.xml": true,
21+
"./components/my-component.xml": true,
22+
"./components/my-component.land.css": true,
23+
"./main/main-page.land.css": true,
24+
"./main/main-page.css": true,
25+
"./main/main-page.ts": true,
26+
"./main/main-page.land.xml": true,
27+
"./main/main-page.xml": true,
28+
"./main/main-page.land.ts": true,
29+
"./main/main-page-vm.ts": true,
30+
31+
"./app_component.scss": true,
32+
"./App_Resources123/some-file.xml": true,
33+
"./MyApp_Resources/some-file.xml": true,
34+
"./App_Resources_Nobody_Names_Folders_Like_That_Roska/some-file.xml": true,
35+
36+
"./package.json": false, // do not include package.json files
37+
"./app.d.ts": false, // do not include ts definitions
38+
"./_app-common.scss": false, // do not include scss partial files
39+
"./_app-variables.scss": false, // do not include scss partial files
40+
"./App_Resources/Android/src/main/res/values/colors.xml": false, // do not include App_Resources
41+
"./App_Resources/Android/src/main/AndroidManifest.xml": false, // do not include App_Resources
42+
};
43+
44+
const loaderOptionsWithIgnore = {
45+
angular: false,
46+
loadCss: true,
47+
unitTesting: false,
48+
ignoredFiles: ["./application", "./activity"]
49+
};
50+
51+
const ignoredTestFiles = {
52+
"./application.ts": false,
53+
"./activity.ts": false,
54+
}
55+
56+
function getRequireContextRegex(source: string): RegExp {
57+
const requireContextStr = `require.context("~/", true, `;
58+
59+
expect(source).toContain(requireContextStr);
60+
61+
const start = source.indexOf(requireContextStr) + requireContextStr.length;
62+
const end = source.indexOf(");\n", start);
63+
const regexStr = source.substring(start, end);
64+
const regex: RegExp = eval(regexStr);
65+
66+
expect(regex instanceof RegExp).toBeTruthy();
67+
return regex;
68+
}
69+
70+
function assertTestFilesAreMatched(testFiles: { [key: string]: boolean }, regex: RegExp) {
71+
for (let fileName in testFiles) {
72+
if (defaultTestFiles[fileName]) {
73+
expect(fileName).toMatch(regex);
74+
}
75+
else {
76+
expect(fileName).not.toMatch(regex);
77+
}
78+
}
79+
}
80+
81+
describe("BundleConfigLoader should create require.context", () => {
82+
it("default case", (done) => {
83+
84+
const loaderContext = {
85+
callback: (error, source: string, map) => {
86+
const regex = getRequireContextRegex(source);
87+
88+
assertTestFilesAreMatched(defaultTestFiles, regex);
89+
90+
done();
91+
},
92+
query: defaultLoaderOptions
93+
}
94+
95+
bundleConfigLoader.call(loaderContext, " ___CODE___");
96+
})
97+
98+
it("with ignored files", (done) => {
99+
100+
const loaderContext = {
101+
callback: (error, source: string, map) => {
102+
const regex = getRequireContextRegex(source);
103+
const testFiles = { ...defaultTestFiles, ...ignoredTestFiles };
104+
105+
assertTestFilesAreMatched(testFiles, regex);
106+
107+
done();
108+
},
109+
query: loaderOptionsWithIgnore
110+
}
111+
112+
bundleConfigLoader.call(loaderContext, " ___CODE___");
113+
})
114+
});

bundle-config-loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getOptions } from "loader-utils";
44
import * as escapeRegExp from "escape-string-regexp";
55

66
// Matches all source, markup and style files that are not in App_Resources
7-
const defaultMatch = "(?<!App_Resources.*)\.(xml|css|js|(?<!d\.)ts|scss)$";
7+
const defaultMatch = "(?<!\\bApp_Resources\\b.*)\\.(xml|css|js|(?<!\\.d\\.)ts|(?<!\\b_[\\w-]*\\.)scss)$";
88

99
const loader: loader.Loader = function (source, map) {
1010
let {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"preuninstall": "node preuninstall.js",
3434
"postpack": "rm -rf node_modules",
3535
"prepare": "npm run tsc && npm run jasmine",
36-
"test": "npm run prepare && npm run jasmine",
36+
"test": "npm run prepare",
3737
"jasmine": "jasmine --config=jasmine-config/jasmine.json",
3838
"version": "rm package-lock.json && conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
3939
},

0 commit comments

Comments
 (0)