Skip to content

Commit

Permalink
feat: shims option is now false by default
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

`shims` option is `false` by default, changing it `true` allows tsup to shim `import.meta.url` in `cjs` build and `__dirname` `__filename` in `esm` build
  • Loading branch information
EGOIST committed May 25, 2022
1 parent f7c53f7 commit 1602446
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function main(options: Options = {}) {
})
.option('--loader <ext=loader>', 'Specify the loader for a file extension')
.option('--no-config', 'Disable config file')
.option('--no-shims', 'Disable cjs and esm shims')
.option('--shims', 'Enable cjs and esm shims')
.option('--inject-style', 'Inject style tag to document head')
.option(
'--treeshake [strategy]',
Expand Down
2 changes: 1 addition & 1 deletion src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function runEsbuild(

const platform = options.platform || 'node'
const loader = options.loader || {}
const injectShims = options.shims !== false
const injectShims = options.shims

pluginContainer.setContext({
format,
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export type Options = {
injectStyle?: boolean
/**
* Inject cjs and esm shims if needed
* @default true
* @default false
*/
shims?: boolean
/**
Expand Down
132 changes: 4 additions & 128 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,141 +1,17 @@
// Vitest Snapshot v1

exports[`--env flag 1`] = `
"var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, \\"__esModule\\", { value: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, copyDefault, desc) => {
if (module2 && typeof module2 === \\"object\\" || typeof module2 === \\"function\\") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== \\"default\\"))
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ ((cache) => {
return (module2, temp) => {
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
};
})(typeof WeakMap !== \\"undefined\\" ? /* @__PURE__ */ new WeakMap() : 0);
// input.ts
var input_exports = {};
__export(input_exports, {
env: () => env
});
var env = \\"test\\";
module.exports = __toCommonJS(input_exports);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
env
});
"
`;

exports[`bundle svelte 1`] = `
"var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, \\"__esModule\\", { value: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, copyDefault, desc) => {
if (module2 && typeof module2 === \\"object\\" || typeof module2 === \\"function\\") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== \\"default\\"))
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ ((cache) => {
return (module2, temp) => {
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
};
})(typeof WeakMap !== \\"undefined\\" ? /* @__PURE__ */ new WeakMap() : 0);
// input.ts
var input_exports = {};
__export(input_exports, {
App: () => App_default
});
// App.svelte
var import_internal = require(\\"svelte/internal\\");
function create_fragment(ctx) {
let span;
return {
c() {
span = (0, import_internal.element)(\\"span\\");
span.textContent = \`\${msg}\`;
(0, import_internal.attr)(span, \\"class\\", \\"svelte-1jo4k3z\\");
},
m(target, anchor) {
(0, import_internal.insert)(target, span, anchor);
},
p: import_internal.noop,
i: import_internal.noop,
o: import_internal.noop,
d(detaching) {
if (detaching)
(0, import_internal.detach)(span);
}
};
}
var msg = \\"hello svelte\\";
var App = class extends import_internal.SvelteComponent {
constructor(options) {
super();
(0, import_internal.init)(this, options, null, create_fragment, import_internal.safe_not_equal, {});
}
};
var App_default = App;
module.exports = __toCommonJS(input_exports);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
App
});
"
`;
exports[`bundle svelte 2`] = `
"/* svelte-css:App.svelte.css */
span.svelte-1jo4k3z {
color: red;
}
"
`;
exports[`code splitting in cjs format 1`] = `
"\\"use strict\\";Object.defineProperty(exports, \\"__esModule\\", {value: true});
var _chunkVF632YU7js = require('./chunk-VF632YU7.js');
// input.ts
var foo = () => Promise.resolve().then(() => _chunkVF632YU7js.__toESM.call(void 0, _chunkVF632YU7js.__require.call(void 0, \\"./foo-UHQYLI73.js\\")));
"\\"use strict\\";Object.defineProperty(exports, \\"__esModule\\", {value: true});// input.ts
var foo = () => Promise.resolve().then(() => require(\\"./foo-D62QZYUQ.js\\"));
exports.foo = foo;
"
`;

exports[`code splitting in cjs format 2`] = `
"\\"use strict\\";Object.defineProperty(exports, \\"__esModule\\", {value: true});
var _chunkVF632YU7js = require('./chunk-VF632YU7.js');
// another-input.ts
var foo = () => Promise.resolve().then(() => _chunkVF632YU7js.__toESM.call(void 0, _chunkVF632YU7js.__require.call(void 0, \\"./foo-UHQYLI73.js\\")));
"\\"use strict\\";Object.defineProperty(exports, \\"__esModule\\", {value: true});// another-input.ts
var foo = () => Promise.resolve().then(() => require(\\"./foo-D62QZYUQ.js\\"));
exports.foo = foo;
Expand Down
6 changes: 4 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ test(`transform import.meta.url in cjs format`, async () => {
{
'input.ts': `export default import.meta.url`,
},
{}
{
flags: ['--shims'],
}
)
expect(await getFileContent('dist/input.js')).toContain('getImportMetaUrl')
})
Expand All @@ -509,7 +511,7 @@ test(`transform __dirname and __filename in esm format`, async () => {
`,
},
{
flags: ['--format', 'esm'],
flags: ['--format', 'esm', '--shims'],
}
)
const code = await getFileContent('dist/input.mjs')
Expand Down

0 comments on commit 1602446

Please sign in to comment.