diff --git a/index.js b/index.js index 8e86cb9b..39e3bb96 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,12 @@ exports.getAppPath = (platform, projectDir) => { } }; +exports.getEntryPathRegExp = (appFullPath, entryModule) => { + const entryModuleFullPath = path.join(appFullPath, entryModule); + // Windows paths contain `\`, so we need to convert each of the `\` to `\\`, so it will be correct inside RegExp + const escapedPath = entryModuleFullPath.replace(/\\/g, "\\\\"); + return new RegExp(escapedPath); +} /** * Converts an array of strings externals to an array of regular expressions. * Input is an array of string, which we need to convert to regular expressions, so all imports for this module will be treated as externals. diff --git a/index.spec.ts b/index.spec.ts index 4e5471bb..eda14f88 100644 --- a/index.spec.ts +++ b/index.spec.ts @@ -1,4 +1,5 @@ -import { getConvertedExternals } from './index'; +import { getConvertedExternals, getEntryPathRegExp } from './index'; +const path = require("path"); describe('index', () => { describe('getConvertedExternals', () => { @@ -51,4 +52,27 @@ describe('index', () => { }); }); }); + + describe('getEntryPathRegExp', () => { + const originalPathJoin = path.join; + const entryModule = "index.js"; + + afterEach(() => { + path.join = originalPathJoin; + }); + + it('returns RegExp that matches Windows', () => { + const appPath = "D:\\Work\\app1\\app"; + path.join = path.win32.join; + const regExp = getEntryPathRegExp(appPath, entryModule); + expect(!!regExp.exec(`${appPath}\\${entryModule}`)).toBe(true); + }); + + it('returns RegExp that works with POSIX paths', () => { + const appPath = "/usr/local/lib/app1/app"; + path.join = path.posix.join; + const regExp = getEntryPathRegExp(appPath, entryModule); + expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true); + }); + }); }); diff --git a/templates/webpack.angular.js b/templates/webpack.angular.js index dd22f5cb..2f196cb2 100644 --- a/templates/webpack.angular.js +++ b/templates/webpack.angular.js @@ -180,7 +180,7 @@ module.exports = env => { module: { rules: [ { - test: new RegExp(join(appFullPath, entryPath)), + test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath), use: [ // Require all Android app components platform === "android" && { diff --git a/templates/webpack.config.spec.ts b/templates/webpack.config.spec.ts index ee5a62dc..8a17cb6f 100644 --- a/templates/webpack.config.spec.ts +++ b/templates/webpack.config.spec.ts @@ -21,6 +21,7 @@ const nativeScriptDevWebpack = { getAppPath: () => 'app', getEntryModule: () => 'EntryModule', getResolver: () => null, + getEntryPathRegExp: () => null, getConvertedExternals: nsWebpackIndex.getConvertedExternals }; diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index 39df050d..a927d9bf 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -142,7 +142,7 @@ module.exports = env => { module: { rules: [ { - test: new RegExp(join(appFullPath, entryPath)), + test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath), use: [ // Require all Android app components platform === "android" && { diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index c3df62a4..730c38ee 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -144,7 +144,7 @@ module.exports = env => { module: { rules: [ { - test: new RegExp(join(appFullPath, entryPath)), + test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath), use: [ // Require all Android app components platform === "android" && { diff --git a/templates/webpack.vue.js b/templates/webpack.vue.js index ece42c8e..3ed688de 100644 --- a/templates/webpack.vue.js +++ b/templates/webpack.vue.js @@ -155,7 +155,7 @@ module.exports = env => { }, module: { rules: [{ - test: new RegExp(join(appFullPath, entryPath + ".(js|ts)")), + test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath + ".(js|ts)"), use: [ // Require all Android app components platform === "android" && {