Skip to content

Commit 7bd0695

Browse files
authored
fix: extracting polyfills (#1)
1 parent 6af6336 commit 7bd0695

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

src/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export default class OptimizePlugin {
237237
async generatePolyfillsChunk (polyfills, cwd, timings) {
238238
const ENTRY = '\0entry';
239239

240-
const entryContent = polyfills.reduce((str, p) => `${str}\nimport "${p}";`, '');
240+
const entryContent = polyfills.reduce((str, p) => `${str}\nimport "${p.replace('.js', '')}";`, '');
241241

242242
const COREJS = require.resolve('core-js/package.json').replace('package.json', '');
243243
const isCoreJsPath = /(?:^|\/)core-js\/(.+)$/;
@@ -306,11 +306,13 @@ export default class OptimizePlugin {
306306
// });
307307
// }
308308
// },
309-
this.options.minify ? (
310-
rollupPluginTerserSimple()
311-
) : (
312-
rollupPluginStripComments()
313-
)
309+
this.options.minify
310+
? (
311+
rollupPluginTerserSimple()
312+
)
313+
: (
314+
rollupPluginStripComments()
315+
)
314316
].filter(Boolean)
315317
});
316318
this.setRollupCache(polyfillsBundle.cache);

test/_util.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function sleep (ms) {
2828
const gzip = util.promisify(gzipSync);
2929
const gzipSize = async x => (await gzip(x)).byteLength;
3030

31-
export async function printSizes (assets, name) {
31+
export async function printSizes (assets, name, console) {
3232
let modernSize = 0;
3333
let legacySize = 0;
3434
const prettyBytes = (size) => {
@@ -58,7 +58,7 @@ export async function printSizes (assets, name) {
5858
console.log(str);
5959
}
6060

61-
export function runWebpack (fixture, { output = {}, plugins = [], module = {}, resolve = {}, ...config } = {}) {
61+
export function runWebpack (fixture, { output = {}, plugins = [], module = {}, resolve = {}, ...config } = {}, console) {
6262
return run(callback => webpack({
6363
mode: 'production',
6464
devtool: false,
@@ -80,10 +80,10 @@ export function runWebpack (fixture, { output = {}, plugins = [], module = {}, r
8080
])
8181
].concat(plugins || []),
8282
...config
83-
}, callback));
83+
}, callback), console);
8484
}
8585

86-
export function watchWebpack (fixture, { output, plugins, context, ...config } = {}) {
86+
export function watchWebpack (fixture, { output, plugins, context, ...config } = {}, console) {
8787
context = context || path.resolve(__dirname, 'fixtures', fixture);
8888
const compiler = webpack({
8989
mode: 'production',
@@ -96,7 +96,7 @@ export function watchWebpack (fixture, { output, plugins, context, ...config } =
9696
},
9797
plugins: plugins || []
9898
});
99-
compiler.doRun = () => run(compiler.run.bind(compiler));
99+
compiler.doRun = () => run(compiler.run.bind(compiler), console);
100100
return compiler;
101101
}
102102

@@ -108,7 +108,7 @@ export function statsWithAssets (stats) {
108108
return stats;
109109
}
110110

111-
function run (runner) {
111+
function run (runner, console) {
112112
return new Promise((resolve, reject) => {
113113
runner((err, stats) => {
114114
if (err) return reject(err);

test/index.test.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ jest.setTimeout(30000);
2424
const concurrency = false;
2525

2626
describe('optimize-plugin', () => {
27+
const $console = {
28+
log: console.log,
29+
warn: console.warn,
30+
info: console.info
31+
};
32+
33+
beforeAll(() => {
34+
console.warn = () => 0;
35+
console.log = () => 0;
36+
console.info = () => 0;
37+
});
38+
39+
afterAll(() => {
40+
console.warn = $console.warn;
41+
console.log = $console.log;
42+
console.info = $console.info;
43+
});
44+
2745
test('exports a class', () => {
2846
expect(OptimizePlugin).toBeInstanceOf(Function);
2947
expect(OptimizePlugin.prototype).toHaveProperty('apply', expect.any(Function));
@@ -42,7 +60,7 @@ describe('optimize-plugin', () => {
4260
plugins: [
4361
new OptimizePlugin({ concurrency })
4462
]
45-
});
63+
}, $console);
4664

4765
const assetNames = Object.keys(stats.assets);
4866
expect(assetNames).toHaveLength(3);
@@ -61,7 +79,7 @@ describe('optimize-plugin', () => {
6179
expect(polyfills).toMatch(/Object\.defineProperty/g);
6280
expect(polyfills).not.toMatch(/require\(/g);
6381

64-
await printSizes(stats.assets, '"it works"');
82+
await printSizes(stats.assets, '"it works"', $console);
6583
});
6684

6785
test('code splitting', async () => {
@@ -72,7 +90,7 @@ describe('optimize-plugin', () => {
7290
plugins: [
7391
new OptimizePlugin({ concurrency })
7492
]
75-
});
93+
}, $console);
7694

7795
const assetNames = Object.keys(stats.assets);
7896
expect(assetNames).toHaveLength(9);
@@ -97,7 +115,7 @@ describe('optimize-plugin', () => {
97115
expect(polyfills).toMatch(/Object\.defineProperty/g);
98116
expect(polyfills).not.toMatch(/require\(/g);
99117

100-
await printSizes(stats.assets, 'code splitting');
118+
await printSizes(stats.assets, 'code splitting', $console);
101119
});
102120

103121
describe('TypeScript Support', () => {
@@ -120,7 +138,7 @@ describe('optimize-plugin', () => {
120138
plugins: [
121139
new OptimizePlugin({ concurrency })
122140
]
123-
});
141+
}, $console);
124142

125143
const assetNames = Object.keys(stats.assets);
126144
expect(assetNames).toHaveLength(3);
@@ -139,7 +157,7 @@ describe('optimize-plugin', () => {
139157
expect(polyfills).toMatch(/Object\.defineProperty/g);
140158
expect(polyfills).not.toMatch(/require\(/g);
141159

142-
await printSizes(stats.assets, 'typescript support');
160+
await printSizes(stats.assets, 'typescript support', $console);
143161
});
144162

145163
test('using Sucrase', async () => {
@@ -158,7 +176,7 @@ describe('optimize-plugin', () => {
158176
plugins: [
159177
new OptimizePlugin({ concurrency })
160178
]
161-
});
179+
}, $console);
162180

163181
const assetNames = Object.keys(stats.assets);
164182
expect(assetNames).toHaveLength(3);
@@ -177,7 +195,7 @@ describe('optimize-plugin', () => {
177195
expect(polyfills).toMatch(/Object\.defineProperty/g);
178196
expect(polyfills).not.toMatch(/require\(/g);
179197

180-
await printSizes(stats.assets, 'sucrase');
198+
await printSizes(stats.assets, 'sucrase', $console);
181199
});
182200
});
183201
});

0 commit comments

Comments
 (0)