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

Commit cc188f7

Browse files
Merge branch 'master' into svetoslavtsenov/update-release-schema
2 parents b3c22a3 + 69cb061 commit cc188f7

16 files changed

+260
-146
lines changed

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
<a name="0.20.0"></a>
35-
# [0.20.0](https://github.com/NativeScript/nativescript-dev-webpack/compare/0.18.3...0.20.0) (2019-02-08)
35+
# [0.20.0](https://github.com/NativeScript/nativescript-dev-webpack/compare/0.19.2...0.20.0) (2019-02-08)
3636

3737

3838
### Bug Fixes

Diff for: bundle-config-loader.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
const unitTestingConfigLoader = require("./unit-testing-config-loader");
2+
13
module.exports = function (source) {
24
this.cacheable();
3-
const { angular = false, loadCss = true, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
5+
const { angular = false, loadCss = true, unitTesting, projectRoot, appFullPath, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
6+
7+
if (unitTesting) {
8+
source = unitTestingConfigLoader({ appFullPath, projectRoot, angular, rootPagesRegExp: registerModules });
9+
this.callback(null, source);
10+
return;
11+
}
412

513
const hmr = `
614
if (module.hot) {

Diff for: index.js

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ exports.getAppPath = (platform, projectDir) => {
5353
}
5454
};
5555

56+
exports.getEntryPathRegExp = (appFullPath, entryModule) => {
57+
const entryModuleFullPath = path.join(appFullPath, entryModule);
58+
// Windows paths contain `\`, so we need to convert each of the `\` to `\\`, so it will be correct inside RegExp
59+
const escapedPath = entryModuleFullPath.replace(/\\/g, "\\\\");
60+
return new RegExp(escapedPath);
61+
}
5662
/**
5763
* Converts an array of strings externals to an array of regular expressions.
5864
* 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 for: index.spec.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getConvertedExternals } from './index';
1+
import { getConvertedExternals, getEntryPathRegExp } from './index';
2+
const path = require("path");
23

34
describe('index', () => {
45
describe('getConvertedExternals', () => {
@@ -51,4 +52,27 @@ describe('index', () => {
5152
});
5253
});
5354
});
55+
56+
describe('getEntryPathRegExp', () => {
57+
const originalPathJoin = path.join;
58+
const entryModule = "index.js";
59+
60+
afterEach(() => {
61+
path.join = originalPathJoin;
62+
});
63+
64+
it('returns RegExp that matches Windows', () => {
65+
const appPath = "D:\\Work\\app1\\app";
66+
path.join = path.win32.join;
67+
const regExp = getEntryPathRegExp(appPath, entryModule);
68+
expect(!!regExp.exec(`${appPath}\\${entryModule}`)).toBe(true);
69+
});
70+
71+
it('returns RegExp that works with POSIX paths', () => {
72+
const appPath = "/usr/local/lib/app1/app";
73+
path.join = path.posix.join;
74+
const regExp = getEntryPathRegExp(appPath, entryModule);
75+
expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true);
76+
});
77+
});
5478
});

Diff for: lib/compiler.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ function logSnapshotWarningMessage($logger) {
174174
function stopWebpackForPlatform($logger, platform) {
175175
$logger.trace(`Stopping webpack watch for platform ${platform}.`);
176176
const webpackProcess = webpackProcesses[platform];
177-
webpackProcess.kill("SIGINT");
178-
179-
delete webpackProcesses[platform];
177+
if (webpackProcess) {
178+
webpackProcess.kill("SIGINT");
179+
delete webpackProcesses[platform];
180+
}
180181
}
181182

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-dev-webpack",
3-
"version": "0.20.3",
3+
"version": "0.21.0",
44
"main": "index",
55
"description": "",
66
"homepage": "http://www.telerik.com",

Diff for: plugins/WatchStateLoggerPlugin.ts

-64
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { join } from "path";
2-
import { writeFileSync, readFileSync } from "fs";
3-
const utils = require("../lib/utils");
42

53
export enum messages {
64
compilationComplete = "Webpack compilation complete.",
@@ -37,10 +35,6 @@ export class WatchStateLoggerPlugin {
3735
.keys(compilation.assets)
3836
.filter(assetKey => compilation.assets[assetKey].emitted);
3937

40-
if (compilation.errors.length > 0) {
41-
WatchStateLoggerPlugin.rewriteHotUpdateChunk(compiler, compilation, emittedFiles);
42-
}
43-
4438
// provide fake paths to the {N} CLI - relative to the 'app' folder
4539
// in order to trigger the livesync process
4640
const emittedFilesFakePaths = emittedFiles
@@ -51,62 +45,4 @@ export class WatchStateLoggerPlugin {
5145
process.send && process.send({ emittedFiles: emittedFilesFakePaths }, error => null);
5246
});
5347
}
54-
55-
/**
56-
* Rewrite an errored chunk to make the hot module replace successful.
57-
* @param compiler the webpack compiler
58-
* @param emittedFiles the emitted files from the current compilation
59-
*/
60-
private static rewriteHotUpdateChunk(compiler, compilation, emittedFiles: string[]) {
61-
const chunk = this.findHotUpdateChunk(emittedFiles);
62-
if (!chunk) {
63-
return;
64-
}
65-
66-
const { name } = utils.parseHotUpdateChunkName(chunk);
67-
if (!name) {
68-
return;
69-
}
70-
71-
const absolutePath = join(compiler.outputPath, chunk);
72-
73-
const newContent = this.getWebpackHotUpdateReplacementContent(compilation.errors, absolutePath, name);
74-
writeFileSync(absolutePath, newContent);
75-
}
76-
77-
private static findHotUpdateChunk(emittedFiles: string[]) {
78-
return emittedFiles.find(file => file.endsWith("hot-update.js"));
79-
}
80-
81-
/**
82-
* Gets only the modules object after 'webpackHotUpdate("bundle",' in the chunk
83-
*/
84-
private static getModulesObjectFromChunk(chunkPath) {
85-
let content = readFileSync(chunkPath, "utf8")
86-
const startIndex = content.indexOf(",") + 1;
87-
let endIndex = content.length - 1;
88-
if(content.endsWith(';')) {
89-
endIndex--;
90-
}
91-
return content.substring(startIndex, endIndex);
92-
}
93-
94-
/**
95-
* Gets the webpackHotUpdate call with updated modules not to include the ones with errors
96-
*/
97-
private static getWebpackHotUpdateReplacementContent(compilationErrors, filePath, moduleName) {
98-
const errorModuleIds = compilationErrors.filter(x => x.module).map(x => x.module.id);
99-
if (!errorModuleIds || errorModuleIds.length == 0) {
100-
// could not determine error modiles so discard everything
101-
return `webpackHotUpdate('${moduleName}', {});`;
102-
}
103-
const updatedModules = this.getModulesObjectFromChunk(filePath);
104-
105-
// we need to filter the modules with a function in the file as it is a relaxed JSON not valid to be parsed and manipulated
106-
return `const filter = function(updatedModules, modules) {
107-
modules.forEach(moduleId => delete updatedModules[moduleId]);
108-
return updatedModules;
109-
}
110-
webpackHotUpdate('${moduleName}', filter(${updatedModules}, ${JSON.stringify(errorModuleIds)}));`;
111-
}
11248
}

Diff for: templates/webpack.angular.js

+34-16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = env => {
4747
report, // --env.report
4848
sourceMap, // --env.sourceMap
4949
hmr, // --env.hmr,
50+
unitTesting, // --env.unitTesting
5051
} = env;
5152

5253
const externals = nsWebpack.getConvertedExternals(env.externals);
@@ -55,6 +56,11 @@ module.exports = env => {
5556
const tsConfigName = "tsconfig.tns.json";
5657
const entryModule = `${nsWebpack.getEntryModule(appFullPath)}.ts`;
5758
const entryPath = `.${sep}${entryModule}`;
59+
const entries = { bundle: entryPath };
60+
if (platform === "ios") {
61+
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules.js";
62+
};
63+
5864
const ngCompilerTransformers = [];
5965
const additionalLazyModuleResources = [];
6066
if (aot) {
@@ -101,9 +107,7 @@ module.exports = env => {
101107
]
102108
},
103109
target: nativescriptTarget,
104-
entry: {
105-
bundle: entryPath,
106-
},
110+
entry: entries,
107111
output: {
108112
pathinfo: false,
109113
path: dist,
@@ -139,6 +143,7 @@ module.exports = env => {
139143
},
140144
devtool: sourceMap ? "inline-source-map" : "none",
141145
optimization: {
146+
runtimeChunk: "single",
142147
splitChunks: {
143148
cacheGroups: {
144149
vendor: {
@@ -175,7 +180,7 @@ module.exports = env => {
175180
module: {
176181
rules: [
177182
{
178-
test: new RegExp(entryPath),
183+
test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath),
179184
use: [
180185
// Require all Android app components
181186
platform === "android" && {
@@ -188,6 +193,9 @@ module.exports = env => {
188193
options: {
189194
angular: true,
190195
loadCss: !snapshot, // load the application css if in debug mode
196+
unitTesting,
197+
appFullPath,
198+
projectRoot,
191199
}
192200
},
193201
].filter(loader => !!loader)
@@ -241,25 +249,23 @@ module.exports = env => {
241249
}),
242250
// Remove all files from the out dir.
243251
new CleanWebpackPlugin([`${dist}/**/*`]),
244-
// Copy native app resources to out dir.
245-
new CopyWebpackPlugin([
246-
{
247-
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
248-
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
249-
context: projectRoot
250-
},
251-
]),
252252
// Copy assets to out dir. Add your own globs as needed.
253253
new CopyWebpackPlugin([
254254
{ from: { glob: "fonts/**" } },
255255
{ from: { glob: "**/*.jpg" } },
256256
{ from: { glob: "**/*.png" } },
257257
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
258258
// Generate a bundle starter script and activate it in package.json
259-
new nsWebpack.GenerateBundleStarterPlugin([
260-
"./vendor",
261-
"./bundle",
262-
]),
259+
new nsWebpack.GenerateBundleStarterPlugin(
260+
// Don't include `runtime.js` when creating a snapshot. The plugin
261+
// configures the WebPack runtime to be generated inside the snapshot
262+
// module and no `runtime.js` module exist.
263+
(snapshot ? [] : ["./runtime"])
264+
.concat([
265+
"./vendor",
266+
"./bundle",
267+
])
268+
),
263269
// For instructions on how to set up workers with webpack
264270
// check out https://github.com/nativescript/worker-loader
265271
new NativeScriptWorkerPlugin(),
@@ -269,6 +275,18 @@ module.exports = env => {
269275
],
270276
};
271277

278+
// Copy the native app resources to the out dir
279+
// only if doing a full build (tns run/build) and not previewing (tns preview)
280+
if (!externals || externals.length === 0) {
281+
config.plugins.push(new CopyWebpackPlugin([
282+
{
283+
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
284+
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
285+
context: projectRoot
286+
},
287+
]));
288+
}
289+
272290

273291
if (report) {
274292
// Generate report files for bundles content

Diff for: templates/webpack.config.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const nativeScriptDevWebpack = {
2121
getAppPath: () => 'app',
2222
getEntryModule: () => 'EntryModule',
2323
getResolver: () => null,
24+
getEntryPathRegExp: () => null,
2425
getConvertedExternals: nsWebpackIndex.getConvertedExternals
2526
};
2627

@@ -83,7 +84,8 @@ describe('webpack.config.js', () => {
8384
});
8485

8586
it('returns empty array when externals are not passed', () => {
86-
const config = webpackConfig(getInput({ platform }));
87+
const input = getInput({ platform });
88+
const config = webpackConfig(input);
8789
expect(config.externals).toEqual([]);
8890
});
8991

0 commit comments

Comments
 (0)