| 1 |
import goog from "google-closure-compiler"; |
| 2 |
import JarPath from "google-closure-compiler-java"; |
| 3 |
import prettier from "prettier"; |
| 4 |
import { Readable } from "stream"; |
| 5 |
import { fileURLToPath } from "url"; |
| 6 |
import rimraf from "rimraf"; |
| 7 |
import fs from "fs"; |
| 8 |
import path from "path"; |
| 9 |
import * as R from "ramda"; |
| 10 |
const { compiler: ClosureCompiler } = goog; |
| 11 |
import { inlineWebworker } from "./goog/generate-webworker-module.js"; |
| 12 |
import { inlineArraybuffer } from "./goog/inline-arraybuffer.js"; |
| 13 |
|
| 14 |
const DEV = process.env.NODE_ENV === "development"; |
| 15 |
const __filename = fileURLToPath(import.meta.url); |
| 16 |
const __dirname = path.dirname(__filename); |
| 17 |
const rootDir = __dirname; //path.resolve(__dirname, "."); |
| 18 |
const nodeModulesDir = path.join(rootDir, "node_modules"); |
| 19 |
const externsDir = path.join(rootDir, "externs"); |
| 20 |
const srcDir = path.join(rootDir, "src"); |
| 21 |
const googDir = path.join(rootDir, "goog"); |
| 22 |
const distDir = path.join(rootDir, "dist"); |
| 23 |
|
| 24 |
function trimString(a) { |
| 25 |
return a |
| 26 |
.split("\n") |
| 27 |
.map((line) => line.trim()) |
| 28 |
.filter((line) => Boolean(line)) |
| 29 |
.join("\n"); |
| 30 |
} |
| 31 |
|
| 32 |
const monkeyPatches = (code) => { |
| 33 |
return R.pipe(R.replace("cwd = process.cwd();", "cwd = '/';"))(code); |
| 34 |
}; |
| 35 |
|
| 36 |
const inputFile = path.join(srcDir, "workers/sab.worker.js"); |
| 37 |
const tmpOutFileName = path.join(rootDir, "dist", "test.min.js"); |
| 38 |
|
| 39 |
if (fs.existsSync(distDir)) { |
| 40 |
rimraf.sync(distDir); |
| 41 |
} |
| 42 |
fs.mkdirSync(distDir); |
| 43 |
|
| 44 |
if (process.env.BUILD_STATIC) { |
| 45 |
fs.writeFileSync( |
| 46 |
path.join(rootDir, "dist", "__csound_wasm.inline.js"), |
| 47 |
inlineArraybuffer("./node_modules/@csound/wasm-bin/lib/csound.static.wasm.z", "binary.wasm"), |
| 48 |
); |
| 49 |
} else { |
| 50 |
fs.writeFileSync( |
| 51 |
path.join(rootDir, "dist", "__csound_wasm.inline.js"), |
| 52 |
inlineArraybuffer("./node_modules/@csound/wasm-bin/lib/csound.dylib.wasm.z", "binary.wasm"), |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
const polyfills = { |
| 57 |
fetch_noop: fs.readFileSync("polyfills/fetch-noop.js", "utf-8"), |
| 58 |
set_timeout_noop: fs.readFileSync("polyfills/set-timeout-noop.js", "utf-8"), |
| 59 |
text_encoding: fs.readFileSync("polyfills/text-encoding.js", "utf-8"), |
| 60 |
performance: fs.readFileSync("polyfills/performance.js", "utf-8"), |
| 61 |
}; |
| 62 |
|
| 63 |
const compilationSequence = [ |
| 64 |
{ |
| 65 |
entry_point: "./src/workers/sab.worker", |
| 66 |
js_output_file: path.join(rootDir, "dist", "__compiled.sab.worker.js"), |
| 67 |
create_source_map: path.join(rootDir, "dist", "__compiled.sab.worker.js.map"), |
| 68 |
source_map_location_mapping: "./src|/dist/src", |
| 69 |
postbuild: () => inlineWebworker("sab.worker.js", "worker.sab", false, []), |
| 70 |
output_wrapper: trimString(`(function(){%output%}).call(this); |
| 71 |
//# sourceMappingURL=__compiled.sab.worker.js.map`), |
| 72 |
define: DEV ? [] : ["PRODUCTION=1"], |
| 73 |
}, |
| 74 |
{ |
| 75 |
entry_point: "./src/workers/vanilla.worker.js", |
| 76 |
js_output_file: path.join(rootDir, "dist", "__compiled.vanilla.worker.js"), |
| 77 |
create_source_map: path.join(rootDir, "dist", "__compiled.vanilla.worker.js.map"), |
| 78 |
source_map_location_mapping: "./src|/dist/src", |
| 79 |
postbuild: () => inlineWebworker("vanilla.worker.js", "worker.vanilla", false, []), |
| 80 |
output_wrapper: trimString(`(function(){%output%}).call(this); |
| 81 |
//# sourceMappingURL=__compiled.vanilla.worker.js.map`), |
| 82 |
define: DEV ? [] : ["PRODUCTION=1"], |
| 83 |
}, |
| 84 |
{ |
| 85 |
entry_point: "./src/workers/worklet.worker.js", |
| 86 |
js_output_file: path.join(rootDir, "dist", "__compiled.worklet.worker.js"), |
| 87 |
create_source_map: path.join(rootDir, "dist", "__compiled.worklet.worker.js.map"), |
| 88 |
source_map_location_mapping: "./src|/dist/src", |
| 89 |
output_wrapper: trimString(` |
| 90 |
let self = AudioWorkletGlobalScope; |
| 91 |
%output% |
| 92 |
//# sourceMappingURL=__compiled.worklet.worker.js.map`), |
| 93 |
postbuild: () => inlineWebworker("worklet.worker.js", "worker.worklet", true, []), |
| 94 |
define: DEV ? [] : ["PRODUCTION=1"], |
| 95 |
}, |
| 96 |
{ |
| 97 |
entry_point: "./src/workers/worklet.singlethread.worker.js", |
| 98 |
js_output_file: path.join(rootDir, "dist", "__compiled.worklet.singlethread.worker.js"), |
| 99 |
create_source_map: path.join(rootDir, "dist", "__compiled.worklet.singlethread.worker.js.map"), |
| 100 |
source_map_location_mapping: |
| 101 |
path.join(rootDir, "dist", "__compiled.worklet.singlethread.worker.js") + |
| 102 |
"|" + |
| 103 |
"dist/src/workers/worklet.singlethread.worker.js", |
| 104 |
define: ["WITH_TEXT_ENCODER_POLYFILL=1", "PRODUCTION=1"], |
| 105 |
output_wrapper: trimString(` |
| 106 |
let self = AudioWorkletGlobalScope; |
| 107 |
%output% |
| 108 |
//# sourceURL=/dist/__compiled.worklet.singlethread.worker.js |
| 109 |
//# sourceMappingURL=/dist/__compiled.worklet.singlethread.worker.js.map`), |
| 110 |
postbuild: () => |
| 111 |
inlineWebworker("worklet.singlethread.worker.js", "worklet.singlethread.worker", true, []), |
| 112 |
}, |
| 113 |
{ |
| 114 |
entry_point: "./src/workers/old-spn.worker.js", |
| 115 |
js_output_file: path.join(rootDir, "dist", "__compiled.old-spn.worker.js"), |
| 116 |
create_source_map: path.join(rootDir, "dist", "__compiled.old-spn.worker.js.map"), |
| 117 |
source_map_location_mapping: "./src|/dist/src", |
| 118 |
output_wrapper: trimString(`(function(){%output%}).call(this); |
| 119 |
//# sourceMappingURL=__compiled.old-spn.worker.js.map`), |
| 120 |
postbuild: () => inlineWebworker("old-spn.worker.js", "worker.old_spn", false, []), |
| 121 |
define: DEV ? [] : ["PRODUCTION=1"], |
| 122 |
}, |
| 123 |
{ |
| 124 |
entry_point: "./src/index.js", |
| 125 |
js_output_file: path.join(rootDir, "dist", "csound.js"), |
| 126 |
create_source_map: path.join(rootDir, "dist", "csound.js.map"), |
| 127 |
define: DEV ? [] : ["PRODUCTION=1"], |
| 128 |
output_manifest: "output.manifest.txt", |
| 129 |
output_wrapper: trimString(`%output% |
| 130 |
const Csound = Csound$$module$src$index; |
| 131 |
Csound.toString = () => 'async (options) => CsoundObj;'; |
| 132 |
export { Csound } |
| 133 |
export default Csound |
| 134 |
//# sourceMappingURL=csound.js.map`), |
| 135 |
}, |
| 136 |
]; |
| 137 |
|
| 138 |
function deepMerge(v1, v2) { |
| 139 |
if (Array.isArray(v1) && Array.isArray(v2)) { |
| 140 |
return R.uniq(R.concat(v1, v2)); |
| 141 |
} else if (typeof v1 === "object" && typeof v2 === "object" && !R.isNil(v1) && !R.isNil(v2)) { |
| 142 |
return R.mergeWith(deepMerge, v1, v2); |
| 143 |
} else { |
| 144 |
return v2; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
const compile = async (config) => { |
| 149 |
console.log("Compiling", config.entry_point, "\n"); |
| 150 |
const defaultConfig = { |
| 151 |
browser_featureset_year: 2020, |
| 152 |
externs: [path.join(rootDir, "externs.js")], |
| 153 |
js: [ |
| 154 |
"./src/*.js", |
| 155 |
"./src/**/*.js", |
| 156 |
"./dist/*inline.js", |
| 157 |
"./goog/mocks/package.json", |
| 158 |
"./goog/mocks/index.js", |
| 159 |
"./node_modules/lines-logger/package.json", |
| 160 |
"./node_modules/lines-logger/lib/browser.js", |
| 161 |
"./node_modules/lines-logger/lib/index.js", |
| 162 |
"./node_modules/rambda/package.json", |
| 163 |
"./node_modules/rambda/dist/rambda.esm.js", |
| 164 |
"./node_modules/path-browserify/package.json", |
| 165 |
"./node_modules/path-browserify/index.js", |
| 166 |
"./node_modules/comlink/package.json", |
| 167 |
"./node_modules/comlink/dist/esm/comlink.mjs", |
| 168 |
"./node_modules/unmute-ios-audio/package.json", |
| 169 |
"./node_modules/unmute-ios-audio/index.js", |
| 170 |
"./node_modules/eventemitter3/umd/eventemitter3.min.js", |
| 171 |
"./node_modules/google-closure-library/package.json", |
| 172 |
"./node_modules/google-closure-library/**/*.js", |
| 173 |
], |
| 174 |
hide_warnings_for: [ |
| 175 |
// "./node_modules/@wasmer/wasi/lib/index.esm.js", |
| 176 |
// "./node_modules/buffer-es6/index.js", |
| 177 |
], |
| 178 |
assume_function_wrapper: true, |
| 179 |
compilation_level: "SIMPLE_OPTIMIZATIONS", //DEV ? "SIMPLE_OPTIMIZATIONS" : "ADVANCED", |
| 180 |
language_in: "ECMASCRIPT_2021", |
| 181 |
process_common_js_modules: true, |
| 182 |
rewrite_polyfills: false, |
| 183 |
module_resolution: "NODE", |
| 184 |
dependency_mode: "PRUNE", |
| 185 |
force_inject_library: "base", |
| 186 |
debug: false, |
| 187 |
source_map_include_content: true, |
| 188 |
}; |
| 189 |
const closureCompiler = new ClosureCompiler(deepMerge(defaultConfig, config)); |
| 190 |
|
| 191 |
closureCompiler.javaPath = `${process.env.JAVA_HOME}/bin/java`; |
| 192 |
closureCompiler.JAR_PATH = JarPath; |
| 193 |
await new Promise((resolve, reject) => { |
| 194 |
const javaProcess = closureCompiler.run((exitCode, inputString, stderr) => { |
| 195 |
console.log(stderr); |
| 196 |
if (exitCode === 0) { |
| 197 |
resolve(); |
| 198 |
} else { |
| 199 |
reject(); |
| 200 |
process.exit(0); |
| 201 |
} |
| 202 |
}); |
| 203 |
}); |
| 204 |
}; |
| 205 |
|
| 206 |
(async () => { |
| 207 |
for (const compileInfo of compilationSequence) { |
| 208 |
const postbuild = R.prop("postbuild", compileInfo); |
| 209 |
const closureData = R.dissoc("postbuild", compileInfo); |
| 210 |
await compile(closureData); |
| 211 |
postbuild && postbuild(); |
| 212 |
} |
| 213 |
})(); |