Skip to content

Commit

Permalink
feat(bundler): support both 'json!f.json' and 'f.json' module id.
Browse files Browse the repository at this point in the history
Be nice to requirejs/system JSON plugin users.
  • Loading branch information
3cp committed Sep 27, 2018
1 parent 6d212ae commit ea005fe
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
14 changes: 11 additions & 3 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,23 @@ exports.Bundle = class {

getBundledModuleIds() {
let allModuleIds = this.getRawBundledModuleIds();
return Array.from(allModuleIds).sort().map(id => {
let allIds = [];
Array.from(allModuleIds).sort().forEach(id => {
let matchingPlugin = this.bundler.loaderOptions.plugins.find(p => p.matches(id));
if (matchingPlugin) {
// make sure text! prefix is added, requirejs needs full form.
// http://requirejs.org/docs/api.html#config-bundles
return matchingPlugin.createModuleId(id);
allIds.push(matchingPlugin.createModuleId(id));
} else if (id.endsWith('.json')) {
allIds.push(id);
// be nice to requirejs json plugin users, add json! prefix
allIds.push(Utils.moduleIdWithPlugin(id, 'json', this.bundler.loaderOptions.type));
} else {
allIds.push(id);
}
return id;
});

return allIds;
}

getBundledFiles() {
Expand Down
10 changes: 9 additions & 1 deletion lib/build/bundled-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const findDeps = require('./find-deps').findDeps;
const cjsTransform = require('./amodro-trace/read/cjs');
const esTransform = require('./amodro-trace/read/es');
const allWriteTransforms = require('./amodro-trace/write/all');
const {moduleIdWithPlugin} = require('./utils');
const logger = require('aurelia-logging').getLogger('BundledSource');

exports.BundledSource = class {
Expand Down Expand Up @@ -49,6 +50,10 @@ exports.BundledSource = class {
return this.bundler.loaderOptions.plugins;
}

_getLoaderType() {
return this.bundler.loaderOptions.type;
}

_getLoaderConfig() {
return this.bundler.loaderConfig;
}
Expand Down Expand Up @@ -95,6 +100,7 @@ exports.BundledSource = class {
dependencyInclusion.description.browserReplacement();

let loaderPlugins = this._getLoaderPlugins();
let loaderType = this._getLoaderType();
let loaderConfig = this._getLoaderConfig();
let moduleId = this.moduleId;
let modulePath = this.path;
Expand All @@ -115,7 +121,9 @@ exports.BundledSource = class {
this.contents = matchingPlugin.transform(moduleId, modulePath, this.contents);
} else if (path.extname(modulePath).toLowerCase() === '.json') {
// support Node.js's json module
let contents = `define(\'${moduleId}\',[],function(){return JSON.parse(${JSON.stringify(this.contents)});});`;
let contents = `define(\'${moduleId}\',[],function(){return JSON.parse(${JSON.stringify(this.contents)});});\n`;
// be nice to requirejs json plugin users, add json! prefix
contents += `define(\'${moduleIdWithPlugin(moduleId, 'json', loaderType)}\',[\'${moduleId}\'],function(m){return m;});\n`;
this.contents = contents;
} else {
// forceCjsWrap bypasses a r.js parse bug.
Expand Down
2 changes: 1 addition & 1 deletion lib/build/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ exports.Bundler = class {
Object.assign(this.loaderConfig, this.project.build.loader.config);

this.loaderOptions.plugins = (this.loaderOptions.plugins || []).map(x => {
let plugin = new LoaderPlugin(this, x);
let plugin = new LoaderPlugin(this.loaderOptions.type, x);

if (plugin.stub && this.loaderConfig.stubModules.indexOf(plugin.name) === -1) {
this.loaderConfig.stubModules.push(plugin.name);
Expand Down
19 changes: 7 additions & 12 deletions lib/build/loader-plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
const {moduleIdWithPlugin} = require('./utils');

exports.LoaderPlugin = class {
constructor(bundler, config) {
this.bundler = bundler;
constructor(type, config) {
this.type = type;
this.config = config;
this.name = config.name;
this.stub = config.stub;
Expand All @@ -19,19 +20,13 @@ exports.LoaderPlugin = class {
}

createModuleId(moduleId) {
let loderConfigType = this.bundler.loaderOptions.type;

switch (loderConfigType) {
case 'require':
return 'text!' + moduleId;
case 'system':
return moduleId + '!text';
default:
throw new Error(`Loader configuration style ${loderConfigType} is not supported.`);
}
// for backward compatibility, use 'text' as plugin name,
// to not break existing app with additional json plugin in aurelia.json
return moduleIdWithPlugin(moduleId, 'text', this.type);
}
};

function regExpFromExtensions(extensions) {
return new RegExp('^.*(' + extensions.map(x => '\\' + x).join('|') + ')$');
}

11 changes: 11 additions & 0 deletions lib/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ exports.resolvePackagePath = function(packageName) {
throw new Error(`cannot resolve npm package folder for "${packageName}"`);
};

exports.moduleIdWithPlugin = function(moduleId, pluginName, type) {
switch (type) {
case 'require':
return pluginName + '!' + moduleId;
case 'system':
return moduleId + '!' + pluginName;
default:
throw new Error(`Loader configuration style ${type} is not supported.`);
}
};

exports.generateBundleName = function(contents, fileName, rev) {
let hash;
if (rev === true) {
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/build/bundled-source.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export {t};
expect(deps).toBeUndefined();
expect(bs.requiresTransform).toBe(false);
expect(bs.contents)
.toBe('define(\'foo/bar/lo.json\',[],function(){return JSON.parse("{\\\"a\\\":1}");});');
.toBe('define(\'foo/bar/lo.json\',[],function(){return JSON.parse("{\\\"a\\\":1}");});\ndefine(\'json!foo/bar/lo.json\',[\'foo/bar/lo.json\'],function(m){return m;});\n');
});

it('transforms npm package non-js file', () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/mocks/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = class Bundler {
this.project = new ProjectMock();
this.loaderOptions = {
type: 'require',
plugins: [new LoaderPlugin(this, {
plugins: [new LoaderPlugin('require', {
name: 'text',
extensions: ['.html', '.css']
})]
Expand Down

0 comments on commit ea005fe

Please sign in to comment.