Skip to content

File loader update #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [[
"env",
{
"targets": {
"node": "4"
}
}
]]
}
195 changes: 94 additions & 101 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,103 @@ var fs = require ("fs");
var loaderUtils = require ("loader-utils");
var path = require ("path");
var tmp = require ("tmp");
var validateOptions = require ("schema-utils");
var schemaUtils = require ("schema-utils");
var schema = require ("./options.json");

/* eslint-disable
multiline-ternary,
*/
module.exports = function(content) {
if (!this.emitFile) throw new Error('SWF Loader\n\nemitFile is required from module system');

var options = loaderUtils.getOptions(this) || {};

schemaUtils(schema, options, 'SWF Loader');

var context = options.context || this.rootContext || this.options && this.options.context;

var url = loaderUtils.interpolateName(this, options.name, {
context,
content,
regExp: options.regExp
});

module.exports = function (content, map) {

if (!this.emitFile) {

throw new Error ("SWF Loader\n\nemitFile is required from module system");

}

this.cacheable && this.cacheable ();
this.value = content;

var options = loaderUtils.getOptions (this) || {};

validateOptions (schema, options, "SWF Loader");

var context = options.context || this.rootContext || this.options && this.options.context
var url = loaderUtils.interpolateName (this, options.name, { context, content, regExp: options.regExp });
var outputPath = "";

if (options.outputPath) {

outputPath = (typeof options.outputPath === "function" ? options.outputPath (url) : options.outputPath);

}

var filePath = this.resourcePath;

if (options.useRelativePath) {

var issuerContext = (this._module && this._module.issuer && this._module.issuer.context) || context;

var relativeUrl = issuerContext && path.relative (issuerContext, filePath).split (path.sep).join ('/');
var relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`;

// eslint-disable-next-line no-bitwise
if (~relativePath.indexOf ("../")) {

outputPath = path.posix.join (outputPath, relativePath, url);

} else {

outputPath = relativePath + url;

}

url = relativePath + url;

} else if (options.outputPath) {

outputPath = typeof options.outputPath === "function" ? options.outputPath (url) : options.outputPath + url;
url = outputPath;

} else {

outputPath = url;

}

outputPath = path.basename (outputPath, ".swf") + ".bundle";
url = path.basename (url, ".swf") + ".bundle";

var publicPath = `__webpack_public_path__ + ${JSON.stringify (url)}`;

if (options.publicPath !== undefined) {

publicPath = JSON.stringify (typeof options.publicPath === "function" ? options.publicPath (url) : options.publicPath + url);

}

if (options.emitFile === undefined || options.emitFile) {

tmp.setGracefulCleanup ();
var tempDirectory = tmp.dirSync ({ postfix: ".bundle" });

execSync ("openfljs process " + this.resourcePath + " " + tempDirectory.name);

var walkSync = function (baseDir, dir = "", filelist = []) {

fs.readdirSync (baseDir).forEach (function (file) {

filelist = fs.statSync (path.join (baseDir, file)).isDirectory ()
? walkSync (path.join (baseDir, file), path.join (dir, file), filelist)
: filelist.concat (path.join (dir, file));

});

return filelist;

}

walkSync (tempDirectory.name).forEach (function (file) {

this.emitFile (path.join (outputPath, file), fs.readFileSync (path.join (tempDirectory.name, file)));

}.bind (this));

}

return `module.exports = ${publicPath};`;

var outputPath = url;

if (options.outputPath) {
if (typeof options.outputPath === 'function') {
outputPath = options.outputPath(url);
} else {
outputPath = path.posix.join(options.outputPath, url);
}
}

if (options.useRelativePath) {
var filePath = this.resourcePath;

var issuer = options.context ? context : this._module && this._module.issuer && this._module.issuer.context;

var relativeUrl = issuer && path.relative(issuer, filePath).split(path.sep).join('/');

var relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`;
// eslint-disable-next-line no-bitwise
if (~relativePath.indexOf('../')) {
outputPath = path.posix.join(outputPath, relativePath, url);
} else {
outputPath = path.posix.join(relativePath, url);
}
}

var outputPathBundle = outputPath.replace(/\.swf$/, '') + '.bundle';
var publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPathBundle)}`;

if (options.publicPath) {
if (typeof options.publicPath === 'function') {
publicPath = options.publicPath(url);
} else if (options.publicPath.endsWith('/')) {
publicPath = options.publicPath + url;
} else {
publicPath = `${options.publicPath}/${url}`;
}

publicPath = publicPath.replace(/\.swf$/, '') + '.bundle';
publicPath = JSON.stringify(publicPath);
}

outputPath = outputPath.replace(/\.swf$/, '') + '.bundle';

if (options.emitFile === undefined || options.emitFile) {

tmp.setGracefulCleanup ();
var tempDirectory = tmp.dirSync ({ postfix: ".bundle" });

execSync ("openfljs process " + this.resourcePath + " " + tempDirectory.name);

var walkSync = function (baseDir, dir = "", filelist = []) {

fs.readdirSync (baseDir).forEach (function (file) {

filelist = fs.statSync (path.join (baseDir, file)).isDirectory ()
? walkSync (path.join (baseDir, file), path.join (dir, file), filelist)
: filelist.concat (path.join (dir, file));

});

return filelist;

}

walkSync (tempDirectory.name).forEach (function (file) {

this.emitFile (path.join (outputPath, file), fs.readFileSync (path.join (tempDirectory.name, file)));

}.bind (this));

}

// TODO revert to ES2015 Module export, when new CSS Pipeline is in place
return `module.exports = ${publicPath};`;

}

module.exports.raw = true;
31 changes: 17 additions & 14 deletions lib/options.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"type": "object",
"properties": {
"name": {},
"regExp": {},
"context": {
"type": "string"
},
"publicPath": {},
"outputPath": {},
"useRelativePath": {
"type": "boolean"
}
},
"additionalProperties": true
"type": "object",
"properties": {
"name": {},
"regExp": {},
"context": {
"type": "string"
},
"publicPath": {},
"outputPath": {},
"useRelativePath": {
"type": "boolean"
},
"emitFile": {
"type": "boolean"
}
},
"additionalProperties": true
}
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,16 @@
"peerDependencies": {
"openfl": "^7.0.0-beta.7",
"webpack": "^2.0.0 || ^3.0.0"
},
"scripts": {
"test": "jest"
},
"devDependencies": {
"babel-jest": "^22.4.3",
"babel-preset-env": "^1.6.1",
"jest": "^22.4.3",
"memory-fs": "^0.4.1",
"openfl": "^7.1.2",
"webpack": "^3.0.0"
}
}
3 changes: 3 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Loader Defaults 1`] = `"module.exports = __webpack_public_path__ + \\"9baf1f9461ece62ac1a2ae67730b00c8.bundle\\";"`;
4 changes: 4 additions & 0 deletions test/fixtures/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */
import swf from './layout.swf';

export default swf;
Binary file added test/fixtures/layout.swf
Binary file not shown.
68 changes: 68 additions & 0 deletions test/helpers/compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable
import/order,
multiline-ternary,
no-param-reassign,
*/
//import del from 'del';
import path from 'path';
import webpack from 'webpack';
import MemoryFS from 'memory-fs';

const module = (config) => {
return {
rules: config.rules || config.loader
? [
{
test: config.loader.test || /\.swf$/,
use: {
loader: path.resolve(__dirname, '../../lib/loader.js'),
options: config.loader.options || {},
},
},
]
: [],
};
};

const plugins = config => ([
new webpack.optimize.CommonsChunkPlugin({
name: ['runtime'],
minChunks: Infinity,
}),
].concat(config.plugins || []));

const output = (config) => {
return {
path: path.resolve(
__dirname,
`../outputs/${config.output ? config.output : ''}`,
),
filename: '[name].bundle.js',
};
};

export default function (fixture, config, options) {
// webpack Config
config = {
devtool: config.devtool || 'sourcemap',
context: path.resolve(__dirname, '..', 'fixtures'),
entry: `./${fixture}`,
output: output(config),
module: module(config),
plugins: plugins(config),
};
// Compiler Options
options = Object.assign({ output: false }, options);

if (options.output) del.sync(config.output.path);

const compiler = webpack(config);

if (!options.output) compiler.outputFileSystem = new MemoryFS();

return new Promise((resolve, reject) => compiler.run((err, stats) => {
if (err) reject(err);

resolve(stats);
}));
}
21 changes: 21 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable
prefer-destructuring,
*/
import webpack from './helpers/compiler';

describe('Loader', () => {
test('Defaults', async () => {
const config = {
loader: {
test: /\.swf$/,
options: {},
},
};

const stats = await webpack('fixture.js', config);

const { source } = stats.toJson().modules[1];

expect(source).toMatchSnapshot();
});
});
Loading