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

Commit 1e6a504

Browse files
committed
fix: add webpack.common template for JS projects
fixes #113
1 parent ae55991 commit 1e6a504

4 files changed

+139
-3
lines changed

Diff for: projectFilesManager.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ function getProjectTemplates(projectDir) {
5757
if (helpers.isAngular({projectDir})) {
5858
templates["webpack.common.js.angular.template"] = "webpack.common.js";
5959
templates["tsconfig.aot.json.template"] = "tsconfig.aot.json";
60+
} else if (helpers.isTypeScript({projectDir})) {
61+
templates["webpack.common.js.typescript.template"] = "webpack.common.js";
6062
} else {
61-
templates["webpack.common.js.nativescript.template"] = "webpack.common.js";
63+
templates["webpack.common.js.javascript.template"] = "webpack.common.js";
6264
}
6365

6466
return getFullTemplatesPath(projectDir, templates);

Diff for: projectHelpers.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const path = require("path");
22
const fs = require("fs");
33

4-
const isTypeScript = packageJson => {
4+
const isTypeScript = ({projectDir, packageJson} = {}) => {
5+
packageJson = packageJson || getPackageJson(projectDir);
6+
57
return packageJson.dependencies.hasOwnProperty("typescript") ||
68
packageJson.devDependencies.hasOwnProperty("typescript") ||
79
isAngular(packageJson);
10+
811
};
912

10-
const isAngular = projectDir => {
13+
const isAngular = ({projectDir, packageJson} = {}) => {
14+
packageJson = packageJson || getPackageJson(projectDir);
15+
1116
return Object.keys(packageJson.dependencies)
1217
.some(dependency => /^@angular\b/.test(dependency));
1318
};

Diff for: webpack.common.js.javascript.template

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
var webpack = require("webpack");
2+
var nsWebpack = require("nativescript-dev-webpack");
3+
var nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
4+
var path = require("path");
5+
var CopyWebpackPlugin = require("copy-webpack-plugin");
6+
var ExtractTextPlugin = require("extract-text-webpack-plugin");
7+
8+
module.exports = function (platform, destinationApp) {
9+
if (!destinationApp) {
10+
// Default destination inside platforms/<platform>/...
11+
destinationApp = nsWebpack.getAppPath(platform);
12+
}
13+
var entry = {};
14+
// Discover entry module from package.json
15+
entry.bundle = "./" + nsWebpack.getEntryModule();
16+
// Vendor entry with third party libraries.
17+
entry.vendor = "./vendor";
18+
// app.css bundle
19+
entry["app.css"] = "./app.css";
20+
21+
var plugins = [
22+
new ExtractTextPlugin("app.css"),
23+
// Vendor libs go to the vendor.js chunk
24+
new webpack.optimize.CommonsChunkPlugin({
25+
name: ["vendor"]
26+
}),
27+
// Define useful constants like TNS_WEBPACK
28+
new webpack.DefinePlugin({
29+
"global.TNS_WEBPACK": "true",
30+
}),
31+
// Copy assets to out dir. Add your own globs as needed.
32+
new CopyWebpackPlugin([
33+
{ from: "app.css" },
34+
{ from: "css/**" },
35+
{ from: "fonts/**" },
36+
{ from: "**/*.jpg" },
37+
{ from: "**/*.png" },
38+
{ from: "**/*.xml" },
39+
], { ignore: ["App_Resources/**"] }),
40+
// Generate a bundle starter script and activate it in package.json
41+
new nsWebpack.GenerateBundleStarterPlugin([
42+
"./vendor",
43+
"./bundle",
44+
]),
45+
];
46+
47+
if (process.env.npm_config_uglify) {
48+
plugins.push(new webpack.LoaderOptionsPlugin({
49+
minimize: true
50+
}));
51+
52+
// Work around an Android issue by setting compress = false
53+
var compress = platform !== "android";
54+
plugins.push(new webpack.optimize.UglifyJsPlugin({
55+
mangle: {
56+
except: nsWebpack.uglifyMangleExcludes,
57+
},
58+
compress: compress,
59+
}));
60+
}
61+
62+
return {
63+
context: path.resolve("./app"),
64+
target: nativescriptTarget,
65+
entry: entry,
66+
output: {
67+
pathinfo: true,
68+
path: path.resolve(destinationApp),
69+
libraryTarget: "commonjs2",
70+
filename: "[name].js",
71+
},
72+
resolve: {
73+
// Resolve platform-specific modules like module.android.js
74+
extensions: [
75+
".js",
76+
".css",
77+
"." + platform + ".js",
78+
"." + platform + ".css",
79+
],
80+
// Resolve {N} system modules from tns-core-modules
81+
modules: [
82+
"node_modules/tns-core-modules",
83+
"node_modules",
84+
]
85+
},
86+
node: {
87+
// Disable node shims that conflict with NativeScript
88+
"http": false,
89+
"timers": false,
90+
"setImmediate": false,
91+
"fs": "empty",
92+
},
93+
module: {
94+
loaders: [
95+
{
96+
test: /\.html$/,
97+
loader: "raw-loader"
98+
},
99+
// Root app.css file gets extracted with bundled dependencies
100+
{
101+
test: /app\.css$/,
102+
loader: ExtractTextPlugin.extract([
103+
"resolve-url-loader",
104+
"nativescript-css-loader",
105+
"nativescript-dev-webpack/platform-css-loader",
106+
]),
107+
},
108+
// Other CSS files get bundled using the raw loader
109+
{
110+
test: /\.css$/,
111+
exclude: /app\.css$/,
112+
loaders: [
113+
"raw-loader",
114+
]
115+
},
116+
// SASS support
117+
{
118+
test: /\.scss$/,
119+
loaders: [
120+
"raw-loader",
121+
"resolve-url-loader",
122+
"sass-loader",
123+
]
124+
},
125+
]
126+
},
127+
plugins: plugins,
128+
};
129+
};
File renamed without changes.

0 commit comments

Comments
 (0)