diff --git a/index.js b/index.js index 0d885a7d..f2e80e46 100644 --- a/index.js +++ b/index.js @@ -1,84 +1,19 @@ -var sources = require("webpack-sources"); -var fs = require("fs"); -var path = require("path"); +const path = require("path"); +const { existsSync } = require("fs"); -var projectDir = path.dirname(path.dirname(__dirname)); -var packageJsonPath = path.join(projectDir, "package.json"); -var packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); +const { getPackageJson, isAngular } = require("./projectHelpers"); +const { sanitize } = require("./utils"); -var isAngular = Object.keys(packageJson.dependencies).filter(function (dependency) { - return /^@angular\b/.test(dependency); -}).length > 0; +const PROJECT_DIR = path.dirname(path.dirname(__dirname)); +const APP_DIR = path.join(PROJECT_DIR, "app"); +Object.assign(exports, require('./plugins')); -if (isAngular) { - exports.UrlResolvePlugin = require("./resource-resolver-plugins/UrlResolvePlugin"); +if (isAngular({projectDir: PROJECT_DIR})) { + Object.assign(exports, require('./plugins/angular')); } -//HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]` -// applied to tns-java-classes.js only -exports.NativeScriptJsonpPlugin = function () { -}; - -exports.NativeScriptJsonpPlugin.prototype.apply = function (compiler) { - compiler.plugin("compilation", function (compilation) { - compilation.plugin("optimize-chunk-assets", function (chunks, callback) { - chunks.forEach(function (chunk) { - chunk.files.forEach(function (file) { - if (file === "vendor.js") { - var src = compilation.assets[file]; - var code = src.source(); - var match = code.match(/window\["nativescriptJsonp"\]/); - if (match) { - compilation.assets[file] = new sources.ConcatSource(code.replace(/window\["nativescriptJsonp"\]/g, "global[\"nativescriptJsonp\"]")); - } - } - }); - }); - callback(); - }); - }); -}; - -exports.GenerateBundleStarterPlugin = function (bundles) { - this.bundles = bundles; -}; - -exports.GenerateBundleStarterPlugin.prototype = { - apply: function (compiler) { - var plugin = this; - plugin.webpackContext = compiler.options.context; - - compiler.plugin("emit", function (compilation, cb) { - compilation.assets["package.json"] = plugin.generatePackageJson(); - compilation.assets["starter.js"] = plugin.generateStarterModule(); - plugin.generateTnsJavaClasses(compilation); - - cb(); - }); - }, - generateTnsJavaClasses: function (compilation) { - const path = compilation.compiler.outputPath; - const isAndroid = path.indexOf("android") > -1; - - if (isAndroid && !compilation.assets["tns-java-classes.js"]) { - compilation.assets["tns-java-classes.js"] = new sources.RawSource(""); - } - }, - generatePackageJson: function () { - var packageJsonPath = path.join(this.webpackContext, "package.json"); - var packageData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); - packageData.main = "starter"; - - return new sources.RawSource(JSON.stringify(packageData, null, 4)); - }, - generateStarterModule: function () { - var moduleSource = this.bundles.map(function (bundle) { - return "require(\"" + bundle + "\");"; - }).join("\n"); - return new sources.RawSource(moduleSource); - }, -}; +exports.uglifyMangleExcludes = require("./mangle-excludes"); exports.getEntryModule = function () { const maybePackageJsonEntry = getPackageJsonEntry(); @@ -90,46 +25,30 @@ exports.getEntryModule = function () { return maybeAotEntry || maybePackageJsonEntry; }; -exports.getAppPath = function (platform) { - var projectDir = path.dirname(path.dirname(__dirname)); - +exports.getAppPath = platform => { if (/ios/i.test(platform)) { - var appName = path.basename(projectDir); - var sanitizedName = appName.split("").filter(function (c) { - return /[a-zA-Z0-9]/.test(c); - }).join(""); - return "platforms/ios/" + sanitizedName + "/app"; + const appName = path.basename(PROJECT_DIR); + const sanitizedName = sanitize(appName); + + return `platforms/ios/${sanitizedName}/app`; } else if (/android/i.test(platform)) { - return path.join(projectDir, "platforms/android/src/main/assets/app"); + return path.join(PROJECT_DIR, "platforms/android/src/main/assets/app"); } else { - throw new Error("Invalid platform: " + platform); + throw new Error(`Invalid platform: ${platform}`); } }; -exports.uglifyMangleExcludes = require("./mangle-excludes"); - function getPackageJsonEntry() { - const packageJsonSource = getAppPackageJsonSource(); + const packageJsonSource = getPackageJson(APP_DIR); const entry = packageJsonSource.main; return entry ? entry.replace(/\.js$/i, "") : null; } -function getAppPackageJsonSource() { - const projectDir = getProjectDir(); - const appPackageJsonPath = path.join(projectDir, "app", "package.json"); - - return JSON.parse(fs.readFileSync(appPackageJsonPath, "utf8")); -} - function getAotEntry(entry) { const aotEntry = `${entry}.aot.ts`; - const projectDir = getProjectDir(); - const aotEntryPath = path.join(projectDir, "app", aotEntry); + const aotEntryPath = path.join(APP_DIR, aotEntry); - return fs.existsSync(aotEntryPath) ? aotEntry : null; + return existsSync(aotEntryPath) ? aotEntry : null; } -function getProjectDir() { - return path.dirname(path.dirname(__dirname)); -} diff --git a/plugins/GenerateBundleStarterPlugin.js b/plugins/GenerateBundleStarterPlugin.js new file mode 100644 index 00000000..f97127a8 --- /dev/null +++ b/plugins/GenerateBundleStarterPlugin.js @@ -0,0 +1,47 @@ +const { RawSource } = require("webpack-sources"); +const { getPackageJson } = require("../projectHelpers"); + +exports.GenerateBundleStarterPlugin = (function() { + function GenerateBundleStarterPlugin(bundles) { + this.bundles = bundles; + }; + + GenerateBundleStarterPlugin.prototype.apply = function(compiler) { + const plugin = this; + plugin.webpackContext = compiler.options.context; + + compiler.plugin("emit", function (compilation, cb) { + compilation.assets["package.json"] = plugin.generatePackageJson(); + compilation.assets["starter.js"] = plugin.generateStarterModule(); + plugin.generateTnsJavaClasses(compilation); + + cb(); + }); + } + + GenerateBundleStarterPlugin.prototype.generateTnsJavaClasses = function (compilation) { + const path = compilation.compiler.outputPath; + const isAndroid = path.indexOf("android") > -1; + + if (isAndroid && !compilation.assets["tns-java-classes.js"]) { + compilation.assets["tns-java-classes.js"] = new RawSource(""); + } + } + + GenerateBundleStarterPlugin.prototype.generatePackageJson = function () { + const packageJson = getPackageJson(this.webpackContext); + packageJson.main = "starter"; + + return new RawSource(JSON.stringify(packageJson, null, 4)); + } + + GenerateBundleStarterPlugin.prototype.generateStarterModule = function () { + const moduleSource = this.bundles + .map(bundle => `require("${bundle}")`) + .join("\n"); + + return new RawSource(moduleSource); + } + + return GenerateBundleStarterPlugin; +})(); diff --git a/plugins/NativeScriptJsonpPlugin.js b/plugins/NativeScriptJsonpPlugin.js new file mode 100644 index 00000000..a75ba42b --- /dev/null +++ b/plugins/NativeScriptJsonpPlugin.js @@ -0,0 +1,38 @@ +const { ConcatSources } = require("webpack-sources"); + +const WINDOWS_GLOBAL_MATCHER = /window\["nativescriptJsonp"\]/g; +const NATIVESCRIPT_GLOBAL = "global[\"nativescriptJsonp\"]"; +const isVendorChunk = name => name === "vendor.js"; + +//HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]` +// applied to tns-java-classes.js only +exports.NativeScriptJsonpPlugin = (function() { + function NativeScriptJsonpPlugin() { + } + + NativeScriptJsonpPlugin.prototype.apply = function (compiler) { + compiler.plugin("compilation", function (compilation) { + compilation.plugin("optimize-chunk-assets", function (chunks, callback) { + chunks.forEach(function (chunk) { + chunk.files + .filter(isVendorChunk) + .forEach(file => replaceGlobal(compilation.assets, file)); + }); + callback(); + }); + }); + }; + + return NativeScriptJsonpPlugin; +})(); + +function replaceGlobal(assets, file) { + const path = assets[file]; + const source = path.source(); + const match = source.match(WINDOWS_GLOBAL_MATCHER); + + if (match) { + const newSource = source.replace(WINDOWS_GLOBAL_MATCHER, NATIVESCRIPT_GLOBAL); + assets[file] = new ConcatSource(newSource); + } +} diff --git a/plugins/NativeScriptSnapshotPlugin.js b/plugins/NativeScriptSnapshotPlugin.js new file mode 100644 index 00000000..d90c6e9c --- /dev/null +++ b/plugins/NativeScriptSnapshotPlugin.js @@ -0,0 +1,88 @@ +const { resolve, join } = require("path"); +const { closeSync, openSync } = require("fs"); + +const ProjectSnapshotGenerator = require("../snapshot/android/project-snapshot-generator"); + +exports.NativeScriptSnapshotPlugin = (function() { + function NativeScriptSnapshotPlugin(options) { + ProjectSnapshotGenerator.call(this, options); // Call the parent constructor + + if (!this.options.chunk) { + throw new Error("No chunk specified."); + } + + console.dir() + + if (this.options.webpackConfig) { + if (this.options.webpackConfig.output && this.options.webpackConfig.output.libraryTarget) { + this.options.webpackConfig.output.libraryTarget = undefined; + } + + if (this.options.webpackConfig.entry) { + if (typeof this.options.webpackConfig.entry === "string" || + this.options.webpackConfig.entry instanceof Array) + this.options.webpackConfig.entry = { bundle: this.options.webpackConfig.entry }; + } + + this.options.webpackConfig.entry["tns-java-classes"] = this.getTnsJavaClassesBuildPath(); + } + } + + // inherit ProjectSnapshotGenerator + NativeScriptSnapshotPlugin.prototype = Object.create(ProjectSnapshotGenerator.prototype); + NativeScriptSnapshotPlugin.prototype.constructor = NativeScriptSnapshotPlugin; + + NativeScriptSnapshotPlugin.prototype.getTnsJavaClassesBuildPath = function() { + return resolve(this.getBuildPath(), "../tns-java-classes.js"); + } + + NativeScriptSnapshotPlugin.prototype.generate = function(webpackChunk) { + const options = this.options; + + const inputFile = join(options.webpackConfig.output.path, webpackChunk.files[0]); + + console.log(`\n Snapshotting bundle at ${inputFile}`); + + const preparedAppRootPath = join(options.projectRoot, "platforms/android/src/main/assets"); + const preprocessedInputFile = join(preparedAppRootPath, "app/_embedded_script_.js"); + + ProjectSnapshotGenerator.prototype.generate.call(this, { + inputFile, + preprocessedInputFile, + targetArchs: options.targetArchs, + useLibs: options.useLibs, + androidNdkPath: options.androidNdkPath, + tnsJavaClassesPath: join(preparedAppRootPath, "app/tns-java-classes.js") + }); + + // Make the original file empty + if (inputFile !== preprocessedInputFile) { + closeSync(openSync(inputFile, "w")); // truncates the input file content + } + } + + NativeScriptSnapshotPlugin.prototype.apply = function(compiler) { + const options = this.options; + + // Generate tns-java-classes.js file + debugger; + ProjectSnapshotGenerator.prototype.generateTnsJavaClassesFile.call(this, { + output: this.getTnsJavaClassesBuildPath(), + options: options.tnsJavaClassesOptions + }); + + // Run the snapshot tool when the packing is done + compiler.plugin("done", function(result) { + debugger; + const chunkToSnapshot = result.compilation.chunks.find(chunk => chunk.name == options.chunk); + if (!chunkToSnapshot) { + throw new Error(`No chunk named '${options.chunk}' found.`); + } + + this.generate(chunkToSnapshot); + + }.bind(this)); + } + + return NativeScriptSnapshotPlugin; +})(); diff --git a/resource-resolver-plugins/UrlResolvePlugin.js b/plugins/UrlResolvePlugin.js similarity index 87% rename from resource-resolver-plugins/UrlResolvePlugin.js rename to plugins/UrlResolvePlugin.js index 1222d695..cf07a13c 100644 --- a/resource-resolver-plugins/UrlResolvePlugin.js +++ b/plugins/UrlResolvePlugin.js @@ -1,8 +1,8 @@ -const ts = require("typescript"); -const fs = require("fs"); -const path = require("path"); +const { forEachChild, SyntaxKind } = require("typescript"); +const { existsSync } = require("fs"); +const { resolve } = require("path"); -const UrlResolvePlugin = (function() { +exports.UrlResolvePlugin = (function() { function UrlResolvePlugin(options) { if (!options || !options.platform) { throw new Error(`Target platform must be specified!`); @@ -40,15 +40,15 @@ const UrlResolvePlugin = (function() { UrlResolvePlugin.prototype.usePlatformUrl = function(sourceFile) { this.setCurrentDirectory(sourceFile); - ts.forEachChild(sourceFile, node => this.traverseDecorators(node)); + forEachChild(sourceFile, node => this.traverseDecorators(node)); } UrlResolvePlugin.prototype.setCurrentDirectory = function(sourceFile) { - this.currentDirectory = path.resolve(sourceFile.path, ".."); + this.currentDirectory = resolve(sourceFile.path, ".."); } UrlResolvePlugin.prototype.traverseDecorators = function(node) { - if (node.kind !== ts.SyntaxKind.ClassDeclaration || !node.decorators) { + if (node.kind !== SyntaxKind.ClassDeclaration || !node.decorators) { return; } @@ -90,9 +90,9 @@ const UrlResolvePlugin = (function() { } UrlResolvePlugin.prototype.noMultiplatformFile = function(url) { - let filePath = path.resolve(this.currentDirectory, url); + let filePath = resolve(this.currentDirectory, url); - return !fs.existsSync(filePath); + return !existsSync(filePath); } UrlResolvePlugin.prototype.replaceUrlsValue = function(element) { @@ -105,5 +105,3 @@ const UrlResolvePlugin = (function() { return UrlResolvePlugin; })(); - -module.exports = UrlResolvePlugin; diff --git a/plugins/angular.js b/plugins/angular.js new file mode 100644 index 00000000..3a406058 --- /dev/null +++ b/plugins/angular.js @@ -0,0 +1,4 @@ +module.exports = Object.assign({}, + require("./UrlResolvePlugin") +); + diff --git a/plugins/index.js b/plugins/index.js new file mode 100644 index 00000000..5acc5c70 --- /dev/null +++ b/plugins/index.js @@ -0,0 +1,6 @@ +module.exports = Object.assign({}, + require("./GenerateBundleStarterPlugin"), + require("./NativeScriptJsonpPlugin"), + require("./NativeScriptSnapshotPlugin") +); + diff --git a/prepublish/common/exports.js b/prepublish/common/exports.js index 0ba564b0..739e8905 100644 --- a/prepublish/common/exports.js +++ b/prepublish/common/exports.js @@ -22,7 +22,7 @@ module.exports = env => { const plugins = getPlugins(platform, env); const extensions = getExtensions(platform); - return { + const config = { context: resolve("./app"), target: nativescriptTarget, entry, @@ -51,6 +51,18 @@ module.exports = env => { module: { rules }, plugins, }; + + if (env.snapshot) { + plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + projectRoot: __dirname, + webpackConfig: config, + targetArchs: ["arm", "arm64"], + tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, + useLibs: false + })); + } + + return config; }; `; - diff --git a/prepublish/common/uglify.js b/prepublish/common/uglify.js index ef57ad82..2b6bcf96 100644 --- a/prepublish/common/uglify.js +++ b/prepublish/common/uglify.js @@ -1,5 +1,4 @@ -module.exports = ` - if (env.uglify) { +module.exports = `if (env.uglify) { plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); // Work around an Android issue by setting compress = false @@ -8,4 +7,5 @@ module.exports = ` mangle: { except: nsWebpack.uglifyMangleExcludes }, compress, })); - }`; + } +`; diff --git a/prepublish/index.js b/prepublish/index.js index e4abb019..2795699a 100644 --- a/prepublish/index.js +++ b/prepublish/index.js @@ -51,10 +51,11 @@ function isArray(resource) { function pluginsBuilder(plugins) { const uglify = require("./common/uglify"); + return `function getPlugins(platform, env) { let plugins = ${plugins}; - ${uglify} + ${uglify} return plugins; }\n`; } diff --git a/snapshot/android/project-snapshot-generator.js b/snapshot/android/project-snapshot-generator.js index fb944cd2..f159f1a4 100644 --- a/snapshot/android/project-snapshot-generator.js +++ b/snapshot/android/project-snapshot-generator.js @@ -13,10 +13,10 @@ const VALID_ANDROID_RUNTIME_TAGS = Object.freeze(["next", "rc"]); function ProjectSnapshotGenerator (options) { this.options = options = options || {}; - options.projectRoot = options.projectRoot ? - (path.isAbsolute(options.projectRoot) ? - options.projectRoot : - path.resolve(process.cwd(), options.projectRoot)) : + options.projectRoot = options.projectRoot ? + (path.isAbsolute(options.projectRoot) ? + options.projectRoot : + path.resolve(process.cwd(), options.projectRoot)) : process.cwd(); if (!options.projectRoot) { @@ -56,7 +56,7 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) { if (shelljs.test("-e", path.join(buildPath, "tns-java-classes.js"))) { shelljs.cp(path.join(buildPath, "tns-java-classes.js"), path.join(assetsPath, "app/tns-java-classes.js")); } - + if (shelljs.test("-e", path.join(buildPath, "ndk-build/libs"))) { // useLibs = true const libsDestinationPath = path.join(projectRoot, "platforms/android/src", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE, "jniLibs"); @@ -75,11 +75,11 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) { const blobsSrcPath = path.join(buildPath, "snapshots/blobs"); const blobsDestinationPath = path.join(assetsPath, "snapshots"); const appPackageJsonPath = path.join(assetsPath, "app/package.json"); - + // Copy the blobs in the prepared app folder shelljs.cp("-R", blobsSrcPath + "/", path.join(assetsPath, "snapshots")); - /* + /* Rename TNSSnapshot.blob files to snapshot.blob files. The xxd tool uses the file name for the name of the static array. This is why the *.blob files are initially named TNSSnapshot.blob. After the xxd step, they must be renamed to snapshot.blob, because this is the filename that the Android runtime is looking for. */ shelljs.exec("find " + blobsDestinationPath + " -name '*.blob' -execdir mv {} snapshot.blob ';'"); @@ -122,9 +122,8 @@ ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() { throw new Error("In order to generate a V8 snapshot you must have the \"android\" platform installed - to do so please run \"tns platform add android\"."); } - // The version could be "next" - if (VALID_ANDROID_RUNTIME_TAGS.includes(currentRuntimeVersion) || - isVersionGte(MIN_ANDROID_RUNTIME_VERSION, currentRuntimeVersion)) { + if (!VALID_ANDROID_RUNTIME_TAGS.includes(currentRuntimeVersion) && + !isVersionGte(currentRuntimeVersion, MIN_ANDROID_RUNTIME_VERSION)) { throw new Error("In order to support heap snapshots, you must have at least tns-android@" + MIN_ANDROID_RUNTIME_VERSION + " installed. Current Android Runtime version is: " + currentRuntimeVersion + "."); @@ -134,7 +133,7 @@ ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() { ProjectSnapshotGenerator.prototype.generateTnsJavaClassesFile = function(generationOptions) { const tnsJavaClassesGenerator = new TnsJavaClassesGenerator(); return tnsJavaClassesGenerator.generate({ - projectRoot: this.options.projectRoot, + projectRoot: this.options.projectRoot, output: generationOptions.output, options: generationOptions.options }); @@ -177,7 +176,7 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) { ProjectSnapshotGenerator.installSnapshotArtefacts(this.options.projectRoot); } - console.log(generationOptions.useLibs ? + console.log(generationOptions.useLibs ? "Snapshot is included in the app as dynamically linked library (.so file)." : "Snapshot is included in the app as binary .blob file. The more space-efficient option is to embed it in a dynamically linked library (.so file)."); } diff --git a/snapshot/android/snapshot-webpack-plugin.js b/snapshot/android/snapshot-webpack-plugin.js deleted file mode 100644 index fd8f3bfc..00000000 --- a/snapshot/android/snapshot-webpack-plugin.js +++ /dev/null @@ -1,81 +0,0 @@ -const path = require('path'); -const fs = require('fs'); -const shelljs = require("shelljs"); -const ProjectSnapshotGenerator = require("./project-snapshot-generator"); - -function SnapshotWebpackPlugin(options) { - ProjectSnapshotGenerator.call(this, options); // Call the parent constructor - - if (!this.options.chunk) { - throw new Error("No chunk specified."); - } - - if (this.options.webpackConfig) { - if (this.options.webpackConfig.output && this.options.webpackConfig.output.libraryTarget) { - this.options.webpackConfig.output.libraryTarget = undefined; - } - - if (this.options.webpackConfig.entry) { - if (typeof this.options.webpackConfig.entry === "string" || - this.options.webpackConfig.entry instanceof Array) - this.options.webpackConfig.entry = { bundle: this.options.webpackConfig.entry }; - } - - this.options.webpackConfig.entry["tns-java-classes"] = this.getTnsJavaClassesBuildPath(); - } -} - -// inherit ProjectSnapshotGenerator -SnapshotWebpackPlugin.prototype = Object.create(ProjectSnapshotGenerator.prototype); -SnapshotWebpackPlugin.prototype.constructor = SnapshotWebpackPlugin; - -module.exports = SnapshotWebpackPlugin; - -SnapshotWebpackPlugin.prototype.getTnsJavaClassesBuildPath = function() { - return path.resolve(this.getBuildPath(), "../tns-java-classes.js"); -} - -SnapshotWebpackPlugin.prototype.generate = function(webpackChunk) { - const options = this.options; - - const inputFile = path.join(options.webpackConfig.output.path, webpackChunk.files[0]); - - console.log("\n Snapshotting bundle at " + inputFile); - - const preparedAppRootPath = path.join(options.projectRoot, "platforms/android/src/main/assets"); - const preprocessedInputFile = path.join(preparedAppRootPath, "app/_embedded_script_.js"); - - ProjectSnapshotGenerator.prototype.generate.call(this, { - inputFile: inputFile, - targetArchs: options.targetArchs, - preprocessedInputFile: preprocessedInputFile, - useLibs: options.useLibs, - androidNdkPath: options.androidNdkPath, - tnsJavaClassesPath: path.join(preparedAppRootPath, "app/tns-java-classes.js") - }); - - // Make the original file empty - if (inputFile != preprocessedInputFile) { - fs.closeSync(fs.openSync(inputFile, 'w')); // truncates the input file content - } -} - -SnapshotWebpackPlugin.prototype.apply = function(compiler) { - const options = this.options; - - // Generate tns-java-classes.js file - debugger; - ProjectSnapshotGenerator.prototype.generateTnsJavaClassesFile.call(this, { output: this.getTnsJavaClassesBuildPath(), options: options.tnsJavaClassesOptions }); - - // Run the snapshot tool when the packing is done - compiler.plugin('done', function(result) { - debugger; - const chunkToSnapshot = result.compilation.chunks.find(function(chunk) { return chunk.name == options.chunk; }); - if (!chunkToSnapshot) { - throw new Error("No chunk named '" + options.chunk + "' found."); - } - - this.generate(chunkToSnapshot); - - }.bind(this)); -} \ No newline at end of file diff --git a/templates/webpack.angular.js b/templates/webpack.angular.js index 47b188a2..6c4676bc 100644 --- a/templates/webpack.angular.js +++ b/templates/webpack.angular.js @@ -5,7 +5,6 @@ const nsWebpack = require("nativescript-dev-webpack"); const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const ExtractTextPlugin = require("extract-text-webpack-plugin"); -const AndroidSnapshotPlugin = require("nativescript-dev-webpack/snapshot/android/snapshot-webpack-plugin"); const { AotPlugin } = require("@ngtools/webpack"); @@ -32,7 +31,7 @@ module.exports = env => { const plugins = getPlugins(platform, env); const extensions = getExtensions(platform); - var config = { + const config = { context: resolve("./app"), target: nativescriptTarget, entry, @@ -63,13 +62,13 @@ module.exports = env => { }; if (env.snapshot) { - plugins.push(new AndroidSnapshotPlugin({ - chunk: "vendor", - projectRoot: __dirname, - webpackConfig: config, + plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + projectRoot: __dirname, + webpackConfig: config, targetArchs: ["arm", "arm64"], - tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, - useLibs: false + tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, + useLibs: false })); } @@ -182,7 +181,7 @@ function getPlugins(platform, env) { }), ]; - + if (env.uglify) { plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index 6387e57a..9a24c060 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -5,7 +5,6 @@ const nsWebpack = require("nativescript-dev-webpack"); const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const ExtractTextPlugin = require("extract-text-webpack-plugin"); -const AndroidSnapshotPlugin = require("nativescript-dev-webpack/snapshot/android/snapshot-webpack-plugin"); const mainSheet = `app.css`; @@ -31,7 +30,7 @@ module.exports = env => { const plugins = getPlugins(platform, env); const extensions = getExtensions(platform); - var config = { + const config = { context: resolve("./app"), target: nativescriptTarget, entry, @@ -62,13 +61,13 @@ module.exports = env => { }; if (env.snapshot) { - plugins.push(new AndroidSnapshotPlugin({ - chunk: "vendor", - projectRoot: __dirname, - webpackConfig: config, + plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + projectRoot: __dirname, + webpackConfig: config, targetArchs: ["arm", "arm64"], - tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, - useLibs: false + tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, + useLibs: false })); } @@ -156,7 +155,7 @@ function getPlugins(platform, env) { "./bundle", ]), ]; - + if (env.uglify) { plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index fde25d95..a369855f 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -5,7 +5,6 @@ const nsWebpack = require("nativescript-dev-webpack"); const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const ExtractTextPlugin = require("extract-text-webpack-plugin"); -const AndroidSnapshotPlugin = require("nativescript-dev-webpack/snapshot/android/snapshot-webpack-plugin"); const mainSheet = `app.css`; @@ -31,7 +30,7 @@ module.exports = env => { const plugins = getPlugins(platform, env); const extensions = getExtensions(platform); - var config = { + const config = { context: resolve("./app"), target: nativescriptTarget, entry, @@ -62,19 +61,20 @@ module.exports = env => { }; if (env.snapshot) { - plugins.push(new AndroidSnapshotPlugin({ - chunk: "vendor", - projectRoot: __dirname, - webpackConfig: config, + plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + projectRoot: __dirname, + webpackConfig: config, targetArchs: ["arm", "arm64"], - tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, - useLibs: false + tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, + useLibs: false })); } return config; }; + function getPlatform(env) { return env.android ? "android" : env.ios ? "ios" : @@ -164,7 +164,7 @@ function getPlugins(platform, env) { "./bundle", ]), ]; - + if (env.uglify) { plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); diff --git a/utils.js b/utils.js index c48f0e20..d6d7a85d 100644 --- a/utils.js +++ b/utils.js @@ -22,4 +22,13 @@ const isVersionGte = (first, second) => { ) >= 0; } -module.exports = { isVersionGte }; +const sanitize = name => name + .split("") + .filter(char => /[a-zA-Z0-9]/.test(char)) + .join(""); + +module.exports = { + isVersionGte, + sanitize, +}; +