-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.common.js
166 lines (159 loc) · 5.78 KB
/
webpack.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var webpack = require("webpack");
var nsWebpack = require("nativescript-dev-webpack");
var nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
var path = require("path");
var CopyWebpackPlugin = require("copy-webpack-plugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var AotPlugin = require("@ngtools/webpack").AotPlugin;
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = function (platform, destinationApp) {
if (!destinationApp) {
//Default destination inside platforms/<platform>/...
destinationApp = nsWebpack.getAppPath(platform);
}
var entry = {};
//Discover entry module from package.json
entry.bundle = "./" + nsWebpack.getEntryModule();
//Vendor entry with third party libraries.
entry.vendor = "./vendor";
//app.css bundle
entry["app.css"] = "./app.css";
var plugins = [
new BundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: false,
generateStatsFile: true,
reportFilename: path.join(__dirname, "report", platform + '-report.html'),
statsFilename: path.join(__dirname, "report", platform + '-stats.json'),
}),
new ExtractTextPlugin("app.css"),
//Vendor libs go to the vendor.js chunk
new webpack.optimize.CommonsChunkPlugin({
name: ["vendor"]
}),
//Define useful constants like TNS_WEBPACK
new webpack.DefinePlugin({
"global.TNS_WEBPACK": "true",
}),
//Copy assets to out dir. Add your own globs as needed.
new CopyWebpackPlugin([
{ from: "app.css" },
{ from: "css/**" },
{ from: "fonts/**" },
{ from: "**/*.jpg" },
{ from: "**/*.png" },
{ from: "**/*.xml" },
], { ignore: ["App_Resources/**"] }),
//Generate a bundle starter script and activate it in package.json
new nsWebpack.GenerateBundleStarterPlugin([
"./vendor",
"./bundle",
]),
// Exclude explicitly required but never declared in XML elements.
// Loader nativescript-dev-webpack/tns-xml-loader should be added for *.xml/html and *.ts files.
new nsWebpack.ExcludeUnusedElementsPlugin(),
//Angular AOT compiler
new AotPlugin({
tsConfigPath: "tsconfig.aot.json",
entryModule: path.resolve(__dirname, "app/app.module#AppModule"),
typeChecking: false
}),
new nsWebpack.StyleUrlResolvePlugin({platform}),
];
if (process.env.npm_config_uglify) {
plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true
}));
//Work around an Android issue by setting compress = false
var compress = platform !== "android";
plugins.push(new webpack.optimize.UglifyJsPlugin({
mangle: {
except: nsWebpack.uglifyMangleExcludes,
},
compress: compress,
}));
}
return {
context: path.resolve("./app"),
target: nativescriptTarget,
entry: entry,
output: {
pathinfo: true,
path: path.resolve(destinationApp),
libraryTarget: "commonjs2",
filename: "[name].js",
},
resolve: {
//Resolve platform-specific modules like module.android.js
extensions: [
".aot.ts",
".ts",
".js",
".css",
"." + platform + ".ts",
"." + platform + ".js",
"." + platform + ".css",
],
//Resolve {N} system modules from tns-core-modules
modules: [
"node_modules/tns-core-modules",
"node_modules"
]
},
node: {
//Disable node shims that conflict with NativeScript
"http": false,
"timers": false,
"setImmediate": false,
"fs": "empty",
},
module: {
loaders: [
{
test: /\.html$|\.xml$/,
loaders: [
"raw-loader",
"nativescript-dev-webpack/tns-xml-loader",
]
},
// Root app.css file gets extracted with bundled dependencies
{
test: /app\.css$/,
loader: ExtractTextPlugin.extract([
"resolve-url-loader",
"nativescript-css-loader",
"nativescript-dev-webpack/platform-css-loader",
{ loader: "./platform-sheet-loader.js", options: {platform} },
]),
},
// Other CSS files get bundled using the raw loader
{
test: /\.css$/,
exclude: /app\.css$/,
loaders: [
"raw-loader",
]
},
// Compile TypeScript files with ahead-of-time compiler.
{
test: /\.ts$/,
loaders: [
"nativescript-dev-webpack/tns-xml-loader",
"nativescript-dev-webpack/tns-aot-loader",
"@ngtools/webpack",
]
},
// SASS support
{
test: /\.scss$/,
loaders: [
"raw-loader",
"resolve-url-loader",
"sass-loader"
]
},
]
},
plugins: plugins,
};
};