|
| 1 | +const { join, dirname } = require('path'); |
| 2 | +const { merge } = require('webpack-merge'); |
| 3 | +const globRegex = require('glob-regex'); |
| 4 | + |
| 5 | +function getKarmaTestsRegex(webpack) { |
| 6 | + const karmaConfig = require(webpack.Utils.project.getProjectFilePath('karma.conf.js')); |
| 7 | + let filesRegex = karmaConfig.filesRegex || |
| 8 | + new RegExp((karmaConfig.filePatterns || []).map((glob) => |
| 9 | + globRegex(`./${glob}`).source // all webpack require.context start with `./` and glob-regex adds ^ |
| 10 | + ).join('|')); |
| 11 | + |
| 12 | + if (!filesRegex || !filesRegex.source) { |
| 13 | + webpack.Utils.log.warn("Karma files regex not found, falling back to tests/**/*.ts"); |
| 14 | + filesRegex = /tests\/.*\.ts/; |
| 15 | + } |
| 16 | + return filesRegex; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * @param {typeof import("@nativescript/webpack")} webpack |
| 21 | + */ |
| 22 | +module.exports = webpack => { |
| 23 | + webpack.chainWebpack((config, env) => { |
| 24 | + if (env.karmaWebpack) { |
| 25 | + return setupKarmaBuild(config, env, webpack); |
| 26 | + } |
| 27 | + |
| 28 | + if (env.unitTesting) { |
| 29 | + return setupUnitTestBuild(config, env, webpack); |
| 30 | + } |
| 31 | + }); |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * @param {import("webpack-chain")} config |
| 36 | + * @param {typeof import("@nativescript/webpack")} webpack |
| 37 | + */ |
| 38 | +function setupKarmaBuild(config, env, webpack) { |
| 39 | + const karmaWebpack = require('karma-webpack/lib/webpack/defaults'); |
| 40 | + const defaults = karmaWebpack.create(); |
| 41 | + delete defaults.optimization; |
| 42 | + |
| 43 | + karmaWebpack.create = () => { |
| 44 | + return defaults; |
| 45 | + }; |
| 46 | + |
| 47 | + config.entryPoints.clear(); |
| 48 | + config.optimization.clear(); |
| 49 | + |
| 50 | + config.plugins.delete('WatchStatePlugin'); |
| 51 | + if (config.plugins.has('AngularCompilerPlugin')) { |
| 52 | + config.plugins.delete('AngularCompilerPlugin'); |
| 53 | + config.module.rules.delete('angular'); |
| 54 | + } |
| 55 | + // config.plugins.delete('CleanWebpackPlugin') |
| 56 | + |
| 57 | + |
| 58 | + config.output.delete('path'); // use temp path |
| 59 | + config.output.set('iife', true); |
| 60 | + config.output.set('libraryTarget', 'global'); |
| 61 | + config.output.set('clean', true); |
| 62 | + |
| 63 | + config.module |
| 64 | + .rule('unit-test') |
| 65 | + .include.add(webpack.Utils.platform.getEntryDirPath()).end() |
| 66 | + .test(/\.(ts|js)/) |
| 67 | + .use('unit-test-loader') |
| 68 | + .loader(join(__dirname, 'loaders', 'unit-test-loader')) |
| 69 | + .options({ |
| 70 | + appPath: webpack.Utils.platform.getEntryDirPath(), |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @param {import("webpack-chain")} config |
| 76 | + * @param {typeof import("@nativescript/webpack")} webpack |
| 77 | + */ |
| 78 | +function setupUnitTestBuild(config, env, webpack) { |
| 79 | + // config.plugins.delete('CleanWebpackPlugin'); |
| 80 | + // config.output.set('clean', false); |
| 81 | + |
| 82 | + // harmless warnings |
| 83 | + config.set( |
| 84 | + 'ignoreWarnings', |
| 85 | + (config.get('ignoreWarnings') || []).concat([ |
| 86 | + /Can't resolve '@nativescript\/unit-test-runner\/app\/stop-process.js'/ |
| 87 | + ]) |
| 88 | + ); |
| 89 | + |
| 90 | + const runnerPath = dirname( |
| 91 | + require.resolve('@nativescript/unit-test-runner/package.json') |
| 92 | + ); |
| 93 | + config.module.rule('css').include.add(runnerPath); |
| 94 | + config.module.rule('xml').include.add(runnerPath); |
| 95 | + config.module.rule('js').include.add(runnerPath); |
| 96 | + const filesRegex = getKarmaTestsRegex(webpack); |
| 97 | + |
| 98 | + config.plugin('DefinePlugin').tap((args) => { |
| 99 | + args[0] = merge(args[0], { |
| 100 | + 'global.TNS_WEBPACK': true, |
| 101 | + }); |
| 102 | + |
| 103 | + return args; |
| 104 | + }); |
| 105 | + |
| 106 | + const entryPath = webpack.Utils.virtualModules.addVirtualEntry(config, 'unit-test-runner', ` |
| 107 | + // VIRTUAL ENTRY START |
| 108 | + const context = require.context( |
| 109 | + "~/", |
| 110 | + /* deep: */ true, |
| 111 | + /* filter: */ ${filesRegex} |
| 112 | + ); |
| 113 | + global.registerWebpackModules(context); |
| 114 | + // VIRTUAL ENTRY END |
| 115 | + `); |
| 116 | + |
| 117 | + // config.entryPoints.clear() |
| 118 | + config.entry('bundle') |
| 119 | + .clear() |
| 120 | + .add('@nativescript/core/globals/index.js') |
| 121 | + .add('@nativescript/core/bundle-entry-points') |
| 122 | + .add('@nativescript/unit-test-runner/app/bundle-app') |
| 123 | + // .add('@nativescript/unit-test-runner/app/entry') |
| 124 | + .add(entryPath); |
| 125 | + if (webpack.Utils.platform.getPlatformName() === 'android') { |
| 126 | + config.entry('bundle') |
| 127 | + .add('@nativescript/core/ui/frame') |
| 128 | + .add('@nativescript/core/ui/frame/activity'); |
| 129 | + } |
| 130 | +} |
0 commit comments