Skip to content

Commit

Permalink
chore(build): fix build process
Browse files Browse the repository at this point in the history
Fix Gulp TS by using a dedicated Babel env. Refactor build scripts.
  • Loading branch information
buzz committed May 28, 2024
1 parent 2c9db2f commit 398ba0a
Show file tree
Hide file tree
Showing 21 changed files with 379 additions and 388 deletions.
32 changes: 25 additions & 7 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
module.exports = (api) => {
// Babel can't transpile to ES on-the-fly. So, we need to transform `import.meta.dirname` to old `__dirname`.
function transformImportMetaDirname() {
return {
visitor: {
MemberExpression(path) {
if (path.node.object.type === 'MetaProperty' && path.node.property.name === 'dirname') {
path.replaceWithSourceString('__dirname')
}
},
},
}
}

const babel = (api) => {
api.cache(true)

const browserTarget = '> 0.25%, not dead'
const nodeTarget = 'node 14.16'
const nodeTarget = { node: '20' }

const buildMixin = {
ignore: ['./__tests__', './**/*.d.ts'],
Expand All @@ -21,10 +34,6 @@ module.exports = (api) => {
// ESM build
ESM: {
presets: [['@babel/preset-env', { modules: false, targets: nodeTarget }]],
plugins: [
// Node.js ESM needs extensions in import statements
'babel-plugin-add-import-extension',
],
...buildMixin,
},

Expand All @@ -38,11 +47,20 @@ module.exports = (api) => {
...buildMixin,
},

// Bundled esm
// Bundled ESM
ESM_ROLLUP: {
presets: [['@babel/preset-env', { targets: browserTarget }]],
...buildMixin,
},

// Gulp
GULP: {
presets: [['@babel/preset-env', { modules: false, targets: nodeTarget }]],
plugins: [transformImportMetaDirname],
sourceMaps: false,
},
},
}
}

module.exports = babel
8 changes: 4 additions & 4 deletions gulp/compile/mediainfolib.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { join } from 'path'
import path from 'node:path'

import { CFLAGS, CPU_CORES, CXXFLAGS, MediaInfoLib_CXXFLAGS, VENDOR_DIR } from '../constants'
import { spawn } from '../utils'
import { CFLAGS, CPU_CORES, CXXFLAGS, MediaInfoLib_CXXFLAGS, VENDOR_DIR } from '../constants.ts'
import { spawn } from '../utils.ts'

const mediainfolibDir = join(VENDOR_DIR, 'MediaInfoLib', 'Project', 'GNU', 'Library')
const mediainfolibDir = path.join(VENDOR_DIR, 'MediaInfoLib', 'Project', 'GNU', 'Library')

async function task() {
await spawn('./autogen.sh', [], mediainfolibDir)
Expand Down
69 changes: 37 additions & 32 deletions gulp/compile/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
// https://github.com/emscripten-core/emscripten/blob/main/src/settings.js

import { copyFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { copyFile, mkdir } from 'node:fs/promises'
import path from 'node:path'

import gulp from 'gulp'

import { BUILD_DIR, CXXFLAGS, DIST_DIR, MediaInfoLib_CXXFLAGS } from '../constants'
import { format, spawn } from '../utils'
import {
BUILD_DIR,
CXXFLAGS,
DIST_DIR,
MediaInfoLib_CXXFLAGS,
WASM_INITIAL_MEMORY,
} from '../constants.ts'
import { format, spawn } from '../utils.ts'

const moduleFilepath = join(BUILD_DIR, 'MediaInfoModule.js')
const moduleFilepath = path.join(BUILD_DIR, 'MediaInfoModule.js')

// TODO: test
// MALLOC=emmalloc
// INITIAL_HEAP = 16777216
// EMBIND_STD_STRING_IS_UTF8
// INCOMING_MODULE_JS_API

function makeArgs(environment: 'web' | 'node', es6: boolean, es6ImportMeta: boolean) {
return [
...CXXFLAGS.split(' '),
...MediaInfoLib_CXXFLAGS.split(' '),
'-s',
'TOTAL_MEMORY=33554432', // 32MiB
'-s',
'ALLOW_MEMORY_GROWTH=1',
'-s',
'ASSERTIONS=0',
'-s',
`ENVIRONMENT=${environment}`,
'-s',
`EXPORT_ES6=${es6 ? '1' : '0'}`,
'-s',
'LEGACY_VM_SUPPORT=0',
'-s',
'MODULARIZE=1',
'-s',
'NO_FILESYSTEM=1',
'-s',
`USE_ES6_IMPORT_META=${es6ImportMeta ? '1' : '0'}`,
'-s',
'EMBIND_STD_STRING_IS_UTF8=1',
`-INITIAL_HEAP=${WASM_INITIAL_MEMORY}`,
'-sALLOW_MEMORY_GROWTH=1',
'-sMALLOC=emmalloc',
'-sASSERTIONS=0',
`-sENVIRONMENT=${environment}`,
`-sEXPORT_ES6=${es6 ? '1' : '0'}`,
'-sLEGACY_VM_SUPPORT=0',
'-sMODULARIZE=1',
'-sNO_FILESYSTEM=1',
`-sUSE_ES6_IMPORT_META=${es6ImportMeta ? '1' : '0'}`,
'-sEMBIND_STD_STRING_IS_UTF8=1',
'-sINCOMING_MODULE_JS_API=locateFile',
'--closure',
'0',
'-lembind',
Expand Down Expand Up @@ -70,7 +74,7 @@ compileMediaInfoModule.description = 'Compile MediaInfoModule'
// MediaInfoModule.js (Node CJS)
async function buildNodeCjs() {
await spawn('emcc', makeArgs('node', false, false), BUILD_DIR)
await format(moduleFilepath, join(BUILD_DIR, 'MediaInfoModule.cjs.js'))
await format(moduleFilepath, path.join(BUILD_DIR, 'MediaInfoModule.cjs.js'))
}

buildNodeCjs.displayName = 'compile:node-cjs'
Expand All @@ -79,7 +83,7 @@ buildNodeCjs.description = 'Build WASM (Node CJS)'
// MediaInfoModule.js (Node ESM)
async function buildNodeEsm() {
await spawn('emcc', makeArgs('node', true, true), BUILD_DIR)
await format(moduleFilepath, join(BUILD_DIR, 'MediaInfoModule.esm.js'))
await format(moduleFilepath, path.join(BUILD_DIR, 'MediaInfoModule.esm.js'))
}

buildNodeEsm.displayName = 'compile:node-esm'
Expand All @@ -88,17 +92,18 @@ buildNodeEsm.description = 'Build WASM (Node ESM)'
// MediaInfoModule.js (Browser)
async function buildBrowser() {
await spawn('emcc', makeArgs('web', true, false), BUILD_DIR)
await format(moduleFilepath, join(BUILD_DIR, 'MediaInfoModule.browser.js'))
await format(moduleFilepath, path.join(BUILD_DIR, 'MediaInfoModule.browser.js'))
}

buildBrowser.displayName = 'compile:browser'
buildBrowser.description = 'Build WASM (Browser)'

async function copyWasm() {
try {
await mkdir(DIST_DIR)
} catch {}
await copyFile(join(BUILD_DIR, 'MediaInfoModule.wasm'), join(DIST_DIR, 'MediaInfoModule.wasm'))
await mkdir(DIST_DIR, { recursive: true })
await copyFile(
path.join(BUILD_DIR, 'MediaInfoModule.wasm'),
path.join(DIST_DIR, 'MediaInfoModule.wasm')
)
}

copyWasm.displayName = 'compile:copy-wasm'
Expand Down
8 changes: 4 additions & 4 deletions gulp/compile/zenlib.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { join } from 'path'
import path from 'node:path'

import { CFLAGS, CPU_CORES, CXXFLAGS, VENDOR_DIR } from '../constants'
import { spawn } from '../utils'
import { CFLAGS, CPU_CORES, CXXFLAGS, VENDOR_DIR } from '../constants.ts'
import { spawn } from '../utils.ts'

const zenlibDir = join(VENDOR_DIR, 'ZenLib', 'Project', 'GNU', 'Library')
const zenlibDir = path.join(VENDOR_DIR, 'ZenLib', 'Project', 'GNU', 'Library')

async function task() {
await spawn('./autogen.sh', [], zenlibDir)
Expand Down
25 changes: 16 additions & 9 deletions gulp/constants.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { cpus } from 'os'
import { join, resolve } from 'path'
import { cpus } from 'node:os'
import path from 'node:path'

const PROJECT_DIR = resolve(__dirname, '..')
const SRC_DIR = join(PROJECT_DIR, 'src')
const DIST_DIR = join(PROJECT_DIR, 'dist')
const BUILD_DIR = join(PROJECT_DIR, 'build')
const VENDOR_DIR = join(BUILD_DIR, 'vendor')
const PROJECT_DIR = path.resolve(import.meta.dirname, '..')
const SRC_DIR = path.join(PROJECT_DIR, 'src')
const DIST_DIR = path.join(PROJECT_DIR, 'dist')
const BUILD_DIR = path.join(PROJECT_DIR, 'build')
const VENDOR_DIR = path.join(BUILD_DIR, 'vendor')

const WASM_INITIAL_MEMORY = 2 ** 25 // 32 MiB

// Global variable name for UMD build
const UMD_NAME = 'MediaInfo'

const LIBMEDIAINFO_VERSION = '24.04'
const LIBZEN_VERSION = '0.4.41'

const CFLAGS = '-Oz'
const CXXFLAGS = '-Oz -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -fno-rtti -fno-exceptions'

// switch off feature to save some bytes
// switch off features to save some bytes
const MediaInfoLib_CXXFLAGS = `-I ../../../Source -I ../../../../ZenLib/Source -s USE_ZLIB=1 \
-DMEDIAINFO_MINIMAL_YES \
-DMEDIAINFO_EXPORT_YES \
Expand Down Expand Up @@ -56,7 +61,7 @@ const MediaInfoLib_CXXFLAGS = `-I ../../../Source -I ../../../../ZenLib/Source -
-DMEDIAINFO_DECODE_NO \
-DMEDIAINFO_IBIUSAGE_NO`

const CPU_CORES = cpus()?.length ?? 1
const CPU_CORES = cpus().length

export {
BUILD_DIR,
Expand All @@ -69,5 +74,7 @@ export {
MediaInfoLib_CXXFLAGS,
PROJECT_DIR,
SRC_DIR,
UMD_NAME,
VENDOR_DIR,
WASM_INITIAL_MEMORY,
}
32 changes: 15 additions & 17 deletions gulp/declaration.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { join } from 'path'
import path from 'node:path'

import gulp from 'gulp'

import { DIST_DIR, PROJECT_DIR, SRC_DIR } from './constants'
import { spawn } from './utils'
import { DIST_DIR, PROJECT_DIR, SRC_DIR } from './constants.ts'
import { spawn } from './utils.ts'

function generateDeclaration() {
return spawn(
'tsc',
[
'--emitDeclarationOnly',
'--declarationDir',
DIST_DIR,
'--declaration',
'true',
join(SRC_DIR, 'index.ts'),
],
PROJECT_DIR
)
async function generateDeclaration() {
const args = [
'--emitDeclarationOnly',
'--declarationDir',
DIST_DIR,
'--declaration',
'true',
'--skipLibCheck',
path.join(SRC_DIR, 'index.ts'),
]
await spawn('tsc', args, PROJECT_DIR)
}

function copyDeclaration() {
return gulp.src(join(SRC_DIR, 'MediaInfoModule.d.ts')).pipe(gulp.dest(DIST_DIR))
return gulp.src(path.join(SRC_DIR, 'MediaInfoModule.d.ts')).pipe(gulp.dest(DIST_DIR))
}

const declarationTask = gulp.parallel([generateDeclaration, copyDeclaration])
Expand Down
22 changes: 22 additions & 0 deletions gulp/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gulp from 'gulp'

import mediainfolib from './compile/mediainfolib.ts'
import wasm from './compile/wasm.ts'
import zenlib from './compile/zenlib.ts'
import declaration from './declaration.ts'
import download from './download.ts'
import generateTypes from './generate-types/generate.ts'
import babel from './transpile/babel.ts'
import rollup from './transpile/rollup.ts'

const defaultTask = gulp.series([
gulp.parallel([
generateTypes,
gulp.series([download, gulp.series([zenlib, mediainfolib, wasm])]),
]),
gulp.parallel([babel, declaration, rollup]),
])
defaultTask.displayName = 'default'
defaultTask.description = 'Build project'

export default defaultTask
41 changes: 19 additions & 22 deletions gulp/download.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import { access, mkdir } from 'fs/promises'
import { basename, join } from 'path'
import { access, mkdir } from 'node:fs/promises'
import path from 'node:path'

import decompress from 'decompress'
import gulp from 'gulp'

import { LIBMEDIAINFO_VERSION, LIBZEN_VERSION, VENDOR_DIR } from './constants'
import { downloadFile } from './utils'

const urls = {
libmediainfo: {
url: `https://mediaarea.net/download/source/libmediainfo/${LIBMEDIAINFO_VERSION}/libmediainfo_${LIBMEDIAINFO_VERSION}.tar.bz2`,
dir: VENDOR_DIR,
},
libzen: {
url: `https://mediaarea.net/download/source/libzen/${LIBZEN_VERSION}/libzen_${LIBZEN_VERSION}.tar.bz2`,
dir: VENDOR_DIR,
},
import { LIBMEDIAINFO_VERSION, LIBZEN_VERSION, VENDOR_DIR } from './constants.ts'
import { downloadFile } from './utils.ts'

const urls = { libmediainfo: LIBMEDIAINFO_VERSION, libzen: LIBZEN_VERSION }

function getUrl(libname: string, version: string) {
return `https://mediaarea.net/download/source/${libname}/${version}/${libname}_${version}.tar.bz2`
}

const task = gulp.parallel(
Object.entries(urls).map(([name, { url: dlUrl, dir }]) => {
Object.entries(urls).map(([libname, version]) => {
const dlTask = async () => {
await mkdir(dir, { recursive: true })
await mkdir(VENDOR_DIR, { recursive: true })
const dlUrl = getUrl(libname, version)
const { pathname } = new URL(dlUrl)
if (pathname === null) throw new Error('URL pathname is null')
const filename = basename(pathname)
const filepath = join(VENDOR_DIR, filename)
const filename = path.basename(pathname)
const filepath = path.join(VENDOR_DIR, filename)

// skip download if file exists
try {
await access(filepath)
} catch {
await downloadFile(dlUrl, filepath)
}
await decompress(filepath, dir)

await decompress(filepath, VENDOR_DIR)
}

dlTask.displayName = `download:${name}`
dlTask.Description = `Download ${name} sources`
dlTask.displayName = `download:${libname}-v${version}`
dlTask.Description = `Download ${libname} v${version} sources`

return dlTask
})
Expand Down
Loading

0 comments on commit 398ba0a

Please sign in to comment.