Skip to content

Commit

Permalink
fix(bundler): skip deps cleanup (remove .js) for UMD file
Browse files Browse the repository at this point in the history
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
3cp committed Feb 20, 2019
1 parent 45646e1 commit a4731da
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/build/amodro-trace/write/replace.js
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions spec/lib/build/bundled-source.spec.js
Expand Up @@ -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;
Expand Down

0 comments on commit a4731da

Please sign in to comment.