Permalink
Browse files

feat(bundler): support Node.js direct json loading require("foo.json")

  • Loading branch information...
huochunpeng committed Sep 2, 2018
1 parent 1a75ad7 commit 8fa88008da6cdb024d9bdcd7d668728ac2618dc1
Showing with 35 additions and 0 deletions.
  1. +4 −0 lib/build/bundled-source.js
  2. +31 −0 spec/lib/build/bundled-source.spec.js
@@ -109,6 +109,10 @@ exports.BundledSource = class {
if (matchingPlugin) {
deps = findDeps(modulePath, this.contents);
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)});});`;
this.contents = contents;
} else {
// forceCjsWrap bypasses a r.js parse bug.
// See lib/amodro-trace/read/cjs.js for more info.
@@ -296,6 +296,37 @@ exports.t = t;
.toBe("define('foo/dist/cjs/lo',['require','exports','module'],function (require, exports, module) {});");
});
it('transforms npm package json file', () => {
let file = {
path: path.resolve(cwd, 'node_modules/foo/bar/lo.json'),
contents: '{"a":1}'
};
let bs = new BundledSource(bundler, file);
bs._getProjectRoot = () => 'src';
bs.includedBy = {
includedBy: {
description: {
name: 'foo',
mainId: 'foo/index',
loaderConfig: {
name: 'foo',
path: '../node_modules/foo',
main: 'index'
}
}
}
};
bs._getLoaderPlugins = () => [];
bs._getLoaderConfig = () => ({paths: {}});
let deps = bs.transform();
expect(deps).toBeUndefined();
expect(bs.requiresTransform).toBe(false);
expect(bs.contents)
.toBe('define(\'foo/bar/lo.json\',[],function(){return JSON.parse("{\\\"a\\\":1}");});');
});
it('transforms npm package non-js file', () => {
let file = {
path: path.resolve(cwd, 'node_modules/foo/bar/lo.html'),

0 comments on commit 8fa8800

Please sign in to comment.