/
testing.ts
130 lines (121 loc) · 3.38 KB
/
testing.ts
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
import fs from 'fs-extra';
import { join } from 'path';
import rollupCommonjs from '@rollup/plugin-commonjs';
import rollupJson from '@rollup/plugin-json';
import rollupResolve from '@rollup/plugin-node-resolve';
import { aliasPlugin } from './plugins/alias-plugin';
import { BuildOptions } from '../utils/options';
import { lazyRequirePlugin } from './plugins/lazy-require';
import { replacePlugin } from './plugins/replace-plugin';
import { writePkgJson } from '../utils/write-pkg-json';
import { RollupOptions, OutputOptions } from 'rollup';
import { prettyMinifyPlugin } from './plugins/pretty-minify';
import { getBanner } from '../utils/banner';
export async function testing(opts: BuildOptions) {
const inputDir = join(opts.buildDir, 'testing');
await Promise.all([
// copy jest testing entry files
fs.copy(join(opts.scriptsBundlesDir, 'helpers', 'jest'), opts.output.testingDir),
copyTestingInternalDts(opts, inputDir),
]);
// write package.json
writePkgJson(opts, opts.output.testingDir, {
name: '@stencil/core/testing',
description: 'Stencil testing suite.',
main: 'index.js',
types: 'index.d.ts',
});
const external = [
'assert',
'buffer',
'child_process',
'console',
'constants',
'crypto',
'fs',
'@jest/core',
'jest-cli',
'jest',
'expect',
'@jest/reporters',
'jest-message-id',
'net',
'os',
'path',
'process',
'puppeteer',
'puppeteer-core',
'readline',
'rollup',
'@rollup/plugin-commonjs',
'@rollup/plugin-node-resolve',
'stream',
'tty',
'typescript',
'url',
'util',
'vm',
'yargs',
'zlib',
];
const output: OutputOptions = {
format: 'cjs',
dir: opts.output.testingDir,
esModule: false,
preferConst: true,
};
const testingBundle: RollupOptions = {
input: join(inputDir, 'index.js'),
output,
external,
plugins: [
lazyRequirePlugin(opts, ['@app-data'], '@stencil/core/internal/app-data'),
lazyRequirePlugin(opts, ['@platform', '@stencil/core/internal/testing'], '@stencil/core/internal/testing'),
lazyRequirePlugin(opts, ['@stencil/core/dev-server'], '../dev-server/index.js'),
lazyRequirePlugin(opts, ['@stencil/core/mock-doc'], '../mock-doc/index.cjs'),
{
name: 'testingImportResolverPlugin',
resolveId(importee) {
if (importee === '@stencil/core/compiler') {
return {
id: '../compiler/stencil.js',
external: true,
};
}
if (importee === 'chalk') {
return require.resolve('ansi-colors');
}
return null;
},
},
aliasPlugin(opts),
replacePlugin(opts),
rollupResolve({
preferBuiltins: true,
}),
rollupCommonjs(),
rollupJson({
preferConst: true,
}),
prettyMinifyPlugin(opts, getBanner(opts, `Stencil Testing`, true)),
],
treeshake: {
moduleSideEffects: false,
},
};
return [testingBundle];
}
async function copyTestingInternalDts(opts: BuildOptions, inputDir: string) {
// copy testing d.ts files
await fs.copy(join(inputDir), join(opts.output.testingDir), {
filter: f => {
if (f.endsWith('.d.ts')) {
return true;
}
if (fs.statSync(f).isDirectory() && !f.includes('platform')) {
return true;
}
return false;
},
});
}