refactor(webpack): use real modules and deprecate virtual modules

This commit is contained in:
Igor Randjelovic
2022-07-10 11:34:23 +02:00
committed by Nathan Walker
parent 2fbc1898db
commit 0556cf9b20
8 changed files with 46 additions and 83 deletions

View File

@ -302,16 +302,6 @@ exports[`javascript configuration for android 1`] = `
), ),
/* config.plugin('WatchStatePlugin') */ /* config.plugin('WatchStatePlugin') */
new WatchStatePlugin(), new WatchStatePlugin(),
/* config.plugin('ContextExclusionPlugin|__@nativescript_webpack_virtual_entry_javascript__') */
new ContextExclusionPlugin(
/__@nativescript_webpack_virtual_entry_javascript__.js$/
),
/* config.plugin('VirtualModulesPlugin') */
new VirtualModulesPlugin(
{
'__jest__/src/__@nativescript_webpack_virtual_entry_javascript__': '// VIRTUAL ENTRY START\\\\nrequire(\\\\'@nativescript/core/bundle-entry-points\\\\')\\\\nconst context = require.context(\\"~/\\", /* deep: */ true, /* filter: */ /.(xml|js|s?css)$/);\\\\nglobal.registerWebpackModules(context);\\\\n// VIRTUAL ENTRY END'
}
),
/* config.plugin('ContextExclusionPlugin|exclude_files') */ /* config.plugin('ContextExclusionPlugin|exclude_files') */
new ContextExclusionPlugin( new ContextExclusionPlugin(
/\\\\b_.+\\\\./ /\\\\b_.+\\\\./
@ -320,7 +310,7 @@ exports[`javascript configuration for android 1`] = `
entry: { entry: {
bundle: [ bundle: [
'@nativescript/core/globals/index', '@nativescript/core/globals/index',
'__jest__/src/__@nativescript_webpack_virtual_entry_javascript__', '__jest__/node_modules/@nativescript/webpack/dist/stubs/virtual-entry-javascript',
'@nativescript/core/bundle-entry-points', '@nativescript/core/bundle-entry-points',
'__jest__/src/app.js', '__jest__/src/app.js',
'@nativescript/core/ui/frame', '@nativescript/core/ui/frame',
@ -632,16 +622,6 @@ exports[`javascript configuration for ios 1`] = `
), ),
/* config.plugin('WatchStatePlugin') */ /* config.plugin('WatchStatePlugin') */
new WatchStatePlugin(), new WatchStatePlugin(),
/* config.plugin('ContextExclusionPlugin|__@nativescript_webpack_virtual_entry_javascript__') */
new ContextExclusionPlugin(
/__@nativescript_webpack_virtual_entry_javascript__.js$/
),
/* config.plugin('VirtualModulesPlugin') */
new VirtualModulesPlugin(
{
'__jest__/src/__@nativescript_webpack_virtual_entry_javascript__': '// VIRTUAL ENTRY START\\\\nrequire(\\\\'@nativescript/core/bundle-entry-points\\\\')\\\\nconst context = require.context(\\"~/\\", /* deep: */ true, /* filter: */ /.(xml|js|s?css)$/);\\\\nglobal.registerWebpackModules(context);\\\\n// VIRTUAL ENTRY END'
}
),
/* config.plugin('ContextExclusionPlugin|exclude_files') */ /* config.plugin('ContextExclusionPlugin|exclude_files') */
new ContextExclusionPlugin( new ContextExclusionPlugin(
/\\\\b_.+\\\\./ /\\\\b_.+\\\\./
@ -650,7 +630,7 @@ exports[`javascript configuration for ios 1`] = `
entry: { entry: {
bundle: [ bundle: [
'@nativescript/core/globals/index', '@nativescript/core/globals/index',
'__jest__/src/__@nativescript_webpack_virtual_entry_javascript__', '__jest__/node_modules/@nativescript/webpack/dist/stubs/virtual-entry-javascript',
'@nativescript/core/bundle-entry-points', '@nativescript/core/bundle-entry-points',
'__jest__/src/app.js' '__jest__/src/app.js'
], ],

View File

@ -302,16 +302,6 @@ exports[`typescript configuration for android 1`] = `
), ),
/* config.plugin('WatchStatePlugin') */ /* config.plugin('WatchStatePlugin') */
new WatchStatePlugin(), new WatchStatePlugin(),
/* config.plugin('ContextExclusionPlugin|__@nativescript_webpack_virtual_entry_typescript__') */
new ContextExclusionPlugin(
/__@nativescript_webpack_virtual_entry_typescript__.js$/
),
/* config.plugin('VirtualModulesPlugin') */
new VirtualModulesPlugin(
{
'__jest__/src/__@nativescript_webpack_virtual_entry_typescript__': '// VIRTUAL ENTRY START\\\\nrequire(\\\\'@nativescript/core/bundle-entry-points\\\\')\\\\nconst context = require.context(\\"~/\\", /* deep: */ true, /* filter: */ /\\\\\\\\.(xml|js|(?<!\\\\\\\\.d\\\\\\\\.)ts|s?css)$/);\\\\nglobal.registerWebpackModules(context);\\\\n// VIRTUAL ENTRY END'
}
),
/* config.plugin('ContextExclusionPlugin|exclude_files') */ /* config.plugin('ContextExclusionPlugin|exclude_files') */
new ContextExclusionPlugin( new ContextExclusionPlugin(
/\\\\b_.+\\\\./ /\\\\b_.+\\\\./
@ -320,7 +310,7 @@ exports[`typescript configuration for android 1`] = `
entry: { entry: {
bundle: [ bundle: [
'@nativescript/core/globals/index', '@nativescript/core/globals/index',
'__jest__/src/__@nativescript_webpack_virtual_entry_typescript__', '__jest__/node_modules/@nativescript/webpack/dist/stubs/virtual-entry-typescript.js',
'@nativescript/core/bundle-entry-points', '@nativescript/core/bundle-entry-points',
'__jest__/src/app.js', '__jest__/src/app.js',
'@nativescript/core/ui/frame', '@nativescript/core/ui/frame',
@ -632,16 +622,6 @@ exports[`typescript configuration for ios 1`] = `
), ),
/* config.plugin('WatchStatePlugin') */ /* config.plugin('WatchStatePlugin') */
new WatchStatePlugin(), new WatchStatePlugin(),
/* config.plugin('ContextExclusionPlugin|__@nativescript_webpack_virtual_entry_typescript__') */
new ContextExclusionPlugin(
/__@nativescript_webpack_virtual_entry_typescript__.js$/
),
/* config.plugin('VirtualModulesPlugin') */
new VirtualModulesPlugin(
{
'__jest__/src/__@nativescript_webpack_virtual_entry_typescript__': '// VIRTUAL ENTRY START\\\\nrequire(\\\\'@nativescript/core/bundle-entry-points\\\\')\\\\nconst context = require.context(\\"~/\\", /* deep: */ true, /* filter: */ /\\\\\\\\.(xml|js|(?<!\\\\\\\\.d\\\\\\\\.)ts|s?css)$/);\\\\nglobal.registerWebpackModules(context);\\\\n// VIRTUAL ENTRY END'
}
),
/* config.plugin('ContextExclusionPlugin|exclude_files') */ /* config.plugin('ContextExclusionPlugin|exclude_files') */
new ContextExclusionPlugin( new ContextExclusionPlugin(
/\\\\b_.+\\\\./ /\\\\b_.+\\\\./
@ -650,7 +630,7 @@ exports[`typescript configuration for ios 1`] = `
entry: { entry: {
bundle: [ bundle: [
'@nativescript/core/globals/index', '@nativescript/core/globals/index',
'__jest__/src/__@nativescript_webpack_virtual_entry_typescript__', '__jest__/node_modules/@nativescript/webpack/dist/stubs/virtual-entry-typescript.js',
'@nativescript/core/bundle-entry-points', '@nativescript/core/bundle-entry-points',
'__jest__/src/app.js' '__jest__/src/app.js'
], ],

View File

@ -1,4 +1,6 @@
import Config from 'webpack-chain'; import Config from 'webpack-chain';
import path from 'path';
import fs from 'fs';
import typescript from '../../src/configuration/typescript'; import typescript from '../../src/configuration/typescript';
import { init } from '../../src'; import { init } from '../../src';
@ -15,29 +17,30 @@ describe('typescript configuration', () => {
}); });
} }
it('filter typescript declaration files', () => { it('filters typescript declaration files', () => {
init({ init({
ios: true, ios: true,
}); });
const tsConfig = typescript(new Config()); // const tsConfig = typescript(new Config());
let regex: RegExp; let regex: RegExp;
// Get the filterRE from the typescript configuration const virtualEntryPath = path.join(
tsConfig.plugin('VirtualModulesPlugin').tap((args) => { __dirname,
const options = args[0]; '../../src/stubs/virtual-entry-typescript.js'
const virtualConfig: string = options[Object.keys(options)[0]]; );
const filterLine = virtualConfig const virtualEntry = fs.readFileSync(virtualEntryPath);
.split('\n')
.find((v) => v.includes('filter'));
const matches = filterLine.match(/\/(?<filter>\S+)\//);
if (matches) { const filterLine = virtualEntry
regex = new RegExp(matches.groups.filter); .toString()
} .split('\n')
.find((v) => v.includes('filter'));
return args; const matches = filterLine.match(/\/(?<filter>\S+)\//);
});
if (matches) {
regex = new RegExp(matches.groups.filter);
}
expect(regex).toBeDefined(); expect(regex).toBeDefined();

View File

@ -1,26 +1,18 @@
import Config from 'webpack-chain'; import Config from 'webpack-chain';
import { getEntryPath, getEntryDirPath } from '../helpers/platform'; import { getEntryPath, getEntryDirPath } from '../helpers/platform';
import { addVirtualEntry } from '../helpers/virtualModules';
import { chainedSetAddAfter } from '../helpers/chain'; import { chainedSetAddAfter } from '../helpers/chain';
import { env as _env, IWebpackEnv } from '../index'; import { env as _env, IWebpackEnv } from '../index';
import { ContextExclusionPlugin } from 'webpack'; import { ContextExclusionPlugin } from 'webpack';
import base from './base'; import base from './base';
import path from 'path';
export default function (config: Config, env: IWebpackEnv = _env): Config { export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env); base(config, env);
const entryPath = getEntryPath(); const entryPath = getEntryPath();
const filterRE = '/.(xml|js|s?css)$/'; const virtualEntryPath = path.resolve(
const virtualEntryPath = addVirtualEntry( __dirname,
config, '../stubs/virtual-entry-javascript'
'javascript',
`
// VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points')
const context = require.context("~/", /* deep: */ true, /* filter: */ ${filterRE});
global.registerWebpackModules(context);
// VIRTUAL ENTRY END
`
); );
// exclude files starting with _ from require.context // exclude files starting with _ from require.context

View File

@ -1,26 +1,18 @@
import Config from 'webpack-chain'; import Config from 'webpack-chain';
import { getEntryDirPath, getEntryPath } from '../helpers/platform'; import { getEntryDirPath, getEntryPath } from '../helpers/platform';
import { addVirtualEntry } from '../helpers/virtualModules';
import { chainedSetAddAfter } from '../helpers/chain'; import { chainedSetAddAfter } from '../helpers/chain';
import { env as _env, IWebpackEnv } from '../index'; import { env as _env, IWebpackEnv } from '../index';
import { ContextExclusionPlugin } from 'webpack'; import { ContextExclusionPlugin } from 'webpack';
import base from './base'; import base from './base';
import path from 'path';
export default function (config: Config, env: IWebpackEnv = _env): Config { export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env); base(config, env);
const entryPath = getEntryPath(); const entryPath = getEntryPath();
const filterRE = '/\\.(xml|js|(?<!\\.d\\.)ts|s?css)$/'; const virtualEntryPath = path.resolve(
const virtualEntryPath = addVirtualEntry( __dirname,
config, '../stubs/virtual-entry-typescript.js'
'typescript',
`
// VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points')
const context = require.context("~/", /* deep: */ true, /* filter: */ ${filterRE});
global.registerWebpackModules(context);
// VIRTUAL ENTRY END
`
); );
// exclude files starting with _ from require.context // exclude files starting with _ from require.context

View File

@ -8,6 +8,9 @@ import { getEntryDirPath } from './platform';
import dedent from 'ts-dedent'; import dedent from 'ts-dedent';
import { getProjectFilePath } from './project'; import { getProjectFilePath } from './project';
/**
* @deprecated Virtual entries are not recommended by the webpack team, use real files instead. For example, resolve a path in node_modules if necessary.
*/
export function addVirtualEntry( export function addVirtualEntry(
config: Config, config: Config,
name: string, name: string,
@ -20,6 +23,9 @@ export function addVirtualEntry(
); );
} }
/**
* @deprecated Virtual modules are not recommended by the webpack team, use real files instead. For example, resolve a path in node_modules if necessary.
*/
export function addVirtualModule( export function addVirtualModule(
config: Config, config: Config,
name: string, name: string,

View File

@ -0,0 +1,5 @@
// VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points')
const context = require.context("~/", /* deep: */ true, /* filter: */ /.(xml|js|s?css)$/);
global.registerWebpackModules(context);
// VIRTUAL ENTRY END

View File

@ -0,0 +1,5 @@
// VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points')
const context = require.context("~/", /* deep: */ true, /* filter: */ /\.(xml|js|(?<!\.d\.)ts|s?css)$/);
global.registerWebpackModules(context);
// VIRTUAL ENTRY END