Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve directives #217

Merged
merged 4 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions env.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"@swc/core": "1.3.46",
"@swc/helpers": "0.5.0",
"arg": "5.0.2",
"magic-string": "0.30.0",
"pretty-bytes": "5.6.0",
"publint": "0.1.11",
"rollup": "3.20.2",
"rollup-plugin-dts": "5.3.0",
"rollup-plugin-preserve-shebang": "1.0.1",
"rollup-plugin-swc3": "0.8.1",
"tslib": "2.5.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import { resolve, dirname } from 'path'
import { wasm } from '@rollup/plugin-wasm'
import { swc } from 'rollup-plugin-swc3'
import commonjs from '@rollup/plugin-commonjs'
import shebang from 'rollup-plugin-preserve-shebang'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import createChunkSizeCollector from './plugins/size-plugin'
import preserveDirectivePlugin from './plugins/directive-plugin'
import {
getTypings,
getExportPaths,
Expand Down Expand Up @@ -87,7 +87,7 @@ function buildInputConfig(
)
const sizePlugin = sizeCollector.plugin(cwd)
const commonPlugins = [
shebang(),
preserveDirectivePlugin(),
sizePlugin,
]
const plugins: Plugin[] = (
Expand Down
41 changes: 41 additions & 0 deletions src/plugins/directive-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Plugin } from 'rollup'
import MagicString from 'magic-string'



export default function preserveDirectivePlugin(): Plugin {
const fileDirectivesMap = new Map()
return {
name: 'use-directive',

transform(code, id) {
const regex = /^(?:['"]use[^'"]+['"][^\n]*|#![^\n]*)/gm
const directives: Set<string> = new Set()

const replacedCode = code.replace(regex, (match) => {
// replace double quotes with single quotes
directives.add(match.replace(/["]/g, "'"))
return ''
})

if (directives.size) fileDirectivesMap.set(id, directives)
return {
code: replacedCode,
map: null,
}
},

renderChunk(code, chunk, { sourcemap }) {
let directives = fileDirectivesMap.get(chunk.facadeModuleId)
if (!directives) return null

const s = new MagicString(code)
s.prepend(`${[...directives].join('\n')}\n`)

return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: true }) : null
}
}
}
}
8 changes: 8 additions & 0 deletions test/unit/use-client/__snapshot__/use-client.js.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
'use client'
var input = (()=>{
React.useState(null);
return null;
});

export { input as default };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";var t=()=>(React.useState(null),null);export{t as default};
7 changes: 7 additions & 0 deletions test/unit/use-client/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
'use client'

export default () => {
React.useState(null)
return null
}
29 changes: 5 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2161,12 +2161,12 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

magic-string@^0.25.7:
version "0.25.9"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
integrity sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==
magic-string@0.30.0, magic-string@^0.30.0:
version "0.30.0"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.0.tgz#fd58a4748c5c4547338a424e90fa5dd17f4de529"
integrity sha512-LA+31JYDJLs82r2ScLrlz1GjSgu66ZV518eyWT+S8VhyQn/JL0u9MeBOvQMGYiPk1DBiSN9DDMOcXvigJZaViQ==
dependencies:
sourcemap-codec "^1.4.8"
"@jridgewell/sourcemap-codec" "^1.4.13"

magic-string@^0.27.0:
version "0.27.0"
Expand All @@ -2175,13 +2175,6 @@ magic-string@^0.27.0:
dependencies:
"@jridgewell/sourcemap-codec" "^1.4.13"

magic-string@^0.30.0:
version "0.30.0"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.0.tgz#fd58a4748c5c4547338a424e90fa5dd17f4de529"
integrity sha512-LA+31JYDJLs82r2ScLrlz1GjSgu66ZV518eyWT+S8VhyQn/JL0u9MeBOvQMGYiPk1DBiSN9DDMOcXvigJZaViQ==
dependencies:
"@jridgewell/sourcemap-codec" "^1.4.13"

make-dir@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
Expand Down Expand Up @@ -2471,13 +2464,6 @@ [email protected]:
optionalDependencies:
"@babel/code-frame" "^7.18.6"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-preserve-shebang/-/rollup-plugin-preserve-shebang-1.0.1.tgz#17109cdb4ed12c3cac9379b802182427cdbee5a1"
integrity sha512-gk7ExGBqvUinhgrvldKHkAKXXwRkWMXMZymNkrtn50uBgHITlhRjhnKmbNGwAIc4Bzgl3yLv7/8Fhi/XeHhFKg==
dependencies:
magic-string "^0.25.7"

[email protected]:
version "0.8.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-swc3/-/rollup-plugin-swc3-0.8.1.tgz#40ce99da95faf6202e18792e1faa4d22a6aaa4ce"
Expand Down Expand Up @@ -2566,11 +2552,6 @@ source-map@^0.6.0, source-map@^0.6.1:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

sourcemap-codec@^1.4.8:
version "1.4.8"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
Expand Down