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

fix: don't include partial scss files in bundle #988

Merged
merged 2 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js",
"args": [
"--config=jasmine-config/jasmine.json"
]
],
"preLaunchTask": "npm:tsc"
},
{
"type": "node",
Expand Down
109 changes: 109 additions & 0 deletions bundle-config-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import bundleConfigLoader from "./bundle-config-loader";

const defaultLoaderOptions = {
angular: false,
loadCss: true,
unitTesting: false,
};

const defaultTestFiles = {
"./app-root.xml": true,
"./app-root.land.xml": true,
"./app.ts": true,
"./app.css": true,
"./app.android.scss": true,
"./app.ios.scss": true,
"./app.land.css": true,
"./components/my-component.css": true,
"./components/my-component.land.ts": true,
"./components/my-component.ts": true,
"./components/my-component.land.xml": true,
"./components/my-component.xml": true,
"./components/my-component.land.css": true,
"./main/main-page.land.css": true,
"./main/main-page.css": true,
"./main/main-page.ts": true,
"./main/main-page.land.xml": true,
"./main/main-page.xml": true,
"./main/main-page.land.ts": true,
"./main/main-page-vm.ts": true,

"./package.json": false,
"./app.d.ts": false,
"./_app-common.scss": false,
"./_app-variables.scss": false,
"./App_Resources/Android/src/main/res/values/colors.xml": false,
"./App_Resources/Android/src/main/AndroidManifest.xml": false,
};

const loaderOptionsWithIgnore = {
angular: false,
loadCss: true,
unitTesting: false,
ignoredFiles: ["./application", "./activity"]
};

const ignoredTestFiles = {
"./application.ts": false,
"./activity.ts": false,
}

function getRequireContextRegex(source: string): RegExp {
const requireContextStr = `require.context("~/", true, `;

expect(source).toContain(requireContextStr);

const start = source.indexOf(requireContextStr) + requireContextStr.length;
const end = source.indexOf(");\n", start);
const regexStr = source.substring(start, end);
const regex: RegExp = eval(regexStr);

expect(regex instanceof RegExp).toBeTruthy();
return regex;
}

function assertTestFilesAreMatched(testFiles: { [key: string]: boolean }, regex: RegExp) {
for (let fileName in testFiles) {
if (defaultTestFiles[fileName]) {
expect(fileName).toMatch(regex);
}
else {
expect(fileName).not.toMatch(regex);
}
}
}

describe("BundleConfigLoader should create require.context", () => {
it("default case", (done) => {

const loaderContext = {
callback: (error, source: string, map) => {
const regex = getRequireContextRegex(source);

assertTestFilesAreMatched(defaultTestFiles, regex);

done();
},
query: defaultLoaderOptions
}

bundleConfigLoader.call(loaderContext, " ___CODE___");
})

it("with ignored files", (done) => {

const loaderContext = {
callback: (error, source: string, map) => {
const regex = getRequireContextRegex(source);
const testFiles = { ...defaultTestFiles, ...ignoredTestFiles };

assertTestFilesAreMatched(testFiles, regex);

done();
},
query: loaderOptionsWithIgnore
}

bundleConfigLoader.call(loaderContext, " ___CODE___");
})
});
2 changes: 1 addition & 1 deletion bundle-config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getOptions } from "loader-utils";
import * as escapeRegExp from "escape-string-regexp";

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

const loader: loader.Loader = function (source, map) {
let {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"preuninstall": "node preuninstall.js",
"postpack": "rm -rf node_modules",
"prepare": "npm run tsc && npm run jasmine",
"test": "npm run prepare && npm run jasmine",
"test": "npm run prepare",
"jasmine": "jasmine --config=jasmine-config/jasmine.json",
"version": "rm package-lock.json && conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
},
Expand Down