Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ emcc-loader is configuable on webpack.config.js.

- buildDir : string
-- [Required] absolute path to temporary directory used by emcc.
- cwd : string
-- [default=undefined] working directory for compilers. If specified, all paths passed to compilers will be relative against cwd.
- cc : string
-- [default=emcc] c compiler path or command.
- cxx : string
Expand Down
39 changes: 30 additions & 9 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export class Compiler {
const dependencies = await utility.getDependencies(
compiler,
srcPath,
flags
flags,
options.cwd
);
utility.addDependencies(context, dependencies);
const latestModifiedTime = await utility.getLatestModifiedTime(
Expand All @@ -128,9 +129,19 @@ export class Compiler {

await utility.mkdirs(path.dirname(outputPath), undefined);
const { stderr } = await utility
.execute(compiler, [...flags, '-c', '-o', outputPath, srcPath], {
shell: true,
})
.execute(
compiler,
[
...flags,
'-c',
'-o',
options.cwd
? path.relative(options.cwd, outputPath)
: outputPath,
options.cwd ? path.relative(options.cwd, srcPath) : srcPath,
],
{ cwd: options.cwd, shell: true }
)
.catch(err => {
if (err.err.code === 'ENOENT') {
throw new Error(
Expand All @@ -157,18 +168,28 @@ export class Compiler {
'-s',
'WASM=1',
'-o',
outputPath,
...objs,
options.cwd ? path.relative(options.cwd, outputPath) : outputPath,
...objs.map(o => (options.cwd ? path.relative(options.cwd, o) : o)),
];
if (options.preJs) {
flags.unshift('--pre-js', options.preJs);
flags.unshift(
'--pre-js',
options.cwd
? path.relative(options.cwd, options.preJs)
: options.preJs
);
}
if (options.postJs) {
flags.unshift('--post-js', options.postJs);
flags.unshift(
'--post-js',
options.cwd
? path.relative(options.cwd, options.postJs)
: options.postJs
);
}
await utility.mkdirs(path.dirname(outputPath), undefined);
const { stderr } = await utility
.execute(options.ld, flags, { shell: true })
.execute(options.ld, flags, { cwd: options.cwd, shell: true })
.catch(err => {
if (err.err.code === 'ENOENT') {
throw new Error(
Expand Down
6 changes: 6 additions & 0 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export type LoaderOption = {
*/
buildDir: string;

/**
* A path to working directory for compiler.
*/
cwd?: string;

/**
* Option for --pre-js
*/
Expand Down Expand Up @@ -74,6 +79,7 @@ export function parseOption(context: loader.LoaderContext): LoaderOption {
cc: <string>options.cc || 'emcc',
cxx: <string>options.cxx || 'em++',
ld: <string>options.ld || 'emcc',
cwd: <string | undefined>options.cwd,
/* eslint-disable @typescript-eslint/no-explicit-any */
commonFlags: <string[]>(<any>options.commonFlags) || [],
cFlags: <string[]>(<any>options.cFlags) || [],
Expand Down
11 changes: 7 additions & 4 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ export const readFile = promisify(fs.readFile);
export async function getDependencies(
compiler: string,
absPath: string,
flags: string[]
flags: string[],
cwd?: string
) {
const { stdout } = await execute(compiler, [...flags, '-MM', absPath], {
shell: true,
}).catch(err => {
const { stdout } = await execute(
compiler,
[...flags, '-MM', cwd ? path.relative(cwd, absPath) : absPath],
{ cwd, shell: true }
).catch(err => {
throw err.err;
});
const dependencies = stdout
Expand Down