@@ -4,7 +4,7 @@ const fs = require('fs')
44const devalue = require ( 'devalue' )
55const chalk = require ( 'chalk' )
66const ConcatSource = require ( 'webpack-sources/lib/ConcatSource' )
7- const { rootPath } = require ( '../../libs/utils' )
7+ const { rootPath, isInstalled } = require ( '../../libs/utils' )
88
99/**
1010 * 生成版本文件
@@ -14,6 +14,7 @@ class SinaHybridPlugin {
1414 constructor ( options ) {
1515 this . options = options
1616 this . version = process . env . npm_package_version
17+ this . shouldSNCHoisting = options . splitSNC
1718 this . rewriteField = genRewriteFn ( [
1819 rootPath ( 'public/manifest.json' ) ,
1920 rootPath ( `src/view/${ this . options . entry } /public/manifest.json` )
@@ -35,6 +36,7 @@ class SinaHybridPlugin {
3536 compiler . plugin ( 'compilation' , compilation => {
3637 const maraCtx = compiler [ 'maraContext' ] || { }
3738
39+ this . splitSNC ( compilation )
3840 this . genVersionFile ( compilation )
3941 this . updateManifestVersion ( )
4042 this . injectDataSource ( compilation , maraCtx . dataSource )
@@ -66,6 +68,31 @@ class SinaHybridPlugin {
6668 this . rewriteField ( 'dataSource' , dataSource )
6769 }
6870
71+ splitSNC ( compilation ) {
72+ if ( ! this . shouldSNCHoisting ) return
73+
74+ compilation . plugin (
75+ 'html-webpack-plugin-alter-asset-tags' ,
76+ ( assets , callback ) => {
77+ const idx = assets . body . findIndex ( tag => {
78+ return tag . attributes . src . indexOf ( '__UNI_SNC__.' ) > - 1
79+ } )
80+
81+ if ( idx < 0 ) return callback ( null , assets )
82+
83+ assets . head . push ( {
84+ tagName : 'script' ,
85+ attributes : { src : assets . body [ idx ] . attributes . src } ,
86+ closeTag : true
87+ } )
88+
89+ assets . body . splice ( idx , 1 )
90+
91+ callback ( null , assets )
92+ }
93+ )
94+ }
95+
6996 prependEntryCode ( compilation , code ) {
7097 const assets = compilation . assets
7198 const concatSource = ( assets , fileName , code ) => {
0 commit comments