feat: core-hmr handling & watch ignore

This commit is contained in:
Igor Randjelovic
2021-03-11 23:05:47 +01:00
parent 0e8336f1d9
commit 6cc0ce3d57
12 changed files with 238 additions and 2 deletions

View File

@ -9,6 +9,12 @@ exports[`angular configuration for android 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -282,6 +288,12 @@ exports[`angular configuration for ios 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,

View File

@ -9,6 +9,12 @@ exports[`base configuration for android 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -270,6 +276,12 @@ exports[`base configuration for ios 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,

View File

@ -9,6 +9,12 @@ exports[`javascript configuration for android 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -159,6 +165,22 @@ exports[`javascript configuration for android 1`] = `
loader: 'xml-namespace-loader'
}
]
},
/* config.module.rule('hmr-core') */
{
test: /\\\\.js$/,
exclude: [
/node_modules/
],
use: [
/* config.module.rule('hmr-core').use('nativescript-hot-loader') */
{
loader: 'nativescript-hot-loader',
options: {
appPath: '__jest__/src'
}
}
]
}
]
},
@ -291,6 +313,12 @@ exports[`javascript configuration for ios 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,
@ -441,6 +469,22 @@ exports[`javascript configuration for ios 1`] = `
loader: 'xml-namespace-loader'
}
]
},
/* config.module.rule('hmr-core') */
{
test: /\\\\.js$/,
exclude: [
/node_modules/
],
use: [
/* config.module.rule('hmr-core').use('nativescript-hot-loader') */
{
loader: 'nativescript-hot-loader',
options: {
appPath: '__jest__/src'
}
}
]
}
]
},

View File

@ -9,6 +9,12 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -298,6 +304,12 @@ exports[`react configuration > android > base config 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -565,6 +577,12 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,
@ -855,6 +873,12 @@ exports[`react configuration > ios > base config 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,

View File

@ -9,6 +9,12 @@ exports[`svelte configuration for android 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -295,6 +301,12 @@ exports[`svelte configuration for ios 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,

View File

@ -9,6 +9,12 @@ exports[`typescript configuration for android 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -159,6 +165,22 @@ exports[`typescript configuration for android 1`] = `
loader: 'xml-namespace-loader'
}
]
},
/* config.module.rule('hmr-core') */
{
test: /\\\\.(js|ts)$/,
exclude: [
/node_modules/
],
use: [
/* config.module.rule('hmr-core').use('nativescript-hot-loader') */
{
loader: 'nativescript-hot-loader',
options: {
appPath: '__jest__/src'
}
}
]
}
]
},
@ -291,6 +313,12 @@ exports[`typescript configuration for ios 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,
@ -441,6 +469,22 @@ exports[`typescript configuration for ios 1`] = `
loader: 'xml-namespace-loader'
}
]
},
/* config.module.rule('hmr-core') */
{
test: /\\\\.(js|ts)$/,
exclude: [
/node_modules/
],
use: [
/* config.module.rule('hmr-core').use('nativescript-hot-loader') */
{
loader: 'nativescript-hot-loader',
options: {
appPath: '__jest__/src'
}
}
]
}
]
},

View File

@ -9,6 +9,12 @@ exports[`vue configuration for android 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/android/app/src/main/assets/app',
pathinfo: false,
@ -302,6 +308,12 @@ exports[`vue configuration for ios 1`] = `
],
devtool: 'inline-source-map',
target: 'node',
watchOptions: {
ignored: [
'__jest__/platforms/platforms/**',
'__jest__/App_Resources/**'
]
},
output: {
path: '__jest__/platforms/ios/jest/app',
pathinfo: false,

View File

@ -1,7 +1,6 @@
import { DefinePlugin, HotModuleReplacementPlugin } from 'webpack';
import Config from 'webpack-chain';
import { resolve } from 'path';
import os from 'os';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import FilterWarningsPlugin from 'webpack-filter-warnings-plugin';
@ -9,10 +8,10 @@ import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import TerserPlugin from 'terser-webpack-plugin';
// import { WatchStateLoggerPlugin } from '../plugins/WatchStateLoggerPlugin';
import { getProjectFilePath, getProjectRootPath } from '../helpers/project';
import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin';
import { addCopyRule, applyCopyRules } from '../helpers/copyRules';
import { WatchStatePlugin } from '../plugins/WatchStatePlugin';
import { getProjectRootPath } from '../helpers/project';
import { hasDependency } from '../helpers/dependencies';
import { applyDotEnvPlugin } from '../helpers/dotEnv';
import { env as _env, IWebpackEnv } from '../index';
@ -80,6 +79,13 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.globalObject('global')
.set('clean', true);
config.watchOptions({
ignored: [
`${getProjectFilePath('platforms')}/platforms/**`,
`${env.appResourcesPath ?? getProjectFilePath('App_Resources')}/**`
]
})
// Set up Terser options
config.optimization.minimizer('TerserPlugin').use(TerserPlugin, [
{

View File

@ -1,6 +1,7 @@
import Config from 'webpack-chain';
import { addVirtualEntry } from '../helpers/virtualModules';
import { getEntryDirPath } from "../helpers/platform";
import { env as _env, IWebpackEnv } from '../index';
import base from './base';
@ -31,5 +32,18 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.use('xml-namespace-loader')
.loader('xml-namespace-loader');
// set up core HMR
config.module
.rule('hmr-core')
.test(/\.js$/)
.exclude
.add(/node_modules/)
.end()
.use('nativescript-hot-loader')
.loader('nativescript-hot-loader')
.options({
appPath: getEntryDirPath()
})
return config;
}

View File

@ -1,6 +1,7 @@
import Config from 'webpack-chain';
import { addVirtualEntry } from '../helpers/virtualModules';
import { getEntryDirPath } from "../helpers/platform";
import { env as _env, IWebpackEnv } from '../index';
import base from './base';
@ -31,5 +32,18 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.use('xml-namespace-loader')
.loader('xml-namespace-loader');
// set up core HMR
config.module
.rule('hmr-core')
.test(/\.(js|ts)$/)
.exclude
.add(/node_modules/)
.end()
.use('nativescript-hot-loader')
.loader('nativescript-hot-loader')
.options({
appPath: getEntryDirPath()
})
return config;
}

View File

@ -0,0 +1,32 @@
import { relative } from "path";
import dedent from "ts-dedent";
// note: this will bail even if module.hot appears in a comment
const MODULE_HOT_RE = /module\.hot/
export default function loader(content: string, map: any) {
if (MODULE_HOT_RE.test(content)) {
// Code already handles HMR - we don't need to do anything
return this.callback(null, content, map)
}
const opts = this.getOptions();
const relativePath = relative(
opts.appPath ?? this.rootContext,
this.resourcePath
).replace(/\\/g, '/')
const hmrCode = this.hot
? dedent`
/* NATIVESCRIPT-HOT-LOADER */
if(module.hot && global._isModuleLoadedForUI && global._isModuleLoadedForUI("./${relativePath}")) {
module.hot.accept()
}
`
: ``;
const source = `${content}\n${hmrCode}`
this.callback(null, source, map)
}

View File

@ -178,11 +178,21 @@ async function parseXML(content: string): Promise<ParseResult> {
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
const hmrCode = this.hot
? dedent`
if(module.hot) {
module.hot.accept()
// module.hot.dispose(() => {})
}
`
: ``;
const code = dedent`
${moduleRegisters.join('\n')}
/* XML-NAMESPACE-LOADER */
const ___XML_NAMESPACE_LOADER_EXPORT___ = ${xml}
export default ___XML_NAMESPACE_LOADER_EXPORT___
${hmrCode}
`;
if (errors.length) {