Skip to content

Commit 11bd6cc

Browse files
authored
Merge pull request #60 from zbranzov/zbranzov/fix-webpack
Fix webpack uglify option.
2 parents ebc52a1 + b2f1858 commit 11bd6cc

File tree

5 files changed

+223
-3
lines changed

5 files changed

+223
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ node_modules
3434
publish/src
3535
publish/package
3636
demo/report/report.html
37-
demo/report/stats.json
37+
demo/report/stats.json
38+
!demo/webpack.config.js

demo/app/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import * as app from "tns-core-modules/application";
2+
import "./bundle-config";
23
app.start({ moduleName: "main-page" });

demo/app/bundle-config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare var global: NodeJS.Global;
2+
3+
if ((<any>global).TNS_WEBPACK) {
4+
require("tns-core-modules/bundle-entry-points");
5+
6+
global.registerModule("main-page", () => require("./main-page"));
7+
global.registerModule("multifab", () => require("./multifab"));
8+
9+
// register application modules
10+
global.registerModule("nativescript-floatingactionbutton", () => require("nativescript-floatingactionbutton"));
11+
}

demo/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
"lazy": "1.0.11",
2929
"nativescript-css-loader": "~0.26.0",
3030
"nativescript-dev-typescript": "libs",
31-
"nativescript-dev-webpack": "^0.7.3",
31+
"nativescript-dev-webpack": "^0.8.0",
3232
"raw-loader": "~0.5.1",
3333
"resolve-url-loader": "~2.1.0",
3434
"tns-platform-declarations": "^3.1.0",
3535
"tslint": "~5.4.3",
3636
"typescript": "~2.3.0",
3737
"webpack": "~3.2.0",
3838
"webpack-bundle-analyzer": "^2.8.2",
39-
"webpack-sources": "~1.0.1"
39+
"webpack-sources": "~1.0.1",
40+
"nativescript-worker-loader": "~0.8.1"
4041
},
4142
"scripts": {
4243
"build.plugin": "cd ../src && npm run build",

demo/webpack.config.js

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
const { resolve, join } = require("path");
2+
3+
const webpack = require("webpack");
4+
const nsWebpack = require("nativescript-dev-webpack");
5+
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const CopyWebpackPlugin = require("copy-webpack-plugin");
7+
const ExtractTextPlugin = require("extract-text-webpack-plugin");
8+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
9+
10+
11+
const mainSheet = `app.css`;
12+
13+
module.exports = env => {
14+
const platform = getPlatform(env);
15+
16+
// Default destination inside platforms/<platform>/...
17+
const path = resolve(nsWebpack.getAppPath(platform));
18+
19+
const entry = {
20+
// Discover entry module from package.json
21+
bundle: `./${nsWebpack.getEntryModule()}`,
22+
23+
// Vendor entry with third-party libraries
24+
vendor: `./vendor`,
25+
26+
// Entry for stylesheet with global application styles
27+
[mainSheet]: `./${mainSheet}`,
28+
};
29+
30+
const rules = getRules();
31+
const plugins = getPlugins(platform, env);
32+
const extensions = getExtensions(platform);
33+
34+
const config = {
35+
context: resolve("./app"),
36+
target: nativescriptTarget,
37+
entry,
38+
output: {
39+
pathinfo: true,
40+
path,
41+
libraryTarget: "commonjs2",
42+
filename: "[name].js",
43+
},
44+
resolve: {
45+
extensions,
46+
47+
// Resolve {N} system modules from tns-core-modules
48+
modules: [
49+
"node_modules/tns-core-modules",
50+
"node_modules",
51+
],
52+
53+
alias: {
54+
'~': resolve("./app")
55+
},
56+
},
57+
node: {
58+
// Disable node shims that conflict with NativeScript
59+
"http": false,
60+
"timers": false,
61+
"setImmediate": false,
62+
"fs": "empty",
63+
},
64+
module: { rules },
65+
plugins,
66+
};
67+
68+
if (env.snapshot) {
69+
plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
70+
chunk: "vendor",
71+
projectRoot: __dirname,
72+
webpackConfig: config,
73+
targetArchs: ["arm", "arm64", "ia32"],
74+
tnsJavaClassesOptions: { packages: ["tns-core-modules" ] },
75+
useLibs: false
76+
}));
77+
}
78+
79+
return config;
80+
};
81+
82+
83+
function getPlatform(env) {
84+
return env.android ? "android" :
85+
env.ios ? "ios" :
86+
() => { throw new Error("You need to provide a target platform!") };
87+
}
88+
89+
function getRules() {
90+
return [
91+
{
92+
test: /\.html$|\.xml$/,
93+
use: [
94+
"raw-loader",
95+
]
96+
},
97+
// Root stylesheet gets extracted with bundled dependencies
98+
{
99+
test: new RegExp(mainSheet),
100+
use: ExtractTextPlugin.extract([
101+
{
102+
loader: "resolve-url-loader",
103+
options: { silent: true },
104+
},
105+
{
106+
loader: "nativescript-css-loader",
107+
options: { minimize: false }
108+
},
109+
"nativescript-dev-webpack/platform-css-loader",
110+
]),
111+
},
112+
// Other CSS files get bundled using the raw loader
113+
{
114+
test: /\.css$/,
115+
exclude: new RegExp(mainSheet),
116+
use: [
117+
"raw-loader",
118+
]
119+
},
120+
// SASS support
121+
{
122+
test: /\.scss$/,
123+
use: [
124+
"raw-loader",
125+
"resolve-url-loader",
126+
"sass-loader",
127+
]
128+
},
129+
130+
131+
// Compile TypeScript files, replace templateUrl and styleUrls.
132+
{
133+
test: /\.ts$/,
134+
loaders: [
135+
"awesome-typescript-loader",
136+
]
137+
}
138+
139+
];
140+
}
141+
142+
function getPlugins(platform, env) {
143+
let plugins = [
144+
new ExtractTextPlugin(mainSheet),
145+
146+
// Vendor libs go to the vendor.js chunk
147+
new webpack.optimize.CommonsChunkPlugin({
148+
name: ["vendor"],
149+
}),
150+
151+
// Define useful constants like TNS_WEBPACK
152+
new webpack.DefinePlugin({
153+
"global.TNS_WEBPACK": "true",
154+
}),
155+
156+
// Copy assets to out dir. Add your own globs as needed.
157+
new CopyWebpackPlugin([
158+
{ from: mainSheet },
159+
{ from: "**/*.css" },
160+
{ from: "fonts/**" },
161+
{ from: "**/*.jpg" },
162+
{ from: "**/*.png" },
163+
{ from: "**/*.xml" },
164+
], { ignore: ["App_Resources/**"] }),
165+
166+
// Generate a bundle starter script and activate it in package.json
167+
new nsWebpack.GenerateBundleStarterPlugin([
168+
"./vendor",
169+
"./bundle",
170+
]),
171+
172+
// Generate report files for bundles content
173+
new BundleAnalyzerPlugin({
174+
analyzerMode: "static",
175+
openAnalyzer: false,
176+
generateStatsFile: true,
177+
reportFilename: join(__dirname, "report", `report.html`),
178+
statsFilename: join(__dirname, "report", `stats.json`),
179+
}),
180+
];
181+
182+
if (env.uglify) {
183+
plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true }));
184+
185+
// Work around an Android issue by setting compress = false
186+
const compress = platform !== "android";
187+
plugins.push(new webpack.optimize.UglifyJsPlugin({
188+
mangle: { except: nsWebpack.uglifyMangleExcludes },
189+
compress,
190+
}));
191+
}
192+
193+
return plugins;
194+
}
195+
196+
// Resolve platform-specific modules like module.android.js
197+
function getExtensions(platform) {
198+
return Object.freeze([
199+
`.${platform}.ts`,
200+
`.${platform}.js`,
201+
".ts",
202+
".js",
203+
".css",
204+
`.${platform}.css`,
205+
]);
206+
}

0 commit comments

Comments
 (0)