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

Commit 1c92563

Browse files
committed
Rework bundling and trigger it with an external npm run command.
- Install webpack config templates for both Android and iOS. - Allow running webpack without triggering tns builds. - Let users extend the bundling process by editing script commands in their package.json files.
1 parent 27a751e commit 1c92563

13 files changed

+292
-226
lines changed

after-prepare-hook.js

-46
This file was deleted.

index.js

+37-143
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,23 @@
1-
var webpack = require("webpack");
2-
var ConcatSource = require("webpack/lib/ConcatSource");
3-
var shelljs = require("shelljs");
4-
var path = require("path");
1+
var sources = require("webpack-sources");
52
var fs = require("fs");
6-
7-
var tnsPackage = "tns-core-modules";
8-
var tnsModulesDir = path.join("node_modules", tnsPackage);
9-
10-
var platform = process.env.PLATFORM;
11-
var platformOutDir = process.env.PLATFORM_DIR;
12-
13-
exports.readPackageJson = function(dir) {
14-
var packageJson = path.join(dir, "package.json");
15-
if (shelljs.test("-f", packageJson)) {
16-
return JSON.parse(shelljs.cat(packageJson));
17-
} else {
18-
return {};
19-
}
20-
};
21-
22-
exports.getPackageMain = function(packageDir) {
23-
if (shelljs.test("-f", packageDir + ".js")) {
24-
return packageDir + ".js";
25-
}
26-
27-
var data = exports.readPackageJson(packageDir);
28-
if (data.main) {
29-
var main = data.main;
30-
if (/\.js$/i.test(main)) {
31-
return path.join(packageDir, main);
32-
} else {
33-
return path.join(packageDir, main + ".js");
34-
}
35-
} else {
36-
var indexPath = path.join(packageDir, "index.js");
37-
if (shelljs.test("-f", indexPath)) {
38-
return path.join(packageDir, "index.js");
39-
} else {
40-
throw new Error("Main module not found for: " + packageDir);
41-
}
42-
}
43-
};
44-
45-
exports.writePackageJson = function writePackageJson(dir, data) {
46-
var packageJson = path.join(dir, "package.json");
47-
fs.writeFileSync(packageJson, JSON.stringify(data, null, 4), 'utf8');
48-
};
49-
50-
function endWithJs(fileName) {
51-
if (/\.js$/i.test(fileName)) {
52-
return fileName;
53-
} else {
54-
return fileName + ".js";
55-
}
56-
}
57-
58-
exports.getEntryPoint = function getEntryPoint(appDir) {
59-
var packageJson = exports.readPackageJson(appDir);
60-
var entryModule = null;
61-
if (packageJson.bundleMain) {
62-
entryModule = endWithJs(packageJson.bundleMain);
63-
} else {
64-
entryModule = exports.getPackageMain(appDir);
65-
}
66-
67-
// Strip leading dir name and return just the submodule.
68-
return entryModule.replace(/^[^\\\/]+[\\\/]/, "./");
69-
};
70-
71-
exports.getBundleDestination = function getBundleDestination(appDir) {
72-
var packageJson = exports.readPackageJson(appDir);
73-
var bundleOutput = "bundle.js";
74-
if (packageJson.bundleOutput) {
75-
bundleOutput = packageJson.bundleOutput;
76-
}
77-
return path.join(platformOutDir, appDir, bundleOutput);
78-
};
3+
var path = require("path");
794

805
//HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]`
816
// applied to tns-java-classes.js only
82-
function FixJsonpPlugin(options) {
7+
exports.NativeScriptJsonpPlugin = function(options) {
838
}
849

85-
FixJsonpPlugin.prototype.apply = function(compiler) {
86-
compiler.plugin('compilation', function(compilation, params) {
87-
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
88-
chunks.forEach(function(chunk) {
89-
chunk.files.forEach(function(file) {
90-
if (file === "tns-java-classes.js") {
10+
exports.NativeScriptJsonpPlugin.prototype.apply = function (compiler) {
11+
compiler.plugin('compilation', function (compilation, params) {
12+
compilation.plugin("optimize-chunk-assets", function (chunks, callback) {
13+
chunks.forEach(function (chunk) {
14+
chunk.files.forEach(function (file) {
15+
if (file === "vendor.js") {
9116
var src = compilation.assets[file];
9217
var code = src.source();
9318
var match = code.match(/window\["nativescriptJsonp"\]/);
9419
if (match) {
95-
compilation.assets[file] = new ConcatSource(code.replace(/window\["nativescriptJsonp"\]/g, "global[\"nativescriptJsonp\"]"));
20+
compilation.assets[file] = new sources.ConcatSource(code.replace(/window\["nativescriptJsonp"\]/g, "global[\"nativescriptJsonp\"]"));
9621
}
9722
}
9823
});
@@ -102,66 +27,35 @@ FixJsonpPlugin.prototype.apply = function(compiler) {
10227
});
10328
};
10429

30+
exports.GenerateBundleStarterPlugin = function(bundles) {
31+
this.bundles = bundles;
32+
}
10533

106-
exports.getConfig = function getConfig(userDefined) {
107-
var platformOutDir = process.env.PLATFORM_DIR;
108-
var appOutDir = path.join(platformOutDir, "app");
109-
110-
if (!userDefined.context) {
111-
userDefined.context = path.resolve("./app");
112-
}
113-
if (!userDefined.entry) {
114-
userDefined.entry = {
115-
"bundle": exports.getEntryPoint("./app"),
116-
};
117-
if (platform === "android") {
118-
userDefined.entry["tns-java-classes"] = "./tns-java-classes";
119-
}
120-
}
121-
if (!userDefined.output) {
122-
userDefined.output = {
123-
pathinfo: true,
124-
path: appOutDir,
125-
libraryTarget: "commonjs2",
126-
filename: "[name].js",
127-
jsonpFunction: "nativescriptJsonp",
128-
};
129-
}
130-
if (!userDefined.resolve) {
131-
userDefined.resolve = {};
132-
}
133-
if (!userDefined.resolve.extensions) {
134-
userDefined.resolve.extensions = [];
135-
}
136-
userDefined.resolve.extensions.push("");
137-
userDefined.resolve.extensions.push(".js");
138-
userDefined.resolve.extensions.push("." + platform + ".js");
139-
140-
if (!userDefined.resolve.modulesDirectories) {
141-
userDefined.resolve.modulesDirectories = [];
142-
}
143-
userDefined.resolve.modulesDirectories.push("node_modules/tns-core-modules");
144-
userDefined.resolve.modulesDirectories.push("node_modules");
34+
exports.GenerateBundleStarterPlugin.prototype = {
35+
apply: function (compiler) {
36+
var plugin = this;
37+
plugin.webpackContext = compiler.options.context;
14538

146-
if (!userDefined.plugins) {
147-
userDefined.plugins = [];
148-
}
149-
if (!userDefined.resolverPlugins) {
150-
userDefined.resolverPlugins = [];
151-
}
152-
userDefined.plugins.push(
153-
new webpack.DefinePlugin({
154-
global: 'global',
155-
__dirname: '__dirname',
156-
"global.TNS_WEBPACK": 'true',
157-
})
158-
);
159-
if (platform === "android") {
160-
userDefined.plugins.push(new webpack.optimize.CommonsChunkPlugin(
161-
"tns-java-classes", "tns-java-classes.js", Infinity));
162-
}
39+
compiler.plugin('emit', function (compilation, cb) {
40+
console.log(" GenerateBundleStarterPlugin: " + plugin.webpackContext);
16341

164-
userDefined.plugins.push(new FixJsonpPlugin());
42+
compilation.assets["package.json"] = plugin.generatePackageJson();
43+
compilation.assets["starter.js"] = plugin.generateStarterModule();
16544

166-
return userDefined;
45+
cb();
46+
});
47+
},
48+
generatePackageJson: function() {
49+
var packageJsonPath = path.join(this.webpackContext, "package.json");
50+
var packageData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
51+
packageData.main = "starter";
52+
53+
return new sources.RawSource(JSON.stringify(packageData, null, 4));
54+
},
55+
generateStarterModule: function() {
56+
var moduleSource = this.bundles.map(function(bundle) {
57+
return "require(\"" + bundle + "\");";
58+
}).join("\n");
59+
return new sources.RawSource(moduleSource);
60+
},
16761
};

package.json

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-dev-webpack",
3-
"version": "0.1.2",
3+
"version": "0.2.1",
44
"main": "index",
55
"description": "",
66
"homepage": "http://www.telerik.com",
@@ -13,23 +13,10 @@
1313
"type": "git",
1414
"url": "https://github.com/NativeScript/nativescript-dev-webpack.git"
1515
},
16-
"nativescript": {
17-
"hooks": [
18-
{
19-
"type": "after-prepare",
20-
"script": "after-prepare-hook.js",
21-
"inject": true
22-
}
23-
]
24-
},
2516
"scripts": {
26-
"postinstall": "node postinstall.js",
27-
"preuninstall": "node preuninstall.js"
17+
"postinstall": "node postinstall.js"
2818
},
2919
"dependencies": {
30-
"shelljs": "^0.5.3",
31-
"webpack": "1.13.2",
32-
"nativescript-hook": "^0.2.1"
3320
},
3421
"devDependencies": {
3522
}

0 commit comments

Comments
 (0)