Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat another entry as an external module (fix #514) #592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { resolve, relative, dirname, basename, extname } from 'path';
import { resolve, relative, dirname, basename, extname, join } from 'path';
import { green, red, yellow, white, blue } from 'kleur';
import { map, series } from 'asyncro';
import glob from 'tiny-glob/sync';
Expand Down Expand Up @@ -376,9 +376,8 @@ const shebang = {};
function createConfig(options, entry, format, writeMeta) {
let { pkg } = options;

let external = ['dns', 'fs', 'path', 'url'].concat(
options.entries.filter(e => e !== entry),
);
let external = ['dns', 'fs', 'path', 'url'];
const externalEntries = options.entries.filter(e => e !== entry);

let outputAliases = {};
// since we transform src/index.js, we need to rename imports for it:
Expand Down Expand Up @@ -469,6 +468,8 @@ function createConfig(options, entry, format, writeMeta) {
const externalTest =
external.length === 0 ? id => false : id => externalPredicate.test(id);

const entryDir = dirname(entry);

function loadNameCache() {
try {
nameCache = JSON.parse(fs.readFileSync(getNameCachePath(), 'utf8'));
Expand Down Expand Up @@ -498,7 +499,18 @@ function createConfig(options, entry, format, writeMeta) {
if (options.multipleEntries && id === '.') {
return true;
}
return externalTest(id);
let result = externalTest(id);
if (!result) {
id = join(entryDir, id);
result = externalEntries.includes(id);
}
if (!result && !extname(id)) {
result =
externalEntries.includes(`${id}.js`) ||
externalEntries.includes(`${id}.cjs`) ||
externalEntries.includes(`${id}.mjs`);
}
return result;
},
treeshake: {
propertyReadSideEffects: false,
Expand Down
187 changes: 187 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,60 @@ exports[`fixtures build basic-flow with microbundle 5`] = `
"
`;

exports[`fixtures build basic-flow with microbundle 6`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e=e||self).basicCss=n()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"testing\\",e}});
//# sourceMappingURL=basic-css.umd.js.map
"
`;

exports[`fixtures build basic-flow with microbundle 7`] = `
"Used script: microbundle

Directory tree:

basic-flow
dist
basic-lib-flow.esm.js
basic-lib-flow.esm.js.map
basic-lib-flow.js
basic-lib-flow.js.map
basic-lib-flow.umd.js
basic-lib-flow.umd.js.map
package.json
src
fruits.js
index.js


Build \\"basicLibFlow\\" to dist:
57 B: basic-lib-flow.js.gz
43 B: basic-lib-flow.js.br
56 B: basic-lib-flow.esm.js.gz
42 B: basic-lib-flow.esm.js.br
172 B: basic-lib-flow.umd.js.gz
133 B: basic-lib-flow.umd.js.br"
`;

exports[`fixtures build basic-flow with microbundle 8`] = `6`;

exports[`fixtures build basic-flow with microbundle 9`] = `
"export default[\\"banana\\",\\"raspberry\\"];
//# sourceMappingURL=basic-lib-flow.esm.js.map
"
`;

exports[`fixtures build basic-flow with microbundle 10`] = `
"module.exports=[\\"banana\\",\\"raspberry\\"];
//# sourceMappingURL=basic-lib-flow.js.map
"
`;

exports[`fixtures build basic-flow with microbundle 11`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=[\\"banana\\",\\"raspberry\\"]:\\"function\\"==typeof define&&define.amd?define(function(){return[\\"banana\\",\\"raspberry\\"]}):(e=e||self).basicLibFlow=[\\"banana\\",\\"raspberry\\"]}(this);
//# sourceMappingURL=basic-lib-flow.umd.js.map
"
`;

exports[`fixtures build basic-json with microbundle 1`] = `
"Used script: microbundle

Expand Down Expand Up @@ -1618,6 +1672,59 @@ exports[`fixtures build macro with microbundle 5`] = `
"
`;

exports[`fixtures build macro with microbundle 6`] = `
"export default function foo(): Promise<any[]>;
"
`;

exports[`fixtures build macro with microbundle 7`] = `
"Used script: microbundle

Directory tree:

macro
dist
macro-lib.esm.js
macro-lib.esm.js.map
macro-lib.js
macro-lib.js.map
macro-lib.umd.js
macro-lib.umd.js.map
package.json
src
index.js
macro.js


Build \\"macroLib\\" to dist:
49 B: macro-lib.js.gz
33 B: macro-lib.js.br
48 B: macro-lib.esm.js.gz
32 B: macro-lib.esm.js.br
157 B: macro-lib.umd.js.gz
125 B: macro-lib.umd.js.br"
`;

exports[`fixtures build macro with microbundle 8`] = `6`;

exports[`fixtures build macro with microbundle 9`] = `
"export default\\"name-macro\\";
//# sourceMappingURL=macro-lib.esm.js.map
"
`;

exports[`fixtures build macro with microbundle 10`] = `
"module.exports=\\"name-macro\\";
//# sourceMappingURL=macro-lib.js.map
"
`;

exports[`fixtures build macro with microbundle 11`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=\\"name-macro\\":\\"function\\"==typeof define&&define.amd?define(function(){return\\"name-macro\\"}):(e=e||self).macroLib=\\"name-macro\\"}(this);
//# sourceMappingURL=macro-lib.umd.js.map
"
`;

exports[`fixtures build mangle-json-file with microbundle 1`] = `
"Used script: microbundle

Expand Down Expand Up @@ -1993,6 +2100,86 @@ exports[`fixtures build name-custom-cli with microbundle 5`] = `
"
`;

exports[`fixtures build no-bundle with microbundle 1`] = `
"Used script: microbundle build --format cjs,es --no-compress --output dist \\"src/*.js\\"

Directory tree:

no-bundle
dist
add.esm.js
add.esm.js.map
add.js
add.js.map
no-bundle.esm.js
no-bundle.esm.js.map
no-bundle.js
no-bundle.js.map
package.json
src
add.js
index.js


Build \\"noBundle\\" to dist:
79 B: add.js.gz
61 B: add.js.br
77 B: add.esm.js.gz
59 B: add.esm.js.br
173 B: no-bundle.js.gz
141 B: no-bundle.js.br
94 B: no-bundle.esm.js.gz
77 B: no-bundle.esm.js.br"
`;

exports[`fixtures build no-bundle with microbundle 2`] = `8`;

exports[`fixtures build no-bundle with microbundle 3`] = `
"function add(a, b) {
return a + b;
}

export default add;
//# sourceMappingURL=add.esm.js.map
"
`;

exports[`fixtures build no-bundle with microbundle 4`] = `
"function add(a, b) {
return a + b;
}

module.exports = add;
//# sourceMappingURL=add.js.map
"
`;

exports[`fixtures build no-bundle with microbundle 5`] = `
"import add from './add';

function bar() {
return add(1, 2);
}

export { bar };
//# sourceMappingURL=no-bundle.esm.js.map
"
`;

exports[`fixtures build no-bundle with microbundle 6`] = `
"function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var add = _interopDefault(require('./add'));

function bar() {
return add(1, 2);
}

exports.bar = bar;
//# sourceMappingURL=no-bundle.js.map
"
`;

exports[`fixtures build no-pkg with microbundle 1`] = `
"Used script: microbundle

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/no-bundle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "no-bundle",
"scripts": {
"build": "microbundle build --format cjs,es --no-compress --output dist \"src/*.js\""
}
}
3 changes: 3 additions & 0 deletions test/fixtures/no-bundle/src/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function add(a, b) {
return a + b;
}
5 changes: 5 additions & 0 deletions test/fixtures/no-bundle/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import add from './add';

export function bar() {
return add(1, 2);
}