From 6ab870699e5d6beb017ff55348386d1fd61c83c8 Mon Sep 17 00:00:00 2001 From: Elena Hristova Date: Thu, 8 Aug 2019 16:34:05 +0300 Subject: [PATCH 1/3] feat: update to angular 8.2 --- demo/AngularApp/.gitignore | 4 +- demo/AngularApp/package.json | 22 +- demo/AngularApp/webpack.config.js | 321 ------------------------------ dependencyManager.js | 4 +- package.json | 10 +- 5 files changed, 21 insertions(+), 340 deletions(-) delete mode 100644 demo/AngularApp/webpack.config.js diff --git a/demo/AngularApp/.gitignore b/demo/AngularApp/.gitignore index 1ccc6d9f..70cda1a4 100644 --- a/demo/AngularApp/.gitignore +++ b/demo/AngularApp/.gitignore @@ -10,4 +10,6 @@ test-results.xml app/item/items.component.android.css app/item/items.component.ios.css -mochawesome-report \ No newline at end of file +mochawesome-report + +webpack.config.js \ No newline at end of file diff --git a/demo/AngularApp/package.json b/demo/AngularApp/package.json index e2241aaa..81f77544 100644 --- a/demo/AngularApp/package.json +++ b/demo/AngularApp/package.json @@ -13,24 +13,24 @@ } }, "dependencies": { - "@angular/common": "8.0.0", - "@angular/compiler": "8.0.0", - "@angular/core": "8.0.0", - "@angular/forms": "8.0.0", + "@angular/common": "8.2.0", + "@angular/compiler": "8.2.0", + "@angular/core": "8.2.0", + "@angular/forms": "8.2.0", "@angular/http": "8.0.0-beta.10", - "@angular/platform-browser": "8.0.0", - "@angular/platform-browser-dynamic": "8.0.0", - "@angular/router": "8.0.0", + "@angular/platform-browser": "8.2.0", + "@angular/platform-browser-dynamic": "8.2.0", + "@angular/router": "8.2.0", "nativescript-angular": "next", "nativescript-theme-core": "~1.0.2", "reflect-metadata": "~0.1.8", "rxjs": "^6.3.3", "tns-core-modules": "next", - "zone.js": "^0.8.4" + "zone.js": "^0.9.1" }, "devDependencies": { - "@angular/compiler-cli": "8.0.0", - "@ngtools/webpack": "8.0.0", + "@angular/compiler-cli": "8.2.0", + "@ngtools/webpack": "8.2.0", "@types/chai": "~4.1.7", "@types/mocha": "~5.2.5", "@types/node": "~10.12.18", @@ -44,7 +44,7 @@ "nativescript-dev-appium": "next", "nativescript-dev-webpack": "next", "node-sass": "^4.12.0", - "typescript": "~3.4.5" + "typescript": "~3.5.3" }, "scripts": { "setup": "npm pack ../../ && npm i -D nativescript-dev-webpack*.tgz", diff --git a/demo/AngularApp/webpack.config.js b/demo/AngularApp/webpack.config.js deleted file mode 100644 index 8137073f..00000000 --- a/demo/AngularApp/webpack.config.js +++ /dev/null @@ -1,321 +0,0 @@ -const { join, relative, resolve, sep, dirname } = require("path"); - -const webpack = require("webpack"); -const nsWebpack = require("nativescript-dev-webpack"); -const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); -const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns-replace-bootstrap"); -const { nsReplaceLazyLoader } = require("nativescript-dev-webpack/transformers/ns-replace-lazy-loader"); -const { nsSupportHmrNg } = require("nativescript-dev-webpack/transformers/ns-support-hmr-ng"); -const { getMainModulePath } = require("nativescript-dev-webpack/utils/ast-utils"); -const CleanWebpackPlugin = require("clean-webpack-plugin"); -const CopyWebpackPlugin = require("copy-webpack-plugin"); -const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"); -const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin"); -const TerserPlugin = require("terser-webpack-plugin"); -const { getAngularCompilerPlugin } = require("nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin"); -const hashSalt = Date.now().toString(); - -module.exports = env => { - // Add your custom Activities, Services and other Android app components here. - const appComponents = [ - "tns-core-modules/ui/frame", - "tns-core-modules/ui/frame/activity", - resolve(__dirname, "app/activity.android.ts") - ]; - - const platform = env && (env.android && "android" || env.ios && "ios"); - if (!platform) { - throw new Error("You need to provide a target platform!"); - } - - const AngularCompilerPlugin = getAngularCompilerPlugin(platform); - const projectRoot = __dirname; - - // Default destination inside platforms//... - const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot)); - - const { - // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. - appPath = "src", - appResourcesPath = "App_Resources", - - // You can provide the following flags when running 'tns run android|ios' - aot, // --env.aot - snapshot, // --env.snapshot, - production, // --env.production - uglify, // --env.uglify - report, // --env.report - sourceMap, // --env.sourceMap - hiddenSourceMap, // --env.hiddenSourceMap - hmr, // --env.hmr, - unitTesting, // --env.unitTesting - verbose, // --env.verbose - } = env; - - const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap; - const externals = nsWebpack.getConvertedExternals(env.externals); - const appFullPath = resolve(projectRoot, appPath); - const appResourcesFullPath = resolve(projectRoot, appResourcesPath); - const tsConfigName = "tsconfig.tns.json"; - const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`; - const entryPath = `.${sep}${entryModule}`; - const entries = { bundle: entryPath, application: "./application.android" }; - const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1); - if (platform === "ios" && !areCoreModulesExternal) { - entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules"; - }; - - const ngCompilerTransformers = []; - const additionalLazyModuleResources = []; - if (aot) { - ngCompilerTransformers.push(nsReplaceBootstrap); - } - - if (hmr) { - ngCompilerTransformers.push(nsSupportHmrNg); - } - - // when "@angular/core" is external, it's not included in the bundles. In this way, it will be used - // directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes - // fixes https://github.com/NativeScript/nativescript-cli/issues/4024 - if (env.externals && env.externals.indexOf("@angular/core") > -1) { - const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule), tsConfigName); - if (appModuleRelativePath) { - const appModuleFolderPath = dirname(resolve(appFullPath, appModuleRelativePath)); - // include the lazy loader inside app module - ngCompilerTransformers.push(nsReplaceLazyLoader); - // include the new lazy loader path in the allowed ones - additionalLazyModuleResources.push(appModuleFolderPath); - } - } - - const ngCompilerPlugin = new AngularCompilerPlugin({ - hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]), - platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)), - mainPath: join(appFullPath, entryModule), - tsConfigPath: join(__dirname, tsConfigName), - skipCodeGeneration: !aot, - sourceMap: !!isAnySourceMapEnabled, - additionalLazyModuleResources: additionalLazyModuleResources - }); - - let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist); - - const itemsToClean = [`${dist}/**/*`]; - if (platform === "android") { - itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`); - itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`); - } - - nsWebpack.processAppComponents(appComponents, platform); - const config = { - mode: production ? "production" : "development", - context: appFullPath, - externals, - watchOptions: { - ignored: [ - appResourcesFullPath, - // Don't watch hidden files - "**/.*", - ] - }, - target: nativescriptTarget, - entry: entries, - output: { - pathinfo: false, - path: dist, - sourceMapFilename, - libraryTarget: "commonjs2", - filename: "[name].js", - globalObject: "global", - hashSalt - }, - resolve: { - extensions: [".ts", ".js", ".scss", ".css"], - // Resolve {N} system modules from tns-core-modules - modules: [ - resolve(__dirname, "node_modules/tns-core-modules"), - resolve(__dirname, "node_modules"), - "node_modules/tns-core-modules", - "node_modules", - ], - alias: { - '~': appFullPath - }, - symlinks: true - }, - resolveLoader: { - symlinks: false - }, - node: { - // Disable node shims that conflict with NativeScript - "http": false, - "timers": false, - "setImmediate": false, - "fs": "empty", - "__dirname": false, - }, - devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"), - optimization: { - runtimeChunk: "single", - splitChunks: { - cacheGroups: { - vendor: { - name: "vendor", - chunks: "all", - test: (module, chunks) => { - const moduleName = module.nameForCondition ? module.nameForCondition() : ''; - return /[\\/]node_modules[\\/]/.test(moduleName) || - appComponents.some(comp => comp === moduleName); - }, - enforce: true, - }, - } - }, - minimize: !!uglify, - minimizer: [ - new TerserPlugin({ - parallel: true, - cache: true, - sourceMap: isAnySourceMapEnabled, - terserOptions: { - output: { - comments: false, - semicolons: !isAnySourceMapEnabled - }, - compress: { - // The Android SBG has problems parsing the output - // when these options are enabled - 'collapse_vars': platform !== "android", - sequences: platform !== "android", - } - } - }) - ], - }, - module: { - rules: [ - { - include: join(appFullPath, entryPath), - use: [ - // Require all Android app components - platform === "android" && { - loader: "nativescript-dev-webpack/android-app-components-loader", - options: { modules: appComponents } - }, - - { - loader: "nativescript-dev-webpack/bundle-config-loader", - options: { - angular: true, - loadCss: !snapshot, // load the application css if in debug mode - unitTesting, - appFullPath, - projectRoot, - ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform) - } - }, - ].filter(loader => !!loader) - }, - - { test: /\.html$|\.xml$/, use: "raw-loader" }, - - // tns-core-modules reads the app.css and its imports using css-loader - { - test: /[\/|\\]app\.css$/, - use: [ - "nativescript-dev-webpack/style-hot-loader", - { loader: "css-loader", options: { url: false } } - ] - }, - { - test: /[\/|\\]app\.scss$/, - use: [ - "nativescript-dev-webpack/style-hot-loader", - { loader: "css-loader", options: { url: false } }, - "sass-loader" - ] - }, - - // Angular components reference css files and their imports using raw-loader - { test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" }, - { test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] }, - - { - test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, - use: [ - "nativescript-dev-webpack/moduleid-compat-loader", - "nativescript-dev-webpack/lazy-ngmodule-hot-loader", - "@ngtools/webpack", - ] - }, - - // Mark files inside `@angular/core` as using SystemJS style dynamic imports. - // Removing this will cause deprecation warnings to appear. - { - test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/, - parser: { system: true }, - }, - ], - }, - plugins: [ - // Define useful constants like TNS_WEBPACK - new webpack.DefinePlugin({ - "global.TNS_WEBPACK": "true", - "process": undefined, - }), - // Remove all files from the out dir. - new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }), - // Copy assets to out dir. Add your own globs as needed. - new CopyWebpackPlugin([ - { from: { glob: "fonts/**" } }, - { from: { glob: "**/*.jpg" } }, - { from: { glob: "**/*.png" } }, - ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }), - new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"), - // For instructions on how to set up workers with webpack - // check out https://github.com/nativescript/worker-loader - new NativeScriptWorkerPlugin(), - ngCompilerPlugin, - // Does IPC communication with the {N} CLI to notify events when running in watch mode. - new nsWebpack.WatchStateLoggerPlugin(), - ], - }; - - if (report) { - // Generate report files for bundles content - config.plugins.push(new BundleAnalyzerPlugin({ - analyzerMode: "static", - openAnalyzer: false, - generateStatsFile: true, - reportFilename: resolve(projectRoot, "report", `report.html`), - statsFilename: resolve(projectRoot, "report", `stats.json`), - })); - } - - if (snapshot) { - config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ - chunk: "vendor", - angular: true, - requireModules: [ - "reflect-metadata", - "@angular/platform-browser", - "@angular/core", - "@angular/common", - "@angular/router", - "nativescript-angular/platform-static", - "nativescript-angular/router", - ], - projectRoot, - webpackConfig: config, - })); - } - - if (hmr) { - config.plugins.push(new webpack.HotModuleReplacementPlugin()); - } - - return config; -}; diff --git a/dependencyManager.js b/dependencyManager.js index 7ed804de..8f348013 100644 --- a/dependencyManager.js +++ b/dependencyManager.js @@ -73,11 +73,11 @@ function getRequiredDeps(packageJson) { } const deps = { - "@angular/compiler-cli": "8.0.0", + "@angular/compiler-cli": "8.2.0", }; if (!dependsOn(packageJson, "@angular-devkit/build-angular")) { - deps["@ngtools/webpack"] = "8.0.0"; + deps["@ngtools/webpack"] = "8.2.0"; } return deps; diff --git a/package.json b/package.json index dd524ae9..7421d2e5 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "generate-android-snapshot": "./bin/generate-android-snapshot" }, "dependencies": { - "@angular-devkit/core": "8.0.0", + "@angular-devkit/core": "8.2.0", "clean-webpack-plugin": "~1.0.0", "copy-webpack-plugin": "~4.6.0", "css-loader": "~2.1.1", @@ -75,9 +75,9 @@ "webpack-sources": "~1.3.0" }, "devDependencies": { - "@angular/compiler": "8.0.0", - "@angular/compiler-cli": "8.0.0", - "@ngtools/webpack": "8.0.0", + "@angular/compiler": "8.2.0", + "@angular/compiler-cli": "8.2.0", + "@ngtools/webpack": "8.2.0", "@types/jasmine": "^3.3.7", "@types/loader-utils": "^1.1.3", "@types/node": "^10.12.12", @@ -89,6 +89,6 @@ "jasmine-spec-reporter": "^4.2.1", "proxyquire": "2.1.0", "tns-core-modules": "next", - "typescript": "~3.4.0" + "typescript": "~3.5.3" } } \ No newline at end of file From bf9372c97bbe06d40f10530ab850854cc140e314 Mon Sep 17 00:00:00 2001 From: Elena Hristova Date: Thu, 8 Aug 2019 16:54:35 +0300 Subject: [PATCH 2/3] chore: remove bundle flag from documentation --- CONTRIBUTING.md | 2 +- README.md | 4 +- bin/ns-bundle | 2 +- demo/JavaScriptApp/.gitignore | 4 +- demo/JavaScriptApp/webpack.config.js | 260 ------------------------ demo/TypeScriptApp/.gitignore | 4 +- demo/TypeScriptApp/webpack.config.js | 291 --------------------------- templates/webpack.angular.js | 3 +- templates/webpack.javascript.js | 3 +- templates/webpack.typescript.js | 3 +- templates/webpack.vue.js | 3 +- 11 files changed, 14 insertions(+), 565 deletions(-) delete mode 100644 demo/JavaScriptApp/webpack.config.js delete mode 100644 demo/TypeScriptApp/webpack.config.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 18043a17..731d8601 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -76,7 +76,7 @@ NOTE: There are three test apps in the repository, located in the `/demo` direct 4. Build the app for Android or iOS. ``` bash - tns run android/ios --bundle + tns run android/ios ``` 5. Install [appium](http://appium.io/) globally. diff --git a/README.md b/README.md index b3b02450..0f0183fd 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,9 @@ For more details, see the [NativeScript docs for building with webpack](http://d ```sh $ npm install --save-dev nativescript-dev-webpack -$ tns run android --bundle +$ tns run android or -$ tns run ios --bundle +$ tns run ios ``` ## Contribute diff --git a/bin/ns-bundle b/bin/ns-bundle index d52d7536..ffbd1a9a 100755 --- a/bin/ns-bundle +++ b/bin/ns-bundle @@ -1,4 +1,4 @@ #!/usr/bin/env node -console.error("Using npm run scripts is no longer supported. Use CLI commands and pass the --bundle flag instead.\nExample:\ntns build android --bundle") +console.error("Using npm run scripts is no longer supported. Use CLI commands instead.\nExample:\ntns build android") process.exit(1); \ No newline at end of file diff --git a/demo/JavaScriptApp/.gitignore b/demo/JavaScriptApp/.gitignore index c457995c..5f778b3c 100644 --- a/demo/JavaScriptApp/.gitignore +++ b/demo/JavaScriptApp/.gitignore @@ -1,3 +1,5 @@ app/main-page.android.css app/main-page.ios.css -mochawesome-report \ No newline at end of file +mochawesome-report + +webpack.config.js \ No newline at end of file diff --git a/demo/JavaScriptApp/webpack.config.js b/demo/JavaScriptApp/webpack.config.js deleted file mode 100644 index ec4ea0b5..00000000 --- a/demo/JavaScriptApp/webpack.config.js +++ /dev/null @@ -1,260 +0,0 @@ -const { join, relative, resolve, sep } = require("path"); - -const webpack = require("webpack"); -const nsWebpack = require("nativescript-dev-webpack"); -const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); -const CleanWebpackPlugin = require("clean-webpack-plugin"); -const CopyWebpackPlugin = require("copy-webpack-plugin"); -const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"); -const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin"); -const TerserPlugin = require("terser-webpack-plugin"); -const hashSalt = Date.now().toString(); - -module.exports = env => { - // Add your custom Activities, Services and other android app components here. - const appComponents = [ - "tns-core-modules/ui/frame", - "tns-core-modules/ui/frame/activity", - resolve(__dirname, "app/activity.android.js") - ]; - - const platform = env && (env.android && "android" || env.ios && "ios"); - if (!platform) { - throw new Error("You need to provide a target platform!"); - } - - const platforms = ["ios", "android"]; - const projectRoot = __dirname; - - // Default destination inside platforms//... - const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot)); - - const { - // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. - appPath = "app", - appResourcesPath = "app/App_Resources", - - // You can provide the following flags when running 'tns run android|ios' - snapshot, // --env.snapshot - production, // --env.production - uglify, // --env.uglify - report, // --env.report - sourceMap, // --env.sourceMap - hiddenSourceMap, // --env.hiddenSourceMap - hmr, // --env.hmr, - unitTesting, // --env.unitTesting, - verbose, // --env.verbose - } = env; - - const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap; - const externals = nsWebpack.getConvertedExternals(env.externals); - const appFullPath = resolve(projectRoot, appPath); - const appResourcesFullPath = resolve(projectRoot, appResourcesPath); - - const entryModule = nsWebpack.getEntryModule(appFullPath, platform); - const entryPath = `.${sep}${entryModule}.js`; - const entries = { bundle: entryPath, application: "./application.android" }; - const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1); - if (platform === "ios" && !areCoreModulesExternal) { - entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules"; - }; - - let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist); - - const itemsToClean = [`${dist}/**/*`]; - if (platform === "android") { - itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`); - itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`); - } - - nsWebpack.processAppComponents(appComponents, platform); - const config = { - mode: production ? "production" : "development", - context: appFullPath, - externals, - watchOptions: { - ignored: [ - appResourcesFullPath, - // Don't watch hidden files - "**/.*", - ] - }, - target: nativescriptTarget, - entry: entries, - output: { - pathinfo: false, - path: dist, - sourceMapFilename, - libraryTarget: "commonjs2", - filename: "[name].js", - globalObject: "global", - hashSalt - }, - resolve: { - extensions: [".js", ".scss", ".css"], - // Resolve {N} system modules from tns-core-modules - modules: [ - "node_modules/tns-core-modules", - "node_modules", - ], - alias: { - '~': appFullPath - }, - // don't resolve symlinks to symlinked modules - symlinks: true - }, - resolveLoader: { - // don't resolve symlinks to symlinked loaders - symlinks: false - }, - node: { - // Disable node shims that conflict with NativeScript - "http": false, - "timers": false, - "setImmediate": false, - "fs": "empty", - "__dirname": false, - }, - devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"), - optimization: { - runtimeChunk: "single", - splitChunks: { - cacheGroups: { - vendor: { - name: "vendor", - chunks: "all", - test: (module, chunks) => { - const moduleName = module.nameForCondition ? module.nameForCondition() : ''; - return /[\\/]node_modules[\\/]/.test(moduleName) || - appComponents.some(comp => comp === moduleName); - - }, - enforce: true, - }, - } - }, - minimize: !!uglify, - minimizer: [ - new TerserPlugin({ - parallel: true, - cache: true, - sourceMap: isAnySourceMapEnabled, - terserOptions: { - output: { - comments: false, - semicolons: !isAnySourceMapEnabled - }, - compress: { - // The Android SBG has problems parsing the output - // when these options are enabled - 'collapse_vars': platform !== "android", - sequences: platform !== "android", - } - } - }) - ], - }, - module: { - rules: [ - { - include: join(appFullPath, entryPath), - use: [ - // Require all Android app components - platform === "android" && { - loader: "nativescript-dev-webpack/android-app-components-loader", - options: { modules: appComponents } - }, - - { - loader: "nativescript-dev-webpack/bundle-config-loader", - options: { - loadCss: !snapshot, // load the application css if in debug mode - unitTesting, - appFullPath, - projectRoot, - ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform) - } - }, - ].filter(loader => !!loader) - }, - - { - test: /\.(js|css|scss|html|xml)$/, - use: "nativescript-dev-webpack/hmr/hot-loader" - }, - - { test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" }, - - { - test: /\.css$/, - use: { loader: "css-loader", options: { url: false } } - }, - - { - test: /\.scss$/, - use: [ - { loader: "css-loader", options: { url: false } }, - "sass-loader" - ] - }, - ] - }, - plugins: [ - // Define useful constants like TNS_WEBPACK - new webpack.DefinePlugin({ - "global.TNS_WEBPACK": "true", - "process": undefined, - }), - // Remove all files from the out dir. - new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }), - // Copy assets to out dir. Add your own globs as needed. - new CopyWebpackPlugin([ - { from: { glob: "fonts/**" } }, - { from: { glob: "**/*.jpg" } }, - { from: { glob: "**/*.png" } }, - ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }), - new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"), - - // For instructions on how to set up workers with webpack - // check out https://github.com/nativescript/worker-loader - new NativeScriptWorkerPlugin(), - new nsWebpack.PlatformFSPlugin({ - platform, - platforms, - }), - // Does IPC communication with the {N} CLI to notify events when running in watch mode. - new nsWebpack.WatchStateLoggerPlugin() - ], - }; - - if (report) { - // Generate report files for bundles content - config.plugins.push(new BundleAnalyzerPlugin({ - analyzerMode: "static", - openAnalyzer: false, - generateStatsFile: true, - reportFilename: resolve(projectRoot, "report", `report.html`), - statsFilename: resolve(projectRoot, "report", `stats.json`), - })); - } - - if (snapshot) { - config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ - chunk: "vendor", - requireModules: [ - "tns-core-modules/bundle-entry-points", - ], - projectRoot, - webpackConfig: config, - })); - } - - if (hmr) { - config.plugins.push(new webpack.HotModuleReplacementPlugin()); - } - - - return config; -}; diff --git a/demo/TypeScriptApp/.gitignore b/demo/TypeScriptApp/.gitignore index 5cd54bcf..637f431c 100644 --- a/demo/TypeScriptApp/.gitignore +++ b/demo/TypeScriptApp/.gitignore @@ -4,4 +4,6 @@ e2e/**/*.js app/app.android.css app/app.ios.css app/main-page.android.css -app/main-page.ios.css \ No newline at end of file +app/main-page.ios.css + +webpack.config.js \ No newline at end of file diff --git a/demo/TypeScriptApp/webpack.config.js b/demo/TypeScriptApp/webpack.config.js deleted file mode 100644 index 64ad184c..00000000 --- a/demo/TypeScriptApp/webpack.config.js +++ /dev/null @@ -1,291 +0,0 @@ -const { join, relative, resolve, sep } = require("path"); - -const webpack = require("webpack"); -const nsWebpack = require("nativescript-dev-webpack"); -const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); -const CleanWebpackPlugin = require("clean-webpack-plugin"); -const CopyWebpackPlugin = require("copy-webpack-plugin"); -const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); -const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"); -const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin"); -const TerserPlugin = require("terser-webpack-plugin"); -const hashSalt = Date.now().toString(); - -module.exports = env => { - // Add your custom Activities, Services and other Android app components here. - const appComponents = [ - "tns-core-modules/ui/frame", - "tns-core-modules/ui/frame/activity", - resolve(__dirname, "app/activity.android.ts") - ]; - - const platform = env && (env.android && "android" || env.ios && "ios"); - if (!platform) { - throw new Error("You need to provide a target platform!"); - } - - const platforms = ["ios", "android"]; - const projectRoot = __dirname; - - // Default destination inside platforms//... - const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot)); - - const { - // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. - appPath = "app", - appResourcesPath = "app/App_Resources", - - // You can provide the following flags when running 'tns run android|ios' - snapshot, // --env.snapshot - production, // --env.production - uglify, // --env.uglify - report, // --env.report - sourceMap, // --env.sourceMap - hiddenSourceMap, // --env.hiddenSourceMap - hmr, // --env.hmr, - unitTesting, // --env.unitTesting, - verbose, // --env.verbose - } = env; - const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap; - const externals = nsWebpack.getConvertedExternals(env.externals); - - const appFullPath = resolve(projectRoot, appPath); - const appResourcesFullPath = resolve(projectRoot, appResourcesPath); - - const entryModule = nsWebpack.getEntryModule(appFullPath, platform); - const entryPath = `.${sep}${entryModule}.ts`; - const entries = { bundle: entryPath, application: "./application.android" }; - - const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json"); - - const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1); - if (platform === "ios" && !areCoreModulesExternal) { - entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules"; - }; - - let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist); - - const itemsToClean = [`${dist}/**/*`]; - if (platform === "android") { - itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`); - itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`); - } - - nsWebpack.processAppComponents(appComponents, platform); - const config = { - mode: production ? "production" : "development", - context: appFullPath, - externals, - watchOptions: { - ignored: [ - appResourcesFullPath, - // Don't watch hidden files - "**/.*", - ] - }, - target: nativescriptTarget, - entry: entries, - output: { - pathinfo: false, - path: dist, - sourceMapFilename, - libraryTarget: "commonjs2", - filename: "[name].js", - globalObject: "global", - hashSalt - }, - resolve: { - extensions: [".ts", ".js", ".scss", ".css"], - // Resolve {N} system modules from tns-core-modules - modules: [ - resolve(__dirname, "node_modules/tns-core-modules"), - resolve(__dirname, "node_modules"), - "node_modules/tns-core-modules", - "node_modules", - ], - alias: { - '~': appFullPath - }, - // resolve symlinks to symlinked modules - symlinks: true - }, - resolveLoader: { - // don't resolve symlinks to symlinked loaders - symlinks: false - }, - node: { - // Disable node shims that conflict with NativeScript - "http": false, - "timers": false, - "setImmediate": false, - "fs": "empty", - "__dirname": false, - }, - devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"), - optimization: { - runtimeChunk: "single", - splitChunks: { - cacheGroups: { - vendor: { - name: "vendor", - chunks: "all", - test: (module, chunks) => { - const moduleName = module.nameForCondition ? module.nameForCondition() : ''; - return /[\\/]node_modules[\\/]/.test(moduleName) || - appComponents.some(comp => comp === moduleName); - - }, - enforce: true, - }, - } - }, - minimize: !!uglify, - minimizer: [ - new TerserPlugin({ - parallel: true, - cache: true, - sourceMap: isAnySourceMapEnabled, - terserOptions: { - output: { - comments: false, - semicolons: !isAnySourceMapEnabled - }, - compress: { - // The Android SBG has problems parsing the output - // when these options are enabled - 'collapse_vars': platform !== "android", - sequences: platform !== "android", - } - } - }) - ], - }, - module: { - rules: [ - { - include: join(appFullPath, entryPath), - use: [ - // Require all Android app components - platform === "android" && { - loader: "nativescript-dev-webpack/android-app-components-loader", - options: { modules: appComponents } - }, - - { - loader: "nativescript-dev-webpack/bundle-config-loader", - options: { - loadCss: !snapshot, // load the application css if in debug mode - unitTesting, - appFullPath, - projectRoot, - ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform) - } - }, - ].filter(loader => !!loader) - }, - - { - test: /\.(ts|css|scss|html|xml)$/, - use: "nativescript-dev-webpack/hmr/hot-loader" - }, - - { test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" }, - - { - test: /\.css$/, - use: { loader: "css-loader", options: { url: false } } - }, - - { - test: /\.scss$/, - use: [ - { loader: "css-loader", options: { url: false } }, - "sass-loader" - ] - }, - - { - test: /\.ts$/, - use: { - loader: "ts-loader", - options: { - configFile: tsConfigPath, - // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds - // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement - transpileOnly: true, - allowTsInNodeModules: true, - compilerOptions: { - sourceMap: isAnySourceMapEnabled, - declaration: false - } - }, - } - }, - ] - }, - plugins: [ - // Define useful constants like TNS_WEBPACK - new webpack.DefinePlugin({ - "global.TNS_WEBPACK": "true", - "process": undefined, - }), - // Remove all files from the out dir. - new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }), - // Copy assets to out dir. Add your own globs as needed. - new CopyWebpackPlugin([ - { from: { glob: "fonts/**" } }, - { from: { glob: "**/*.jpg" } }, - { from: { glob: "**/*.png" } }, - ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }), - new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"), - // For instructions on how to set up workers with webpack - // check out https://github.com/nativescript/worker-loader - new NativeScriptWorkerPlugin(), - new nsWebpack.PlatformFSPlugin({ - platform, - platforms, - }), - // Does IPC communication with the {N} CLI to notify events when running in watch mode. - new nsWebpack.WatchStateLoggerPlugin(), - // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds - // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement - new ForkTsCheckerWebpackPlugin({ - tsconfig: tsConfigPath, - async: false, - useTypescriptIncrementalApi: true, - memoryLimit: 4096 - }) - ], - }; - - if (report) { - // Generate report files for bundles content - config.plugins.push(new BundleAnalyzerPlugin({ - analyzerMode: "static", - openAnalyzer: false, - generateStatsFile: true, - reportFilename: resolve(projectRoot, "report", `report.html`), - statsFilename: resolve(projectRoot, "report", `stats.json`), - })); - } - - if (snapshot) { - config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ - chunk: "vendor", - requireModules: [ - "tns-core-modules/bundle-entry-points", - ], - projectRoot, - webpackConfig: config, - })); - } - - if (hmr) { - config.plugins.push(new webpack.HotModuleReplacementPlugin()); - } - - - return config; -}; diff --git a/templates/webpack.angular.js b/templates/webpack.angular.js index fa623eee..e6110ff0 100644 --- a/templates/webpack.angular.js +++ b/templates/webpack.angular.js @@ -35,8 +35,7 @@ module.exports = env => { const { // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. + // the nsconfig.json configuration file. appPath = "src", appResourcesPath = "App_Resources", diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index f9add996..7460dd2a 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -30,8 +30,7 @@ module.exports = env => { const { // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. + // the nsconfig.json configuration file. appPath = "app", appResourcesPath = "app/App_Resources", diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index 9e0a3e84..44a60a10 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -31,8 +31,7 @@ module.exports = env => { const { // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. + // the nsconfig.json configuration file. appPath = "app", appResourcesPath = "app/App_Resources", diff --git a/templates/webpack.vue.js b/templates/webpack.vue.js index 61068c7f..28bbfe9f 100644 --- a/templates/webpack.vue.js +++ b/templates/webpack.vue.js @@ -34,8 +34,7 @@ module.exports = env => { const { // The 'appPath' and 'appResourcesPath' values are fetched from - // the nsconfig.json configuration file - // when bundling with `tns run android|ios --bundle`. + // the nsconfig.json configuration file. appPath = "app", appResourcesPath = "app/App_Resources", From 2cf708b5d3e4d1c790730c2f048d7eca6f845f80 Mon Sep 17 00:00:00 2001 From: Elena Hristova Date: Thu, 8 Aug 2019 17:21:23 +0300 Subject: [PATCH 3/3] chore: restore demo app webpack.configs --- demo/AngularApp/.gitignore | 4 +- demo/AngularApp/webpack.config.js | 320 +++++++++++++++++++++++++++ demo/JavaScriptApp/.gitignore | 4 +- demo/JavaScriptApp/webpack.config.js | 259 ++++++++++++++++++++++ demo/TypeScriptApp/.gitignore | 4 +- demo/TypeScriptApp/webpack.config.js | 290 ++++++++++++++++++++++++ 6 files changed, 872 insertions(+), 9 deletions(-) create mode 100644 demo/AngularApp/webpack.config.js create mode 100644 demo/JavaScriptApp/webpack.config.js create mode 100644 demo/TypeScriptApp/webpack.config.js diff --git a/demo/AngularApp/.gitignore b/demo/AngularApp/.gitignore index 70cda1a4..1ccc6d9f 100644 --- a/demo/AngularApp/.gitignore +++ b/demo/AngularApp/.gitignore @@ -10,6 +10,4 @@ test-results.xml app/item/items.component.android.css app/item/items.component.ios.css -mochawesome-report - -webpack.config.js \ No newline at end of file +mochawesome-report \ No newline at end of file diff --git a/demo/AngularApp/webpack.config.js b/demo/AngularApp/webpack.config.js new file mode 100644 index 00000000..04179234 --- /dev/null +++ b/demo/AngularApp/webpack.config.js @@ -0,0 +1,320 @@ +const { join, relative, resolve, sep, dirname } = require("path"); + +const webpack = require("webpack"); +const nsWebpack = require("nativescript-dev-webpack"); +const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); +const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns-replace-bootstrap"); +const { nsReplaceLazyLoader } = require("nativescript-dev-webpack/transformers/ns-replace-lazy-loader"); +const { nsSupportHmrNg } = require("nativescript-dev-webpack/transformers/ns-support-hmr-ng"); +const { getMainModulePath } = require("nativescript-dev-webpack/utils/ast-utils"); +const CleanWebpackPlugin = require("clean-webpack-plugin"); +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"); +const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin"); +const TerserPlugin = require("terser-webpack-plugin"); +const { getAngularCompilerPlugin } = require("nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin"); +const hashSalt = Date.now().toString(); + +module.exports = env => { + // Add your custom Activities, Services and other Android app components here. + const appComponents = [ + "tns-core-modules/ui/frame", + "tns-core-modules/ui/frame/activity", + resolve(__dirname, "app/activity.android.ts") + ]; + + const platform = env && (env.android && "android" || env.ios && "ios"); + if (!platform) { + throw new Error("You need to provide a target platform!"); + } + + const AngularCompilerPlugin = getAngularCompilerPlugin(platform); + const projectRoot = __dirname; + + // Default destination inside platforms//... + const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot)); + + const { + // The 'appPath' and 'appResourcesPath' values are fetched from + // the nsconfig.json configuration file. + appPath = "src", + appResourcesPath = "App_Resources", + + // You can provide the following flags when running 'tns run android|ios' + aot, // --env.aot + snapshot, // --env.snapshot, + production, // --env.production + uglify, // --env.uglify + report, // --env.report + sourceMap, // --env.sourceMap + hiddenSourceMap, // --env.hiddenSourceMap + hmr, // --env.hmr, + unitTesting, // --env.unitTesting + verbose, // --env.verbose + } = env; + + const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap; + const externals = nsWebpack.getConvertedExternals(env.externals); + const appFullPath = resolve(projectRoot, appPath); + const appResourcesFullPath = resolve(projectRoot, appResourcesPath); + const tsConfigName = "tsconfig.tns.json"; + const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`; + const entryPath = `.${sep}${entryModule}`; + const entries = { bundle: entryPath, application: "./application.android" }; + const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1); + if (platform === "ios" && !areCoreModulesExternal) { + entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules"; + }; + + const ngCompilerTransformers = []; + const additionalLazyModuleResources = []; + if (aot) { + ngCompilerTransformers.push(nsReplaceBootstrap); + } + + if (hmr) { + ngCompilerTransformers.push(nsSupportHmrNg); + } + + // when "@angular/core" is external, it's not included in the bundles. In this way, it will be used + // directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes + // fixes https://github.com/NativeScript/nativescript-cli/issues/4024 + if (env.externals && env.externals.indexOf("@angular/core") > -1) { + const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule), tsConfigName); + if (appModuleRelativePath) { + const appModuleFolderPath = dirname(resolve(appFullPath, appModuleRelativePath)); + // include the lazy loader inside app module + ngCompilerTransformers.push(nsReplaceLazyLoader); + // include the new lazy loader path in the allowed ones + additionalLazyModuleResources.push(appModuleFolderPath); + } + } + + const ngCompilerPlugin = new AngularCompilerPlugin({ + hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]), + platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)), + mainPath: join(appFullPath, entryModule), + tsConfigPath: join(__dirname, tsConfigName), + skipCodeGeneration: !aot, + sourceMap: !!isAnySourceMapEnabled, + additionalLazyModuleResources: additionalLazyModuleResources + }); + + let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist); + + const itemsToClean = [`${dist}/**/*`]; + if (platform === "android") { + itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`); + itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`); + } + + nsWebpack.processAppComponents(appComponents, platform); + const config = { + mode: production ? "production" : "development", + context: appFullPath, + externals, + watchOptions: { + ignored: [ + appResourcesFullPath, + // Don't watch hidden files + "**/.*", + ] + }, + target: nativescriptTarget, + entry: entries, + output: { + pathinfo: false, + path: dist, + sourceMapFilename, + libraryTarget: "commonjs2", + filename: "[name].js", + globalObject: "global", + hashSalt + }, + resolve: { + extensions: [".ts", ".js", ".scss", ".css"], + // Resolve {N} system modules from tns-core-modules + modules: [ + resolve(__dirname, "node_modules/tns-core-modules"), + resolve(__dirname, "node_modules"), + "node_modules/tns-core-modules", + "node_modules", + ], + alias: { + '~': appFullPath + }, + symlinks: true + }, + resolveLoader: { + symlinks: false + }, + node: { + // Disable node shims that conflict with NativeScript + "http": false, + "timers": false, + "setImmediate": false, + "fs": "empty", + "__dirname": false, + }, + devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"), + optimization: { + runtimeChunk: "single", + splitChunks: { + cacheGroups: { + vendor: { + name: "vendor", + chunks: "all", + test: (module, chunks) => { + const moduleName = module.nameForCondition ? module.nameForCondition() : ''; + return /[\\/]node_modules[\\/]/.test(moduleName) || + appComponents.some(comp => comp === moduleName); + }, + enforce: true, + }, + } + }, + minimize: !!uglify, + minimizer: [ + new TerserPlugin({ + parallel: true, + cache: true, + sourceMap: isAnySourceMapEnabled, + terserOptions: { + output: { + comments: false, + semicolons: !isAnySourceMapEnabled + }, + compress: { + // The Android SBG has problems parsing the output + // when these options are enabled + 'collapse_vars': platform !== "android", + sequences: platform !== "android", + } + } + }) + ], + }, + module: { + rules: [ + { + include: join(appFullPath, entryPath), + use: [ + // Require all Android app components + platform === "android" && { + loader: "nativescript-dev-webpack/android-app-components-loader", + options: { modules: appComponents } + }, + + { + loader: "nativescript-dev-webpack/bundle-config-loader", + options: { + angular: true, + loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, + ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform) + } + }, + ].filter(loader => !!loader) + }, + + { test: /\.html$|\.xml$/, use: "raw-loader" }, + + // tns-core-modules reads the app.css and its imports using css-loader + { + test: /[\/|\\]app\.css$/, + use: [ + "nativescript-dev-webpack/style-hot-loader", + { loader: "css-loader", options: { url: false } } + ] + }, + { + test: /[\/|\\]app\.scss$/, + use: [ + "nativescript-dev-webpack/style-hot-loader", + { loader: "css-loader", options: { url: false } }, + "sass-loader" + ] + }, + + // Angular components reference css files and their imports using raw-loader + { test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" }, + { test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] }, + + { + test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, + use: [ + "nativescript-dev-webpack/moduleid-compat-loader", + "nativescript-dev-webpack/lazy-ngmodule-hot-loader", + "@ngtools/webpack", + ] + }, + + // Mark files inside `@angular/core` as using SystemJS style dynamic imports. + // Removing this will cause deprecation warnings to appear. + { + test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/, + parser: { system: true }, + }, + ], + }, + plugins: [ + // Define useful constants like TNS_WEBPACK + new webpack.DefinePlugin({ + "global.TNS_WEBPACK": "true", + "process": undefined, + }), + // Remove all files from the out dir. + new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }), + // Copy assets to out dir. Add your own globs as needed. + new CopyWebpackPlugin([ + { from: { glob: "fonts/**" } }, + { from: { glob: "**/*.jpg" } }, + { from: { glob: "**/*.png" } }, + ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }), + new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"), + // For instructions on how to set up workers with webpack + // check out https://github.com/nativescript/worker-loader + new NativeScriptWorkerPlugin(), + ngCompilerPlugin, + // Does IPC communication with the {N} CLI to notify events when running in watch mode. + new nsWebpack.WatchStateLoggerPlugin(), + ], + }; + + if (report) { + // Generate report files for bundles content + config.plugins.push(new BundleAnalyzerPlugin({ + analyzerMode: "static", + openAnalyzer: false, + generateStatsFile: true, + reportFilename: resolve(projectRoot, "report", `report.html`), + statsFilename: resolve(projectRoot, "report", `stats.json`), + })); + } + + if (snapshot) { + config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + angular: true, + requireModules: [ + "reflect-metadata", + "@angular/platform-browser", + "@angular/core", + "@angular/common", + "@angular/router", + "nativescript-angular/platform-static", + "nativescript-angular/router", + ], + projectRoot, + webpackConfig: config, + })); + } + + if (hmr) { + config.plugins.push(new webpack.HotModuleReplacementPlugin()); + } + + return config; +}; \ No newline at end of file diff --git a/demo/JavaScriptApp/.gitignore b/demo/JavaScriptApp/.gitignore index 5f778b3c..c457995c 100644 --- a/demo/JavaScriptApp/.gitignore +++ b/demo/JavaScriptApp/.gitignore @@ -1,5 +1,3 @@ app/main-page.android.css app/main-page.ios.css -mochawesome-report - -webpack.config.js \ No newline at end of file +mochawesome-report \ No newline at end of file diff --git a/demo/JavaScriptApp/webpack.config.js b/demo/JavaScriptApp/webpack.config.js new file mode 100644 index 00000000..c4684e61 --- /dev/null +++ b/demo/JavaScriptApp/webpack.config.js @@ -0,0 +1,259 @@ +const { join, relative, resolve, sep } = require("path"); + +const webpack = require("webpack"); +const nsWebpack = require("nativescript-dev-webpack"); +const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); +const CleanWebpackPlugin = require("clean-webpack-plugin"); +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"); +const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin"); +const TerserPlugin = require("terser-webpack-plugin"); +const hashSalt = Date.now().toString(); + +module.exports = env => { + // Add your custom Activities, Services and other android app components here. + const appComponents = [ + "tns-core-modules/ui/frame", + "tns-core-modules/ui/frame/activity", + resolve(__dirname, "app/activity.android.js") + ]; + + const platform = env && (env.android && "android" || env.ios && "ios"); + if (!platform) { + throw new Error("You need to provide a target platform!"); + } + + const platforms = ["ios", "android"]; + const projectRoot = __dirname; + + // Default destination inside platforms//... + const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot)); + + const { + // The 'appPath' and 'appResourcesPath' values are fetched from + // the nsconfig.json configuration file. + appPath = "app", + appResourcesPath = "app/App_Resources", + + // You can provide the following flags when running 'tns run android|ios' + snapshot, // --env.snapshot + production, // --env.production + uglify, // --env.uglify + report, // --env.report + sourceMap, // --env.sourceMap + hiddenSourceMap, // --env.hiddenSourceMap + hmr, // --env.hmr, + unitTesting, // --env.unitTesting, + verbose, // --env.verbose + } = env; + + const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap; + const externals = nsWebpack.getConvertedExternals(env.externals); + const appFullPath = resolve(projectRoot, appPath); + const appResourcesFullPath = resolve(projectRoot, appResourcesPath); + + const entryModule = nsWebpack.getEntryModule(appFullPath, platform); + const entryPath = `.${sep}${entryModule}.js`; + const entries = { bundle: entryPath, application: "./application.android" }; + const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1); + if (platform === "ios" && !areCoreModulesExternal) { + entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules"; + }; + + let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist); + + const itemsToClean = [`${dist}/**/*`]; + if (platform === "android") { + itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`); + itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`); + } + + nsWebpack.processAppComponents(appComponents, platform); + const config = { + mode: production ? "production" : "development", + context: appFullPath, + externals, + watchOptions: { + ignored: [ + appResourcesFullPath, + // Don't watch hidden files + "**/.*", + ] + }, + target: nativescriptTarget, + entry: entries, + output: { + pathinfo: false, + path: dist, + sourceMapFilename, + libraryTarget: "commonjs2", + filename: "[name].js", + globalObject: "global", + hashSalt + }, + resolve: { + extensions: [".js", ".scss", ".css"], + // Resolve {N} system modules from tns-core-modules + modules: [ + "node_modules/tns-core-modules", + "node_modules", + ], + alias: { + '~': appFullPath + }, + // don't resolve symlinks to symlinked modules + symlinks: true + }, + resolveLoader: { + // don't resolve symlinks to symlinked loaders + symlinks: false + }, + node: { + // Disable node shims that conflict with NativeScript + "http": false, + "timers": false, + "setImmediate": false, + "fs": "empty", + "__dirname": false, + }, + devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"), + optimization: { + runtimeChunk: "single", + splitChunks: { + cacheGroups: { + vendor: { + name: "vendor", + chunks: "all", + test: (module, chunks) => { + const moduleName = module.nameForCondition ? module.nameForCondition() : ''; + return /[\\/]node_modules[\\/]/.test(moduleName) || + appComponents.some(comp => comp === moduleName); + + }, + enforce: true, + }, + } + }, + minimize: !!uglify, + minimizer: [ + new TerserPlugin({ + parallel: true, + cache: true, + sourceMap: isAnySourceMapEnabled, + terserOptions: { + output: { + comments: false, + semicolons: !isAnySourceMapEnabled + }, + compress: { + // The Android SBG has problems parsing the output + // when these options are enabled + 'collapse_vars': platform !== "android", + sequences: platform !== "android", + } + } + }) + ], + }, + module: { + rules: [ + { + include: join(appFullPath, entryPath), + use: [ + // Require all Android app components + platform === "android" && { + loader: "nativescript-dev-webpack/android-app-components-loader", + options: { modules: appComponents } + }, + + { + loader: "nativescript-dev-webpack/bundle-config-loader", + options: { + loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, + ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform) + } + }, + ].filter(loader => !!loader) + }, + + { + test: /\.(js|css|scss|html|xml)$/, + use: "nativescript-dev-webpack/hmr/hot-loader" + }, + + { test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" }, + + { + test: /\.css$/, + use: { loader: "css-loader", options: { url: false } } + }, + + { + test: /\.scss$/, + use: [ + { loader: "css-loader", options: { url: false } }, + "sass-loader" + ] + }, + ] + }, + plugins: [ + // Define useful constants like TNS_WEBPACK + new webpack.DefinePlugin({ + "global.TNS_WEBPACK": "true", + "process": undefined, + }), + // Remove all files from the out dir. + new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }), + // Copy assets to out dir. Add your own globs as needed. + new CopyWebpackPlugin([ + { from: { glob: "fonts/**" } }, + { from: { glob: "**/*.jpg" } }, + { from: { glob: "**/*.png" } }, + ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }), + new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"), + + // For instructions on how to set up workers with webpack + // check out https://github.com/nativescript/worker-loader + new NativeScriptWorkerPlugin(), + new nsWebpack.PlatformFSPlugin({ + platform, + platforms, + }), + // Does IPC communication with the {N} CLI to notify events when running in watch mode. + new nsWebpack.WatchStateLoggerPlugin() + ], + }; + + if (report) { + // Generate report files for bundles content + config.plugins.push(new BundleAnalyzerPlugin({ + analyzerMode: "static", + openAnalyzer: false, + generateStatsFile: true, + reportFilename: resolve(projectRoot, "report", `report.html`), + statsFilename: resolve(projectRoot, "report", `stats.json`), + })); + } + + if (snapshot) { + config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + requireModules: [ + "tns-core-modules/bundle-entry-points", + ], + projectRoot, + webpackConfig: config, + })); + } + + if (hmr) { + config.plugins.push(new webpack.HotModuleReplacementPlugin()); + } + + + return config; +}; \ No newline at end of file diff --git a/demo/TypeScriptApp/.gitignore b/demo/TypeScriptApp/.gitignore index 637f431c..5cd54bcf 100644 --- a/demo/TypeScriptApp/.gitignore +++ b/demo/TypeScriptApp/.gitignore @@ -4,6 +4,4 @@ e2e/**/*.js app/app.android.css app/app.ios.css app/main-page.android.css -app/main-page.ios.css - -webpack.config.js \ No newline at end of file +app/main-page.ios.css \ No newline at end of file diff --git a/demo/TypeScriptApp/webpack.config.js b/demo/TypeScriptApp/webpack.config.js new file mode 100644 index 00000000..2418be9d --- /dev/null +++ b/demo/TypeScriptApp/webpack.config.js @@ -0,0 +1,290 @@ +const { join, relative, resolve, sep } = require("path"); + +const webpack = require("webpack"); +const nsWebpack = require("nativescript-dev-webpack"); +const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); +const CleanWebpackPlugin = require("clean-webpack-plugin"); +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); +const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"); +const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin"); +const TerserPlugin = require("terser-webpack-plugin"); +const hashSalt = Date.now().toString(); + +module.exports = env => { + // Add your custom Activities, Services and other Android app components here. + const appComponents = [ + "tns-core-modules/ui/frame", + "tns-core-modules/ui/frame/activity", + resolve(__dirname, "app/activity.android.ts") + ]; + + const platform = env && (env.android && "android" || env.ios && "ios"); + if (!platform) { + throw new Error("You need to provide a target platform!"); + } + + const platforms = ["ios", "android"]; + const projectRoot = __dirname; + + // Default destination inside platforms//... + const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot)); + + const { + // The 'appPath' and 'appResourcesPath' values are fetched from + // the nsconfig.json configuration file. + appPath = "app", + appResourcesPath = "app/App_Resources", + + // You can provide the following flags when running 'tns run android|ios' + snapshot, // --env.snapshot + production, // --env.production + uglify, // --env.uglify + report, // --env.report + sourceMap, // --env.sourceMap + hiddenSourceMap, // --env.hiddenSourceMap + hmr, // --env.hmr, + unitTesting, // --env.unitTesting, + verbose, // --env.verbose + } = env; + const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap; + const externals = nsWebpack.getConvertedExternals(env.externals); + + const appFullPath = resolve(projectRoot, appPath); + const appResourcesFullPath = resolve(projectRoot, appResourcesPath); + + const entryModule = nsWebpack.getEntryModule(appFullPath, platform); + const entryPath = `.${sep}${entryModule}.ts`; + const entries = { bundle: entryPath, application: "./application.android" }; + + const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json"); + + const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1); + if (platform === "ios" && !areCoreModulesExternal) { + entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules"; + }; + + let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist); + + const itemsToClean = [`${dist}/**/*`]; + if (platform === "android") { + itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`); + itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`); + } + + nsWebpack.processAppComponents(appComponents, platform); + const config = { + mode: production ? "production" : "development", + context: appFullPath, + externals, + watchOptions: { + ignored: [ + appResourcesFullPath, + // Don't watch hidden files + "**/.*", + ] + }, + target: nativescriptTarget, + entry: entries, + output: { + pathinfo: false, + path: dist, + sourceMapFilename, + libraryTarget: "commonjs2", + filename: "[name].js", + globalObject: "global", + hashSalt + }, + resolve: { + extensions: [".ts", ".js", ".scss", ".css"], + // Resolve {N} system modules from tns-core-modules + modules: [ + resolve(__dirname, "node_modules/tns-core-modules"), + resolve(__dirname, "node_modules"), + "node_modules/tns-core-modules", + "node_modules", + ], + alias: { + '~': appFullPath + }, + // resolve symlinks to symlinked modules + symlinks: true + }, + resolveLoader: { + // don't resolve symlinks to symlinked loaders + symlinks: false + }, + node: { + // Disable node shims that conflict with NativeScript + "http": false, + "timers": false, + "setImmediate": false, + "fs": "empty", + "__dirname": false, + }, + devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"), + optimization: { + runtimeChunk: "single", + splitChunks: { + cacheGroups: { + vendor: { + name: "vendor", + chunks: "all", + test: (module, chunks) => { + const moduleName = module.nameForCondition ? module.nameForCondition() : ''; + return /[\\/]node_modules[\\/]/.test(moduleName) || + appComponents.some(comp => comp === moduleName); + + }, + enforce: true, + }, + } + }, + minimize: !!uglify, + minimizer: [ + new TerserPlugin({ + parallel: true, + cache: true, + sourceMap: isAnySourceMapEnabled, + terserOptions: { + output: { + comments: false, + semicolons: !isAnySourceMapEnabled + }, + compress: { + // The Android SBG has problems parsing the output + // when these options are enabled + 'collapse_vars': platform !== "android", + sequences: platform !== "android", + } + } + }) + ], + }, + module: { + rules: [ + { + include: join(appFullPath, entryPath), + use: [ + // Require all Android app components + platform === "android" && { + loader: "nativescript-dev-webpack/android-app-components-loader", + options: { modules: appComponents } + }, + + { + loader: "nativescript-dev-webpack/bundle-config-loader", + options: { + loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, + ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform) + } + }, + ].filter(loader => !!loader) + }, + + { + test: /\.(ts|css|scss|html|xml)$/, + use: "nativescript-dev-webpack/hmr/hot-loader" + }, + + { test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" }, + + { + test: /\.css$/, + use: { loader: "css-loader", options: { url: false } } + }, + + { + test: /\.scss$/, + use: [ + { loader: "css-loader", options: { url: false } }, + "sass-loader" + ] + }, + + { + test: /\.ts$/, + use: { + loader: "ts-loader", + options: { + configFile: tsConfigPath, + // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds + // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement + transpileOnly: true, + allowTsInNodeModules: true, + compilerOptions: { + sourceMap: isAnySourceMapEnabled, + declaration: false + } + }, + } + }, + ] + }, + plugins: [ + // Define useful constants like TNS_WEBPACK + new webpack.DefinePlugin({ + "global.TNS_WEBPACK": "true", + "process": undefined, + }), + // Remove all files from the out dir. + new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }), + // Copy assets to out dir. Add your own globs as needed. + new CopyWebpackPlugin([ + { from: { glob: "fonts/**" } }, + { from: { glob: "**/*.jpg" } }, + { from: { glob: "**/*.png" } }, + ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }), + new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"), + // For instructions on how to set up workers with webpack + // check out https://github.com/nativescript/worker-loader + new NativeScriptWorkerPlugin(), + new nsWebpack.PlatformFSPlugin({ + platform, + platforms, + }), + // Does IPC communication with the {N} CLI to notify events when running in watch mode. + new nsWebpack.WatchStateLoggerPlugin(), + // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds + // https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement + new ForkTsCheckerWebpackPlugin({ + tsconfig: tsConfigPath, + async: false, + useTypescriptIncrementalApi: true, + memoryLimit: 4096 + }) + ], + }; + + if (report) { + // Generate report files for bundles content + config.plugins.push(new BundleAnalyzerPlugin({ + analyzerMode: "static", + openAnalyzer: false, + generateStatsFile: true, + reportFilename: resolve(projectRoot, "report", `report.html`), + statsFilename: resolve(projectRoot, "report", `stats.json`), + })); + } + + if (snapshot) { + config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ + chunk: "vendor", + requireModules: [ + "tns-core-modules/bundle-entry-points", + ], + projectRoot, + webpackConfig: config, + })); + } + + if (hmr) { + config.plugins.push(new webpack.HotModuleReplacementPlugin()); + } + + + return config; +}; \ No newline at end of file