@@ -10,23 +10,33 @@ import commonjs from '@rollup/plugin-commonjs';
1010import json from '@rollup/plugin-json' ;
1111import { nodeResolve } from '@rollup/plugin-node-resolve' ;
1212import replace from '@rollup/plugin-replace' ;
13- import terser from '@rollup/plugin-terser' ;
1413import { parse as parseMd } from 'marked' ;
1514import fs from 'node:fs' ;
1615import { dirname , resolve } from 'node:path' ;
1716import { fileURLToPath } from 'node:url' ;
17+ import { dts } from 'rollup-plugin-dts' ;
1818import styles from 'rollup-plugin-styles' ;
1919import { watchExternal } from 'rollup-plugin-watch-external' ;
2020import shell from 'shelljs' ;
21-
22- import { inputPlugins , outputPlugins , solidSvg } from './src/rollup-plugin' ;
21+ import { minify } from 'terser' ;
22+
23+ import {
24+ escapeTmplText ,
25+ inputPlugins ,
26+ outputPlugins ,
27+ solidSvg ,
28+ } from './src/rollup-plugin' ;
2329import { getMetaData , updateReadme } from './src/rollup-plugin/metaHeader' ;
2430import { siteUrl } from './src/rollup-plugin/siteUrl' ;
2531
2632const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
2733
2834const isDevMode = process . env . NODE_ENV === 'development' ;
29- const isNpmMode = process . env . NODE_ENV === 'npm' ;
35+
36+ const minifyCode = async ( code : string ) => {
37+ const res = await minify ( code , { ecma : 2020 , mangle : false } ) ;
38+ return res . code ! ;
39+ } ;
3040
3141const latestChangeHtml = await ( ( ) => {
3242 const md = fs
@@ -75,7 +85,7 @@ const packlist = [
7585 'worker/ImageUpscale' ,
7686 'userscript/otherSite' ,
7787 'userscript/ehTagRules' ,
78- ] ;
88+ ] as const ;
7989
8090const babelConfig = {
8191 presets : [ '@babel/preset-env' , '@babel/preset-typescript' , 'solid' ] ,
@@ -123,6 +133,7 @@ const getPlugins = (...otherPlugins: InputPluginOption[]) => [
123133 isDevMode : `${ isDevMode } ` ,
124134 'process.env.NODE_ENV' : isDevMode ? `'development'` : `'production'` ,
125135 'inject@LatestChange' : latestChangeHtml ,
136+ scriptVersion : `'${ meta . version } '` ,
126137 } ,
127138 preventAssignment : true ,
128139 } ) ,
@@ -138,8 +149,6 @@ const getPlugins = (...otherPlugins: InputPluginOption[]) => [
138149 styles ( { mode : 'extract' , modules : { generateScopedName } } ) ,
139150 solidSvg ( ) ,
140151
141- // ts({ transpiler: 'babel', transpileOnly: true, babelConfig }),
142-
143152 babel ( {
144153 babelHelpers : 'runtime' ,
145154 extensions : [ '.ts' , '.tsx' ] ,
@@ -166,8 +175,6 @@ export const buildOptions = (
166175 watchFiles && isDevMode && watchExternal ( { entries : watchFiles } ) ,
167176 ) ;
168177
169- if ( isNpmMode ) options . plugins . push ( terser ( ) ) ;
170-
171178 Object . assign ( options . output , {
172179 file : `dist/${ path . replace ( / ( \/ i n d e x ) ? \. t s x ? / , '' ) } .js` ,
173180 plugins : [
@@ -227,8 +234,7 @@ shell.rm('-rf', resolve(__dirname, 'dist'));
227234if ( isDevMode )
228235 shell . exec ( 'serve dist --cors -l 2405' , { async : true , silent : true } ) ;
229236
230- // oxlint-disable-next-line no-mutable-exports
231- let optionList : RollupOptions [ ] = [
237+ const optionList : RollupOptions [ ] = [
232238 buildOptions ( 'dev' ) ,
233239
234240 ...packlist . map ( ( path ) => buildOptions ( path ) ) ,
@@ -252,7 +258,8 @@ let optionList: RollupOptions[] = [
252258 / \s + \/ \/ i m p o r t l i s t / ,
253259 packlist
254260 . map ( ( path ) => {
255- if ( path === 'userscript/main' ) return '' ;
261+ if ( path === 'userscript/main' )
262+ return `\ncase 'main':\ncode = \`inject('${ path } ')\`;\nbreak;` ;
256263 return `\ncase '${ path } ':\ncode = \`inject('${ path } ')\`;\nbreak;` ;
257264 } )
258265 . join ( '' ) ,
@@ -333,6 +340,90 @@ if (!isDevMode)
333340 } ) ,
334341 ) ;
335342
336- if ( isNpmMode ) optionList = [ buildOptions ( 'userscript/dmzjDecrypt' ) ] ;
343+ const umdPacklist = [
344+ 'helper/languages' ,
345+ 'helper' ,
346+ 'request' ,
347+ 'components/Manga' ,
348+ 'components/IconButton' ,
349+ 'components/Toast' ,
350+ 'userscript/detectAd' ,
351+ 'worker/detectAd' ,
352+ 'worker/ImageRecognition' ,
353+ 'worker/ImageUpscale' ,
354+ ] ;
355+ if ( ! isDevMode )
356+ optionList . push (
357+ buildOptions (
358+ 'userscript/import' ,
359+ umdPacklist . map ( ( path ) => `dist/${ path } .js` ) ,
360+ ( options ) => {
361+ options . output . file = 'dist/umd/import.js' ;
362+ options . output . plugins . unshift ( {
363+ name : 'selfImport' ,
364+ async renderChunk ( rawCode ) {
365+ let importListCode = '' ;
366+
367+ for ( const path of umdPacklist )
368+ importListCode += `\ncase '${ path } ':\ncode = \`inject('${ path } ')\`;\nbreak;\n` ;
369+
370+ for ( const [ name , url ] of Object . entries ( meta . resource ) ) {
371+ const res = await fetch ( url ) ;
372+ let code = await res . text ( ) ;
373+
374+ if ( name === '@tensorflow/tfjs-backend-webgpu' )
375+ code = code . replace (
376+ '@tensorflow/tfjs-core' ,
377+ '@tensorflow/tfjs' ,
378+ ) ;
379+
380+ code = await minifyCode ( code ) ;
381+ importListCode += `\ncase '${ name } ':\ncode = \`${ escapeTmplText ( code ) } \`;\nbreak;\n` ;
382+ }
383+
384+ return rawCode . replace ( / \s + \/ \/ i m p o r t l i s t / , ( ) => importListCode ) ;
385+ } ,
386+ } ) ;
387+ return options ;
388+ } ,
389+ ) ,
390+ buildOptions (
391+ 'umd' ,
392+ [ ...umdPacklist . map ( ( path ) => `dist/${ path } .js` ) , 'dist/umd/import.js' ] ,
393+ ( options ) => {
394+ options . output . plugins . unshift ( {
395+ name : 'selfUMD' ,
396+ async renderChunk ( rawCode ) {
397+ let code = rawCode ;
398+ const importCode = fs
399+ . readFileSync ( `dist/umd/import.js` )
400+ . toString ( )
401+ . replaceAll ( 'require$1' , 'require' ) ;
402+ code = `${ importCode } \n${ code } ` ;
403+
404+ if ( ! isDevMode ) code = await minifyCode ( code ) ;
405+
406+ const name = 'initComicReader' ;
407+ code = `
408+ (function (global, factory) {
409+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
410+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
411+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.${ name } = global.${ name } || {}));
412+ })(this, (function (exports) {
413+ ${ code }
414+ }));` ;
415+
416+ return code ;
417+ } ,
418+ } ) ;
419+ return options ;
420+ } ,
421+ ) ,
422+ {
423+ input : './src/umd.tsx' ,
424+ output : [ { file : 'dist/umd.d.ts' , format : 'es' } ] ,
425+ plugins : [ dts ( ) ] ,
426+ } ,
427+ ) ;
337428
338429export default optionList ;
0 commit comments