Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

refactor: HMR and webpack improvements #966

Merged
merged 10 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
node_modules
*.tgz
package-lock.json
*.js.map

plugins/NativeScriptAngularCompilerPlugin.d.ts
plugins/NativeScriptAngularCompilerPlugin.js
plugins/NativeScriptAngularCompilerPlugin.js.map

transformers/*.d.ts
transformers/*.js
transformers/*.js.map

utils/*.d.ts
utils/*.js
utils/*.js.map

hmr/*.d.ts
hmr/*.js

plugins/PlatformFSPlugin.d.ts
plugins/PlatformFSPlugin.js
plugins/PlatformFSPlugin.js.map

plugins/WatchStateLoggerPlugin.d.ts
plugins/WatchStateLoggerPlugin.js
plugins/WatchStateLoggerPlugin.js.map

host/resolver.d.ts
host/resolver.js
host/resolver.js.map

jasmine-config/reporter.d.ts
jasmine-config/reporter.js
jasmine-config/reporter.js.map

bundle-config-loader.d.ts
bundle-config-loader.js

**/*.spec.js*
**/*.spec.d.ts*
Expand Down
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"args": [ "--env.android", "--env.aot" ],
"runtimeArgs": [ "--preserve-symlinks" ],
"stopOnEntry": true,
},
{
"type": "node",
"request": "launch",
"name": "TypeScriptApp Webpack",
"cwd": "${workspaceFolder}/demo/TypeScriptApp",
"program": "${workspaceFolder}/demo/TypeScriptApp/node_modules/.bin/webpack",
"args": [ "--env.android" ],
"stopOnEntry": true,
"preLaunchTask": "npm:tsc"
}
]
}
13 changes: 13 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label":"npm:tsc",
"type": "npm",
"script": "tsc",
"problemMatcher": []
}
]
}
22 changes: 18 additions & 4 deletions bundle-config-loader.js → bundle-config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const unitTestingConfigLoader = require("./unit-testing-config-loader");
import unitTestingConfigLoader from "./unit-testing-config-loader";
import { loader } from "webpack";
import { getOptions } from "loader-utils";

module.exports = function (source, map) {
this.cacheable();
const { angular = false, loadCss = true, unitTesting, projectRoot, appFullPath, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
// Matches all source, markup and style files that are not in App_Resources
const defaultMatch = /(?<!App_Resources.*)\.(xml|css|js|(?<!d\.)ts|scss)$/;

const loader: loader.Loader = function (source, map) {
const {
angular = false,
loadCss = true,
unitTesting,
projectRoot,
appFullPath,
registerModules = defaultMatch,
} = getOptions(this);

if (unitTesting) {
source = unitTestingConfigLoader({ appFullPath, projectRoot, angular, rootPagesRegExp: registerModules });
Expand Down Expand Up @@ -68,3 +79,6 @@ module.exports = function (source, map) {

this.callback(null, source, map);
};


export default loader;
1 change: 0 additions & 1 deletion demo/AngularApp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module.exports = env => {

// Default destination inside platforms/<platform>/...
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";

const {
// The 'appPath' and 'appResourcesPath' values are fetched from
Expand Down
20 changes: 5 additions & 15 deletions demo/JavaScriptApp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = env => {

// Default destination inside platforms/<platform>/...
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";

const {
// The 'appPath' and 'appResourcesPath' values are fetched from
Expand Down Expand Up @@ -103,7 +102,7 @@ module.exports = env => {
'~': appFullPath
},
// don't resolve symlinks to symlinked modules
symlinks: false
symlinks: true
},
resolveLoader: {
// don't resolve symlinks to symlinked loaders
Expand Down Expand Up @@ -174,24 +173,15 @@ module.exports = env => {
unitTesting,
appFullPath,
projectRoot,
registerModules: /(?<!App_Resources.*)(?<!\.\/application)(?<!\.\/activity)\.(xml|css|js|(?<!d\.)ts|scss)$/
}
},
].filter(loader => !!loader)
},

{
test: /-page\.js$/,
use: "nativescript-dev-webpack/script-hot-loader"
},

{
test: /\.(css|scss)$/,
use: "nativescript-dev-webpack/style-hot-loader"
},

{
test: /\.(html|xml)$/,
use: "nativescript-dev-webpack/markup-hot-loader"
test: /\.(js|css|scss|html|xml)$/,
use: "nativescript-dev-webpack/hmr/hot-loader"
},

{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
Expand Down Expand Up @@ -234,7 +224,7 @@ module.exports = env => {
platforms,
}),
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
new nsWebpack.WatchStateLoggerPlugin(),
new nsWebpack.WatchStateLoggerPlugin()
],
};

Expand Down
18 changes: 4 additions & 14 deletions demo/TypeScriptApp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = env => {

// Default destination inside platforms/<platform>/...
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";

const {
// The 'appPath' and 'appResourcesPath' values are fetched from
Expand Down Expand Up @@ -180,24 +179,15 @@ module.exports = env => {
unitTesting,
appFullPath,
projectRoot,
registerModules: /(?<!App_Resources.*)(?<!\.\/application)(?<!\.\/activity)\.(xml|css|js|(?<!d\.)ts|scss)$/
}
},
].filter(loader => !!loader)
},

{
test: /-page\.ts$/,
use: "nativescript-dev-webpack/script-hot-loader"
},

{
test: /\.(css|scss)$/,
use: "nativescript-dev-webpack/style-hot-loader"
},

{
test: /\.(html|xml)$/,
use: "nativescript-dev-webpack/markup-hot-loader"
test: /\.(ts|css|scss|html|xml)$/,
use: "nativescript-dev-webpack/hmr/hot-loader"
},

{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
Expand Down Expand Up @@ -265,7 +255,7 @@ module.exports = env => {
async: false,
useTypescriptIncrementalApi: true,
memoryLimit: 4096
}),
})
],
};

Expand Down
7 changes: 0 additions & 7 deletions hmr/hmr-update.js

This file was deleted.

10 changes: 10 additions & 0 deletions hmr/hmr-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import update from "../hot";
import { knownFolders } from "tns-core-modules/file-system";

declare const __webpack_require__: any;

export function hmrUpdate() {
const applicationFiles = knownFolders.currentApp();
const latestHash = __webpack_require__["h"]();
return update(latestHash, filename => applicationFiles.getFile(filename));
}
44 changes: 44 additions & 0 deletions hmr/hot-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { loader } from "webpack";
import { convertToUnixPath } from "../lib/utils";
import { extname } from "path";
import { getOptions } from "loader-utils";

const extMap = {
".css": "style",
".scss": "style",
".less": "style",
".js": "script",
".ts": "script",
".xml": "markup",
".html": "markup",
}

const loader: loader.Loader = function (source, map) {
const moduleRelativePath = this.resourcePath.replace(this.rootContext, ".");
const modulePath = convertToUnixPath(moduleRelativePath);
const ext = extname(modulePath).toLowerCase();
const moduleType = extMap[ext] || "unknown";

const options = getOptions(this) || {};
const alwaysSelfAccept = options.alwaysSelfAccept;
const trace = options.trace;

const shouldAutoAcceptCheck = `&& global._isModuleLoadedForUI && global._isModuleLoadedForUI("${modulePath}")`;
const traceCode = `console.log("[hot-loader]: Self-accept module: ${modulePath}");`;

const hotCode = `
if (module.hot ${alwaysSelfAccept ? "" : shouldAutoAcceptCheck} ) {
${trace ? traceCode : ""}
module.hot.accept();
module.hot.dispose(() => {
global.hmrRefresh({ type: "${moduleType}", path: "${modulePath}" });
});
}`;

this.callback(null, `${source}; ${hotCode} `, map);
};

export default loader;



1 change: 0 additions & 1 deletion hmr/index.js

This file was deleted.

1 change: 1 addition & 0 deletions hmr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { hmrUpdate } from "./hmr-update";
3 changes: 2 additions & 1 deletion jasmine-config/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"spec_dir": ".",
"spec_files": [
"./!(node_modules)/**/*.spec.js",
"!node_modules/**/*.spec.js",
"!demo/**/*.spec.js",
"./*.spec.js"
],
"helpers": [
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
"url": "https://github.com/NativeScript/nativescript-dev-webpack.git"
},
"scripts": {
"tsc": "tsc",
"postinstall": "node postinstall.js",
"preuninstall": "node preuninstall.js",
"postpack": "rm -rf node_modules",
"prepare": "tsc && npm run jasmine",
"prepare": "npm run tsc && npm run jasmine",
"test": "npm run prepare && npm run jasmine",
"jasmine": "jasmine --config=jasmine-config/jasmine.json",
"version": "rm package-lock.json && conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
Expand All @@ -49,8 +50,8 @@
"clean-webpack-plugin": "~1.0.0",
"copy-webpack-plugin": "~4.6.0",
"css-loader": "~2.1.1",
"fork-ts-checker-webpack-plugin": "1.3.0",
"extra-watch-webpack-plugin": "1.0.3",
"fork-ts-checker-webpack-plugin": "1.3.0",
"global-modules-path": "2.0.0",
"minimatch": "3.0.4",
"nativescript-hook": "0.2.4",
Expand All @@ -77,13 +78,17 @@
"@angular/compiler-cli": "8.0.0",
"@ngtools/webpack": "8.0.0",
"@types/jasmine": "^3.3.7",
"@types/loader-utils": "^1.1.3",
"@types/node": "^10.12.12",
"@types/proxyquire": "1.3.28",
"@types/semver": "^6.0.0",
"@types/webpack": "^4.4.34",
"conventional-changelog-cli": "^1.3.22",
"jasmine": "^3.2.0",
"jasmine-spec-reporter": "^4.2.1",
"loader-utils": "^1.2.3",
"proxyquire": "2.1.0",
"tns-core-modules": "next",
"typescript": "~3.4.0"
}
}
1 change: 0 additions & 1 deletion templates/webpack.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = env => {

// Default destination inside platforms/<platform>/...
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";

const {
// The 'appPath' and 'appResourcesPath' values are fetched from
Expand Down
15 changes: 2 additions & 13 deletions templates/webpack.javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = env => {

// Default destination inside platforms/<platform>/...
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";

const {
// The 'appPath' and 'appResourcesPath' values are fetched from
Expand Down Expand Up @@ -179,18 +178,8 @@ module.exports = env => {
},

{
test: /-page\.js$/,
use: "nativescript-dev-webpack/script-hot-loader"
},

{
test: /\.(css|scss)$/,
use: "nativescript-dev-webpack/style-hot-loader"
},

{
test: /\.(html|xml)$/,
use: "nativescript-dev-webpack/markup-hot-loader"
test: /\.(js|css|scss|html|xml)$/,
use: "nativescript-dev-webpack/hmr/hot-loader"
},

{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
Expand Down
Loading