audiosprite-loader
Advanced tools
Comparing version
@@ -10,6 +10,5 @@ "use strict"; | ||
plugin_1.Plugin.onReady(function (data) { | ||
var str = "\nconst Howl = require(\"howler\").Howl;\n\nwindow.$_audiosprite = window.$_audiosprite || new Howl(" + data + ");\n\nmodule.exports = {\n play: function () {\n window.$_audiosprite.play(\"" + soundId + "\");\n }\n}"; | ||
callback(null, str); | ||
callback(null, "const Howl = require(\"howler\").Howl;\nwindow.$_audiosprite = window.$_audiosprite || new Howl(" + data + ");\n\nmodule.exports = {\n play: function () {\n return window.$_audiosprite.play(\"" + soundId + "\");\n }\n}"); | ||
}); | ||
}; | ||
module.exports.raw = true; |
@@ -8,3 +8,4 @@ export declare class Plugin { | ||
apply(compiler: any): void; | ||
addCompilationAsset(compilation: any, filename: string): Promise<{}>; | ||
isHotUpdateCompilation(assets: any): any; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var audiosprite = require("audiosprite"); | ||
var fs = require("fs"); | ||
var Plugin = /** @class */ (function () { | ||
@@ -8,3 +9,3 @@ function Plugin(options) { | ||
if (!options.format) { | ||
options.format = "howler"; | ||
options.format = "howler2"; | ||
} | ||
@@ -33,8 +34,12 @@ if (!options.output) { | ||
return console.error(err); | ||
// howler 2.x | ||
var json = result; | ||
json.src = json.urls; | ||
var data = JSON.stringify(json); | ||
var data = JSON.stringify(result); | ||
Plugin.onReadyCallbacks.map(function (callback) { return callback(data); }); | ||
next(); | ||
// read and add audio files as compilation assets | ||
// generated files will be removed from the filesystem | ||
Promise.all([ | ||
_this.addCompilationAsset(compilation, _this.options.output + ".ac3"), | ||
_this.addCompilationAsset(compilation, _this.options.output + ".m4a"), | ||
_this.addCompilationAsset(compilation, _this.options.output + ".mp3"), | ||
_this.addCompilationAsset(compilation, _this.options.output + ".ogg") | ||
]).then(function () { return next(); }); | ||
}); | ||
@@ -46,7 +51,16 @@ } | ||
}); | ||
// compiler.plugin("emit", (compilation, callback) => { | ||
// callback(); | ||
// }); | ||
compiler.plugin('done', function () { | ||
// console.log('Plugin: done'); | ||
}; | ||
Plugin.prototype.addCompilationAsset = function (compilation, filename) { | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(filename, function (err, buffer) { | ||
if (err) | ||
console.error(err); | ||
compilation.assets[filename] = { | ||
source: function () { return buffer; }, | ||
size: function () { return buffer.length; } | ||
}; | ||
fs.unlink(filename, function (err) { if (err) | ||
console.log(err); }); | ||
resolve(); | ||
}); | ||
}); | ||
@@ -59,3 +73,2 @@ }; | ||
}; | ||
; | ||
Plugin.FILES = {}; | ||
@@ -62,0 +75,0 @@ Plugin.onReadyCallbacks = []; |
{ | ||
"name": "audiosprite-loader", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Audio Sprite Loader and Plugin for Webpack", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"prepublish": "tsc" | ||
}, | ||
@@ -9,0 +10,0 @@ "repository": { |
@@ -17,6 +17,12 @@ # Webpack Audio Sprite Loader & Plugin | ||
Install the loader. | ||
``` | ||
npm install audiosprite-loader | ||
``` | ||
**`webpack.config.js`** | ||
```javascript | ||
const AudioSpritePlugin = require("webpack-audiosprite-plugin"); | ||
const AudioSpritePlugin = require("audiosprite-loader"); | ||
@@ -23,0 +29,0 @@ module.exports = { |
import * as path from "path"; | ||
import { Plugin } from "./plugin" | ||
@@ -12,18 +11,14 @@ | ||
Plugin.onReady((data) => { | ||
const str = ` | ||
const Howl = require("howler").Howl; | ||
callback(null, `const Howl = require("howler").Howl; | ||
window.$_audiosprite = window.$_audiosprite || new Howl(${ data }); | ||
window.$_audiosprite = window.$_audiosprite || new Howl(${data}); | ||
module.exports = { | ||
play: function () { | ||
window.$_audiosprite.play("${ soundId }"); | ||
return window.$_audiosprite.play("${ soundId }"); | ||
} | ||
}`; | ||
}`); | ||
}); | ||
callback(null, str); | ||
}) | ||
} | ||
module.exports.raw = true; |
import * as audiosprite from "audiosprite"; | ||
import * as fs from "fs"; | ||
@@ -15,3 +16,3 @@ export class Plugin { | ||
if (!options.format) { | ||
options.format = "howler"; | ||
options.format = "howler2"; | ||
} | ||
@@ -41,11 +42,13 @@ | ||
// howler 2.x | ||
let json: any = result; | ||
json.src = json.urls; | ||
const data = JSON.stringify(json); | ||
const data = JSON.stringify(result); | ||
Plugin.onReadyCallbacks.map(callback => callback(data)); | ||
next(); | ||
// read and add audio files as compilation assets | ||
// generated files will be removed from the filesystem | ||
Promise.all([ | ||
this.addCompilationAsset(compilation, `${this.options.output}.ac3`), | ||
this.addCompilationAsset(compilation, `${this.options.output}.m4a`), | ||
this.addCompilationAsset(compilation, `${this.options.output}.mp3`), | ||
this.addCompilationAsset(compilation, `${this.options.output}.ogg`) | ||
]).then(() => next()); | ||
}); | ||
@@ -57,9 +60,18 @@ | ||
}); | ||
} | ||
// compiler.plugin("emit", (compilation, callback) => { | ||
// callback(); | ||
// }); | ||
addCompilationAsset (compilation, filename: string) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filename, (err, buffer) => { | ||
if (err) console.error(err); | ||
compiler.plugin('done', () => { | ||
// console.log('Plugin: done'); | ||
compilation.assets[filename] = { | ||
source: () => buffer, | ||
size: () => buffer.length | ||
}; | ||
fs.unlink(filename, (err) => { if (err) console.log(err) }); | ||
resolve(); | ||
}); | ||
}); | ||
@@ -72,5 +84,4 @@ } | ||
}); | ||
}; | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
require('./child.js'); | ||
const beep = require("./audio/beep.mp3"); | ||
@@ -2,0 +4,0 @@ const boop = require("./audio/boop.wav"); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
284008
0.69%21
10.53%254
11.89%53
12.77%1
Infinity%