diff --git a/lib/build/amodro-trace/write/replace.js b/lib/build/amodro-trace/write/replace.js index 4c7c3b358..fb4a58199 100644 --- a/lib/build/amodro-trace/write/replace.js +++ b/lib/build/amodro-trace/write/replace.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 => { diff --git a/package-lock.json b/package-lock.json index 48fc4f625..a8b4afc83 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6931,7 +6931,7 @@ "split": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", - "integrity": "sha1-YFvZvjA6pZ+zX5Ip++oN3snqB9k=", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", "dev": true, "requires": { "through": "2" @@ -6948,7 +6948,7 @@ "split2": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/split2/-/split2-2.2.0.tgz", - "integrity": "sha1-GGsldbz4PoW30YRldWI47k7kJJM=", + "integrity": "sha512-RAb22TG39LhI31MbreBgIuKiIKhVsawfTgEGqKHTK87aG+ul/PB8Sqoi3I7kVdRWiCfrKxK3uo4/YUkpNvhPbw==", "dev": true, "requires": { "through2": "^2.0.2" diff --git a/spec/lib/build/bundled-source.spec.js b/spec/lib/build/bundled-source.spec.js index 611a77177..ae43fddfa 100644 --- a/spec/lib/build/bundled-source.spec.js +++ b/spec/lib/build/bundled-source.spec.js @@ -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 '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;