-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathbuild.js
165 lines (136 loc) · 3.88 KB
/
build.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* A build task that compiles jQuery Migrate JS modules into one bundle.
*/
import path from "node:path";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import * as rollup from "rollup";
import { minify } from "./minify.js";
import { getTimestamp } from "./lib/getTimestamp.js";
import { compareSize } from "./compare_size.js";
import util from "node:util";
import { exec as nodeExec } from "node:child_process";
import { isCleanWorkingDir } from "./lib/isCleanWorkingDir.js";
const exec = util.promisify( nodeExec );
function read( filename ) {
return readFile( filename, "utf8" );
}
async function readJSON( filename ) {
return JSON.parse( await read( filename ) );
}
async function getOutputRollupOptions( {
esm = false
} = {} ) {
const wrapperFilePath = path.join( "src", `wrapper${
esm ? "-esm" : ""
}.js` );
const wrapperSource = await read( wrapperFilePath );
// Catch `// @CODE` and subsequent comment lines event if they don't start
// in the first column.
const wrapper = wrapperSource.split(
/[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/
);
return {
// The ESM format is not actually used as we strip it during the
// build, inserting our own wrappers; it's just that it doesn't
// generate any extra wrappers so there's nothing for us to remove.
format: "esm",
intro: wrapper[ 0 ].replace( /\n*$/, "" ),
outro: wrapper[ 1 ].replace( /^\n*/, "" )
};
}
async function writeCompiled( { code, dir, filename, version } ) {
const compiledContents = code
// Embed Version
.replace( /@VERSION/g, version )
// Embed Date
// yyyy-mm-ddThh:mmZ
.replace( /@DATE/g, new Date().toISOString().replace( /:\d+\.\d+Z$/, "Z" ) );
await writeFile( path.join( dir, filename ), compiledContents );
console.log( `[${ getTimestamp() }] ${ filename } v${ version } created.` );
}
export async function build( {
dir = "dist",
filename = "jquery-migrate.js",
esm = false,
watch = false,
version
} = {} ) {
const pkg = await readJSON( "package.json" );
// Add the short commit hash to the version string
// when the version is not for a release.
if ( !version ) {
const { stdout } = await exec( "git rev-parse --short HEAD" );
const isClean = await isCleanWorkingDir();
// "+SHA" is semantically correct
// Add ".dirty" as well if the working dir is not clean
version = `${ pkg.version }+${ stdout.trim() }${
isClean ? "" : ".dirty"
}`;
}
const inputRollupOptions = {};
const outputRollupOptions = await getOutputRollupOptions( { esm } );
const src = "src/migrate.js";
inputRollupOptions.input = path.resolve( src );
await mkdir( dir, { recursive: true } );
if ( watch ) {
const watcher = rollup.watch( {
...inputRollupOptions,
output: [ outputRollupOptions ],
watch: {
include: "src/**",
skipWrite: true
}
} );
watcher.on( "event", async( event ) => {
switch ( event.code ) {
case "ERROR":
console.error( event.error );
break;
case "BUNDLE_END":
const {
output: [ { code } ]
} = await event.result.generate( outputRollupOptions );
await writeCompiled( {
code,
dir,
filename,
version
} );
break;
}
} );
return watcher;
} else {
const bundle = await rollup.rollup( inputRollupOptions );
const {
output: [ { code } ]
} = await bundle.generate( outputRollupOptions );
await writeCompiled( { code, dir, filename, version } );
await minify( { dir, filename, version } );
}
}
export async function buildDefaultFiles( {
version = process.env.VERSION,
watch
} = {} ) {
await Promise.all( [
build( { version, watch } ),
build( {
dir: "dist-module",
filename: "jquery-migrate.module.js",
esm: true,
version,
watch
} )
] );
if ( watch ) {
console.log( "Watching files..." );
} else {
return compareSize( {
files: [
"dist/jquery-migrate.min.js",
"dist-module/jquery-migrate.module.min.js"
]
} );
}
}