1- import {
2- chmodSync ,
3- copyFileSync ,
4- existsSync ,
5- readFileSync ,
6- rmSync ,
7- writeFileSync
8- } from 'node:fs'
1+ import { existsSync , mkdirSync , rmSync , writeFileSync } from 'node:fs'
92import path from 'node:path'
103
114import { globSync as tinyGlobSync } from 'tinyglobby'
@@ -36,32 +29,14 @@ const {
3629} = constants
3730
3831const CONSTANTS_JS = 'constants.js'
32+ const CONSTANTS_STUB_CODE = `'use strict'\n\nmodule.exports = require('../${ CONSTANTS_JS } ')\n`
3933
34+ const distConstantsPath = path . join ( rootDistPath , CONSTANTS_JS )
4035const distModuleSyncPath = path . join ( rootDistPath , 'module-sync' )
4136const distRequirePath = path . join ( rootDistPath , 'require' )
4237
43- const binBasenames = [ 'cli.js' , 'npm-cli.js' , 'npx-cli.js' ]
4438const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
4539
46- function copyConstantsModuleSync ( srcPath , destPath ) {
47- copyFileSync (
48- path . join ( srcPath , CONSTANTS_JS ) ,
49- path . join ( destPath , CONSTANTS_JS )
50- )
51- }
52-
53- function modifyConstantsModuleExportsSync ( distPath ) {
54- const filepath = path . join ( distPath , CONSTANTS_JS )
55- let code = readFileSync ( filepath , 'utf8' )
56- code = code
57- // Remove @rollup /commonjs interop from code.
58- . replace ( / v a r c o n s t a n t s \$ \d + = { } ; ? \n + / , '' )
59- . replace ( / O b j e c t \. d e f i n e P r o p e r t y \( c o n s t a n t s \$ \d + [ \s \S ] + ?\} \) ; ? \n / , '' )
60- . replace ( / ^ (?: e x p o r t s .[ $ \w ] + | [ $ \w ] + \. d e f a u l t ) \s * = .* (?: \n | $ ) / gm, '' )
61- code = code + 'module.exports = constants\n'
62- writeFileSync ( filepath , code , 'utf8' )
63- }
64-
6540function removeDtsFilesSync ( distPath ) {
6641 for ( const filepath of tinyGlobSync ( [ '**/*.d.ts' ] , {
6742 absolute : true ,
@@ -71,21 +46,6 @@ function removeDtsFilesSync(distPath) {
7146 }
7247}
7348
74- function rewriteConstantsModuleSync ( distPath ) {
75- writeFileSync (
76- path . join ( distPath , CONSTANTS_JS ) ,
77- `'use strict'\n\nmodule.exports = require('../constants.js')\n` ,
78- 'utf8'
79- )
80- }
81-
82- function setBinPermsSync ( distPath ) {
83- for ( const binBasename of binBasenames ) {
84- // Make file chmod +x.
85- chmodSync ( path . join ( distPath , binBasename ) , 0o755 )
86- }
87- }
88-
8949function updateDepStatsSync ( depStats ) {
9050 const { content : pkgJson } = editablePkgJson
9151 const oldDepStats = existsSync ( depStatsPath )
@@ -129,8 +89,8 @@ export default () => {
12989 const moduleSyncConfig = baseConfig ( {
13090 input : {
13191 cli : `${ rootSrcPath } /cli.ts` ,
132- 'npm-cli' : `${ rootSrcPath } /shadow/npm-cli .ts` ,
133- 'npx-cli ' : `${ rootSrcPath } /shadow/npx-cli .ts` ,
92+ constants : `${ rootSrcPath } /constants .ts` ,
93+ 'shadow-bin ' : `${ rootSrcPath } /shadow/shadow-bin .ts` ,
13494 'npm-injection' : `${ rootSrcPath } /shadow/npm-injection.ts`
13595 } ,
13696 output : [
@@ -163,11 +123,13 @@ export default () => {
163123 } ,
164124 plugins : [
165125 {
166- writeBundle ( ) {
167- setBinPermsSync ( distModuleSyncPath )
168- copyConstantsModuleSync ( distModuleSyncPath , rootDistPath )
169- modifyConstantsModuleExportsSync ( rootDistPath )
170- rewriteConstantsModuleSync ( distModuleSyncPath )
126+ generateBundle ( _options , bundle ) {
127+ const constantsBundle = bundle [ CONSTANTS_JS ]
128+ if ( constantsBundle ) {
129+ mkdirSync ( rootDistPath , { recursive : true } )
130+ writeFileSync ( distConstantsPath , constantsBundle . code , 'utf8' )
131+ bundle [ CONSTANTS_JS ] . code = CONSTANTS_STUB_CODE
132+ }
171133 }
172134 }
173135 ]
@@ -176,8 +138,8 @@ export default () => {
176138 const requireConfig = baseConfig ( {
177139 input : {
178140 cli : `${ rootSrcPath } /cli.ts` ,
179- 'npm-cli' : `${ rootSrcPath } /shadow/npm-cli .ts` ,
180- 'npx-cli ' : `${ rootSrcPath } /shadow/npx-cli .ts` ,
141+ constants : `${ rootSrcPath } /constants .ts` ,
142+ 'shadow-bin ' : `${ rootSrcPath } /shadow/shadow-bin .ts` ,
181143 'npm-injection' : `${ rootSrcPath } /shadow/npm-injection.ts`
182144 } ,
183145 output : [
@@ -192,10 +154,13 @@ export default () => {
192154 ] ,
193155 plugins : [
194156 {
157+ generateBundle ( _options , bundle ) {
158+ if ( bundle [ CONSTANTS_JS ] ) {
159+ bundle [ CONSTANTS_JS ] . code = CONSTANTS_STUB_CODE
160+ }
161+ } ,
195162 writeBundle ( ) {
196- setBinPermsSync ( distRequirePath )
197163 removeDtsFilesSync ( distRequirePath )
198- rewriteConstantsModuleSync ( distRequirePath )
199164 updateDepStatsSync ( requireConfig . meta . depStats )
200165 }
201166 }
0 commit comments