source.preEntry

  • Type: string | string[]
  • Default: undefined

Add a script before the entry file of each page. This script will be executed before the page code. It can be used to execute global logics, such as injecting polyfills, setting global styles, etc.

Add a Single Script

First create a src/polyfill.ts file:

console.log('I am a polyfill');

Then configure src/polyfill.ts to source.preEntry:

export default {
  source: {
    preEntry: './src/polyfill.ts',
  },
};

Re-run the compilation and visit any page, you can see that the code in src/polyfill.ts has been executed, and the I am a polyfill is logged in the console.

Add Global Style

You can also configure the global style through source.preEntry, this CSS code will be loaded earlier than the page code, such as introducing a normalize.css file:

export default {
  source: {
    preEntry: './src/normalize.css',
  },
};

Add Multiple Scripts

You can add multiple scripts by setting preEntry to an array, and they will be executed in array order:

export default {
  source: {
    preEntry: ['./src/polyfill-a.ts', './src/polyfill-b.ts'],
  },
};