Skip to content

Commit 01c1e71

Browse files
committed
chore: update webpack config
NativeScript/nativescript-dev-webpack#858 cause some breakign changes and this PR solves them.
1 parent 97fa2a3 commit 01c1e71

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

Diff for: package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
"id": "org.nativescript.nativescriptsdkexamplesng",
2424
"tns-ios": {
2525
"version": "5.4.0-2019-03-26-093523-01"
26+
},
27+
"tns-android": {
28+
"version": "5.3.1"
2629
}
2730
},
2831
"dependencies": {
@@ -53,7 +56,7 @@
5356
"lazy": "1.0.11",
5457
"markdown-snippet-injector": "^0.2.2",
5558
"nativescript-dev-typescript": "^0.9.0",
56-
"nativescript-dev-webpack": "^0.20.3",
59+
"nativescript-dev-webpack": "^0.22.0-next-2019-04-17-111030-01",
5760
"opener": "^1.4.1",
5861
"rimraf": "^2.5.3",
5962
"tar.gz": "^1.0.5",

Diff for: webpack.config.js

+54-21
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const CleanWebpackPlugin = require("clean-webpack-plugin");
1111
const CopyWebpackPlugin = require("copy-webpack-plugin");
1212
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
1313
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
14-
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
15-
const { AngularCompilerPlugin } = require("@ngtools/webpack");
16-
const hashSalt = Date.now().toString();
14+
const TerserPlugin = require("terser-webpack-plugin");
15+
const { getAngularCompilerPlugin } = require("nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin");
16+
const hashSalt = Date.now().toString();
1717

1818
module.exports = env => {
1919
// Add your custom Activities, Services and other Android app components here.
@@ -27,6 +27,7 @@ module.exports = env => {
2727
throw new Error("You need to provide a target platform!");
2828
}
2929

30+
const AngularCompilerPlugin = getAngularCompilerPlugin(platform);
3031
const projectRoot = __dirname;
3132

3233
// Default destination inside platforms/<platform>/...
@@ -46,15 +47,23 @@ module.exports = env => {
4647
uglify, // --env.uglify
4748
report, // --env.report
4849
sourceMap, // --env.sourceMap
50+
hiddenSourceMap, // --env.hiddenSourceMap
4951
hmr, // --env.hmr,
52+
unitTesting, // --env.unitTesting
5053
} = env;
5154

55+
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
5256
const externals = nsWebpack.getConvertedExternals(env.externals);
5357
const appFullPath = resolve(projectRoot, appPath);
5458
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
5559
const tsConfigName = "tsconfig.tns.json";
56-
const entryModule = `${nsWebpack.getEntryModule(appFullPath)}.ts`;
60+
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
5761
const entryPath = `.${sep}${entryModule}`;
62+
const entries = { bundle: entryPath };
63+
if (platform === "ios") {
64+
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules.js";
65+
};
66+
5867
const ngCompilerTransformers = [];
5968
const additionalLazyModuleResources = [];
6069
if (aot) {
@@ -82,13 +91,15 @@ module.exports = env => {
8291
const ngCompilerPlugin = new AngularCompilerPlugin({
8392
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
8493
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule))),
85-
mainPath: resolve(appPath, entryModule),
94+
mainPath: join(appFullPath, entryModule),
8695
tsConfigPath: join(__dirname, tsConfigName),
8796
skipCodeGeneration: !aot,
88-
sourceMap: !!sourceMap,
97+
sourceMap: !!isAnySourceMapEnabled,
8998
additionalLazyModuleResources: additionalLazyModuleResources
9099
});
91100

101+
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
102+
92103
const config = {
93104
mode: uglify ? "production" : "development",
94105
context: appFullPath,
@@ -101,12 +112,11 @@ module.exports = env => {
101112
]
102113
},
103114
target: nativescriptTarget,
104-
entry: {
105-
bundle: entryPath,
106-
},
115+
entry: entries,
107116
output: {
108117
pathinfo: false,
109118
path: dist,
119+
sourceMapFilename,
110120
libraryTarget: "commonjs2",
111121
filename: "[name].js",
112122
globalObject: "global",
@@ -137,8 +147,9 @@ module.exports = env => {
137147
"fs": "empty",
138148
"__dirname": false,
139149
},
140-
devtool: sourceMap ? "inline-source-map" : "none",
150+
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
141151
optimization: {
152+
runtimeChunk: "single",
142153
splitChunks: {
143154
cacheGroups: {
144155
vendor: {
@@ -155,12 +166,14 @@ module.exports = env => {
155166
},
156167
minimize: !!uglify,
157168
minimizer: [
158-
new UglifyJsPlugin({
169+
new TerserPlugin({
159170
parallel: true,
160171
cache: true,
161-
uglifyOptions: {
172+
sourceMap: isAnySourceMapEnabled,
173+
terserOptions: {
162174
output: {
163175
comments: false,
176+
semicolons: !isAnySourceMapEnabled
164177
},
165178
compress: {
166179
// The Android SBG has problems parsing the output
@@ -175,7 +188,7 @@ module.exports = env => {
175188
module: {
176189
rules: [
177190
{
178-
test: new RegExp(entryPath),
191+
test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath),
179192
use: [
180193
// Require all Android app components
181194
platform === "android" && {
@@ -188,6 +201,9 @@ module.exports = env => {
188201
options: {
189202
angular: true,
190203
loadCss: !snapshot, // load the application css if in debug mode
204+
unitTesting,
205+
appFullPath,
206+
projectRoot,
191207
}
192208
},
193209
].filter(loader => !!loader)
@@ -200,14 +216,14 @@ module.exports = env => {
200216
test: /[\/|\\]app\.css$/,
201217
use: [
202218
"nativescript-dev-webpack/style-hot-loader",
203-
{ loader: "css-loader", options: { minimize: false, url: false } }
219+
{ loader: "css-loader", options: { url: false } }
204220
]
205221
},
206222
{
207223
test: /[\/|\\]app\.scss$/,
208224
use: [
209225
"nativescript-dev-webpack/style-hot-loader",
210-
{ loader: "css-loader", options: { minimize: false, url: false } },
226+
{ loader: "css-loader", options: { url: false } },
211227
"sass-loader"
212228
]
213229
},
@@ -239,8 +255,6 @@ module.exports = env => {
239255
"global.TNS_WEBPACK": "true",
240256
"process": undefined,
241257
}),
242-
// Remove all files from the out dir.
243-
new CleanWebpackPlugin([`${dist}/**/*`]),
244258
// Copy native app resources to out dir.
245259
new CopyWebpackPlugin([
246260
{
@@ -249,6 +263,7 @@ module.exports = env => {
249263
context: projectRoot
250264
},
251265
]),
266+
252267
// Copy assets to out dir. Add your own globs as needed.
253268
new CopyWebpackPlugin([
254269
{ from: { glob: "ng-ui-widgets-category/web-view/web-view-html/*.html"} },
@@ -257,10 +272,16 @@ module.exports = env => {
257272
{ from: { glob: "**/*.png" } },
258273
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
259274
// Generate a bundle starter script and activate it in package.json
260-
new nsWebpack.GenerateBundleStarterPlugin([
261-
"./vendor",
262-
"./bundle",
263-
]),
275+
new nsWebpack.GenerateBundleStarterPlugin(
276+
// Don't include `runtime.js` when creating a snapshot. The plugin
277+
// configures the WebPack runtime to be generated inside the snapshot
278+
// module and no `runtime.js` module exist.
279+
(snapshot ? [] : ["./runtime"])
280+
.concat([
281+
"./vendor",
282+
"./bundle",
283+
])
284+
),
264285
// For instructions on how to set up workers with webpack
265286
// check out https://github.com/nativescript/worker-loader
266287
new NativeScriptWorkerPlugin(),
@@ -270,6 +291,18 @@ module.exports = env => {
270291
],
271292
};
272293

294+
// Copy the native app resources to the out dir
295+
// only if doing a full build (tns run/build) and not previewing (tns preview)
296+
if (!externals || externals.length === 0) {
297+
config.plugins.push(new CopyWebpackPlugin([
298+
{
299+
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
300+
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
301+
context: projectRoot
302+
},
303+
]));
304+
}
305+
273306

274307
if (report) {
275308
// Generate report files for bundles content

0 commit comments

Comments
 (0)