diff --git a/bin/install-ns-webpack b/bin/install-ns-webpack new file mode 100644 index 00000000..01adbf50 --- /dev/null +++ b/bin/install-ns-webpack @@ -0,0 +1,6 @@ +#!/usr/bin/env node +var installer = require("../installer"); + +installer.addProjectFiles(); +installer.addNpmScripts(); +installer.addProjectDependencies(); diff --git a/bin/install-ns-webpack.cmd b/bin/install-ns-webpack.cmd new file mode 100644 index 00000000..5de779b8 --- /dev/null +++ b/bin/install-ns-webpack.cmd @@ -0,0 +1 @@ +@node %~dp0\install-ns-webpack %* diff --git a/bin/remove-ns-webpack b/bin/remove-ns-webpack new file mode 100644 index 00000000..d3daba03 --- /dev/null +++ b/bin/remove-ns-webpack @@ -0,0 +1,6 @@ +#!/usr/bin/env node +var installer = require("../installer"); + +installer.removeProjectFiles(); +installer.removeNpmScripts(); +installer.removeProjectDependencies(); diff --git a/bin/remove-ns-webpack.cmd b/bin/remove-ns-webpack.cmd new file mode 100644 index 00000000..5de779b8 --- /dev/null +++ b/bin/remove-ns-webpack.cmd @@ -0,0 +1 @@ +@node %~dp0\install-ns-webpack %* diff --git a/index.js b/index.js index 1253ddc5..92c44433 100644 --- a/index.js +++ b/index.js @@ -17,11 +17,11 @@ if (isAngular) { //HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]` // applied to tns-java-classes.js only -exports.NativeScriptJsonpPlugin = function (options) { +exports.NativeScriptJsonpPlugin = function () { }; exports.NativeScriptJsonpPlugin.prototype.apply = function (compiler) { - compiler.plugin("compilation", function (compilation, params) { + compiler.plugin("compilation", function (compilation) { compilation.plugin("optimize-chunk-assets", function (chunks, callback) { chunks.forEach(function (chunk) { chunk.files.forEach(function (file) { @@ -56,8 +56,8 @@ exports.ExcludeUnusedElementsPlugin.prototype.apply = function (compiler) { if (result.context.indexOf("tns-core-modules") === -1) { if (result.contextInfo.issuer && - result.contextInfo.issuer.indexOf("element-registry") !== -1 && global["ELEMENT_REGISTRY"] && - !global["ELEMENT_REGISTRY"][result.request]) { + result.contextInfo.issuer.indexOf("element-registry") !== -1 && global.ELEMENT_REGISTRY && + !global.ELEMENT_REGISTRY[result.request]) { return callback(); } else { @@ -65,8 +65,8 @@ exports.ExcludeUnusedElementsPlugin.prototype.apply = function (compiler) { } } - if (result.contextInfo.issuer.indexOf("bundle-entry-points") !== -1 && global["ELEMENT_REGISTRY"] && - !global["ELEMENT_REGISTRY"][result.request]) { + if (result.contextInfo.issuer.indexOf("bundle-entry-points") !== -1 && global.ELEMENT_REGISTRY && + !global.ELEMENT_REGISTRY[result.request]) { return callback(); } @@ -85,8 +85,6 @@ exports.GenerateBundleStarterPlugin.prototype = { plugin.webpackContext = compiler.options.context; compiler.plugin("emit", function (compilation, cb) { - console.log(" GenerateBundleStarterPlugin: " + plugin.webpackContext); - compilation.assets["package.json"] = plugin.generatePackageJson(); compilation.assets["starter.js"] = plugin.generateStarterModule(); @@ -135,12 +133,20 @@ exports.getAppPath = function (platform) { }; exports.uglifyMangleExcludes = [ + //Control names + "AbsoluteLayout", "ActionBar", "ActivityIndicator", "Button", "DatePicker", + "DockLayout", + "EditableTextBase", + "FlexboxLayout", + "GridLayout", "Image", "Label", + "Layout", + "LayoutBase", "ListPicker", "ListView", "Page", @@ -148,6 +154,7 @@ exports.uglifyMangleExcludes = [ "SearchBar", "SegmentedBar", "Slider", + "StackLayout", "Switch", "TabView", "TextBase", @@ -155,6 +162,70 @@ exports.uglifyMangleExcludes = [ "TextView", "TimePicker", "View", + "WrapLayout", + + //Android native class extenders + "BroadcastReceiver", + "CustomTypefaceSpan", + "DialogFragmentClassInner", + "FragmentClass", + "ListViewAdapter", + "LruBitmapCache", + "NativeScriptActivity", + "OurTabHost", + "PageChangedListener", + "PagerAdapterClassInner", + "PinchGestureListener", + "SegmentedBarColorDrawable", + "SwipeGestureListener", + "SwipeGestureListener", + "TapAndDoubleTapGestureListener", + "WebViewClientClassInner", + + //iOS native class extenders + "AnimatedTransitioning", + "AnimationDelegateImpl", + "DataSource", + "FrameHandlerImpl", + "ListPickerDataSource", + "ListPickerDelegateImpl", + "ListViewCell", + "LocationListenerImpl", + "NSURLSessionTaskDelegateImpl", + "NotificationObserver", + "ObserverClass", + "Responder", + "SelectionHandlerImpl", + "SliderChangeHandlerImpl", + "SwitchChangeHandlerImpl", + "TapBarItemHandlerImpl", + "TapHandlerImpl", + "TimerTargetImpl", + "TouchGestureRecognizer", + "TransitionDelegate", + "UIActionSheetDelegateImpl", + "UIAlertViewDelegateImpl", + "UIDatePickerChangeHandlerImpl", + "UIDocumentInteractionControllerDelegateImpl", + "UIGestureRecognizerDelegateImpl", + "UIGestureRecognizerImpl", + "UIImagePickerControllerDelegateImpl", + "UINavigationControllerAnimatedDelegate", + "UINavigationControllerDelegateImpl", + "UINavigationControllerImpl", + "UIScrollViewDelegateImpl", + "UISearchBarDelegateImpl", + "UITabBarControllerDelegateImpl", + "UITabBarControllerImpl", + "UITableViewDelegateImpl", + "UITableViewRowHeightDelegateImpl", + "UITextFieldDelegateImpl", + "UITextFieldImpl", + "UITextViewDelegateImpl", + "UITimePickerChangeHandlerImpl", + "UIViewControllerImpl", + "UIWebViewDelegateImpl", + "Window", ]; function getPackageJsonEntry() { diff --git a/installer.js b/installer.js new file mode 100644 index 00000000..ebf97ea6 --- /dev/null +++ b/installer.js @@ -0,0 +1,263 @@ +var path = require("path"); +var fs = require("fs"); +var childProcess = require("child_process"); + +var projectDir = path.dirname(path.dirname(__dirname)); +var appDir = path.join(projectDir, "app"); + +var packageJsonPath = path.join(projectDir, "package.json"); +var packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); + +var isAngular = Object.keys(packageJson.dependencies).filter(function (dependency) { + return /^@angular\b/.test(dependency); +}).length > 0; + +var isTypeScript = fs.existsSync(path.join(projectDir, "tsconfig.json")); +if (isAngular) { + isTypeScript = true; +} + +function getProjectTemplates() { + var templates = { + "webpack.android.js.template": "webpack.android.js", + "webpack.ios.js.template": "webpack.ios.js", + }; + + if (isAngular) { + templates["webpack.common.js.angular.template"] = "webpack.common.js"; + templates["tsconfig.aot.json.template"] = "tsconfig.aot.json"; + } else { + templates["webpack.common.js.nativescript.template"] = "webpack.common.js"; + } + return templates; +} + +function getAppTemplates() { + var templates = { + "vendor-platform.android.ts.template": tsOrJs("vendor-platform.android"), + "vendor-platform.ios.ts.template": tsOrJs("vendor-platform.ios"), + }; + + if (isAngular) { + templates["vendor.ts.angular.template"] = tsOrJs("vendor"); + } else { + templates["vendor.ts.nativescript.template"] = tsOrJs("vendor"); + } + return templates; +} + +function addProjectFiles() { + var projectTemplates = getProjectTemplates(); + Object.keys(projectTemplates).forEach(function(templateName) { + var templateDestination = projectTemplates[templateName]; + copyProjectTemplate(templateName, templateDestination); + }); + + var appTemplates = getAppTemplates(); + Object.keys(appTemplates).forEach(function(templateName) { + var templateDestination = appTemplates[templateName]; + copyAppTemplate(templateName, templateDestination); + }); +} +exports.addProjectFiles = addProjectFiles; + +function removeProjectFiles() { + var projectTemplates = getProjectTemplates(); + Object.keys(projectTemplates).forEach(function(templateName) { + var templateDestination = projectTemplates[templateName]; + deleteProjectFile(templateDestination); + }); + + var appTemplates = getAppTemplates(); + Object.keys(appTemplates).forEach(function(templateName) { + var templateDestination = appTemplates[templateName]; + deleteAppFile(templateDestination); + }); +} +exports.removeProjectFiles = removeProjectFiles; + +function getScriptTemplates() { + return { + "clean-[PLATFORM]": "tns clean-app [PLATFORM]", + "prewebpack-[PLATFORM]": "npm run clean-[PLATFORM]", + "webpack-[PLATFORM]": "webpack --config=webpack.[PLATFORM].js --progress", + "prestart-[PLATFORM]-bundle": "npm run webpack-[PLATFORM]", + "start-[PLATFORM]-bundle": "tns run [PLATFORM] --bundle --disable-npm-install", + "prebuild-[PLATFORM]-bundle": "npm run webpack-[PLATFORM]", + "build-[PLATFORM]-bundle": "tns build [PLATFORM] --bundle --disable-npm-install", + }; +} + +function addNpmScripts() { + var scriptTemplates = getScriptTemplates(); + Object.keys(scriptTemplates).forEach(function(templateName) { + addPlatformScript(packageJson, templateName, scriptTemplates[templateName]); + }); +} +exports.addNpmScripts = addNpmScripts; + +function removeNpmScripts() { + var scriptTemplates = getScriptTemplates(); + Object.keys(scriptTemplates).forEach(function(templateName) { + removePlatformScripts(packageJson, templateName); + }); +} +exports.removeNpmScripts = removeNpmScripts; + +function getProjectDependencies() { + var dependencies = { + "webpack": "2.2.0", + "webpack-sources": "~0.1.3", + "copy-webpack-plugin": "~3.0.1", + "raw-loader": "~0.5.1", + "nativescript-css-loader": "~0.26.0", + "resolve-url-loader": "~1.6.0", + "extract-text-webpack-plugin": "~2.0.0-beta.4", + }; + + if (isAngular) { + dependencies["@angular/compiler-cli"] = "~2.4.3"; + dependencies["@ngtools/webpack"] = "1.2.4"; + dependencies["typescript"] = "^2.0.10"; + dependencies["htmlparser2"] = "~3.9.2"; + } else { + dependencies["awesome-typescript-loader"] = "~3.0.0-beta.9"; + } + return dependencies; +} + +function addProjectDependencies() { + configureDevDependencies(packageJson, function (add) { + var dependencies = getProjectDependencies(); + Object.keys(dependencies).forEach(function(dependencyName) { + add(dependencyName, dependencies[dependencyName]); + }); + }); +} +exports.addProjectDependencies = addProjectDependencies; + +function removeProjectDependencies() { + configureDevDependencies(packageJson, function (add, remove) { + var dependencies = getProjectDependencies(); + Object.keys(dependencies).forEach(function(dependencyName) { + remove(dependencyName); + }); + }); +} +exports.removeProjectDependencies = removeProjectDependencies; + + +function addPlatformScript(packageJson, nameTemplate, commandTemplate) { + if (!packageJson.scripts) { + packageJson.scripts = {}; + } + + var scripts = packageJson.scripts; + ["android", "ios"].forEach(function (platform) { + var name = nameTemplate.replace(/\[PLATFORM\]/g, platform); + var command = commandTemplate.replace(/\[PLATFORM\]/g, platform); + if (!scripts[name]) { + console.log("Registering script: " + name); + scripts[name] = command; + } + }); +} + +function removePlatformScripts(packageJson, nameTemplate) { + if (!packageJson.scripts) { + return; + } + + var scripts = packageJson.scripts; + ["android", "ios"].forEach(function (platform) { + var name = nameTemplate.replace(/\[PLATFORM\]/g, platform); + console.log("Removing script: " + name); + delete scripts[name]; + }); +} + +function configureDevDependencies(packageJson, adderCallback) { + var pendingNpmInstall = false; + if (!packageJson.devDependencies) { + packageJson.devDependencies = {}; + } + var dependencies = packageJson.devDependencies; + + adderCallback(function (name, version) { + if (!dependencies[name]) { + dependencies[name] = version; + console.info("Adding dev dependency: " + name + "@" + version); + pendingNpmInstall = true; + } else { + console.info("Dev dependency: '" + name + "' already added. Leaving version: " + dependencies[name]); + } + }, function(name) { + console.info("Removing dev dependency: " + name); + delete dependencies[name]; + }); + + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); + + if (pendingNpmInstall) { + console.info("Installing new dependencies..."); + //Run `npm install` after everything else. + setTimeout(function () { + var spawnArgs = []; + if (/^win/.test(process.platform)) { + spawnArgs = ["cmd.exe", ["/c", "npm", "install"]]; + } else { + spawnArgs = ["npm", ["install"]]; + } + spawnArgs.push({ cwd: projectDir, stdio: "inherit" }); + var npm = childProcess.spawn.apply(null, spawnArgs); + npm.on("close", function (code) { + process.exit(code); + }); + }, 100); + } +} + +function tsOrJs(name) { + if (isTypeScript) { + return name + ".ts"; + } else { + return name + ".js"; + } +} + +function copyProjectTemplate(templateName, projectPath) { + var destinationPath = path.join(projectDir, projectPath); + copyTemplate(templateName, destinationPath); +} + +function deleteProjectFile(projectPath) { + var destinationPath = path.join(projectDir, projectPath); + deleteFile(destinationPath); +} + +function copyAppTemplate(templateName, appPath) { + var destinationPath = path.join(appDir, appPath); + copyTemplate(templateName, destinationPath); +} + +function deleteAppFile(appPath) { + var destinationPath = path.join(appDir, appPath); + deleteFile(destinationPath); +} + +function deleteFile(destinationPath) { + if (fs.existsSync(destinationPath)) { + console.log("Deleting file: " + destinationPath); + fs.unlink(destinationPath); + } +} + +function copyTemplate(templateName, destinationPath) { + var templatePath = path.join(__dirname, templateName); + // Create destination file, only if not present. + if (!fs.existsSync(destinationPath)) { + console.log("Creating file: " + destinationPath); + var content = fs.readFileSync(templatePath, "utf8"); + fs.writeFileSync(destinationPath, content); + } +} diff --git a/nativescript-target/NsNodeGlobalsPlugin.js b/nativescript-target/NsNodeGlobalsPlugin.js new file mode 100644 index 00000000..217c6ba0 --- /dev/null +++ b/nativescript-target/NsNodeGlobalsPlugin.js @@ -0,0 +1,26 @@ +// HACK: Prevent webpack from replacing "global" +// Fixes StackOverflow error caused by DefinePlugin with webpack 2.2+ + +var ConstDependency = require("webpack/lib/dependencies/ConstDependency"); + +function NsNodeGlobalsPlugin() {} +NsNodeGlobalsPlugin.prototype.apply = function(compiler) { + compiler.plugin("compilation", function(compilation, params) { + params.normalModuleFactory.plugin("parser", function(parser, parserOptions) { + parser.plugin("expression global", function(expr) { + var dep = new ConstDependency("global", expr.range); + dep.loc = expr.loc; + this.state.current.addDependency(dep); + return true; + }); + parser.plugin("expression __dirname", function(expr) { + var dep = new ConstDependency("__dirname", expr.range); + dep.loc = expr.loc; + this.state.current.addDependency(dep); + return true; + }); + }); + }); +}; + +module.exports = NsNodeGlobalsPlugin; diff --git a/nativescript-target/index.js b/nativescript-target/index.js index 68a5b519..b426b223 100644 --- a/nativescript-target/index.js +++ b/nativescript-target/index.js @@ -2,6 +2,7 @@ module.exports = function nativescriptTarget(compiler) { var options = this; var webpackLib = "webpack/lib"; + var NsNodeGlobalsPlugin = require("./NsNodeGlobalsPlugin"); // Custom template plugin var NsJsonpTemplatePlugin = require("./NsJsonpTemplatePlugin"); @@ -10,6 +11,7 @@ module.exports = function nativescriptTarget(compiler) { var LoaderTargetPlugin = require(webpackLib + "/LoaderTargetPlugin"); compiler.apply( + new NsNodeGlobalsPlugin(), new NsJsonpTemplatePlugin(options.output), new FunctionModulePlugin(options.output), new NodeSourcePlugin(options.node), diff --git a/package.json b/package.json index f1257e66..862e4bc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-dev-webpack", - "version": "0.3.2", + "version": "0.3.3", "main": "index", "description": "", "homepage": "http://www.telerik.com", @@ -16,6 +16,10 @@ "scripts": { "postinstall": "node postinstall.js" }, + "bin": { + "install-ns-webpack": "./bin/install-ns-webpack", + "remove-ns-webpack": "./bin/remove-ns-webpack" + }, "dependencies": {}, "devDependencies": {} } diff --git a/postinstall.js b/postinstall.js index 1f4dfed0..d92ae7b4 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,144 +1,5 @@ -var path = require("path"); -var fs = require("fs"); -var childProcess = require("child_process"); +var installer = require("./installer"); -var projectDir = path.dirname(path.dirname(__dirname)); -var appDir = path.join(projectDir, "app"); - -var packageJsonPath = path.join(projectDir, "package.json"); -var packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); - -var isAngular = Object.keys(packageJson.dependencies).filter(function (dependency) { - return /^@angular\b/.test(dependency); -}).length > 0; - -var isTypeScript = fs.existsSync(path.join(projectDir, "tsconfig.json")); -if (isAngular) { - isTypeScript = true; -} - -copyProjectTemplate("webpack.android.js.template", "webpack.android.js"); -copyProjectTemplate("webpack.ios.js.template", "webpack.ios.js"); - -if (isAngular) { - copyProjectTemplate("webpack.common.js.angular.template", "webpack.common.js"); - copyProjectTemplate("tsconfig.aot.json.template", "tsconfig.aot.json"); -} else { - copyProjectTemplate("webpack.common.js.nativescript.template", "webpack.common.js"); -} - -copyAppTemplate("vendor-platform.android.ts.template", tsOrJs("vendor-platform.android")); -copyAppTemplate("vendor-platform.ios.ts.template", tsOrJs("vendor-platform.ios")); -if (isAngular) { - copyAppTemplate("vendor.ts.angular.template", tsOrJs("vendor")); -} else { - copyAppTemplate("vendor.ts.nativescript.template", tsOrJs("vendor")); -} - -addPlatformScript(packageJson, "clean-[PLATFORM]", "tns clean-app [PLATFORM]"); -addPlatformScript(packageJson, "prewebpack-[PLATFORM]", "npm run clean-[PLATFORM]"); -addPlatformScript(packageJson, "webpack-[PLATFORM]", "webpack --config=webpack.[PLATFORM].js --progress"); -addPlatformScript(packageJson, "prestart-[PLATFORM]-bundle", "npm run webpack-[PLATFORM]"); -addPlatformScript(packageJson, "start-[PLATFORM]-bundle", "tns run [PLATFORM] --bundle --disable-npm-install"); -addPlatformScript(packageJson, "prebuild-[PLATFORM]-bundle", "npm run webpack-[PLATFORM]"); -addPlatformScript(packageJson, "build-[PLATFORM]-bundle", "tns build [PLATFORM] --bundle --disable-npm-install"); - -configureDevDependencies(packageJson, function (add) { - add("webpack", "~2.1.0-beta.27"); - add("webpack-sources", "~0.1.3"); - add("copy-webpack-plugin", "~3.0.1"); - add("raw-loader", "~0.5.1"); - add("nativescript-css-loader", "~0.26.0"); - add("resolve-url-loader", "~1.6.0"); - add("extract-text-webpack-plugin", "~2.0.0-beta.4"); - - if (isAngular) { - add("@angular/compiler-cli", "2.4.3"); - add("@ngtools/webpack", "1.2.1"); - add("typescript", "^2.0.10"); - add("htmlparser2", "~3.9.2"); - } else { - add("awesome-typescript-loader", "~3.0.0-beta.9"); - } -}); - -fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4)); - -function addPlatformScript(packageJson, nameTemplate, commandTemplate) { - if (!packageJson.scripts) { - packageJson.scripts = {}; - } - - var scripts = packageJson.scripts; - ["android", "ios"].forEach(function (platform) { - var name = nameTemplate.replace(/\[PLATFORM\]/g, platform); - var command = commandTemplate.replace(/\[PLATFORM\]/g, platform); - if (!scripts[name]) { - scripts[name] = command; - console.log("Registering script: " + name); - } - }); -} - -function configureDevDependencies(packageJson, adderCallback) { - var pendingNpmInstall = false; - if (!packageJson.devDependencies) { - packageJson.devDependencies = {}; - } - var dependencies = packageJson.devDependencies; - - adderCallback(function (name, version) { - if (!dependencies[name]) { - dependencies[name] = version; - console.info("Adding dev dependency: " + name + "@" + version); - pendingNpmInstall = true; - } else { - console.info("Dev dependency: '" + name + "' already added. Leaving version: " + dependencies[name]); - } - }); - - if (pendingNpmInstall) { - console.info("Installing new dependencies..."); - //Run `npm install` after everything else. - setTimeout(function () { - var spawnArgs = []; - if (/^win/.test(process.platform)) { - spawnArgs = ["cmd.exe", ["/c", "npm", "install"]]; - } else { - spawnArgs = ["npm", ["install"]]; - } - spawnArgs.push({ cwd: projectDir, stdio: "inherit" }); - var npm = childProcess.spawn.apply(null, spawnArgs); - npm.on("close", function (code) { - process.exit(code); - }); - }, 100); - } -} - -function tsOrJs(name) { - if (isTypeScript) { - return name + ".ts"; - } else { - return name + ".js"; - } -} - -function copyProjectTemplate(templateName, projectPath) { - var destinationPath = path.join(projectDir, projectPath); - copyTemplate(templateName, destinationPath); -} - -function copyAppTemplate(templateName, appPath) { - var destinationPath = path.join(appDir, appPath); - copyTemplate(templateName, destinationPath); -} - -function copyTemplate(templateName, destinationPath) { - var templatePath = path.join(__dirname, templateName); - // Create destination file, only if not present. - if (!fs.existsSync(destinationPath)) { - var content = fs.readFileSync(templatePath, "utf8"); - fs.writeFileSync(destinationPath, content); - } -} +installer.addProjectFiles(); +installer.addNpmScripts(); +installer.addProjectDependencies(); diff --git a/tns-xml-loader.js b/tns-xml-loader.js index 4779aa3e..f3671760 100644 --- a/tns-xml-loader.js +++ b/tns-xml-loader.js @@ -20,25 +20,59 @@ if (!global[ELEMENT_REGISTRY]) { }; } -module.exports = function (source, map) { +function parseResource(source, map) { this.cacheable(); - var loader = this; + let templateSource; + try { + templateSource = getTemplateSource(this.resourcePath, source); + } catch(e) { + this.emitWarning(e.message); + return this.callback(null, source, map); + } + + if (templateSource === "") { + return this.callback(null, source, map); + } var parser = new htmlparser.Parser({ onopentag: function (name, attribs) { // kebab-case to CamelCase var elementName = name.split("-").map(function (s) { return s[0].toUpperCase() + s.substring(1); }).join(""); + // Module path from element name var modulePath = MODULES[elementName] || UI_PATH + (elementName.toLowerCase().indexOf("layout") !== -1 ? "layouts/" : "") + elementName.split(/(?=[A-Z])/).join("-").toLowerCase(); + // Update ELEMENT_REGISTRY global[ELEMENT_REGISTRY][modulePath] = elementName; } }, { decodeEntities: true, lowerCaseTags: false }); - parser.write(source); + + parser.write(templateSource); parser.end(); - this.callback(null, source, map); -}; \ No newline at end of file + return this.callback(null, source, map); +} + +function getTemplateSource(path, source) { + if (isTemplate(path)) { + return source; + } else if (isComponent(path)) { + const templateMatcher = /template\s*:\s*([`'"])((.|\n)*?)\1/; + return templateMatcher.test(source) ? source.replace(templateMatcher, "$2") : ""; + } else { + throw new Error(`The NativeScript XML loader must be used with HTML, XML or TypeScript files`); + } +} + +function isComponent(resource) { + return /\.ts$/i.test(resource); +} + +function isTemplate(resource) { + return /\.html$|\.xml$/i.test(resource); +} + +module.exports = parseResource; diff --git a/vendor-platform.android.ts.template b/vendor-platform.android.ts.template index e0679c8b..ba9742fd 100644 --- a/vendor-platform.android.ts.template +++ b/vendor-platform.android.ts.template @@ -1,12 +1,12 @@ -//Resolve JavaScript classes that extend a Java class, and need to resolve -//their JavaScript module from a bundled script. For example: -//NativeScriptApplication, NativeScriptActivity, etc. +// Resolve JavaScript classes that extend a Java class, and need to resolve +// their JavaScript module from a bundled script. For example: +// NativeScriptApplication, NativeScriptActivity, etc. // -//This module gets bundled together with the rest of the app code and the -//`require` calls get resolved to the correct bundling import call. +// This module gets bundled together with the rest of the app code and the +// `require` calls get resolved to the correct bundling import call. // -//At runtime the module gets loaded *before* the rest of the app code, so code -//placed here needs to be careful about its dependencies. +// At runtime the module gets loaded *before* the rest of the app code, so code +// placed here needs to be careful about its dependencies. require("application"); require("ui/frame"); diff --git a/vendor.ts.angular.template b/vendor.ts.angular.template index 6f23bd36..0d969654 100644 --- a/vendor.ts.angular.template +++ b/vendor.ts.angular.template @@ -1,13 +1,13 @@ require("./vendor-platform"); -require('reflect-metadata'); -require('@angular/platform-browser'); -require('@angular/core'); -require('@angular/common'); -require('@angular/forms'); -require('@angular/http'); -require('@angular/router'); +require("reflect-metadata"); +require("@angular/platform-browser"); +require("@angular/core"); +require("@angular/common"); +require("@angular/forms"); +require("@angular/http"); +require("@angular/router"); -require('nativescript-angular/platform-static'); -require('nativescript-angular/forms'); -require('nativescript-angular/router'); +require("nativescript-angular/platform-static"); +require("nativescript-angular/forms"); +require("nativescript-angular/router"); diff --git a/webpack.common.js.angular.template b/webpack.common.js.angular.template index 61850fa7..550163c3 100644 --- a/webpack.common.js.angular.template +++ b/webpack.common.js.angular.template @@ -27,8 +27,6 @@ module.exports = function (platform, destinationApp) { }), //Define useful constants like TNS_WEBPACK new webpack.DefinePlugin({ - global: "global", - __dirname: "__dirname", "global.TNS_WEBPACK": "true", }), //Copy assets to out dir. Add your own globs as needed. @@ -47,7 +45,7 @@ module.exports = function (platform, destinationApp) { ]), // Exclude explicitly required but never declared in XML elements. - // Loader nativescript-dev-webpack/tns-xml-loader should be added for *.xml/html files. + // Loader nativescript-dev-webpack/tns-xml-loader should be added for *.xml/html and *.ts files. new nsWebpack.ExcludeUnusedElementsPlugin(), //Angular AOT compiler @@ -60,6 +58,10 @@ module.exports = function (platform, destinationApp) { ]; 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({ @@ -109,7 +111,7 @@ module.exports = function (platform, destinationApp) { test: /\.html$|\.xml$/, loaders: [ "raw-loader", - 'nativescript-dev-webpack/tns-xml-loader' + "nativescript-dev-webpack/tns-xml-loader", ] }, // Root app.css file gets extracted with bundled dependencies @@ -133,6 +135,7 @@ module.exports = function (platform, destinationApp) { { test: /\.ts$/, loaders: [ + "nativescript-dev-webpack/tns-xml-loader", "nativescript-dev-webpack/tns-aot-loader", "@ngtools/webpack", ] diff --git a/webpack.common.js.nativescript.template b/webpack.common.js.nativescript.template index be887e3b..5f331941 100644 --- a/webpack.common.js.nativescript.template +++ b/webpack.common.js.nativescript.template @@ -26,8 +26,6 @@ module.exports = function (platform, destinationApp) { }), //Define useful constants like TNS_WEBPACK new webpack.DefinePlugin({ - global: "global", - __dirname: "__dirname", "global.TNS_WEBPACK": "true", }), //Copy assets to out dir. Add your own globs as needed. @@ -47,6 +45,10 @@ module.exports = function (platform, destinationApp) { ]; 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({