forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack-regression-tests.js
More file actions
128 lines (108 loc) · 3.3 KB
/
Copy pathwebpack-regression-tests.js
File metadata and controls
128 lines (108 loc) · 3.3 KB
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
const path = require("path")
const fs = require("fs-extra")
const chalk = require("chalk")
const cp = require("child_process")
const mainScript = `
const store = observable.object({ count: 0 });
autorun(function () {
console.log('store.count: ' + store.count);
});
store.count++;
store.count--;
`
const files = {
"package.json": `
{
"name": "mobx-webpack-build",
"version": "0.0.1",
"devDependencies": {}
}
`,
"webpack.config.js": `
module.exports = {
entry: './index.js',
output: {
path: __dirname,
filename: './bundle.js'
}
};`,
"index.cjs.js": `
const mobx = require('../');
const observable = mobx.observable;
const autorun = mobx.autorun;
${mainScript}
`,
"index.es.js": `
import { observable, autorun } from '../';
${mainScript}
`
}
function exec(cmd) {
return new Promise((resolve, reject) => {
cp.exec(cmd, (err, stdout, stderr) => {
if (err) return reject(err)
return resolve({ stdout, stderr })
})
})
}
function writeFile(key, name = key) {
return new Promise((resolve, reject) => {
fs.writeFile(name, files[key], "utf8", err => {
if (err) reject(err)
else resolve()
})
})
}
function runWebpackBuild({ webpackVersion, moduleFormat }) {
const buildDir = path.resolve(__dirname, "..", `.wp-build.${webpackVersion}.${moduleFormat}`)
const immediate = () => new Promise(r => setImmediate(r))
function initBuildFolder() {
fs.mkdirSync(buildDir)
process.chdir(buildDir)
return Promise.all([
writeFile("package.json"),
writeFile("webpack.config.js"),
writeFile(`index.${moduleFormat}.js`, "index.js")
])
}
function installWebpack() {
console.log(
chalk.yellow(`Installing webpack@${webpackVersion}, using ${moduleFormat} modules`)
)
// TODO: npm? v5 was giving me issues
return exec(`yarn add --dev webpack@${webpackVersion}`)
}
function execWebpack() {
return exec(path.resolve("node_modules", ".bin", "webpack"))
}
const execBundle = () => {
console.log(chalk.yellow(`Excuting bundle.js with ${process.execPath}`))
return exec(`"${process.execPath}" bundle.js`)
}
function reportStatus({ stdout, stderr }) {
console.log(chalk.red.bold("Output:"))
console.log(stdout)
if (stdout !== "store.count: 0\nstore.count: 1\nstore.count: 0\n") {
return Promise.reject("Stdout from test program was not as expected:\n\n" + stdout)
}
console.log(chalk.green("Success"))
return Promise.resolve()
}
console.log(chalk.cyan(`Running webpack build in ${buildDir}`))
return (
fs
.remove(buildDir)
// Need to wait until after I/O stuff completes or there's intermittent
// access-denied exceptions
.then(immediate)
.then(initBuildFolder)
.then(installWebpack)
.then(execWebpack)
.then(execBundle)
.then(reportStatus)
)
}
runWebpackBuild({ webpackVersion: "2", moduleFormat: "es" })
.then(() => runWebpackBuild({ webpackVersion: "2", moduleFormat: "cjs" }))
.then(() => runWebpackBuild({ webpackVersion: "1", moduleFormat: "cjs" }))
.catch(e => console.error(e))