-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnativescript.webpack.js
130 lines (119 loc) · 4.42 KB
/
nativescript.webpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { join, dirname } = require('path');
const { existsSync } = require('fs');
const { merge } = require('webpack-merge');
function getTestEntrypoint(webpack) {
const testTsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.ts');
const testJsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.js');
if (existsSync(testTsEntryPath)) {
return testTsEntryPath;
}
if (existsSync(testJsEntryPath)) {
return testJsEntryPath;
}
return null;
}
/**
* @param {typeof import("@nativescript/webpack")} webpack
*/
module.exports = webpack => {
if (!getTestEntrypoint(webpack)) {
webpack.Utils.log.warn('Test entrypoint not found. Loading deprecated @nativescript/unit-test-runner config. Please update your unit testing config.');
return require('./nativescript.webpack.compat')(webpack);
}
webpack.chainWebpack((config, env) => {
if (env.unitTesting) {
return setupUnitTestBuild(config, env, webpack);
} else {
config
.plugin("IgnorePlugin|unit_tests")
.use(IgnorePlugin, [{
checkResource: (resource, context) => {
if (context === webpack.Utils.platform.getEntryDirPath()) {
if (/(^\.\/test|\.spec)\.(ts|js)$/.test(resource)) {
// console.log('ignoring unit test', context, resource);
return true;
}
}
return false;
}
}]);
}
});
};
/**
* @param {import("webpack-chain")} config
* @param {typeof import("@nativescript/webpack")} webpack
*/
function setupUnitTestBuild(config, env, webpack) {
const testEntrypointPath = getTestEntrypoint(webpack);
if (!testEntrypointPath) { // this should never happen
webpack.Utils.log.error('No test entrypoint found');
return;
}
// config.plugins.delete('CleanWebpackPlugin');
// config.output.set('clean', false);
// harmless warnings
config.set(
'ignoreWarnings',
(config.get('ignoreWarnings') || []).concat([
/Can't resolve '@nativescript\/unit-test-runner\/app\/stop-process.js'/
])
);
const runnerPath = dirname(
require.resolve('@nativescript/unit-test-runner/package.json')
);
config.module.rule('css').include.add(runnerPath);
config.module.rule('xml').include.add(runnerPath);
config.module.rule('js').include.add(runnerPath);
if (!env.testTsConfig && env.testTSConfig) {
webpack.Utils.log.warn('Mapping env.testTSConfig to env.testTsConfig');
}
env.testTsConfig = env.testTsConfig || env.testTSConfig;
const defaultTsConfig = webpack.Utils.project.getProjectFilePath('tsconfig.spec.json');
const tsConfigPath = env.testTsConfig || (require('fs').existsSync(defaultTsConfig) ? defaultTsConfig : undefined);
if (tsConfigPath) {
config.when(config.module.rules.has('ts'), (config) => config.module.rule('ts').uses.get('ts-loader').options(merge(config.module.rule('ts').uses.get('ts-loader').get('options'), { configFile: tsConfigPath })));
config.when(config.plugins.has('AngularWebpackPlugin'), (config) => config.plugin('AngularWebpackPlugin').tap((args) => {
args[0] = merge(args[0], { tsconfig: tsConfigPath });
return args;
}));
}
config.plugin('DefinePlugin').tap((args) => {
args[0] = merge(args[0], {
'global.TNS_WEBPACK': true,
});
return args;
});
if (env.codeCoverage) {
config.module
.rule('istanbul-loader')
.enforce('post')
.include
.add(webpack.Utils.platform.getEntryDirPath())
.end()
.exclude
.add(/\.spec\.(tsx?|jsx?)$/)
.add(join(webpack.Utils.platform.getEntryDirPath(), 'tests'))
.add(join(webpack.Utils.platform.getEntryDirPath(), 'test.ts'))
.add(join(webpack.Utils.platform.getEntryDirPath(), 'test.js'))
.end()
.test(/\.(tsx?|jsx?)/)
.use('@jsdevtools/coverage-istanbul-loader')
.loader(require.resolve('@jsdevtools/coverage-istanbul-loader'))
.options({ esModules: true });
}
// config.entryPoints.clear()
config.entry('bundle')
.clear()
.add('@nativescript/core/globals/index.js')
.add('@nativescript/core/bundle-entry-points')
// .add('@nativescript/unit-test-runner/app/bundle-app')
.add(testEntrypointPath);
// .add('@nativescript/unit-test-runner/app/entry')
// .add(entryPath);
if (webpack.Utils.platform.getPlatformName() === 'android') {
config.entry('bundle')
.add('@nativescript/core/ui/frame')
.add('@nativescript/core/ui/frame/activity');
}
}