html-webpack-deploy-plugin icon indicating copy to clipboard operation
html-webpack-deploy-plugin copied to clipboard

The order of script

Open GuichiZhao opened this issue 6 years ago • 3 comments
trafficstars

I want to place asset script tag before main.js but after packages, how that possible

GuichiZhao avatar Oct 23 '19 11:10 GuichiZhao

Sorry for the very delayed response to this, I'm only just seeing it now.

You can control whether scripts are appended or prepended via the append option on each tag/script.

There are a couple of examples in the README, or if you provide a more specific example I can help get it working properly

jharris4 avatar May 22 '20 16:05 jharris4

@jharris4


'moment': {
    copy: [
        {
            from: 'moment.js',
            to: 'moment.js'
        },
        {
            from: 'locale/zh-cn.js',
            to: 'zh-cn.js'
        }
    ],
        scripts: [
            {
                variableName: 'moment',
                path: 'moment.min.js',
                devPath: 'moment.js'
            },
            {
                path: 'zh-cn.js'
            }
        ]
},


From the moment package, I need to copy out two files and generate two script individually. Say, moment.js which is the moment core and zh-cn.js which is the locale file.

However, the generated html is something like this:

Screen Shot 2020-05-25 at 6 03 05 pm

As you can see, zh-cn.js is appended after AFTER main.js for some reason(a bug?), such behavior breaks the logic of code as zh-cn.js will change the moment locale automatically. So the locale set in main.js not work

GuichiZhao avatar May 25 '20 10:05 GuichiZhao

As documented in the README, the root options of the plugin have an option called prependExternals which defaults to true.

The description is: Whether to default append to false for any <script> tag that has an external or variableName option specified

So what's happening is that this script:

{
  variableName: 'moment',
  path: 'moment.min.js',
  devPath: 'moment.js'
}

is getting prepended, whereas this script:

{
  path: 'zh-cn.js'
}

is getting appended.

So if that's not what you wanted, then you should change the options of the plugin, probably to something like this (adding append: false option for zh-cn.js):

'moment': {
    copy: [
        {
            from: 'moment.js',
            to: 'moment.js'
        },
        {
            from: 'locale/zh-cn.js',
            to: 'zh-cn.js'
        }
    ],
        scripts: [
            {
                variableName: 'moment',
                path: 'moment.min.js',
                devPath: 'moment.js'
            },
            {
                path: 'zh-cn.js',
                append: false
            }
        ]
}

jharris4 avatar May 25 '20 20:05 jharris4