Skip to content

Commit c0c0394

Browse files
committed
Fix: use this.warn in rollup 2
1 parent c4771c5 commit c0c0394

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,20 @@ function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynami
3737
return {
3838
name: "rollup-plugin-external-globals",
3939
options,
40-
transform
40+
transform,
4141
};
42+
function getDebug(context) {
43+
return (err, message) => {
44+
if (context.debug) {
45+
context.debug({
46+
message,
47+
cause: err
48+
});
49+
} else if (context.warn) {
50+
context.warn(message, err.loc ?? err.pos ?? null);
51+
}
52+
};
53+
}
4254
async function options(rawOptions) {
4355
const plugins = Array.isArray(rawOptions.plugins)
4456
? [...rawOptions.plugins]
@@ -59,10 +71,7 @@ function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynami
5971
try {
6072
ast = this.parse(code);
6173
} catch (err) {
62-
this.debug({
63-
message: `Failed to parse code, skip ${id}`,
64-
cause: err
65-
});
74+
getDebug(this)(err, `Failed to parse code, skip ${id}`);
6675
return;
6776
}
6877
code = new MagicString(code);

test/test.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ const commonjs = require("@rollup/plugin-commonjs");
99

1010
const createPlugin = require("..");
1111

12-
async function _bundle(rollup, file, globals, {plugins = []} = {}, options = {}) {
12+
async function _bundle(rollup, file, globals, {plugins = [], postPlugins = []} = {}, options = {}) {
1313
const warns = [];
1414
const bundle = await rollup.rollup({
1515
input: [file],
1616
plugins: [
1717
...plugins,
18-
createPlugin(globals, options)
18+
createPlugin(globals, options),
19+
...postPlugins
1920
],
2021
experimentalCodeSplitting: true,
2122
onwarn(warn) {
@@ -679,5 +680,35 @@ for (const r of [rollup, rollup2]) {
679680
`);
680681
})
681682
);
683+
684+
it("ignore non js files", () =>
685+
withDir(`
686+
- entry.txt: |
687+
hello bar
688+
`, async resolve => {
689+
const {output: {"entry.js": {code}}} = await bundle(
690+
resolve("entry.txt"),
691+
{
692+
bar: "BAR"
693+
},
694+
{
695+
postPlugins: [
696+
{
697+
name: "test",
698+
transform(code, id) {
699+
if (id.endsWith(".txt")) {
700+
return `export default ${JSON.stringify(code.trim())}`;
701+
}
702+
}
703+
}]
704+
}
705+
);
706+
assert.equal(code.trim(), endent`
707+
var entry = "hello bar";
708+
709+
export { entry as default };
710+
`);
711+
})
712+
);
682713
});
683714
}

0 commit comments

Comments
 (0)