Skip to content

Commit 23f25fa

Browse files
committed
add webpack config to git
1 parent befce66 commit 23f25fa

File tree

3 files changed

+447
-1
lines changed

3 files changed

+447
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ demo/lib
5757
demo-ng/lib
5858
hooks
5959
*.log
60-
report
60+
report
61+
!webpack.*.js

demo-ng/webpack.config.js

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

0 commit comments

Comments
 (0)