From 1601caff4e175024248a548a39755d98b175705a Mon Sep 17 00:00:00 2001 From: Ikko Ashimine Date: Thu, 17 Feb 2022 00:07:43 +0900 Subject: [PATCH 01/10] chore: fix typo in moduleid-compat-loader.js (#9776) occurences -> occurrences --- packages/webpack/helpers/moduleid-compat-loader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/webpack/helpers/moduleid-compat-loader.js b/packages/webpack/helpers/moduleid-compat-loader.js index ec8ec963d..6ade9e7f7 100644 --- a/packages/webpack/helpers/moduleid-compat-loader.js +++ b/packages/webpack/helpers/moduleid-compat-loader.js @@ -6,11 +6,11 @@ module.exports = function (source, map) { this.cacheable(); - // Strips occurences of `moduleId: module.id,`, since it is no longer needed for webpack builds + // Strips occurrences of `moduleId: module.id,`, since it is no longer needed for webpack builds const noModuleIdsSource = source.replace(/moduleId\:\s*module\.id\s*(\,)?/g, result => // Try to preserve char count so sourcemaps may remain intact "/*" + result.substring(2, result.length - 2) + "*/" ); this.callback(null, noModuleIdsSource, map); -}; \ No newline at end of file +}; From cb7bd2a40193231dd49fc77a1ba1b183bdf2e074 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Fri, 18 Feb 2022 13:37:32 +0100 Subject: [PATCH 02/10] fix(webpack): exclude other platforms from require.context (#9686) fixes #9682 --- .../__snapshots__/angular.spec.ts.snap | 8 ++++++++ .../__snapshots__/base.spec.ts.snap | 8 ++++++++ .../__snapshots__/javascript.spec.ts.snap | 8 ++++++++ .../__snapshots__/react.spec.ts.snap | 16 ++++++++++++++++ .../__snapshots__/svelte.spec.ts.snap | 8 ++++++++ .../__snapshots__/typescript.spec.ts.snap | 8 ++++++++ .../configuration/__snapshots__/vue.spec.ts.snap | 8 ++++++++ packages/webpack5/src/configuration/base.ts | 15 ++++++++++++++- packages/webpack5/src/helpers/platform.ts | 9 ++++++++- 9 files changed, 86 insertions(+), 2 deletions(-) diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap index 51ea03a17..85e10c856 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap @@ -331,6 +331,10 @@ exports[`angular configuration for android 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -748,6 +752,10 @@ exports[`angular configuration for ios 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap index 658a93148..bc6600d08 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap @@ -242,6 +242,10 @@ exports[`base configuration for android 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -556,6 +560,10 @@ exports[`base configuration for ios 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap index 22ba2df01..a71dcd704 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap @@ -242,6 +242,10 @@ exports[`javascript configuration for android 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -565,6 +569,10 @@ exports[`javascript configuration for ios 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap index 501497ffe..923b82741 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap @@ -264,6 +264,10 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -589,6 +593,10 @@ exports[`react configuration > android > base config 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -921,6 +929,10 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -1247,6 +1259,10 @@ exports[`react configuration > ios > base config 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap index e55821c56..0d39260c5 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap @@ -269,6 +269,10 @@ exports[`svelte configuration for android 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -604,6 +608,10 @@ exports[`svelte configuration for ios 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap index e5cad4940..595dc6c2c 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap @@ -242,6 +242,10 @@ exports[`typescript configuration for android 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -565,6 +569,10 @@ exports[`typescript configuration for ios 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap index 87bf13417..a1dd43938 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap @@ -282,6 +282,10 @@ exports[`vue configuration for android 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(ios)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { @@ -630,6 +634,10 @@ exports[`vue configuration for ios 1`] = ` new ContextExclusionPlugin( /(.*)App_Resources(.*)/ ), + /* config.plugin('ContextExclusionPlugin|Other_Platforms') */ + new ContextExclusionPlugin( + /\\\\.(android)\\\\.(\\\\w+)$/ + ), /* config.plugin('DefinePlugin') */ new DefinePlugin( { diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 0c2659f6d..79cc6e049 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -22,8 +22,9 @@ import { env as _env, IWebpackEnv } from '../index'; import { getValue } from '../helpers/config'; import { getIPS } from '../helpers/host'; import { - getPlatformName, + getAvailablePlatforms, getAbsoluteDistPath, + getPlatformName, getEntryDirPath, getEntryPath, } from '../helpers/platform'; @@ -365,6 +366,18 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { .plugin('ContextExclusionPlugin|App_Resources') .use(ContextExclusionPlugin, [new RegExp(`(.*)App_Resources(.*)`)]); + // Makes sure that require.context will never include code from + // another platform (ie .android.ts when building for ios) + const otherPlatformsRE = getAvailablePlatforms() + .filter((platform) => platform !== getPlatformName()) + .join('|'); + + config + .plugin('ContextExclusionPlugin|Other_Platforms') + .use(ContextExclusionPlugin, [ + new RegExp(`\\.(${otherPlatformsRE})\\.(\\w+)$`), + ]); + // Filter common undesirable warnings config.set( 'ignoreWarnings', diff --git a/packages/webpack5/src/helpers/platform.ts b/packages/webpack5/src/helpers/platform.ts index f59389af1..1e07b62b3 100644 --- a/packages/webpack5/src/helpers/platform.ts +++ b/packages/webpack5/src/helpers/platform.ts @@ -40,6 +40,13 @@ export function getPlatform(): INativeScriptPlatform { return platforms[getPlatformName()]; } +/** + * Utility to get all registered/available platforms + */ +export function getAvailablePlatforms(): string[] { + return Object.keys(platforms); +} + /** * Utility to get the currently targeted platform name */ @@ -61,7 +68,7 @@ export function getPlatformName(): Platform { throw error(` Invalid platform: ${env.platform} - Valid platforms: ${Object.keys(platforms).join(', ')} + Valid platforms: ${getAvailablePlatforms().join(', ')} `); } From 61ff7e47622d0464b7f32b1ae7a6df1ce65141de Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Fri, 18 Feb 2022 13:39:04 +0100 Subject: [PATCH 03/10] feat(webpack): read nativescript.config.ts main if set before fallback to package.json (#9769) implements #9658 BREAKING CHANGES: Possibly breaking if a project has a main field set in the nativescript.config.ts - since after this lands, this value will be used instead of package.json main. The impact is likely very small, the steps to migrate: (Option A) remove main from nativescript.config.ts if set (Option B) update main to the correct path in nativescript.config.ts if set incorrectly --- packages/core/config/config.interface.ts | 2 +- .../__tests__/helpers/platform.spec.ts | 49 +++++++++++++++++++ packages/webpack5/scripts/jest.setup.ts | 6 +-- packages/webpack5/src/helpers/config.ts | 10 ++-- packages/webpack5/src/helpers/platform.ts | 8 +++ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 packages/webpack5/__tests__/helpers/platform.spec.ts diff --git a/packages/core/config/config.interface.ts b/packages/core/config/config.interface.ts index 996979e6c..f12e6bd86 100644 --- a/packages/core/config/config.interface.ts +++ b/packages/core/config/config.interface.ts @@ -147,7 +147,7 @@ export interface NativeScriptConfig { */ id?: string; /** - * App's main entry file (currently ignored - set it in package.json main field) + * App's main entry file - this setting overrides the value set in package.json */ main?: string; /** diff --git a/packages/webpack5/__tests__/helpers/platform.spec.ts b/packages/webpack5/__tests__/helpers/platform.spec.ts new file mode 100644 index 000000000..58424c525 --- /dev/null +++ b/packages/webpack5/__tests__/helpers/platform.spec.ts @@ -0,0 +1,49 @@ +import { env } from '../../src/'; +import { addPlatform, getEntryPath } from '../../src/helpers/platform'; + +import { getValue } from '../../src/helpers/config'; + +describe('getEntryPath', () => { + it('uses platform getEntryPath if the platform specifies it', () => { + env.platform = 'testPlatform'; + addPlatform('testPlatform', { + getEntryPath() { + return 'custom-entry-path'; + }, + }); + + const res = getEntryPath(); + expect(res).toEqual('custom-entry-path'); + + // cleanup env + delete env.platform; + }); + + it('uses main from nativescript.config.ts if set', () => { + env.ios = true; + + // mock getValue + const getValueMock = getValue as jest.Mock; + const getValueMockImpl = getValueMock.getMockImplementation(); + + getValueMock.mockImplementation((key) => { + if (key === 'main') { + return 'main-from-config'; + } + }); + + const res = getEntryPath(); + expect(res).toEqual('__jest__/main-from-config'); + + // reset mock implementation + getValueMock.mockImplementation(getValueMockImpl); + }); + + it('uses main from package.json', () => { + env.ios = true; + + const res = getEntryPath(); + // set in jest.setup.ts mock for package.json... + expect(res).toEqual('__jest__/src/app.js'); + }); +}); diff --git a/packages/webpack5/scripts/jest.setup.ts b/packages/webpack5/scripts/jest.setup.ts index 65a90cfae..3f2ba5d44 100644 --- a/packages/webpack5/scripts/jest.setup.ts +++ b/packages/webpack5/scripts/jest.setup.ts @@ -13,10 +13,10 @@ jest.mock('cosmiconfig', () => ({ }, })); +const getValueMock = jest.fn(); +getValueMock.mockImplementation((key, defaultValue) => defaultValue); jest.mock('../src/helpers/config.ts', () => ({ - getValue(key, defaultValue) { - return defaultValue; - }, + getValue: getValueMock, })); jest.mock('os', () => { diff --git a/packages/webpack5/src/helpers/config.ts b/packages/webpack5/src/helpers/config.ts index ae649ead9..221d1feee 100644 --- a/packages/webpack5/src/helpers/config.ts +++ b/packages/webpack5/src/helpers/config.ts @@ -1,5 +1,5 @@ +import { warnOnce } from './log'; import { env } from '../index'; -import { error, warnOnce } from './log'; function getCLILib() { if (!env.nativescriptLibPath) { @@ -28,7 +28,9 @@ export function getValue(key: string, defaultValue?: any): T { return defaultValue; } - return (lib.projectConfigService as { - getValue(key: string, defaultValue?: any): T; - }).getValue(key, defaultValue); + return ( + lib.projectConfigService as { + getValue(key: string, defaultValue?: any): T; + } + ).getValue(key, defaultValue); } diff --git a/packages/webpack5/src/helpers/platform.ts b/packages/webpack5/src/helpers/platform.ts index 1e07b62b3..f077416d7 100644 --- a/packages/webpack5/src/helpers/platform.ts +++ b/packages/webpack5/src/helpers/platform.ts @@ -2,6 +2,7 @@ import { dirname, resolve } from 'path'; import { getPackageJson, getProjectRootPath } from './project'; import { error, info, warnOnce } from './log'; +import { getValue } from './config'; import { env } from '../'; import AndroidPlatform from '../platforms/android'; @@ -99,6 +100,13 @@ export function getEntryPath() { return platform.getEntryPath(); } + // try main from nativescript.config.ts + const main = getValue('main'); + + if (main) { + return resolve(getProjectRootPath(), main); + } + // fallback to main field in package.json const packageJson = getPackageJson(); From 9c9c831ac015c2bb1f6f72d09d1c9b7fed6659d6 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Fri, 18 Feb 2022 13:39:28 +0100 Subject: [PATCH 04/10] feat(webpack): allow watching node_modules (#9781) --- .../configuration/__snapshots__/base.spec.ts.snap | 6 ++++++ packages/webpack5/__tests__/configuration/base.spec.ts | 8 ++++++++ packages/webpack5/src/configuration/base.ts | 7 +++++++ packages/webpack5/src/index.ts | 1 + 4 files changed, 22 insertions(+) diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap index bc6600d08..ad09e5509 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap @@ -636,3 +636,9 @@ exports[`base configuration for ios 1`] = ` } }" `; + +exports[`base configuration support env.watchNodeModules 1`] = ` +Object { + "managedPaths": Array [], +} +`; diff --git a/packages/webpack5/__tests__/configuration/base.spec.ts b/packages/webpack5/__tests__/configuration/base.spec.ts index df76f6ffd..35e6f9fe4 100644 --- a/packages/webpack5/__tests__/configuration/base.spec.ts +++ b/packages/webpack5/__tests__/configuration/base.spec.ts @@ -22,6 +22,14 @@ describe('base configuration', () => { }); } + it('support env.watchNodeModules', () => { + init({ + ios: true, + watchNodeModules: true, + }); + expect(base(new Config()).get('snapshot')).toMatchSnapshot(); + }); + it('supports dotenv', () => { const fsSpy = jest.spyOn(fs, 'existsSync'); fsSpy.mockReturnValue(true); diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 79cc6e049..7019de400 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -124,6 +124,13 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { ], }); + // allow watching node_modules + config.when(env.watchNodeModules, (config) => { + config.set('snapshot', { + managedPaths: [], + }); + }); + // Set up Terser options config.optimization.minimizer('TerserPlugin').use(TerserPlugin, [ { diff --git a/packages/webpack5/src/index.ts b/packages/webpack5/src/index.ts index a8ca9ffe7..eb77451cb 100644 --- a/packages/webpack5/src/index.ts +++ b/packages/webpack5/src/index.ts @@ -45,6 +45,7 @@ export interface IWebpackEnv { // misc replace?: string[] | string; + watchNodeModules?: boolean; } interface IChainEntry { From 4fa51b60648fa76b6b25847c64c66678817ee0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20KLINGLER?= Date: Fri, 18 Feb 2022 13:58:44 +0100 Subject: [PATCH 05/10] feat(webpack): add support for .mjs files (#9747) Co-authored-by: Igor Randjelovic --- .../__snapshots__/angular.spec.ts.snap | 8 ++++++-- .../__snapshots__/base.spec.ts.snap | 8 ++++++-- .../__snapshots__/javascript.spec.ts.snap | 8 ++++++-- .../__snapshots__/react.spec.ts.snap | 16 ++++++++++++---- .../__snapshots__/svelte.spec.ts.snap | 4 ++++ .../__snapshots__/typescript.spec.ts.snap | 8 ++++++-- .../configuration/__snapshots__/vue.spec.ts.snap | 8 ++++++-- packages/webpack5/src/configuration/base.ts | 4 +++- 8 files changed, 49 insertions(+), 15 deletions(-) diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap index 85e10c856..d6ad59889 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap @@ -48,6 +48,8 @@ exports[`angular configuration for android 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -89,7 +91,7 @@ exports[`angular configuration for android 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -469,6 +471,8 @@ exports[`angular configuration for ios 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -510,7 +514,7 @@ exports[`angular configuration for ios 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap index ad09e5509..e6e48102c 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap @@ -40,6 +40,8 @@ exports[`base configuration for android 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -77,7 +79,7 @@ exports[`base configuration for android 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -358,6 +360,8 @@ exports[`base configuration for ios 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -395,7 +399,7 @@ exports[`base configuration for ios 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap index a71dcd704..098dd5a2e 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap @@ -40,6 +40,8 @@ exports[`javascript configuration for android 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -77,7 +79,7 @@ exports[`javascript configuration for android 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -367,6 +369,8 @@ exports[`javascript configuration for ios 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -404,7 +408,7 @@ exports[`javascript configuration for ios 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap index 923b82741..92c19dea8 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap @@ -43,6 +43,8 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -87,7 +89,7 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -390,6 +392,8 @@ exports[`react configuration > android > base config 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -427,7 +431,7 @@ exports[`react configuration > android > base config 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -708,6 +712,8 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -752,7 +758,7 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -1056,6 +1062,8 @@ exports[`react configuration > ios > base config 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -1093,7 +1101,7 @@ exports[`react configuration > ios > base config 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap index 0d39260c5..76398655e 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap @@ -44,6 +44,8 @@ exports[`svelte configuration for android 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -383,6 +385,8 @@ exports[`svelte configuration for ios 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap index 595dc6c2c..fd5828fc1 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap @@ -40,6 +40,8 @@ exports[`typescript configuration for android 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -77,7 +79,7 @@ exports[`typescript configuration for android 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -367,6 +369,8 @@ exports[`typescript configuration for ios 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -404,7 +408,7 @@ exports[`typescript configuration for ios 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap index a1dd43938..f5c5ae2e3 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap @@ -43,6 +43,8 @@ exports[`vue configuration for android 1`] = ` '.ts', '.android.js', '.js', + '.android.mjs', + '.mjs', '.android.css', '.css', '.android.scss', @@ -80,7 +82,7 @@ exports[`vue configuration for android 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { @@ -395,6 +397,8 @@ exports[`vue configuration for ios 1`] = ` '.ts', '.ios.js', '.js', + '.ios.mjs', + '.mjs', '.ios.css', '.css', '.ios.scss', @@ -432,7 +436,7 @@ exports[`vue configuration for ios 1`] = ` }, /* config.module.rule('workers') */ { - test: /\\\\.(js|ts)$/, + test: /\\\\.(mjs|js|ts)$/, use: [ /* config.module.rule('workers').use('nativescript-worker-loader') */ { diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 7019de400..842f28ea4 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -180,6 +180,8 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { .add('.ts') .add(`.${platform}.js`) .add('.js') + .add(`.${platform}.mjs`) + .add('.mjs') .add(`.${platform}.css`) .add('.css') .add(`.${platform}.scss`) @@ -223,7 +225,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { // worker-loader should be declared before ts-loader config.module .rule('workers') - .test(/\.(js|ts)$/) + .test(/\.(mjs|js|ts)$/) .use('nativescript-worker-loader') .loader('nativescript-worker-loader'); From 0a7318f430e948f6aac2bdfd2192f4fd0f1b3c0e Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Sun, 20 Feb 2022 16:13:57 +0100 Subject: [PATCH 06/10] chore: update feature_request template labels --- .github/ISSUE_TEMPLATE/feature_request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE/feature_request.yaml b/.github/ISSUE_TEMPLATE/feature_request.yaml index 19c5528be..64c04bb7a 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yaml +++ b/.github/ISSUE_TEMPLATE/feature_request.yaml @@ -1,6 +1,6 @@ name: 🚀 Feature request description: Suggest an idea for NativeScript -labels: ["enhancement-pending-triage"] +labels: ["feature-pending-triage"] body: - type: markdown From d3674de88212db48409dd476e5450f4e7d8df0f0 Mon Sep 17 00:00:00 2001 From: farfromrefuge Date: Tue, 1 Mar 2022 21:34:28 +0100 Subject: [PATCH 07/10] feat: allow better tree shaking (#9798) --- packages/core/ui/styling/css-selector/index.ts | 16 ++++++++-------- packages/core/ui/styling/linear-gradient.ts | 2 +- packages/core/ui/styling/style-properties.ts | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/core/ui/styling/css-selector/index.ts b/packages/core/ui/styling/css-selector/index.ts index b661399fa..4f8b6556d 100644 --- a/packages/core/ui/styling/css-selector/index.ts +++ b/packages/core/ui/styling/css-selector/index.ts @@ -3,7 +3,7 @@ import { isCssVariable } from '../../core/properties'; import { isNullOrUndefined } from '../../../utils/types'; import * as cssParser from '../../../css'; -import * as parser from '../../../css/parser'; +import { Combinator as ICombinator , SimpleSelectorSequence as ISimpleSelectorSequence, Selector as ISelector, SimpleSelector as ISimpleSelector, parseSelector} from '../../../css/parser'; /** * An interface describing the shape of a type on which the selectors may apply. @@ -527,7 +527,7 @@ function createDeclaration(decl: cssParser.Declaration): any { return { property: isCssVariable(decl.property) ? decl.property : decl.property.toLowerCase(), value: decl.value }; } -function createSimpleSelectorFromAst(ast: parser.SimpleSelector): SimpleSelector { +function createSimpleSelectorFromAst(ast: ISimpleSelector): SimpleSelector { if (ast.type === '.') { return new ClassSelector(ast.identifier); } @@ -553,7 +553,7 @@ function createSimpleSelectorFromAst(ast: parser.SimpleSelector): SimpleSelector } } -function createSimpleSelectorSequenceFromAst(ast: parser.SimpleSelectorSequence): SimpleSelectorSequence | SimpleSelector { +function createSimpleSelectorSequenceFromAst(ast: ISimpleSelectorSequence): SimpleSelectorSequence | SimpleSelector { if (ast.length === 0) { return new InvalidSelector(new Error('Empty simple selector sequence.')); } else if (ast.length === 1) { @@ -563,7 +563,7 @@ function createSimpleSelectorSequenceFromAst(ast: parser.SimpleSelectorSequence) } } -function createSelectorFromAst(ast: parser.Selector): SimpleSelector | SimpleSelectorSequence | Selector { +function createSelectorFromAst(ast: ISelector): SimpleSelector | SimpleSelectorSequence | Selector { if (ast.length === 0) { return new InvalidSelector(new Error('Empty selector.')); } else if (ast.length === 1) { @@ -571,10 +571,10 @@ function createSelectorFromAst(ast: parser.Selector): SimpleSelector | SimpleSel } else { const simpleSelectorSequences = []; let simpleSelectorSequence: SimpleSelectorSequence | SimpleSelector; - let combinator: parser.Combinator; + let combinator: ICombinator; for (let i = 0; i < ast.length; i++) { - simpleSelectorSequence = createSimpleSelectorSequenceFromAst(ast[i][0]); - combinator = ast[i][1]; + simpleSelectorSequence = createSimpleSelectorSequenceFromAst(ast[i][0]); + combinator = ast[i][1]; if (combinator) { simpleSelectorSequence.combinator = combinator; } @@ -587,7 +587,7 @@ function createSelectorFromAst(ast: parser.Selector): SimpleSelector | SimpleSel export function createSelector(sel: string): SimpleSelector | SimpleSelectorSequence | Selector { try { - const parsedSelector = parser.parseSelector(sel); + const parsedSelector = parseSelector(sel); if (!parsedSelector) { return new InvalidSelector(new Error('Empty selector')); } diff --git a/packages/core/ui/styling/linear-gradient.ts b/packages/core/ui/styling/linear-gradient.ts index 72b3cad6a..04a6fb6bc 100644 --- a/packages/core/ui/styling/linear-gradient.ts +++ b/packages/core/ui/styling/linear-gradient.ts @@ -1,7 +1,7 @@ import { CoreTypes } from '../../core-types'; import { Color } from '../../color'; import { ColorStop } from './gradient'; -import { LinearGradient as CSSLinearGradient } from '../../css/parser'; +import type { LinearGradient as CSSLinearGradient } from '../../css/parser'; export class LinearGradient { public angle: number; diff --git a/packages/core/ui/styling/style-properties.ts b/packages/core/ui/styling/style-properties.ts index e57310201..8e04803b1 100644 --- a/packages/core/ui/styling/style-properties.ts +++ b/packages/core/ui/styling/style-properties.ts @@ -14,7 +14,7 @@ import { decompose2DTransformMatrix, getTransformMatrix, matrixArrayToCssMatrix, import { Trace } from '../../trace'; import { CoreTypes } from '../../core-types'; -import * as parser from '../../css/parser'; +import { parseBackground } from '../../css/parser'; import { LinearGradient } from './linear-gradient'; import { CSSShadow, parseCSSShadow } from './css-shadow'; @@ -785,7 +785,7 @@ export const backgroundImageProperty = new CssProperty { if (typeof value === 'string') { - const parsed = parser.parseBackground(value); + const parsed = parseBackground(value); if (parsed) { value = typeof parsed.value.image === 'object' ? LinearGradient.parse(parsed.value.image) : value; } @@ -837,7 +837,7 @@ backgroundPositionProperty.register(Style); function convertToBackgrounds(this: void, value: string): [CssProperty, any][] { if (typeof value === 'string') { - const backgrounds = parser.parseBackground(value).value; + const backgrounds = parseBackground(value).value; let backgroundColor = unsetValue; if (backgrounds.color) { backgroundColor = backgrounds.color instanceof Color ? backgrounds.color : new Color(backgrounds.color); From 151d6e8c85641a0d5ad5bf7d3342c36ae7fca608 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Mar 2022 09:38:27 +0100 Subject: [PATCH 08/10] fix(webpack): xml-namespace-loader incorrect dependency mapping (#9780) --- .../src/loaders/xml-namespace-loader/index.ts | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/webpack5/src/loaders/xml-namespace-loader/index.ts b/packages/webpack5/src/loaders/xml-namespace-loader/index.ts index edb3fbf68..585f989a7 100644 --- a/packages/webpack5/src/loaders/xml-namespace-loader/index.ts +++ b/packages/webpack5/src/loaders/xml-namespace-loader/index.ts @@ -75,16 +75,22 @@ async function parseXML(content: string): Promise { const resolvePaths = [ localNamespacePath, localModulePath, - `${localModulePath}.xml`, moduleName, namespace, - `${moduleName}.xml`, `~/${moduleName}`, `~/${namespace}`, + ]; + + // fallbacks for codeless namespaces + const fallbackResolvePaths = [ + `${localModulePath}.xml`, + `${moduleName}.xml`, `~/${moduleName}.xml`, ]; - DEBUG && console.log({ resolvePaths }); + + DEBUG && console.log({ resolvePaths, fallbackResolvePaths }); let resolvedPath; + let isFallbackPath = false; for (const p of resolvePaths) { resolvedPath = await resolveAsync(this.context, p).catch(noop); @@ -95,7 +101,23 @@ async function parseXML(content: string): Promise { } } - DEBUG && console.log({ resolvedPath }); + if (!resolvedPath) { + for (const p of fallbackResolvePaths) { + resolvedPath = await resolveAsync(this.context, p).catch(noop); + + // break on first match + if (resolvedPath) { + isFallbackPath = true; + break; + } + } + } + + DEBUG && + console.log({ + resolvedPath, + isFallbackPath, + }); // bail if we haven't resolved a path if (!resolvedPath) { @@ -104,10 +126,15 @@ async function parseXML(content: string): Promise { const { dir, name } = parse(resolvedPath); - // register resolved path + short name - namespaces.push({ name: namespace, path: resolvedPath }); - namespaces.push({ name: moduleName, path: resolvedPath }); - this.addDependency(resolvedPath); + DEBUG && console.log({ namespace, moduleName }); + + // check if we are not in a fallback path, in which case we shouldn't register it as a namespace + if (!isFallbackPath) { + // register resolved path + short name + namespaces.push({ name: namespace, path: resolvedPath }); + namespaces.push({ name: moduleName, path: resolvedPath }); + this.addDependency(resolvedPath); + } const noExtFilename = join(dir, name); From af4b7e3b33fc76c0fcaa63572fa5fd548bd351a2 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Mar 2022 10:22:28 +0100 Subject: [PATCH 09/10] feat: support webpack profiling (#9804) Co-authored-by: Martin Guillon --- .../__tests__/configuration/base.spec.ts | 10 ++++++++ packages/webpack5/src/bin/index.ts | 24 ++++++++++++++++++- packages/webpack5/src/configuration/base.ts | 5 ++++ packages/webpack5/src/index.ts | 3 +++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/webpack5/__tests__/configuration/base.spec.ts b/packages/webpack5/__tests__/configuration/base.spec.ts index 35e6f9fe4..9f6ffeac7 100644 --- a/packages/webpack5/__tests__/configuration/base.spec.ts +++ b/packages/webpack5/__tests__/configuration/base.spec.ts @@ -109,4 +109,14 @@ describe('base configuration', () => { force: true, }); }); + + it('supports --env.profile', () => { + init({ + platform: 'ios', + profile: true, + }); + const config = base(new Config()); + + expect(config.get('profile')).toBe(true); + }); }); diff --git a/packages/webpack5/src/bin/index.ts b/packages/webpack5/src/bin/index.ts index b8e1656bb..87ceed551 100644 --- a/packages/webpack5/src/bin/index.ts +++ b/packages/webpack5/src/bin/index.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node -import { redBright, green, greenBright } from 'chalk'; +import { redBright, green, greenBright, yellow } from 'chalk'; import { program } from 'commander'; import dedent from 'ts-dedent'; import webpack from 'webpack'; @@ -115,6 +115,28 @@ program errorDetails: env.verbose, }) ); + + // if webpack profile is enabled we write the stats to a JSON file + if (configuration.profile || env.profile) { + console.log( + [ + '', + '|', + `| The build profile has been written to ${yellow( + 'webpack.stats.json' + )}`, + `| You can analyse the stats at ${green( + 'https://webpack.github.io/analyse/' + )}`, + '|', + '', + ].join('\n') + ); + fs.writeFileSync( + path.join(process.cwd(), 'webpack.stats.json'), + JSON.stringify(stats.toJson()) + ); + } } }; diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 842f28ea4..d8548711f 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -222,6 +222,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { }); }); + // enable profiling with --env.profile + config.when(env.profile, (config) => { + config.profile(true); + }); + // worker-loader should be declared before ts-loader config.module .rule('workers') diff --git a/packages/webpack5/src/index.ts b/packages/webpack5/src/index.ts index eb77451cb..47ecb41ce 100644 --- a/packages/webpack5/src/index.ts +++ b/packages/webpack5/src/index.ts @@ -43,6 +43,9 @@ export interface IWebpackEnv { // enable verbose output verbose?: boolean; + // enable webpack profiling + profile?: boolean; + // misc replace?: string[] | string; watchNodeModules?: boolean; From 028bfc4f1cf3854ac28140cd64bf8587a4a2f486 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Mar 2022 13:04:07 +0100 Subject: [PATCH 10/10] chore(release): @nativescript/webpack 5.0.5-rc.0 --- packages/webpack5/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack5/package.json b/packages/webpack5/package.json index 565c50ff0..d551b1505 100644 --- a/packages/webpack5/package.json +++ b/packages/webpack5/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/webpack", - "version": "5.0.4", + "version": "5.0.5-rc.0", "private": false, "main": "dist/index.js", "files": [