From 5f74e915722ce99c3734ddfc5aeb352f6720a366 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Mon, 15 Jul 2019 18:21:01 +0300 Subject: [PATCH 1/2] fix: don't include partial scss files in bundle --- .vscode/launch.json | 3 +- bundle-config-loader.spec.ts | 109 +++++++++++++++++++++++++++++++++++ bundle-config-loader.ts | 2 +- package.json | 2 +- 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 bundle-config-loader.spec.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 3cd1efa9..5c1dca3e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,8 @@ "program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js", "args": [ "--config=jasmine-config/jasmine.json" - ] + ], + "preLaunchTask": "npm:tsc" }, { "type": "node", diff --git a/bundle-config-loader.spec.ts b/bundle-config-loader.spec.ts new file mode 100644 index 00000000..3098dc69 --- /dev/null +++ b/bundle-config-loader.spec.ts @@ -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___"); + }) +}); diff --git a/bundle-config-loader.ts b/bundle-config-loader.ts index f1f0458a..83c4738a 100644 --- a/bundle-config-loader.ts +++ b/bundle-config-loader.ts @@ -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 = "(? Date: Tue, 16 Jul 2019 08:52:51 +0300 Subject: [PATCH 2/2] fix: imporve regex after review --- bundle-config-loader.spec.ts | 19 ++++++++++++------- bundle-config-loader.ts | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/bundle-config-loader.spec.ts b/bundle-config-loader.spec.ts index 3098dc69..ec8ad99b 100644 --- a/bundle-config-loader.spec.ts +++ b/bundle-config-loader.spec.ts @@ -28,12 +28,17 @@ const defaultTestFiles = { "./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, + "./app_component.scss": true, + "./App_Resources123/some-file.xml": true, + "./MyApp_Resources/some-file.xml": true, + "./App_Resources_Nobody_Names_Folders_Like_That_Roska/some-file.xml": true, + + "./package.json": false, // do not include package.json files + "./app.d.ts": false, // do not include ts definitions + "./_app-common.scss": false, // do not include scss partial files + "./_app-variables.scss": false, // do not include scss partial files + "./App_Resources/Android/src/main/res/values/colors.xml": false, // do not include App_Resources + "./App_Resources/Android/src/main/AndroidManifest.xml": false, // do not include App_Resources }; const loaderOptionsWithIgnore = { @@ -96,7 +101,7 @@ describe("BundleConfigLoader should create require.context", () => { callback: (error, source: string, map) => { const regex = getRequireContextRegex(source); const testFiles = { ...defaultTestFiles, ...ignoredTestFiles }; - + assertTestFilesAreMatched(testFiles, regex); done(); diff --git a/bundle-config-loader.ts b/bundle-config-loader.ts index 83c4738a..6732058c 100644 --- a/bundle-config-loader.ts +++ b/bundle-config-loader.ts @@ -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 = "(?