Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"babylon-walk": "^1.0.2",
"browser-resolve": "^1.11.2",
"chalk": "^2.1.0",
"child-process-promise": "^2.2.1",
"chokidar": "^1.7.0",
"commander": "^2.11.0",
"cross-spawn": "^5.1.0",
Expand Down
10 changes: 7 additions & 3 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,17 @@ class Bundler extends EventEmitter {
return bundle;
}

async resolveAsset(name, parent) {
async resolveAsset(name, parent, options = {}) {
let {path, pkg} = await this.resolver.resolve(name, parent);
if (this.loadedAssets.has(path)) {
return this.loadedAssets.get(path);
}

let asset = this.parser.getAsset(path, pkg, this.options);
let asset = this.parser.getAsset(
path,
pkg,
Object.assign({}, this.options, options)
);
this.loadedAssets.set(path, asset);

if (this.watcher) {
Expand All @@ -270,7 +274,7 @@ class Bundler extends EventEmitter {

async resolveDep(asset, dep) {
try {
return await this.resolveAsset(dep.name, asset.name);
return await this.resolveAsset(dep.name, asset.name, dep);
} catch (err) {
let thrown = err;

Expand Down
2 changes: 2 additions & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Parser {
this.registerExtension('scss', './assets/SASSAsset');

this.registerExtension('html', './assets/HTMLAsset');
this.registerExtension('rs', './assets/RustAsset');
this.registerExtension('wasm', './assets/WasmAsset');

let extensions = options.extensions || {};
for (let ext in extensions) {
Expand Down
4 changes: 3 additions & 1 deletion src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class HTMLAsset extends Asset {
for (let attr in node.attrs) {
let elements = ATTRS[attr];
if (elements && elements.includes(node.tag)) {
let assetPath = this.addURLDependency(node.attrs[attr]);
let assetPath = this.addURLDependency(node.attrs[attr], {
from: 'html'
});
if (!isURL(assetPath)) {
assetPath = urlJoin(this.options.publicURL, assetPath);
}
Expand Down
35 changes: 35 additions & 0 deletions src/assets/RustAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path');
const {exec} = require('child-process-promise');

const md5 = require('../utils/md5');
const JSAsset = require('./JSAsset');

const rustTarget = `wasm32-unknown-unknown`;

class RustAsset extends JSAsset {
collectDependencies() {
// Do nothing. Dependencies are collected by cargo :).
}

async parse() {
const release = process.env.NODE_ENV === 'production';
const {dir, base} = path.parse(this.name);
const wasmPath = path.join(
this.options.publicURL,
md5(this.name) + '.wasm'
);
const cmd = `rustc +nightly --target ${rustTarget} -O --crate-type=cdylib ${base} -o .${wasmPath} ${
release ? ' --release' : ''
}`;

await exec(cmd, {cwd: dir});

const pathToWasm = JSON.stringify(wasmPath);

this.contents = `module.exports=${pathToWasm};`;

return await super.parse(this.contents);
}
}

module.exports = RustAsset;
20 changes: 20 additions & 0 deletions src/assets/WasmAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const RawAsset = require('./RawAsset');
const path = require('path');

class WasmAsset extends RawAsset {
// Don't load raw assets. They will be copied by the RawPackager directly.
load() {}

generate(pathToAsset) {
pathToAsset =
pathToAsset ||
JSON.stringify(
path.join(this.options.publicURL, this.generateBundleName())
);
return {
js: `module.exports=${pathToAsset};`
};
}
}

module.exports = WasmAsset;
1 change: 1 addition & 0 deletions src/utils/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const mkdirp = require('mkdirp');
exports.readFile = promisify(fs.readFile);
exports.writeFile = promisify(fs.writeFile);
exports.stat = promisify(fs.stat);
exports.readdir = promisify(fs.readdir);

exports.exists = function(filename) {
return new Promise(resolve => {
Expand Down