Skip to content
Permalink
Browse files

fix(bundler): skip deps cleanup (remove .js) for UMD file

When the UMD file was prepared by browserify, those `require('./a.js')` were matched by a hash map like {'./a.js':1}. We cannot clean them up to `require('./a')`, as it will end up with missing entry.

closes #1054
  • Loading branch information...
huochunpeng committed Feb 20, 2019
1 parent 45646e1 commit a4731daa3dc43a974ba4f6e020e8d4b741eb8944
Showing with 52 additions and 2 deletions.
  1. +8 −0 lib/build/amodro-trace/write/replace.js
  2. +2 −2 package-lock.json
  3. +42 −0 spec/lib/build/bundled-source.spec.js
@@ -11,6 +11,8 @@ const astMatcher = require('../../ast-matcher').astMatcher;
// it is definitely a named AMD module at this stage
var amdDep = astMatcher('define(__str, [__anl_deps], __any)');
var cjsDep = astMatcher('require(__any_dep)');
var isUMD = astMatcher('typeof define === "function" && define.amd');
var isUMD2 = astMatcher('typeof define == "function" && define.amd');

module.exports = function stubs(options) {
options = options || {};
@@ -48,6 +50,12 @@ module.exports = function stubs(options) {
// need node location
const parsed = esprima.parse(contents, {range: true});

if (isUMD(parsed) || isUMD2(parsed)) {
// Skip lib in umd format, because browersify umd build could
// use require('./file.js') which we should not strip .js
return contents;
}

const amdMatch = amdDep(parsed);
if (amdMatch) {
amdMatch.forEach(result => {

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -741,6 +741,48 @@ export {t};
.toBe("define('foo/index',['require','exports','module','pack-name.js','@pack/name.js','pack-name.js/foo','@pack/name.js/foo','./bar'],function (require, exports, module) {require('pack-name.js'); require('@pack/name.js'); require('pack-name.js/foo'); require('@pack/name.js/foo'); require('./bar');});");
});

it('does not clears up deps with ".js" for browserify umd build', () => {
const umd = `(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.foo = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
module.exports = 'bar';
},{}],2:[function(require,module,exports){
module.exports = require('./bar.js');
},{"./bar.js":1}]},{},[2])(2)
});
`;
let file = {
path: path.resolve(cwd, 'node_modules/foo/index.js'),
contents: umd
};

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'
},
browserReplacement: () => undefined
}
}
};
bs._getLoaderPlugins = () => [];
bs._getLoaderConfig = () => ({paths: {}});
bs._getUseCache = () => undefined;

let deps = bs.transform();
expect(deps).toBeUndefined();
expect(bs.requiresTransform).toBe(false);
expect(bs.contents).toContain("define('foo/index',[]");
expect(bs.contents).toContain("require('./bar.js')");
});


describe('cache', () => {
let oldGetCache;
let oldSetCache;

0 comments on commit a4731da

Please sign in to comment.
You can’t perform that action at this time.